]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: stamp build settings for binaries in cmd
authorBryan C. Mills <bcmills@google.com>
Tue, 8 Mar 2022 22:01:12 +0000 (17:01 -0500)
committerBryan Mills <bcmills@google.com>
Fri, 18 Mar 2022 22:14:22 +0000 (22:14 +0000)
Also update cmd/dist to avoid setting gcflags and ldflags explicitly
when the set of flags to be set is empty (a verbose way of specifying
the default behavior).

Stamping was disabled for the Go standard library in CL 356014 due to
the cmd/dist flags causing cmd/go to (correctly) report the resulting
binaries as stale.

With cmd/dist fixed, we can also remove the special case in cmd/go,
which will allow tests of binaries in 'cmd' to read the build info
embedded in the test binary. That build info may be useful to
determine (say) whether runtime.GOROOT ought to work without GOROOT
set in the environment.

For #51483
Updates #37475

Change-Id: I64d04f5990190094eb6c0522db829d3bdfa50ef3
Reviewed-on: https://go-review.googlesource.com/c/go/+/391809
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/dist/build.go
src/cmd/dist/test.go
src/cmd/go/internal/load/pkg.go

index d37c3f83efb4d474175a46062956222c450ad906..ba09ce9a7b8d721df51ced2e929477fa84d327c0 100644 (file)
@@ -1502,8 +1502,19 @@ func goInstall(goBinary string, args ...string) {
        goCmd(goBinary, "install", args...)
 }
 
+func appendCompilerFlags(args []string) []string {
+       if gogcflags != "" {
+               args = append(args, "-gcflags=all="+gogcflags)
+       }
+       if goldflags != "" {
+               args = append(args, "-ldflags=all="+goldflags)
+       }
+       return args
+}
+
 func goCmd(goBinary string, cmd string, args ...string) {
-       goCmd := []string{goBinary, cmd, "-gcflags=all=" + gogcflags, "-ldflags=all=" + goldflags}
+       goCmd := []string{goBinary, cmd}
+       goCmd = appendCompilerFlags(goCmd)
        if vflag > 0 {
                goCmd = append(goCmd, "-v")
        }
@@ -1517,12 +1528,11 @@ func goCmd(goBinary string, cmd string, args ...string) {
 }
 
 func checkNotStale(goBinary string, targets ...string) {
-       out := run(workdir, CheckExit,
-               append([]string{
-                       goBinary,
-                       "list", "-gcflags=all=" + gogcflags, "-ldflags=all=" + goldflags,
-                       "-f={{if .Stale}}\tSTALE {{.ImportPath}}: {{.StaleReason}}{{end}}",
-               }, targets...)...)
+       goCmd := []string{goBinary, "list"}
+       goCmd = appendCompilerFlags(goCmd)
+       goCmd = append(goCmd, "-f={{if .Stale}}\tSTALE {{.ImportPath}}: {{.StaleReason}}{{end}}")
+
+       out := run(workdir, CheckExit, append(goCmd, targets...)...)
        if strings.Contains(out, "\tSTALE ") {
                os.Setenv("GODEBUG", "gocachehash=1")
                for _, target := range []string{"runtime/internal/sys", "cmd/dist", "cmd/link"} {
index cd3c26ab3a334a6cf3fe21dcc7366f23002cf748..a540a2abda669e70e02b6dbc899224a2bcea0f6e 100644 (file)
@@ -397,7 +397,9 @@ func (t *tester) registerStdTest(pkg string) {
                                "-short=" + short(),
                                t.tags(),
                                t.timeout(timeoutSec),
-                               "-gcflags=all=" + gcflags,
+                       }
+                       if gcflags != "" {
+                               args = append(args, "-gcflags=all="+gcflags)
                        }
                        if t.race {
                                args = append(args, "-race")
index 403bc330e776c48b7632fff3704a01f80f221a8a..ab70845959650fee08117810444dc79247ffce17 100644 (file)
@@ -2233,11 +2233,6 @@ var vcsStatusCache par.Cache
 // Note that the GoVersion field is not set here to avoid encoding it twice.
 // It is stored separately in the binary, mostly for historical reasons.
 func (p *Package) setBuildInfo(includeVCS bool) {
-       // TODO: build and vcs information is not embedded for executables in GOROOT.
-       // cmd/dist uses -gcflags=all= -ldflags=all= by default, which means these
-       // executables always appear stale unless the user sets the same flags.
-       // Perhaps it's safe to omit those flags when GO_GCFLAGS and GO_LDFLAGS
-       // are not set?
        setPkgErrorf := func(format string, args ...any) {
                if p.Error == nil {
                        p.Error = &PackageError{Err: fmt.Errorf(format, args...)}
@@ -2313,51 +2308,49 @@ func (p *Package) setBuildInfo(includeVCS bool) {
        // Add command-line flags relevant to the build.
        // This is informational, not an exhaustive list.
        // Please keep the list sorted.
-       if !p.Standard {
-               if cfg.BuildASan {
-                       appendSetting("-asan", "true")
-               }
-               if BuildAsmflags.present {
-                       appendSetting("-asmflags", BuildAsmflags.String())
-               }
-               appendSetting("-compiler", cfg.BuildContext.Compiler)
-               if BuildGccgoflags.present && cfg.BuildContext.Compiler == "gccgo" {
-                       appendSetting("-gccgoflags", BuildGccgoflags.String())
-               }
-               if BuildGcflags.present && cfg.BuildContext.Compiler == "gc" {
-                       appendSetting("-gcflags", BuildGcflags.String())
-               }
-               if BuildLdflags.present {
-                       appendSetting("-ldflags", BuildLdflags.String())
-               }
-               if cfg.BuildMSan {
-                       appendSetting("-msan", "true")
-               }
-               if cfg.BuildRace {
-                       appendSetting("-race", "true")
-               }
-               if tags := cfg.BuildContext.BuildTags; len(tags) > 0 {
-                       appendSetting("-tags", strings.Join(tags, ","))
-               }
-               cgo := "0"
-               if cfg.BuildContext.CgoEnabled {
-                       cgo = "1"
-               }
-               appendSetting("CGO_ENABLED", cgo)
-               if cfg.BuildContext.CgoEnabled {
-                       for _, name := range []string{"CGO_CFLAGS", "CGO_CPPFLAGS", "CGO_CXXFLAGS", "CGO_LDFLAGS"} {
-                               appendSetting(name, cfg.Getenv(name))
-                       }
-               }
-               appendSetting("GOARCH", cfg.BuildContext.GOARCH)
-               if cfg.RawGOEXPERIMENT != "" {
-                       appendSetting("GOEXPERIMENT", cfg.RawGOEXPERIMENT)
-               }
-               appendSetting("GOOS", cfg.BuildContext.GOOS)
-               if key, val := cfg.GetArchEnv(); key != "" && val != "" {
-                       appendSetting(key, val)
+       if cfg.BuildASan {
+               appendSetting("-asan", "true")
+       }
+       if BuildAsmflags.present {
+               appendSetting("-asmflags", BuildAsmflags.String())
+       }
+       appendSetting("-compiler", cfg.BuildContext.Compiler)
+       if gccgoflags := BuildGccgoflags.String(); gccgoflags != "" && cfg.BuildContext.Compiler == "gccgo" {
+               appendSetting("-gccgoflags", gccgoflags)
+       }
+       if gcflags := BuildGcflags.String(); gcflags != "" && cfg.BuildContext.Compiler == "gc" {
+               appendSetting("-gcflags", gcflags)
+       }
+       if ldflags := BuildLdflags.String(); ldflags != "" {
+               appendSetting("-ldflags", ldflags)
+       }
+       if cfg.BuildMSan {
+               appendSetting("-msan", "true")
+       }
+       if cfg.BuildRace {
+               appendSetting("-race", "true")
+       }
+       if tags := cfg.BuildContext.BuildTags; len(tags) > 0 {
+               appendSetting("-tags", strings.Join(tags, ","))
+       }
+       cgo := "0"
+       if cfg.BuildContext.CgoEnabled {
+               cgo = "1"
+       }
+       appendSetting("CGO_ENABLED", cgo)
+       if cfg.BuildContext.CgoEnabled {
+               for _, name := range []string{"CGO_CFLAGS", "CGO_CPPFLAGS", "CGO_CXXFLAGS", "CGO_LDFLAGS"} {
+                       appendSetting(name, cfg.Getenv(name))
                }
        }
+       appendSetting("GOARCH", cfg.BuildContext.GOARCH)
+       if cfg.RawGOEXPERIMENT != "" {
+               appendSetting("GOEXPERIMENT", cfg.RawGOEXPERIMENT)
+       }
+       appendSetting("GOOS", cfg.BuildContext.GOOS)
+       if key, val := cfg.GetArchEnv(); key != "" && val != "" {
+               appendSetting(key, val)
+       }
 
        // Add VCS status if all conditions are true:
        //