summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/go/go/0009-go-Filter-build-paths-on-staticly-linked-arches.patch
blob: 5e751e032d863fd754d502c79568c03800fbca9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
From 18011f72125bbea273d07ee5d792ac0ce6059572 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>
---
 src/cmd/go/internal/load/pkg.go | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -2264,6 +2264,17 @@ func isBadEmbedName(name string) bool {
 // to their VCS information.
 var vcsStatusCache par.ErrCache[string, vcs.Status]
 
+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, " ")
+}
+
 func appendBuildSetting(info *debug.BuildInfo, key, value string) {
 	value = strings.ReplaceAll(value, "\n", " ") // make value safe
 	info.Settings = append(info.Settings, debug.BuildSetting{Key: key, Value: value})
@@ -2376,7 +2387,7 @@ func (p *Package) setBuildInfo(ctx conte
 	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).
@@ -2419,7 +2430,7 @@ func (p *Package) setBuildInfo(ctx conte
 	// 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)