Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | From 3bdbce685c688a27eece36ccc8be9b50b4849498 Mon Sep 17 00:00:00 2001 |
| 2 | From: Richard Purdie <richard.purdie@linuxfoundation.org> |
| 3 | Date: Sat, 2 Jul 2022 23:08:13 +0100 |
| 4 | Subject: [PATCH] go: Filter build paths on staticly linked arches |
| 5 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 6 | Filter out build time paths from ldflags and other flags variables when they're |
| 7 | embedded in the go binary so that builds are reproducible regardless of build |
| 8 | location. This codepath is hit for statically linked go binaries such as those |
| 9 | on mips/ppc. |
| 10 | |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame^] | 11 | Upstream-Status: Submitted [https://github.com/golang/go/pull/56410] |
| 12 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 13 | Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org> |
| 14 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 15 | --- |
| 16 | src/cmd/go/internal/load/pkg.go | 15 +++++++++++++-- |
| 17 | 1 file changed, 13 insertions(+), 2 deletions(-) |
| 18 | |
| 19 | diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go |
| 20 | index 046f508..353cbc4 100644 |
| 21 | --- a/src/cmd/go/internal/load/pkg.go |
| 22 | +++ b/src/cmd/go/internal/load/pkg.go |
| 23 | @@ -2256,6 +2256,17 @@ func (p *Package) collectDeps() { |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 24 | // to their VCS information (vcsStatusError). |
| 25 | var vcsStatusCache par.Cache |
| 26 | |
| 27 | +func filterCompilerFlags(flags string) string { |
| 28 | + var newflags []string |
| 29 | + for _, flag := range strings.Fields(flags) { |
| 30 | + if strings.HasPrefix(flag, "--sysroot") || strings.HasPrefix(flag, "-fmacro-prefix-map") || strings.HasPrefix(flag, "-fdebug-prefix-map") { |
| 31 | + continue |
| 32 | + } |
| 33 | + newflags = append(newflags, flag) |
| 34 | + } |
| 35 | + return strings.Join(newflags, " ") |
| 36 | +} |
| 37 | + |
| 38 | // setBuildInfo gathers build information, formats it as a string to be |
| 39 | // embedded in the binary, then sets p.Internal.BuildInfo to that string. |
| 40 | // setBuildInfo should only be called on a main package with no errors. |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 41 | @@ -2353,7 +2364,7 @@ func (p *Package) setBuildInfo(includeVCS bool) { |
| 42 | if gcflags := BuildGcflags.String(); gcflags != "" && cfg.BuildContext.Compiler == "gc" { |
| 43 | appendSetting("-gcflags", gcflags) |
| 44 | } |
| 45 | - if ldflags := BuildLdflags.String(); ldflags != "" { |
| 46 | + if ldflags := filterCompilerFlags(BuildLdflags.String()); ldflags != "" { |
| 47 | // https://go.dev/issue/52372: only include ldflags if -trimpath is not set, |
| 48 | // since it can include system paths through various linker flags (notably |
| 49 | // -extar, -extld, and -extldflags). |
| 50 | @@ -2392,7 +2403,7 @@ func (p *Package) setBuildInfo(includeVCS bool) { |
| 51 | // subset of flags that are known not to be paths? |
| 52 | if cfg.BuildContext.CgoEnabled && !cfg.BuildTrimpath { |
| 53 | for _, name := range []string{"CGO_CFLAGS", "CGO_CPPFLAGS", "CGO_CXXFLAGS", "CGO_LDFLAGS"} { |
| 54 | - appendSetting(name, cfg.Getenv(name)) |
| 55 | + appendSetting(name, filterCompilerFlags(cfg.Getenv(name))) |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 56 | } |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 57 | } |
| 58 | appendSetting("GOARCH", cfg.BuildContext.GOARCH) |