blob: 4eddd39809cdcb702a9f54bcc5d1dc8325b83b1e [file] [log] [blame]
Brad Bishop1d80a2e2019-11-15 16:35:03 -05001From a13ae484e41139094505d2834437e9262a5315f7 Mon Sep 17 00:00:00 2001
2From: Alex Kube <alexander.j.kube@gmail.com>
3Date: Wed, 23 Oct 2019 21:14:22 +0430
4Subject: [PATCH 2/9] cmd/go: make content-based hash generation less pedantic
5
6Upstream-Status: Inappropriate [OE specific]
7
8Go 1.10's build tool now uses content-based hashes to
9determine when something should be built or re-built.
10This same mechanism is used to maintain a built-artifact
11cache for speeding up builds.
12
13However, the hashes it generates include information that
14doesn't work well with OE, nor with using a shared runtime
15library.
16
17First, it embeds path names to source files, unless
18building within GOROOT. This prevents the building
19of a package in GOPATH for later staging into GOROOT.
20
21This patch adds support for the environment variable
22GOPATH_OMIT_IN_ACTIONID. If present, path name
23embedding is disabled.
24
25Second, if cgo is enabled, the build ID for cgo-related
26packages will include the current value of the environment
27variables for invoking the compiler (CC, CXX, FC) and
28any CGO_xxFLAGS variables. Only if the settings used
29during a compilation exactly match, character for character,
30the values used for compiling runtime/cgo or any other
31cgo-enabled package being imported, will the tool
32decide that the imported package is up-to-date.
33
34This is done to help ensure correctness, but is overly
35simplistic and effectively prevents the reuse of built
36artifacts that use cgo (or shared runtime, which includes
37runtime/cgo).
38
39This patch filters out all compiler flags except those
40beginning with '-m'. The default behavior can be restored
41by setting the CGO_PEDANTIC environment variable.
42
43Adapted to Go 1.13 from patches originally submitted to
44the meta/recipes-devtools/go tree by
45Matt Madison <matt@madison.systems>.
46
47Signed-off-by: Alexander J Kube <alexander.j.kube@gmail.com>
48---
49 src/cmd/go/internal/envcmd/env.go | 2 +-
50 src/cmd/go/internal/work/exec.go | 66 ++++++++++++++++++++++---------
51 2 files changed, 49 insertions(+), 19 deletions(-)
52
53diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go
54index 7b5ec5e..292f117 100644
55--- a/src/cmd/go/internal/envcmd/env.go
56+++ b/src/cmd/go/internal/envcmd/env.go
57@@ -154,7 +154,7 @@ func ExtraEnvVars() []cfg.EnvVar {
58 func ExtraEnvVarsCostly() []cfg.EnvVar {
59 var b work.Builder
60 b.Init()
61- cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{})
62+ cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{}, false)
63 if err != nil {
64 // Should not happen - b.CFlags was given an empty package.
65 fmt.Fprintf(os.Stderr, "go: invalid cflags: %v\n", err)
66diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
67index 7dd9a90..ccebaf8 100644
68--- a/src/cmd/go/internal/work/exec.go
69+++ b/src/cmd/go/internal/work/exec.go
70@@ -32,6 +32,8 @@ import (
71 "time"
72 )
73
74+var omitGopath = os.Getenv("GOPATH_OMIT_IN_ACTIONID") != ""
75+
76 // actionList returns the list of actions in the dag rooted at root
77 // as visited in a depth-first post-order traversal.
78 func actionList(root *Action) []*Action {
79@@ -205,7 +207,7 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
80 // The compiler hides the exact value of $GOROOT
81 // when building things in GOROOT.
82 // Assume b.WorkDir is being trimmed properly.
83- if !p.Goroot && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) {
84+ if !p.Goroot && !omitGopath && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) {
85 fmt.Fprintf(h, "dir %s\n", p.Dir)
86 }
87 fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
88@@ -219,13 +221,13 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
89 }
90 if len(p.CgoFiles)+len(p.SwigFiles) > 0 {
91 fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo"))
92- cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p)
93- fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(), cppflags, cflags, ldflags)
94+ cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p, true)
95+ fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(true), cppflags, cflags, ldflags)
96 if len(p.CXXFiles)+len(p.SwigFiles) > 0 {
97- fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(), cxxflags)
98+ fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(true), cxxflags)
99 }
100 if len(p.FFiles) > 0 {
101- fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(), fflags)
102+ fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(true), fflags)
103 }
104 // TODO(rsc): Should we include the SWIG version or Fortran/GCC/G++/Objective-C compiler versions?
105 }
106@@ -2229,33 +2231,48 @@ var (
107 // gccCmd returns a gcc command line prefix
108 // defaultCC is defined in zdefaultcc.go, written by cmd/dist.
109 func (b *Builder) GccCmd(incdir, workdir string) []string {
110- return b.compilerCmd(b.ccExe(), incdir, workdir)
111+ return b.compilerCmd(b.ccExe(false), incdir, workdir)
112 }
113
114 // gxxCmd returns a g++ command line prefix
115 // defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
116 func (b *Builder) GxxCmd(incdir, workdir string) []string {
117- return b.compilerCmd(b.cxxExe(), incdir, workdir)
118+ return b.compilerCmd(b.cxxExe(false), incdir, workdir)
119 }
120
121 // gfortranCmd returns a gfortran command line prefix.
122 func (b *Builder) gfortranCmd(incdir, workdir string) []string {
123- return b.compilerCmd(b.fcExe(), incdir, workdir)
124+ return b.compilerCmd(b.fcExe(false), incdir, workdir)
125 }
126
127 // ccExe returns the CC compiler setting without all the extra flags we add implicitly.
128-func (b *Builder) ccExe() []string {
129- return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch))
130+func (b *Builder) ccExe(filtered bool) []string {
131+ return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch), filtered)
132 }
133
134 // cxxExe returns the CXX compiler setting without all the extra flags we add implicitly.
135-func (b *Builder) cxxExe() []string {
136- return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
137+func (b *Builder) cxxExe(filtered bool) []string {
138+ return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch), filtered)
139 }
140
141 // fcExe returns the FC compiler setting without all the extra flags we add implicitly.
142-func (b *Builder) fcExe() []string {
143- return b.compilerExe(cfg.Getenv("FC"), "gfortran")
144+func (b *Builder) fcExe(filtered bool) []string {
145+ return b.compilerExe(os.Getenv("FC"), "gfortran", filtered)
146+}
147+
148+var filterFlags = os.Getenv("CGO_PEDANTIC") == ""
149+
150+func filterCompilerFlags(flags []string) []string {
151+ var newflags []string
152+ if !filterFlags {
153+ return flags
154+ }
155+ for _, flag := range flags {
156+ if strings.HasPrefix(flag, "-m") {
157+ newflags = append(newflags, flag)
158+ }
159+ }
160+ return newflags
161 }
162
163 // compilerExe returns the compiler to use given an
164@@ -2264,11 +2281,16 @@ func (b *Builder) fcExe() []string {
165 // of the compiler but can have additional arguments if they
166 // were present in the environment value.
167 // For example if CC="gcc -DGOPHER" then the result is ["gcc", "-DGOPHER"].
168-func (b *Builder) compilerExe(envValue string, def string) []string {
169+func (b *Builder) compilerExe(envValue string, def string, filtered bool) []string {
170 compiler := strings.Fields(envValue)
171 if len(compiler) == 0 {
172 compiler = []string{def}
173 }
174+
175+ if filtered {
176+ return append(compiler[0:1], filterCompilerFlags(compiler[1:])...)
177+ }
178+
179 return compiler
180 }
181
182@@ -2429,7 +2451,7 @@ func envList(key, def string) []string {
183 }
184
185 // CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo.
186-func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
187+func (b *Builder) CFlags(p *load.Package, filtered bool) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
188 defaults := "-g -O2"
189
190 if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
191@@ -2448,6 +2470,14 @@ func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, l
192 return
193 }
194
195+ if filtered {
196+ cppflags = filterCompilerFlags(cppflags)
197+ cflags = filterCompilerFlags(cflags)
198+ cxxflags = filterCompilerFlags(cxxflags)
199+ fflags = filterCompilerFlags(fflags)
200+ ldflags = filterCompilerFlags(ldflags)
201+ }
202+
203 return
204 }
205
206@@ -2462,7 +2492,7 @@ var cgoRe = lazyregexp.New(`[/\\:]`)
207
208 func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
209 p := a.Package
210- cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
211+ cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p, false)
212 if err != nil {
213 return nil, nil, err
214 }
215@@ -2821,7 +2851,7 @@ func (b *Builder) swigIntSize(objdir string) (intsize string, err error) {
216
217 // Run SWIG on one SWIG input file.
218 func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
219- cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
220+ cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p, false)
221 if err != nil {
222 return "", "", err
223 }
224--
2252.17.1 (Apple Git-112)
226