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