1*4882a593SmuzhiyunFrom c403b45995c5daa6747ac4d95b39bc9a6feb2cda Mon Sep 17 00:00:00 2001
2*4882a593SmuzhiyunFrom: Alex Kube <alexander.j.kube@gmail.com>
3*4882a593SmuzhiyunDate: Wed, 23 Oct 2019 21:14:22 +0430
4*4882a593SmuzhiyunSubject: [PATCH] cmd/go: make content-based hash generation less pedantic
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunUpstream-Status: Inappropriate [OE specific]
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunGo 1.10's build tool now uses content-based hashes to
9*4882a593Smuzhiyundetermine when something should be built or re-built.
10*4882a593SmuzhiyunThis same mechanism is used to maintain a built-artifact
11*4882a593Smuzhiyuncache for speeding up builds.
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunHowever, the hashes it generates include information that
14*4882a593Smuzhiyundoesn't work well with OE, nor with using a shared runtime
15*4882a593Smuzhiyunlibrary.
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunFirst, it embeds path names to source files, unless
18*4882a593Smuzhiyunbuilding within GOROOT.  This prevents the building
19*4882a593Smuzhiyunof a package in GOPATH for later staging into GOROOT.
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunThis patch adds support for the environment variable
22*4882a593SmuzhiyunGOPATH_OMIT_IN_ACTIONID.  If present, path name
23*4882a593Smuzhiyunembedding is disabled.
24*4882a593Smuzhiyun
25*4882a593SmuzhiyunSecond, if cgo is enabled, the build ID for cgo-related
26*4882a593Smuzhiyunpackages will include the current value of the environment
27*4882a593Smuzhiyunvariables for invoking the compiler (CC, CXX, FC) and
28*4882a593Smuzhiyunany CGO_xxFLAGS variables.  Only if the settings used
29*4882a593Smuzhiyunduring a compilation exactly match, character for character,
30*4882a593Smuzhiyunthe values used for compiling runtime/cgo or any other
31*4882a593Smuzhiyuncgo-enabled package being imported, will the tool
32*4882a593Smuzhiyundecide that the imported package is up-to-date.
33*4882a593Smuzhiyun
34*4882a593SmuzhiyunThis is done to help ensure correctness, but is overly
35*4882a593Smuzhiyunsimplistic and effectively prevents the reuse of built
36*4882a593Smuzhiyunartifacts that use cgo (or shared runtime, which includes
37*4882a593Smuzhiyunruntime/cgo).
38*4882a593Smuzhiyun
39*4882a593SmuzhiyunThis patch filters out all compiler flags except those
40*4882a593Smuzhiyunbeginning with '-m'.  The default behavior can be restored
41*4882a593Smuzhiyunby setting the CGO_PEDANTIC environment variable.
42*4882a593Smuzhiyun
43*4882a593SmuzhiyunAdapted to Go 1.13 from patches originally submitted to
44*4882a593Smuzhiyunthe meta/recipes-devtools/go tree by
45*4882a593SmuzhiyunMatt Madison <matt@madison.systems>.
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunSigned-off-by: Alexander J Kube <alexander.j.kube@gmail.com>
48*4882a593Smuzhiyun---
49*4882a593Smuzhiyun src/cmd/go/internal/envcmd/env.go |  2 +-
50*4882a593Smuzhiyun src/cmd/go/internal/work/exec.go  | 66 ++++++++++++++++++++++---------
51*4882a593Smuzhiyun 2 files changed, 49 insertions(+), 19 deletions(-)
52*4882a593Smuzhiyun
53*4882a593Smuzhiyundiff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go
54*4882a593Smuzhiyunindex 20d0587..ff6f0d8 100644
55*4882a593Smuzhiyun--- a/src/cmd/go/internal/envcmd/env.go
56*4882a593Smuzhiyun+++ b/src/cmd/go/internal/envcmd/env.go
57*4882a593Smuzhiyun@@ -160,7 +160,7 @@ func ExtraEnvVars() []cfg.EnvVar {
58*4882a593Smuzhiyun func ExtraEnvVarsCostly() []cfg.EnvVar {
59*4882a593Smuzhiyun 	var b work.Builder
60*4882a593Smuzhiyun 	b.Init()
61*4882a593Smuzhiyun-	cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{})
62*4882a593Smuzhiyun+	cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{}, false)
63*4882a593Smuzhiyun 	if err != nil {
64*4882a593Smuzhiyun 		// Should not happen - b.CFlags was given an empty package.
65*4882a593Smuzhiyun 		fmt.Fprintf(os.Stderr, "go: invalid cflags: %v\n", err)
66*4882a593Smuzhiyundiff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
67*4882a593Smuzhiyunindex 5a225fb..a37872e 100644
68*4882a593Smuzhiyun--- a/src/cmd/go/internal/work/exec.go
69*4882a593Smuzhiyun+++ b/src/cmd/go/internal/work/exec.go
70*4882a593Smuzhiyun@@ -38,6 +38,8 @@ import (
71*4882a593Smuzhiyun 	"cmd/go/internal/trace"
72*4882a593Smuzhiyun )
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun+var omitGopath = os.Getenv("GOPATH_OMIT_IN_ACTIONID") != ""
75*4882a593Smuzhiyun+
76*4882a593Smuzhiyun // actionList returns the list of actions in the dag rooted at root
77*4882a593Smuzhiyun // as visited in a depth-first post-order traversal.
78*4882a593Smuzhiyun func actionList(root *Action) []*Action {
79*4882a593Smuzhiyun@@ -229,7 +231,7 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
80*4882a593Smuzhiyun 	// Assume b.WorkDir is being trimmed properly.
81*4882a593Smuzhiyun 	// When -trimpath is used with a package built from the module cache,
82*4882a593Smuzhiyun 	// use the module path and version instead of the directory.
83*4882a593Smuzhiyun-	if !p.Goroot && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) {
84*4882a593Smuzhiyun+	if !p.Goroot && !omitGopath && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) {
85*4882a593Smuzhiyun 		fmt.Fprintf(h, "dir %s\n", p.Dir)
86*4882a593Smuzhiyun 	} else if cfg.BuildTrimpath && p.Module != nil {
87*4882a593Smuzhiyun 		fmt.Fprintf(h, "module %s@%s\n", p.Module.Path, p.Module.Version)
88*4882a593Smuzhiyun@@ -248,9 +250,9 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
89*4882a593Smuzhiyun 	}
90*4882a593Smuzhiyun 	if len(p.CgoFiles)+len(p.SwigFiles)+len(p.SwigCXXFiles) > 0 {
91*4882a593Smuzhiyun 		fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo"))
92*4882a593Smuzhiyun-		cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p)
93*4882a593Smuzhiyun+		cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p, true)
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun-		ccExe := b.ccExe()
96*4882a593Smuzhiyun+		ccExe := b.ccExe(true)
97*4882a593Smuzhiyun 		fmt.Fprintf(h, "CC=%q %q %q %q\n", ccExe, cppflags, cflags, ldflags)
98*4882a593Smuzhiyun 		// Include the C compiler tool ID so that if the C
99*4882a593Smuzhiyun 		// compiler changes we rebuild the package.
100*4882a593Smuzhiyun@@ -263,14 +265,14 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
101*4882a593Smuzhiyun 			}
102*4882a593Smuzhiyun 		}
103*4882a593Smuzhiyun 		if len(p.CXXFiles)+len(p.SwigCXXFiles) > 0 {
104*4882a593Smuzhiyun-			cxxExe := b.cxxExe()
105*4882a593Smuzhiyun+			cxxExe := b.cxxExe(true)
106*4882a593Smuzhiyun 			fmt.Fprintf(h, "CXX=%q %q\n", cxxExe, cxxflags)
107*4882a593Smuzhiyun 			if cxxID, err := b.gccToolID(cxxExe[0], "c++"); err == nil {
108*4882a593Smuzhiyun 				fmt.Fprintf(h, "CXX ID=%q\n", cxxID)
109*4882a593Smuzhiyun 			}
110*4882a593Smuzhiyun 		}
111*4882a593Smuzhiyun 		if len(p.FFiles) > 0 {
112*4882a593Smuzhiyun-			fcExe := b.fcExe()
113*4882a593Smuzhiyun+			fcExe := b.fcExe(true)
114*4882a593Smuzhiyun 			fmt.Fprintf(h, "FC=%q %q\n", fcExe, fflags)
115*4882a593Smuzhiyun 			if fcID, err := b.gccToolID(fcExe[0], "f95"); err == nil {
116*4882a593Smuzhiyun 				fmt.Fprintf(h, "FC ID=%q\n", fcID)
117*4882a593Smuzhiyun@@ -2438,33 +2440,48 @@ var (
118*4882a593Smuzhiyun // gccCmd returns a gcc command line prefix
119*4882a593Smuzhiyun // defaultCC is defined in zdefaultcc.go, written by cmd/dist.
120*4882a593Smuzhiyun func (b *Builder) GccCmd(incdir, workdir string) []string {
121*4882a593Smuzhiyun-	return b.compilerCmd(b.ccExe(), incdir, workdir)
122*4882a593Smuzhiyun+	return b.compilerCmd(b.ccExe(false), incdir, workdir)
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun // gxxCmd returns a g++ command line prefix
126*4882a593Smuzhiyun // defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
127*4882a593Smuzhiyun func (b *Builder) GxxCmd(incdir, workdir string) []string {
128*4882a593Smuzhiyun-	return b.compilerCmd(b.cxxExe(), incdir, workdir)
129*4882a593Smuzhiyun+	return b.compilerCmd(b.cxxExe(false), incdir, workdir)
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun // gfortranCmd returns a gfortran command line prefix.
133*4882a593Smuzhiyun func (b *Builder) gfortranCmd(incdir, workdir string) []string {
134*4882a593Smuzhiyun-	return b.compilerCmd(b.fcExe(), incdir, workdir)
135*4882a593Smuzhiyun+	return b.compilerCmd(b.fcExe(false), incdir, workdir)
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun // ccExe returns the CC compiler setting without all the extra flags we add implicitly.
139*4882a593Smuzhiyun-func (b *Builder) ccExe() []string {
140*4882a593Smuzhiyun-	return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch))
141*4882a593Smuzhiyun+func (b *Builder) ccExe(filtered bool) []string {
142*4882a593Smuzhiyun+	return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch), filtered)
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun // cxxExe returns the CXX compiler setting without all the extra flags we add implicitly.
146*4882a593Smuzhiyun-func (b *Builder) cxxExe() []string {
147*4882a593Smuzhiyun-	return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
148*4882a593Smuzhiyun+func (b *Builder) cxxExe(filtered bool) []string {
149*4882a593Smuzhiyun+	return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch), filtered)
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun // fcExe returns the FC compiler setting without all the extra flags we add implicitly.
153*4882a593Smuzhiyun-func (b *Builder) fcExe() []string {
154*4882a593Smuzhiyun-	return b.compilerExe(cfg.Getenv("FC"), "gfortran")
155*4882a593Smuzhiyun+func (b *Builder) fcExe(filtered bool) []string {
156*4882a593Smuzhiyun+	return b.compilerExe(os.Getenv("FC"), "gfortran", filtered)
157*4882a593Smuzhiyun+}
158*4882a593Smuzhiyun+
159*4882a593Smuzhiyun+var filterFlags = os.Getenv("CGO_PEDANTIC") == ""
160*4882a593Smuzhiyun+
161*4882a593Smuzhiyun+func filterCompilerFlags(flags []string) []string {
162*4882a593Smuzhiyun+	var newflags []string
163*4882a593Smuzhiyun+	if !filterFlags {
164*4882a593Smuzhiyun+		return flags
165*4882a593Smuzhiyun+	}
166*4882a593Smuzhiyun+	for _, flag := range flags {
167*4882a593Smuzhiyun+		if strings.HasPrefix(flag, "-m") {
168*4882a593Smuzhiyun+			newflags = append(newflags, flag)
169*4882a593Smuzhiyun+		}
170*4882a593Smuzhiyun+	}
171*4882a593Smuzhiyun+	return newflags
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun // compilerExe returns the compiler to use given an
175*4882a593Smuzhiyun@@ -2473,11 +2490,16 @@ func (b *Builder) fcExe() []string {
176*4882a593Smuzhiyun // of the compiler but can have additional arguments if they
177*4882a593Smuzhiyun // were present in the environment value.
178*4882a593Smuzhiyun // For example if CC="gcc -DGOPHER" then the result is ["gcc", "-DGOPHER"].
179*4882a593Smuzhiyun-func (b *Builder) compilerExe(envValue string, def string) []string {
180*4882a593Smuzhiyun+func (b *Builder) compilerExe(envValue string, def string, filtered bool) []string {
181*4882a593Smuzhiyun 	compiler := strings.Fields(envValue)
182*4882a593Smuzhiyun 	if len(compiler) == 0 {
183*4882a593Smuzhiyun 		compiler = strings.Fields(def)
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun+
186*4882a593Smuzhiyun+	if filtered {
187*4882a593Smuzhiyun+		return append(compiler[0:1], filterCompilerFlags(compiler[1:])...)
188*4882a593Smuzhiyun+	}
189*4882a593Smuzhiyun+
190*4882a593Smuzhiyun 	return compiler
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun@@ -2667,7 +2689,7 @@ func envList(key, def string) []string {
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun // CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo.
197*4882a593Smuzhiyun-func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
198*4882a593Smuzhiyun+func (b *Builder) CFlags(p *load.Package, filtered bool) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
199*4882a593Smuzhiyun 	defaults := "-g -O2"
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun 	if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
202*4882a593Smuzhiyun@@ -2686,6 +2708,14 @@ func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, l
203*4882a593Smuzhiyun 		return
204*4882a593Smuzhiyun 	}
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun+	if filtered {
207*4882a593Smuzhiyun+		cppflags = filterCompilerFlags(cppflags)
208*4882a593Smuzhiyun+		cflags = filterCompilerFlags(cflags)
209*4882a593Smuzhiyun+		cxxflags = filterCompilerFlags(cxxflags)
210*4882a593Smuzhiyun+		fflags = filterCompilerFlags(fflags)
211*4882a593Smuzhiyun+		ldflags = filterCompilerFlags(ldflags)
212*4882a593Smuzhiyun+	}
213*4882a593Smuzhiyun+
214*4882a593Smuzhiyun 	return
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun@@ -2700,7 +2730,7 @@ var cgoRe = lazyregexp.New(`[/\\:]`)
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
220*4882a593Smuzhiyun 	p := a.Package
221*4882a593Smuzhiyun-	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
222*4882a593Smuzhiyun+	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p, false)
223*4882a593Smuzhiyun 	if err != nil {
224*4882a593Smuzhiyun 		return nil, nil, err
225*4882a593Smuzhiyun 	}
226*4882a593Smuzhiyun@@ -3151,7 +3181,7 @@ func (b *Builder) swigIntSize(objdir string) (intsize string, err error) {
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun // Run SWIG on one SWIG input file.
229*4882a593Smuzhiyun func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
230*4882a593Smuzhiyun-	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
231*4882a593Smuzhiyun+	cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p, false)
232*4882a593Smuzhiyun 	if err != nil {
233*4882a593Smuzhiyun 		return "", "", err
234*4882a593Smuzhiyun 	}
235*4882a593Smuzhiyun--
236*4882a593Smuzhiyun2.20.1
237*4882a593Smuzhiyun
238