From 0433f5770b9c92b06d5a15c8d2a70f00597faac4 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Tue, 8 Mar 2022 17:01:12 -0500 Subject: [PATCH] cmd/go: stamp build settings for binaries in cmd 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 Run-TryBot: Bryan Mills Reviewed-by: Russ Cox TryBot-Result: Gopher Robot --- src/cmd/dist/build.go | 24 ++++++--- src/cmd/dist/test.go | 4 +- src/cmd/go/internal/load/pkg.go | 89 +++++++++++++++------------------ 3 files changed, 61 insertions(+), 56 deletions(-) diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go index d37c3f83efb4d..ba09ce9a7b8d7 100644 --- a/src/cmd/dist/build.go +++ b/src/cmd/dist/build.go @@ -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"} { diff --git a/src/cmd/dist/test.go b/src/cmd/dist/test.go index cd3c26ab3a334..a540a2abda669 100644 --- a/src/cmd/dist/test.go +++ b/src/cmd/dist/test.go @@ -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") diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 403bc330e776c..ab70845959650 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -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: //