Skip to content

Commit f53dcb6

Browse files
committed
cmd/internal/testdir: unify link command
There are three places where we manually construct a "go tool link" command. Unify them. Test binaries don't need the symbol table or debug info, so pass -s -w always. Change-Id: I40143894172877738e250f291d7e7ef8dce62488 Reviewed-on: https://go-review.googlesource.com/c/go/+/692875 Reviewed-by: David Chase <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent a3895fe commit f53dcb6

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/cmd/internal/testdir/testdir_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,19 +233,23 @@ var stdlibImportcfgFile = sync.OnceValue(func() string {
233233
return filename
234234
})
235235

236-
func linkFile(runcmd runCmd, goname string, importcfg string, ldflags []string) (err error) {
236+
// linkFile links infile with the given importcfg and ldflags, writes to outfile.
237+
// infile can be the name of an object file or a go source file.
238+
func linkFile(runcmd runCmd, outfile, infile string, importcfg string, ldflags []string) (err error) {
237239
if importcfg == "" {
238240
importcfg = stdlibImportcfgFile()
239241
}
240-
pfile := strings.ReplaceAll(goname, ".go", ".o")
241-
cmd := []string{goTool, "tool", "link", "-w", "-o", "a.exe", "-importcfg=" + importcfg}
242+
if strings.HasSuffix(infile, ".go") {
243+
infile = infile[:len(infile)-3] + ".o"
244+
}
245+
cmd := []string{goTool, "tool", "link", "-s", "-w", "-o", outfile, "-importcfg=" + importcfg}
242246
if *linkshared {
243247
cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
244248
}
245249
if ldflags != nil {
246250
cmd = append(cmd, ldflags...)
247251
}
248-
cmd = append(cmd, pfile)
252+
cmd = append(cmd, infile)
249253
_, err = runcmd(cmd...)
250254
return
251255
}
@@ -853,7 +857,7 @@ func (t test) run() error {
853857
}
854858

855859
if i == len(pkgs)-1 {
856-
err = linkFile(runcmd, pkg.files[0], importcfgfile, ldflags)
860+
err = linkFile(runcmd, "a.exe", pkg.files[0], importcfgfile, ldflags)
857861
if err != nil {
858862
return err
859863
}
@@ -974,8 +978,7 @@ func (t test) run() error {
974978
if err != nil {
975979
return err
976980
}
977-
cmd = []string{goTool, "tool", "link", "-importcfg=" + stdlibImportcfgFile(), "-o", "a.exe", "all.a"}
978-
_, err = runcmd(cmd...)
981+
err = linkFile(runcmd, "a.exe", "all.a", stdlibImportcfgFile(), nil)
979982
if err != nil {
980983
return err
981984
}
@@ -1033,9 +1036,7 @@ func (t test) run() error {
10331036
return err
10341037
}
10351038
exe := filepath.Join(tempDir, "test.exe")
1036-
cmd := []string{goTool, "tool", "link", "-s", "-w", "-importcfg=" + stdlibImportcfgFile()}
1037-
cmd = append(cmd, "-o", exe, pkg)
1038-
if _, err := runcmd(cmd...); err != nil {
1039+
if err := linkFile(runcmd, exe, pkg, stdlibImportcfgFile(), nil); err != nil {
10391040
return err
10401041
}
10411042
out, err = runcmd(append([]string{exe}, args...)...)

0 commit comments

Comments
 (0)