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 <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Cherry Mui 2025-08-02 18:06:57 -04:00
parent a3895fe9f1
commit f53dcb6280

View file

@ -233,19 +233,23 @@ var stdlibImportcfgFile = sync.OnceValue(func() string {
return filename return filename
}) })
func linkFile(runcmd runCmd, goname string, importcfg string, ldflags []string) (err error) { // linkFile links infile with the given importcfg and ldflags, writes to outfile.
// infile can be the name of an object file or a go source file.
func linkFile(runcmd runCmd, outfile, infile string, importcfg string, ldflags []string) (err error) {
if importcfg == "" { if importcfg == "" {
importcfg = stdlibImportcfgFile() importcfg = stdlibImportcfgFile()
} }
pfile := strings.ReplaceAll(goname, ".go", ".o") if strings.HasSuffix(infile, ".go") {
cmd := []string{goTool, "tool", "link", "-w", "-o", "a.exe", "-importcfg=" + importcfg} infile = infile[:len(infile)-3] + ".o"
}
cmd := []string{goTool, "tool", "link", "-s", "-w", "-o", outfile, "-importcfg=" + importcfg}
if *linkshared { if *linkshared {
cmd = append(cmd, "-linkshared", "-installsuffix=dynlink") cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
} }
if ldflags != nil { if ldflags != nil {
cmd = append(cmd, ldflags...) cmd = append(cmd, ldflags...)
} }
cmd = append(cmd, pfile) cmd = append(cmd, infile)
_, err = runcmd(cmd...) _, err = runcmd(cmd...)
return return
} }
@ -853,7 +857,7 @@ func (t test) run() error {
} }
if i == len(pkgs)-1 { if i == len(pkgs)-1 {
err = linkFile(runcmd, pkg.files[0], importcfgfile, ldflags) err = linkFile(runcmd, "a.exe", pkg.files[0], importcfgfile, ldflags)
if err != nil { if err != nil {
return err return err
} }
@ -974,8 +978,7 @@ func (t test) run() error {
if err != nil { if err != nil {
return err return err
} }
cmd = []string{goTool, "tool", "link", "-importcfg=" + stdlibImportcfgFile(), "-o", "a.exe", "all.a"} err = linkFile(runcmd, "a.exe", "all.a", stdlibImportcfgFile(), nil)
_, err = runcmd(cmd...)
if err != nil { if err != nil {
return err return err
} }
@ -1033,9 +1036,7 @@ func (t test) run() error {
return err return err
} }
exe := filepath.Join(tempDir, "test.exe") exe := filepath.Join(tempDir, "test.exe")
cmd := []string{goTool, "tool", "link", "-s", "-w", "-importcfg=" + stdlibImportcfgFile()} if err := linkFile(runcmd, exe, pkg, stdlibImportcfgFile(), nil); err != nil {
cmd = append(cmd, "-o", exe, pkg)
if _, err := runcmd(cmd...); err != nil {
return err return err
} }
out, err = runcmd(append([]string{exe}, args...)...) out, err = runcmd(append([]string{exe}, args...)...)