blob: 705c9221419bb9863625649f05ca5327b5cc3f7d [file] [log] [blame]
Andrew Geissler6aa7eec2023-03-03 12:41:14 -06001From 18011f72125bbea273d07ee5d792ac0ce6059572 Mon Sep 17 00:00:00 2001
Patrick Williams92b42cb2022-09-03 06:53:57 -05002From: Richard Purdie <richard.purdie@linuxfoundation.org>
3Date: Sat, 2 Jul 2022 23:08:13 +0100
Andrew Geissler6aa7eec2023-03-03 12:41:14 -06004Subject: [PATCH 9/9] go: Filter build paths on staticly linked arches
Patrick Williams92b42cb2022-09-03 06:53:57 -05005
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>
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
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060019index 56a4e5e..22edbdb 100644
Patrick Williams92b42cb2022-09-03 06:53:57 -050020--- a/src/cmd/go/internal/load/pkg.go
21+++ b/src/cmd/go/internal/load/pkg.go
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060022@@ -2266,6 +2266,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.
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060040@@ -2372,7 +2383,7 @@ func (p *Package) setBuildInfo(autoVCS bool) {
Patrick Williams92b42cb2022-09-03 06:53:57 -050041 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).
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060049@@ -2418,7 +2429,7 @@ func (p *Package) setBuildInfo(autoVCS bool) {
Patrick Williams92b42cb2022-09-03 06:53:57 -050050 // 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)
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060058--
592.30.2
60