blob: 74f37a99efe9e8da17e0ff3c0649989944e6ebe2 [file] [log] [blame]
Brad Bishop316dfdd2018-06-25 12:45:53 -04001From 8b2feaee81d7a16adc59e61d06c1e7314d3a5408 Mon Sep 17 00:00:00 2001
2From: Matt Madison <matt@madison.systems>
3Date: Mon, 19 Feb 2018 08:50:59 -0800
4Subject: [PATCH 2/9] 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 src/cmd/go/internal/envcmd/env.go | 2 +-
46 src/cmd/go/internal/work/exec.go | 63 ++++++++++++++++++++++++++++-----------
47 2 files changed, 46 insertions(+), 19 deletions(-)
48
49diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go
50index f891123f9c..ebacfbfdbc 100644
51--- a/src/cmd/go/internal/envcmd/env.go
52+++ b/src/cmd/go/internal/envcmd/env.go
53@@ -113,7 +113,7 @@ func findEnv(env []cfg.EnvVar, name string) string {
54 func ExtraEnvVars() []cfg.EnvVar {
55 var b work.Builder
56 b.Init()
57- cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{})
58+ cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{}, false)
59 if err != nil {
60 // Should not happen - b.CFlags was given an empty package.
61 fmt.Fprintf(os.Stderr, "go: invalid cflags: %v\n", err)
62diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
63index c4c1500eb2..b0f6b45647 100644
64--- a/src/cmd/go/internal/work/exec.go
65+++ b/src/cmd/go/internal/work/exec.go
66@@ -173,6 +173,8 @@ func (b *Builder) Do(root *Action) {
67 wg.Wait()
68 }
69
70+var omitGopath = os.Getenv("GOPATH_OMIT_IN_ACTIONID") != ""
71+
72 // buildActionID computes the action ID for a build action.
73 func (b *Builder) buildActionID(a *Action) cache.ActionID {
74 p := a.Package
75@@ -189,7 +191,7 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
76 // but it does not hide the exact value of $GOPATH.
77 // Include the full dir in that case.
78 // Assume b.WorkDir is being trimmed properly.
79- if !p.Goroot && !strings.HasPrefix(p.Dir, b.WorkDir) {
80+ if !p.Goroot && !omitGopath && !strings.HasPrefix(p.Dir, b.WorkDir) {
81 fmt.Fprintf(h, "dir %s\n", p.Dir)
82 }
83 fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
84@@ -197,13 +199,13 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
85 fmt.Fprintf(h, "omitdebug %v standard %v local %v prefix %q\n", p.Internal.OmitDebug, p.Standard, p.Internal.Local, p.Internal.LocalPrefix)
86 if len(p.CgoFiles)+len(p.SwigFiles) > 0 {
87 fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo"))
88- cppflags, cflags, cxxflags, fflags, _, _ := b.CFlags(p)
89- fmt.Fprintf(h, "CC=%q %q %q\n", b.ccExe(), cppflags, cflags)
90+ cppflags, cflags, cxxflags, fflags, _, _ := b.CFlags(p, true)
91+ fmt.Fprintf(h, "CC=%q %q %q\n", b.ccExe(true), cppflags, cflags)
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 }
102@@ -1731,33 +1733,33 @@ var (
103 // 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(os.Getenv("FC"), "gfortran")
140+func (b *Builder) fcExe(filtered bool) []string {
141+ return b.compilerExe(os.Getenv("FC"), "gfortran", filtered)
142 }
143
144 // compilerExe returns the compiler to use given an
145@@ -1766,11 +1768,14 @@ func (b *Builder) fcExe() []string {
146 // of the compiler but can have additional arguments if they
147 // were present in the environment value.
148 // For example if CC="gcc -DGOPHER" then the result is ["gcc", "-DGOPHER"].
149-func (b *Builder) compilerExe(envValue string, def string) []string {
150+func (b *Builder) compilerExe(envValue string, def string, filtered bool) []string {
151 compiler := strings.Fields(envValue)
152 if len(compiler) == 0 {
153 compiler = []string{def}
154 }
155+ if filtered {
156+ return append(compiler[0:1], filterCompilerFlags(compiler[1:])...)
157+ }
158 return compiler
159 }
160
161@@ -1920,8 +1925,23 @@ func envList(key, def string) []string {
162 return strings.Fields(v)
163 }
164
165+var filterFlags = os.Getenv("CGO_PEDANTIC") == ""
166+
167+func filterCompilerFlags(flags []string) []string {
168+ var newflags []string
169+ if !filterFlags {
170+ return flags
171+ }
172+ for _, flag := range flags {
173+ if strings.HasPrefix(flag, "-m") {
174+ newflags = append(newflags, flag)
175+ }
176+ }
177+ return newflags
178+}
179+
180 // CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo.
181-func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
182+func (b *Builder) CFlags(p *load.Package, filtered bool) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
183 defaults := "-g -O2"
184
185 if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
186@@ -1939,6 +1959,13 @@ func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, l
187 if ldflags, err = buildFlags("LDFLAGS", defaults, p.CgoLDFLAGS, checkLinkerFlags); err != nil {
188 return
189 }
190+ if filtered {
191+ cppflags = filterCompilerFlags(cppflags)
192+ cflags = filterCompilerFlags(cflags)
193+ cxxflags = filterCompilerFlags(cxxflags)
194+ fflags = filterCompilerFlags(fflags)
195+ ldflags = filterCompilerFlags(ldflags)
196+ }
197
198 return
199 }
200@@ -1954,7 +1981,7 @@ var cgoRe = regexp.MustCompile(`[/\\:]`)
201
202 func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
203 p := a.Package
204- cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
205+ cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p, false)
206 if err != nil {
207 return nil, nil, err
208 }
209@@ -2306,7 +2333,7 @@ func (b *Builder) swigIntSize(objdir string) (intsize string, err error) {
210
211 // Run SWIG on one SWIG input file.
212 func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
213- cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
214+ cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p, false)
215 if err != nil {
216 return "", "", err
217 }
218--
2192.14.1
220