Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame^] | 1 | From a13ae484e41139094505d2834437e9262a5315f7 Mon Sep 17 00:00:00 2001 |
| 2 | From: Alex Kube <alexander.j.kube@gmail.com> |
| 3 | Date: Wed, 23 Oct 2019 21:14:22 +0430 |
| 4 | Subject: [PATCH 2/9] cmd/go: make content-based hash generation less pedantic |
| 5 | |
| 6 | Upstream-Status: Inappropriate [OE specific] |
| 7 | |
| 8 | Go 1.10's build tool now uses content-based hashes to |
| 9 | determine when something should be built or re-built. |
| 10 | This same mechanism is used to maintain a built-artifact |
| 11 | cache for speeding up builds. |
| 12 | |
| 13 | However, the hashes it generates include information that |
| 14 | doesn't work well with OE, nor with using a shared runtime |
| 15 | library. |
| 16 | |
| 17 | First, it embeds path names to source files, unless |
| 18 | building within GOROOT. This prevents the building |
| 19 | of a package in GOPATH for later staging into GOROOT. |
| 20 | |
| 21 | This patch adds support for the environment variable |
| 22 | GOPATH_OMIT_IN_ACTIONID. If present, path name |
| 23 | embedding is disabled. |
| 24 | |
| 25 | Second, if cgo is enabled, the build ID for cgo-related |
| 26 | packages will include the current value of the environment |
| 27 | variables for invoking the compiler (CC, CXX, FC) and |
| 28 | any CGO_xxFLAGS variables. Only if the settings used |
| 29 | during a compilation exactly match, character for character, |
| 30 | the values used for compiling runtime/cgo or any other |
| 31 | cgo-enabled package being imported, will the tool |
| 32 | decide that the imported package is up-to-date. |
| 33 | |
| 34 | This is done to help ensure correctness, but is overly |
| 35 | simplistic and effectively prevents the reuse of built |
| 36 | artifacts that use cgo (or shared runtime, which includes |
| 37 | runtime/cgo). |
| 38 | |
| 39 | This patch filters out all compiler flags except those |
| 40 | beginning with '-m'. The default behavior can be restored |
| 41 | by setting the CGO_PEDANTIC environment variable. |
| 42 | |
| 43 | Adapted to Go 1.13 from patches originally submitted to |
| 44 | the meta/recipes-devtools/go tree by |
| 45 | Matt Madison <matt@madison.systems>. |
| 46 | |
| 47 | Signed-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 |
| 55 | @@ -156,7 +156,7 @@ func ExtraEnvVars() []cfg.EnvVar { |
| 56 | 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 |
| 66 | @@ -32,6 +32,8 @@ import ( |
| 67 | "time" |
| 68 | ) |
| 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 { |
| 75 | @@ -208,7 +210,7 @@ func (b *Builder) buildActionID(a *Actio |
| 76 | // 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) |
| 84 | @@ -224,13 +226,13 @@ func (b *Builder) buildActionID(a *Actio |
| 85 | } |
| 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 | } |
| 102 | @@ -2228,33 +2230,48 @@ 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(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 |
| 160 | @@ -2263,11 +2280,16 @@ func (b *Builder) fcExe() []string { |
| 161 | // 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 | |
| 178 | @@ -2428,7 +2450,7 @@ func envList(key, def string) []string { |
| 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 | @@ -2447,6 +2469,14 @@ func (b *Builder) CFlags(p *load.Package |
| 188 | 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 | |
| 202 | @@ -2461,7 +2491,7 @@ var cgoRe = lazyregexp.New(`[/\\:]`) |
| 203 | |
| 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 | } |
| 211 | @@ -2820,7 +2850,7 @@ func (b *Builder) swigIntSize(objdir str |
| 212 | |
| 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 | } |