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