blob: d0511c0c40562f8610174e3e63a3c4f1fa5dd220 [file] [log] [blame]
Andrew Geissler82c905d2020-04-13 13:39:40 -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
53--- a/src/cmd/go/internal/envcmd/env.go
54+++ b/src/cmd/go/internal/envcmd/env.go
Andrew Geisslerc9f78652020-09-18 14:11:35 -050055@@ -157,7 +157,7 @@ func ExtraEnvVars() []cfg.EnvVar {
Andrew Geissler82c905d2020-04-13 13:39:40 -050056 func ExtraEnvVarsCostly() []cfg.EnvVar {
57 var b work.Builder
58 b.Init()
59- cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{})
60+ cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{}, false)
61 if err != nil {
62 // Should not happen - b.CFlags was given an empty package.
63 fmt.Fprintf(os.Stderr, "go: invalid cflags: %v\n", err)
64--- a/src/cmd/go/internal/work/exec.go
65+++ b/src/cmd/go/internal/work/exec.go
Andrew Geisslerc9f78652020-09-18 14:11:35 -050066@@ -33,6 +33,8 @@ import (
67 "cmd/go/internal/str"
Andrew Geissler82c905d2020-04-13 13:39:40 -050068 )
69
70+var omitGopath = os.Getenv("GOPATH_OMIT_IN_ACTIONID") != ""
71+
72 // actionList returns the list of actions in the dag rooted at root
73 // as visited in a depth-first post-order traversal.
74 func actionList(root *Action) []*Action {
Andrew Geisslerc9f78652020-09-18 14:11:35 -050075@@ -209,7 +211,7 @@ func (b *Builder) buildActionID(a *Actio
Andrew Geissler82c905d2020-04-13 13:39:40 -050076 // Assume b.WorkDir is being trimmed properly.
77 // When -trimpath is used with a package built from the module cache,
78 // use the module path and version instead of the directory.
79- if !p.Goroot && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) {
80+ if !p.Goroot && !omitGopath && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) {
81 fmt.Fprintf(h, "dir %s\n", p.Dir)
82 } else if cfg.BuildTrimpath && p.Module != nil {
83 fmt.Fprintf(h, "module %s@%s\n", p.Module.Path, p.Module.Version)
Andrew Geisslerc9f78652020-09-18 14:11:35 -050084@@ -228,13 +230,13 @@ func (b *Builder) buildActionID(a *Actio
Andrew Geissler82c905d2020-04-13 13:39:40 -050085 }
86 if len(p.CgoFiles)+len(p.SwigFiles) > 0 {
87 fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo"))
88- cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p)
89- fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(), cppflags, cflags, ldflags)
90+ cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p, true)
91+ fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(true), cppflags, cflags, ldflags)
92 if len(p.CXXFiles)+len(p.SwigFiles) > 0 {
93- fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(), cxxflags)
94+ fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(true), cxxflags)
95 }
96 if len(p.FFiles) > 0 {
97- fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(), fflags)
98+ fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(true), fflags)
99 }
100 // TODO(rsc): Should we include the SWIG version or Fortran/GCC/G++/Objective-C compiler versions?
101 }
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500102@@ -2298,33 +2300,48 @@ var (
Andrew Geissler82c905d2020-04-13 13:39:40 -0500103 // gccCmd returns a gcc command line prefix
104 // defaultCC is defined in zdefaultcc.go, written by cmd/dist.
105 func (b *Builder) GccCmd(incdir, workdir string) []string {
106- return b.compilerCmd(b.ccExe(), incdir, workdir)
107+ return b.compilerCmd(b.ccExe(false), incdir, workdir)
108 }
109
110 // gxxCmd returns a g++ command line prefix
111 // defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
112 func (b *Builder) GxxCmd(incdir, workdir string) []string {
113- return b.compilerCmd(b.cxxExe(), incdir, workdir)
114+ return b.compilerCmd(b.cxxExe(false), incdir, workdir)
115 }
116
117 // gfortranCmd returns a gfortran command line prefix.
118 func (b *Builder) gfortranCmd(incdir, workdir string) []string {
119- return b.compilerCmd(b.fcExe(), incdir, workdir)
120+ return b.compilerCmd(b.fcExe(false), incdir, workdir)
121 }
122
123 // ccExe returns the CC compiler setting without all the extra flags we add implicitly.
124-func (b *Builder) ccExe() []string {
125- return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch))
126+func (b *Builder) ccExe(filtered bool) []string {
127+ return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch), filtered)
128 }
129
130 // cxxExe returns the CXX compiler setting without all the extra flags we add implicitly.
131-func (b *Builder) cxxExe() []string {
132- return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
133+func (b *Builder) cxxExe(filtered bool) []string {
134+ return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch), filtered)
135 }
136
137 // fcExe returns the FC compiler setting without all the extra flags we add implicitly.
138-func (b *Builder) fcExe() []string {
139- return b.compilerExe(cfg.Getenv("FC"), "gfortran")
140+func (b *Builder) fcExe(filtered bool) []string {
141+ return b.compilerExe(os.Getenv("FC"), "gfortran", filtered)
142+}
143+
144+var filterFlags = os.Getenv("CGO_PEDANTIC") == ""
145+
146+func filterCompilerFlags(flags []string) []string {
147+ var newflags []string
148+ if !filterFlags {
149+ return flags
150+ }
151+ for _, flag := range flags {
152+ if strings.HasPrefix(flag, "-m") {
153+ newflags = append(newflags, flag)
154+ }
155+ }
156+ return newflags
157 }
158
159 // compilerExe returns the compiler to use given an
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500160@@ -2333,11 +2350,16 @@ func (b *Builder) fcExe() []string {
Andrew Geissler82c905d2020-04-13 13:39:40 -0500161 // of the compiler but can have additional arguments if they
162 // were present in the environment value.
163 // For example if CC="gcc -DGOPHER" then the result is ["gcc", "-DGOPHER"].
164-func (b *Builder) compilerExe(envValue string, def string) []string {
165+func (b *Builder) compilerExe(envValue string, def string, filtered bool) []string {
166 compiler := strings.Fields(envValue)
167 if len(compiler) == 0 {
168 compiler = []string{def}
169 }
170+
171+ if filtered {
172+ return append(compiler[0:1], filterCompilerFlags(compiler[1:])...)
173+ }
174+
175 return compiler
176 }
177
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500178@@ -2510,7 +2532,7 @@ func envList(key, def string) []string {
Andrew Geissler82c905d2020-04-13 13:39:40 -0500179 }
180
181 // CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo.
182-func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
183+func (b *Builder) CFlags(p *load.Package, filtered bool) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
184 defaults := "-g -O2"
185
186 if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500187@@ -2529,6 +2551,14 @@ func (b *Builder) CFlags(p *load.Package
Andrew Geissler82c905d2020-04-13 13:39:40 -0500188 return
189 }
190
191+ if filtered {
192+ cppflags = filterCompilerFlags(cppflags)
193+ cflags = filterCompilerFlags(cflags)
194+ cxxflags = filterCompilerFlags(cxxflags)
195+ fflags = filterCompilerFlags(fflags)
196+ ldflags = filterCompilerFlags(ldflags)
197+ }
198+
199 return
200 }
201
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500202@@ -2543,7 +2573,7 @@ var cgoRe = lazyregexp.New(`[/\\:]`)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500203
204 func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
205 p := a.Package
206- cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
207+ cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p, false)
208 if err != nil {
209 return nil, nil, err
210 }
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500211@@ -2902,7 +2932,7 @@ func (b *Builder) swigIntSize(objdir str
Andrew Geissler82c905d2020-04-13 13:39:40 -0500212
213 // Run SWIG on one SWIG input file.
214 func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
215- cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
216+ cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p, false)
217 if err != nil {
218 return "", "", err
219 }