| From 6c2438f187ca912c54a71b4ac65ab98999a019d2 Mon Sep 17 00:00:00 2001 |
| From: Richard Purdie <richard.purdie@linuxfoundation.org> |
| Date: Sat, 2 Jul 2022 23:08:13 +0100 |
| Subject: [PATCH 9/9] go: Filter build paths on staticly linked arches |
| |
| Filter out build time paths from ldflags and other flags variables when they're |
| embedded in the go binary so that builds are reproducible regardless of build |
| location. This codepath is hit for statically linked go binaries such as those |
| on mips/ppc. |
| |
| Upstream-Status: Submitted [https://github.com/golang/go/pull/56410] |
| |
| Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org> |
| Signed-off-by: Jose Quaresma <jose.quaresma@foundries.io> |
| --- |
| src/cmd/go/internal/load/pkg.go | 15 +++++++++++++-- |
| 1 file changed, 13 insertions(+), 2 deletions(-) |
| |
| diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go |
| index 1549800afb..f41fb2c4ef 100644 |
| --- a/src/cmd/go/internal/load/pkg.go |
| +++ b/src/cmd/go/internal/load/pkg.go |
| @@ -2277,6 +2277,17 @@ func appendBuildSetting(info *debug.BuildInfo, key, value string) { |
| info.Settings = append(info.Settings, debug.BuildSetting{Key: key, Value: value}) |
| } |
| |
| +func filterCompilerFlags(flags string) string { |
| + var newflags []string |
| + for _, flag := range strings.Fields(flags) { |
| + if strings.HasPrefix(flag, "--sysroot") || strings.HasPrefix(flag, "-fmacro-prefix-map") || strings.HasPrefix(flag, "-fdebug-prefix-map") { |
| + continue |
| + } |
| + newflags = append(newflags, flag) |
| + } |
| + return strings.Join(newflags, " ") |
| +} |
| + |
| // setBuildInfo gathers build information and sets it into |
| // p.Internal.BuildInfo, which will later be formatted as a string and embedded |
| // in the binary. setBuildInfo should only be called on a main package with no |
| @@ -2384,7 +2395,7 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) { |
| if gcflags := BuildGcflags.String(); gcflags != "" && cfg.BuildContext.Compiler == "gc" { |
| appendSetting("-gcflags", gcflags) |
| } |
| - if ldflags := BuildLdflags.String(); ldflags != "" { |
| + if ldflags := filterCompilerFlags(BuildLdflags.String()); ldflags != "" { |
| // https://go.dev/issue/52372: only include ldflags if -trimpath is not set, |
| // since it can include system paths through various linker flags (notably |
| // -extar, -extld, and -extldflags). |
| @@ -2427,7 +2438,7 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) { |
| // subset of flags that are known not to be paths? |
| if cfg.BuildContext.CgoEnabled && !cfg.BuildTrimpath { |
| for _, name := range []string{"CGO_CFLAGS", "CGO_CPPFLAGS", "CGO_CXXFLAGS", "CGO_LDFLAGS"} { |
| - appendSetting(name, cfg.Getenv(name)) |
| + appendSetting(name, filterCompilerFlags(cfg.Getenv(name))) |
| } |
| } |
| appendSetting("GOARCH", cfg.BuildContext.GOARCH) |
| -- |
| 2.44.0 |
| |