From f53dcb6280a66acb7f6a66d39eaf51e5f0f5698b Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Sat, 2 Aug 2025 18:06:57 -0400 Subject: [PATCH] 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 LUCI-TryBot-Result: Go LUCI --- src/cmd/internal/testdir/testdir_test.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/cmd/internal/testdir/testdir_test.go b/src/cmd/internal/testdir/testdir_test.go index 666645873b..2e2f55b06e 100644 --- a/src/cmd/internal/testdir/testdir_test.go +++ b/src/cmd/internal/testdir/testdir_test.go @@ -233,19 +233,23 @@ var stdlibImportcfgFile = sync.OnceValue(func() string { 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 == "" { importcfg = stdlibImportcfgFile() } - pfile := strings.ReplaceAll(goname, ".go", ".o") - cmd := []string{goTool, "tool", "link", "-w", "-o", "a.exe", "-importcfg=" + importcfg} + if strings.HasSuffix(infile, ".go") { + infile = infile[:len(infile)-3] + ".o" + } + cmd := []string{goTool, "tool", "link", "-s", "-w", "-o", outfile, "-importcfg=" + importcfg} if *linkshared { cmd = append(cmd, "-linkshared", "-installsuffix=dynlink") } if ldflags != nil { cmd = append(cmd, ldflags...) } - cmd = append(cmd, pfile) + cmd = append(cmd, infile) _, err = runcmd(cmd...) return } @@ -853,7 +857,7 @@ func (t test) run() error { } 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 { return err } @@ -974,8 +978,7 @@ func (t test) run() error { if err != nil { return err } - cmd = []string{goTool, "tool", "link", "-importcfg=" + stdlibImportcfgFile(), "-o", "a.exe", "all.a"} - _, err = runcmd(cmd...) + err = linkFile(runcmd, "a.exe", "all.a", stdlibImportcfgFile(), nil) if err != nil { return err } @@ -1033,9 +1036,7 @@ func (t test) run() error { return err } exe := filepath.Join(tempDir, "test.exe") - cmd := []string{goTool, "tool", "link", "-s", "-w", "-importcfg=" + stdlibImportcfgFile()} - cmd = append(cmd, "-o", exe, pkg) - if _, err := runcmd(cmd...); err != nil { + if err := linkFile(runcmd, exe, pkg, stdlibImportcfgFile(), nil); err != nil { return err } out, err = runcmd(append([]string{exe}, args...)...) -- 2.51.0