blob: 280f911a21f600155564a55310b3a2327d3657b2 [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
Patrick Williams2390b1b2022-11-03 13:47:49 -050011Upstream-Status: Submitted [https://github.com/golang/go/pull/56410]
12
Andrew Geissler615f2f12022-07-15 14:00:58 -050013Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
14
Patrick Williams92b42cb2022-09-03 06:53:57 -050015---
16 src/cmd/go/internal/load/pkg.go | 15 +++++++++++++--
17 1 file changed, 13 insertions(+), 2 deletions(-)
18
19diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
20index 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 Geissler615f2f12022-07-15 14:00:58 -050024 // 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 Williams92b42cb2022-09-03 06:53:57 -050041@@ -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 Geissler615f2f12022-07-15 14:00:58 -050056 }
Patrick Williams92b42cb2022-09-03 06:53:57 -050057 }
58 appendSetting("GOARCH", cfg.BuildContext.GOARCH)