summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/go/go-1.15/0002-cmd-go-make-content-based-hash-generation-less-pedan.patch
blob: d0511c0c40562f8610174e3e63a3c4f1fa5dd220 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
From a13ae484e41139094505d2834437e9262a5315f7 Mon Sep 17 00:00:00 2001
From: Alex Kube <alexander.j.kube@gmail.com>
Date: Wed, 23 Oct 2019 21:14:22 +0430
Subject: [PATCH 2/9] cmd/go: make content-based hash generation less pedantic

Upstream-Status: Inappropriate [OE specific]

Go 1.10's build tool now uses content-based hashes to
determine when something should be built or re-built.
This same mechanism is used to maintain a built-artifact
cache for speeding up builds.

However, the hashes it generates include information that
doesn't work well with OE, nor with using a shared runtime
library.

First, it embeds path names to source files, unless
building within GOROOT.  This prevents the building
of a package in GOPATH for later staging into GOROOT.

This patch adds support for the environment variable
GOPATH_OMIT_IN_ACTIONID.  If present, path name
embedding is disabled.

Second, if cgo is enabled, the build ID for cgo-related
packages will include the current value of the environment
variables for invoking the compiler (CC, CXX, FC) and
any CGO_xxFLAGS variables.  Only if the settings used
during a compilation exactly match, character for character,
the values used for compiling runtime/cgo or any other
cgo-enabled package being imported, will the tool
decide that the imported package is up-to-date.

This is done to help ensure correctness, but is overly
simplistic and effectively prevents the reuse of built
artifacts that use cgo (or shared runtime, which includes
runtime/cgo).

This patch filters out all compiler flags except those
beginning with '-m'.  The default behavior can be restored
by setting the CGO_PEDANTIC environment variable.

Adapted to Go 1.13 from patches originally submitted to
the meta/recipes-devtools/go tree by
Matt Madison <matt@madison.systems>.

Signed-off-by: Alexander J Kube <alexander.j.kube@gmail.com>
---
 src/cmd/go/internal/envcmd/env.go |  2 +-
 src/cmd/go/internal/work/exec.go  | 66 ++++++++++++++++++++++---------
 2 files changed, 49 insertions(+), 19 deletions(-)

--- a/src/cmd/go/internal/envcmd/env.go
+++ b/src/cmd/go/internal/envcmd/env.go
@@ -157,7 +157,7 @@ func ExtraEnvVars() []cfg.EnvVar {
 func ExtraEnvVarsCostly() []cfg.EnvVar {
 	var b work.Builder
 	b.Init()
-	cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{})
+	cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{}, false)
 	if err != nil {
 		// Should not happen - b.CFlags was given an empty package.
 		fmt.Fprintf(os.Stderr, "go: invalid cflags: %v\n", err)
--- a/src/cmd/go/internal/work/exec.go
+++ b/src/cmd/go/internal/work/exec.go
@@ -33,6 +33,8 @@ import (
 	"cmd/go/internal/str"
 )
 
+var omitGopath = os.Getenv("GOPATH_OMIT_IN_ACTIONID") != ""
+
 // actionList returns the list of actions in the dag rooted at root
 // as visited in a depth-first post-order traversal.
 func actionList(root *Action) []*Action {
@@ -209,7 +211,7 @@ func (b *Builder) buildActionID(a *Actio
 	// Assume b.WorkDir is being trimmed properly.
 	// When -trimpath is used with a package built from the module cache,
 	// use the module path and version instead of the directory.
-	if !p.Goroot && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) {
+	if !p.Goroot && !omitGopath && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) {
 		fmt.Fprintf(h, "dir %s\n", p.Dir)
 	} else if cfg.BuildTrimpath && p.Module != nil {
 		fmt.Fprintf(h, "module %s@%s\n", p.Module.Path, p.Module.Version)
@@ -228,13 +230,13 @@ func (b *Builder) buildActionID(a *Actio
 	}
 	if len(p.CgoFiles)+len(p.SwigFiles) > 0 {
 		fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo"))
-		cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p)
-		fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(), cppflags, cflags, ldflags)
+		cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p, true)
+		fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(true), cppflags, cflags, ldflags)
 		if len(p.CXXFiles)+len(p.SwigFiles) > 0 {
-			fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(), cxxflags)
+			fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(true), cxxflags)
 		}
 		if len(p.FFiles) > 0 {
-			fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(), fflags)
+			fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(true), fflags)
 		}
 		// TODO(rsc): Should we include the SWIG version or Fortran/GCC/G++/Objective-C compiler versions?
 	}
@@ -2298,33 +2300,48 @@ var (
 // gccCmd returns a gcc command line prefix
 // defaultCC is defined in zdefaultcc.go, written by cmd/dist.
 func (b *Builder) GccCmd(incdir, workdir string) []string {
-	return b.compilerCmd(b.ccExe(), incdir, workdir)
+	return b.compilerCmd(b.ccExe(false), incdir, workdir)
 }
 
 // gxxCmd returns a g++ command line prefix
 // defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
 func (b *Builder) GxxCmd(incdir, workdir string) []string {
-	return b.compilerCmd(b.cxxExe(), incdir, workdir)
+	return b.compilerCmd(b.cxxExe(false), incdir, workdir)
 }
 
 // gfortranCmd returns a gfortran command line prefix.
 func (b *Builder) gfortranCmd(incdir, workdir string) []string {
-	return b.compilerCmd(b.fcExe(), incdir, workdir)
+	return b.compilerCmd(b.fcExe(false), incdir, workdir)
 }
 
 // ccExe returns the CC compiler setting without all the extra flags we add implicitly.
-func (b *Builder) ccExe() []string {
-	return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch))
+func (b *Builder) ccExe(filtered bool) []string {
+	return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch), filtered)
 }
 
 // cxxExe returns the CXX compiler setting without all the extra flags we add implicitly.
-func (b *Builder) cxxExe() []string {
-	return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
+func (b *Builder) cxxExe(filtered bool) []string {
+	return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch), filtered)
 }
 
 // fcExe returns the FC compiler setting without all the extra flags we add implicitly.
-func (b *Builder) fcExe() []string {
-	return b.compilerExe(cfg.Getenv("FC"), "gfortran")
+func (b *Builder) fcExe(filtered bool) []string {
+	return b.compilerExe(os.Getenv("FC"), "gfortran", filtered)
+}
+
+var filterFlags = os.Getenv("CGO_PEDANTIC") == ""
+
+func filterCompilerFlags(flags []string) []string {
+	var newflags []string
+	if !filterFlags {
+		return flags
+	}
+	for _, flag := range flags {
+		if strings.HasPrefix(flag, "-m") {
+			newflags = append(newflags, flag)
+		}
+	}
+	return newflags
 }
 
 // compilerExe returns the compiler to use given an
@@ -2333,11 +2350,16 @@ func (b *Builder) fcExe() []string {
 // of the compiler but can have additional arguments if they
 // were present in the environment value.
 // For example if CC="gcc -DGOPHER" then the result is ["gcc", "-DGOPHER"].
-func (b *Builder) compilerExe(envValue string, def string) []string {
+func (b *Builder) compilerExe(envValue string, def string, filtered bool) []string {
 	compiler := strings.Fields(envValue)
 	if len(compiler) == 0 {
 		compiler = []string{def}
 	}
+
+	if filtered {
+		return append(compiler[0:1], filterCompilerFlags(compiler[1:])...)
+	}
+
 	return compiler
 }
 
@@ -2510,7 +2532,7 @@ func envList(key, def string) []string {
 }
 
 // CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo.
-func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
+func (b *Builder) CFlags(p *load.Package, filtered bool) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
 	defaults := "-g -O2"
 
 	if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
@@ -2529,6 +2551,14 @@ func (b *Builder) CFlags(p *load.Package
 		return
 	}
 
+	if filtered {
+		cppflags = filterCompilerFlags(cppflags)
+		cflags = filterCompilerFlags(cflags)
+		cxxflags = filterCompilerFlags(cxxflags)
+		fflags = filterCompilerFlags(fflags)
+		ldflags = filterCompilerFlags(ldflags)
+	}
+
 	return
 }
 
@@ -2543,7 +2573,7 @@ var cgoRe = lazyregexp.New(`[/\\:]`)
 
 func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
 	p := a.Package
-	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
+	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p, false)
 	if err != nil {
 		return nil, nil, err
 	}
@@ -2902,7 +2932,7 @@ func (b *Builder) swigIntSize(objdir str
 
 // Run SWIG on one SWIG input file.
 func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
-	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
+	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p, false)
 	if err != nil {
 		return "", "", err
 	}