Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame^] | 1 | Filter out build time paths from ldflags and other flags variables when they're |
| 2 | embedded in the go binary so that builds are reproducible regardless of build |
| 3 | location. This codepath is hit for statically linked go binaries such as those |
| 4 | on mips/ppc. |
| 5 | |
| 6 | Upstream-Status: Pending |
| 7 | Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org> |
| 8 | |
| 9 | Index: go/src/cmd/go/internal/load/pkg.go |
| 10 | =================================================================== |
| 11 | --- go.orig/src/cmd/go/internal/load/pkg.go |
| 12 | +++ go/src/cmd/go/internal/load/pkg.go |
| 13 | @@ -2225,6 +2225,17 @@ func (p *Package) collectDeps() { |
| 14 | // to their VCS information (vcsStatusError). |
| 15 | var vcsStatusCache par.Cache |
| 16 | |
| 17 | +func filterCompilerFlags(flags string) string { |
| 18 | + var newflags []string |
| 19 | + for _, flag := range strings.Fields(flags) { |
| 20 | + if strings.HasPrefix(flag, "--sysroot") || strings.HasPrefix(flag, "-fmacro-prefix-map") || strings.HasPrefix(flag, "-fdebug-prefix-map") { |
| 21 | + continue |
| 22 | + } |
| 23 | + newflags = append(newflags, flag) |
| 24 | + } |
| 25 | + return strings.Join(newflags, " ") |
| 26 | +} |
| 27 | + |
| 28 | // setBuildInfo gathers build information, formats it as a string to be |
| 29 | // embedded in the binary, then sets p.Internal.BuildInfo to that string. |
| 30 | // setBuildInfo should only be called on a main package with no errors. |
| 31 | @@ -2329,7 +2340,7 @@ func (p *Package) setBuildInfo(includeVC |
| 32 | appendSetting("-gcflags", BuildGcflags.String()) |
| 33 | } |
| 34 | if BuildLdflags.present { |
| 35 | - appendSetting("-ldflags", BuildLdflags.String()) |
| 36 | + appendSetting("-ldflags", filterCompilerFlags(BuildLdflags.String())) |
| 37 | } |
| 38 | if cfg.BuildMSan { |
| 39 | appendSetting("-msan", "true") |
| 40 | @@ -2347,7 +2358,7 @@ func (p *Package) setBuildInfo(includeVC |
| 41 | appendSetting("CGO_ENABLED", cgo) |
| 42 | if cfg.BuildContext.CgoEnabled { |
| 43 | for _, name := range []string{"CGO_CFLAGS", "CGO_CPPFLAGS", "CGO_CXXFLAGS", "CGO_LDFLAGS"} { |
| 44 | - appendSetting(name, cfg.Getenv(name)) |
| 45 | + appendSetting(name, filterCompilerFlags(cfg.Getenv(name))) |
| 46 | } |
| 47 | } |
| 48 | appendSetting("GOARCH", cfg.BuildContext.GOARCH) |