Skip to content

Commit 0433f57

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent d68615f commit 0433f57

File tree

3 files changed

+61
-56
lines changed

3 files changed

+61
-56
lines changed

src/cmd/dist/build.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,8 +1502,19 @@ func goInstall(goBinary string, args ...string) {
15021502
goCmd(goBinary, "install", args...)
15031503
}
15041504

1505+
func appendCompilerFlags(args []string) []string {
1506+
if gogcflags != "" {
1507+
args = append(args, "-gcflags=all="+gogcflags)
1508+
}
1509+
if goldflags != "" {
1510+
args = append(args, "-ldflags=all="+goldflags)
1511+
}
1512+
return args
1513+
}
1514+
15051515
func goCmd(goBinary string, cmd string, args ...string) {
1506-
goCmd := []string{goBinary, cmd, "-gcflags=all=" + gogcflags, "-ldflags=all=" + goldflags}
1516+
goCmd := []string{goBinary, cmd}
1517+
goCmd = appendCompilerFlags(goCmd)
15071518
if vflag > 0 {
15081519
goCmd = append(goCmd, "-v")
15091520
}
@@ -1517,12 +1528,11 @@ func goCmd(goBinary string, cmd string, args ...string) {
15171528
}
15181529

15191530
func checkNotStale(goBinary string, targets ...string) {
1520-
out := run(workdir, CheckExit,
1521-
append([]string{
1522-
goBinary,
1523-
"list", "-gcflags=all=" + gogcflags, "-ldflags=all=" + goldflags,
1524-
"-f={{if .Stale}}\tSTALE {{.ImportPath}}: {{.StaleReason}}{{end}}",
1525-
}, targets...)...)
1531+
goCmd := []string{goBinary, "list"}
1532+
goCmd = appendCompilerFlags(goCmd)
1533+
goCmd = append(goCmd, "-f={{if .Stale}}\tSTALE {{.ImportPath}}: {{.StaleReason}}{{end}}")
1534+
1535+
out := run(workdir, CheckExit, append(goCmd, targets...)...)
15261536
if strings.Contains(out, "\tSTALE ") {
15271537
os.Setenv("GODEBUG", "gocachehash=1")
15281538
for _, target := range []string{"runtime/internal/sys", "cmd/dist", "cmd/link"} {

src/cmd/dist/test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,9 @@ func (t *tester) registerStdTest(pkg string) {
397397
"-short=" + short(),
398398
t.tags(),
399399
t.timeout(timeoutSec),
400-
"-gcflags=all=" + gcflags,
400+
}
401+
if gcflags != "" {
402+
args = append(args, "-gcflags=all="+gcflags)
401403
}
402404
if t.race {
403405
args = append(args, "-race")

src/cmd/go/internal/load/pkg.go

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,11 +2233,6 @@ var vcsStatusCache par.Cache
22332233
// Note that the GoVersion field is not set here to avoid encoding it twice.
22342234
// It is stored separately in the binary, mostly for historical reasons.
22352235
func (p *Package) setBuildInfo(includeVCS bool) {
2236-
// TODO: build and vcs information is not embedded for executables in GOROOT.
2237-
// cmd/dist uses -gcflags=all= -ldflags=all= by default, which means these
2238-
// executables always appear stale unless the user sets the same flags.
2239-
// Perhaps it's safe to omit those flags when GO_GCFLAGS and GO_LDFLAGS
2240-
// are not set?
22412236
setPkgErrorf := func(format string, args ...any) {
22422237
if p.Error == nil {
22432238
p.Error = &PackageError{Err: fmt.Errorf(format, args...)}
@@ -2313,51 +2308,49 @@ func (p *Package) setBuildInfo(includeVCS bool) {
23132308
// Add command-line flags relevant to the build.
23142309
// This is informational, not an exhaustive list.
23152310
// Please keep the list sorted.
2316-
if !p.Standard {
2317-
if cfg.BuildASan {
2318-
appendSetting("-asan", "true")
2319-
}
2320-
if BuildAsmflags.present {
2321-
appendSetting("-asmflags", BuildAsmflags.String())
2322-
}
2323-
appendSetting("-compiler", cfg.BuildContext.Compiler)
2324-
if BuildGccgoflags.present && cfg.BuildContext.Compiler == "gccgo" {
2325-
appendSetting("-gccgoflags", BuildGccgoflags.String())
2326-
}
2327-
if BuildGcflags.present && cfg.BuildContext.Compiler == "gc" {
2328-
appendSetting("-gcflags", BuildGcflags.String())
2329-
}
2330-
if BuildLdflags.present {
2331-
appendSetting("-ldflags", BuildLdflags.String())
2332-
}
2333-
if cfg.BuildMSan {
2334-
appendSetting("-msan", "true")
2335-
}
2336-
if cfg.BuildRace {
2337-
appendSetting("-race", "true")
2338-
}
2339-
if tags := cfg.BuildContext.BuildTags; len(tags) > 0 {
2340-
appendSetting("-tags", strings.Join(tags, ","))
2341-
}
2342-
cgo := "0"
2343-
if cfg.BuildContext.CgoEnabled {
2344-
cgo = "1"
2345-
}
2346-
appendSetting("CGO_ENABLED", cgo)
2347-
if cfg.BuildContext.CgoEnabled {
2348-
for _, name := range []string{"CGO_CFLAGS", "CGO_CPPFLAGS", "CGO_CXXFLAGS", "CGO_LDFLAGS"} {
2349-
appendSetting(name, cfg.Getenv(name))
2350-
}
2351-
}
2352-
appendSetting("GOARCH", cfg.BuildContext.GOARCH)
2353-
if cfg.RawGOEXPERIMENT != "" {
2354-
appendSetting("GOEXPERIMENT", cfg.RawGOEXPERIMENT)
2355-
}
2356-
appendSetting("GOOS", cfg.BuildContext.GOOS)
2357-
if key, val := cfg.GetArchEnv(); key != "" && val != "" {
2358-
appendSetting(key, val)
2311+
if cfg.BuildASan {
2312+
appendSetting("-asan", "true")
2313+
}
2314+
if BuildAsmflags.present {
2315+
appendSetting("-asmflags", BuildAsmflags.String())
2316+
}
2317+
appendSetting("-compiler", cfg.BuildContext.Compiler)
2318+
if gccgoflags := BuildGccgoflags.String(); gccgoflags != "" && cfg.BuildContext.Compiler == "gccgo" {
2319+
appendSetting("-gccgoflags", gccgoflags)
2320+
}
2321+
if gcflags := BuildGcflags.String(); gcflags != "" && cfg.BuildContext.Compiler == "gc" {
2322+
appendSetting("-gcflags", gcflags)
2323+
}
2324+
if ldflags := BuildLdflags.String(); ldflags != "" {
2325+
appendSetting("-ldflags", ldflags)
2326+
}
2327+
if cfg.BuildMSan {
2328+
appendSetting("-msan", "true")
2329+
}
2330+
if cfg.BuildRace {
2331+
appendSetting("-race", "true")
2332+
}
2333+
if tags := cfg.BuildContext.BuildTags; len(tags) > 0 {
2334+
appendSetting("-tags", strings.Join(tags, ","))
2335+
}
2336+
cgo := "0"
2337+
if cfg.BuildContext.CgoEnabled {
2338+
cgo = "1"
2339+
}
2340+
appendSetting("CGO_ENABLED", cgo)
2341+
if cfg.BuildContext.CgoEnabled {
2342+
for _, name := range []string{"CGO_CFLAGS", "CGO_CPPFLAGS", "CGO_CXXFLAGS", "CGO_LDFLAGS"} {
2343+
appendSetting(name, cfg.Getenv(name))
23592344
}
23602345
}
2346+
appendSetting("GOARCH", cfg.BuildContext.GOARCH)
2347+
if cfg.RawGOEXPERIMENT != "" {
2348+
appendSetting("GOEXPERIMENT", cfg.RawGOEXPERIMENT)
2349+
}
2350+
appendSetting("GOOS", cfg.BuildContext.GOOS)
2351+
if key, val := cfg.GetArchEnv(); key != "" && val != "" {
2352+
appendSetting(key, val)
2353+
}
23612354

23622355
// Add VCS status if all conditions are true:
23632356
//

0 commit comments

Comments
 (0)