blob: 66b856187457d5e3e912a7895c62a1145188129d [file] [log] [blame]
From 47db69e20ed66fb62b01affd83d829654b829893 Mon Sep 17 00:00:00 2001
From: Matt Madison <matt@madison.systems>
Date: Mon, 19 Feb 2018 08:50:59 -0800
Subject: [PATCH] cmd/go: make content-based hash generation less pedantic
Go 1.10's build tool now uses content-based hashes to
determine when something should be built or re-built.
This same mechanism is used to maintain a built-artifact
cache for speeding up builds.
However, the hashes it generates include information that
doesn't work well with OE, nor with using a shared runtime
library.
First, it embeds path names to source files, unless
building within GOROOT. This prevents the building
of a package in GOPATH for later staging into GOROOT.
This patch adds support for the environment variable
GOPATH_OMIT_IN_ACTIONID. If present, path name
embedding is disabled.
Second, if cgo is enabled, the build ID for cgo-related
packages will include the current value of the environment
variables for invoking the compiler (CC, CXX, FC) and
any CGO_xxFLAGS variables. Only if the settings used
during a compilation exactly match, character for character,
the values used for compiling runtime/cgo or any other
cgo-enabled package being imported, will the tool
decide that the imported package is up-to-date.
This is done to help ensure correctness, but is overly
simplistic and effectively prevents the reuse of built
artifacts that use cgo (or shared runtime, which includes
runtime/cgo).
This patch filters out all compiler flags except those
beginning with '-m'. The default behavior can be restored
by setting the CGO_PEDANTIC environment variable.
Upstream-Status: Inappropriate [OE specific]
Signed-off-by: Matt Madison <matt@madison.systems>
---
src/cmd/go/internal/envcmd/env.go | 2 +-
src/cmd/go/internal/work/exec.go | 63 ++++++++++++++++++++++---------
2 files changed, 46 insertions(+), 19 deletions(-)
diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go
index cedbfbf..5763a0d 100644
--- a/src/cmd/go/internal/envcmd/env.go
+++ b/src/cmd/go/internal/envcmd/env.go
@@ -128,7 +128,7 @@ func ExtraEnvVars() []cfg.EnvVar {
func ExtraEnvVarsCostly() []cfg.EnvVar {
var b work.Builder
b.Init()
- cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{})
+ cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{}, false)
if err != nil {
// Should not happen - b.CFlags was given an empty package.
fmt.Fprintf(os.Stderr, "go: invalid cflags: %v\n", err)
diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
index 12e1527..e41bfac 100644
--- a/src/cmd/go/internal/work/exec.go
+++ b/src/cmd/go/internal/work/exec.go
@@ -174,6 +174,8 @@ func (b *Builder) Do(root *Action) {
wg.Wait()
}
+var omitGopath = os.Getenv("GOPATH_OMIT_IN_ACTIONID") != ""
+
// buildActionID computes the action ID for a build action.
func (b *Builder) buildActionID(a *Action) cache.ActionID {
p := a.Package
@@ -190,7 +192,7 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
// but it does not hide the exact value of $GOPATH.
// Include the full dir in that case.
// Assume b.WorkDir is being trimmed properly.
- if !p.Goroot && !strings.HasPrefix(p.Dir, b.WorkDir) {
+ if !p.Goroot && !omitGopath && !strings.HasPrefix(p.Dir, b.WorkDir) {
fmt.Fprintf(h, "dir %s\n", p.Dir)
}
fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
@@ -201,13 +203,13 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
}
if len(p.CgoFiles)+len(p.SwigFiles) > 0 {
fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo"))
- cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p)
- fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(), cppflags, cflags, ldflags)
+ cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p, true)
+ fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(true), cppflags, cflags, ldflags)
if len(p.CXXFiles)+len(p.SwigFiles) > 0 {
- fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(), cxxflags)
+ fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(true), cxxflags)
}
if len(p.FFiles) > 0 {
- fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(), fflags)
+ fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(true), fflags)
}
// TODO(rsc): Should we include the SWIG version or Fortran/GCC/G++/Objective-C compiler versions?
}
@@ -2096,33 +2098,33 @@ var (
// gccCmd returns a gcc command line prefix
// defaultCC is defined in zdefaultcc.go, written by cmd/dist.
func (b *Builder) GccCmd(incdir, workdir string) []string {
- return b.compilerCmd(b.ccExe(), incdir, workdir)
+ return b.compilerCmd(b.ccExe(false), incdir, workdir)
}
// gxxCmd returns a g++ command line prefix
// defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
func (b *Builder) GxxCmd(incdir, workdir string) []string {
- return b.compilerCmd(b.cxxExe(), incdir, workdir)
+ return b.compilerCmd(b.cxxExe(false), incdir, workdir)
}
// gfortranCmd returns a gfortran command line prefix.
func (b *Builder) gfortranCmd(incdir, workdir string) []string {
- return b.compilerCmd(b.fcExe(), incdir, workdir)
+ return b.compilerCmd(b.fcExe(false), incdir, workdir)
}
// ccExe returns the CC compiler setting without all the extra flags we add implicitly.
-func (b *Builder) ccExe() []string {
- return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch))
+func (b *Builder) ccExe(filtered bool) []string {
+ return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch), filtered)
}
// cxxExe returns the CXX compiler setting without all the extra flags we add implicitly.
-func (b *Builder) cxxExe() []string {
- return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
+func (b *Builder) cxxExe(filtered bool) []string {
+ return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch), filtered)
}
// fcExe returns the FC compiler setting without all the extra flags we add implicitly.
-func (b *Builder) fcExe() []string {
- return b.compilerExe(os.Getenv("FC"), "gfortran")
+func (b *Builder) fcExe(filtered bool) []string {
+ return b.compilerExe(os.Getenv("FC"), "gfortran", filtered)
}
// compilerExe returns the compiler to use given an
@@ -2131,11 +2133,14 @@ func (b *Builder) fcExe() []string {
// of the compiler but can have additional arguments if they
// were present in the environment value.
// For example if CC="gcc -DGOPHER" then the result is ["gcc", "-DGOPHER"].
-func (b *Builder) compilerExe(envValue string, def string) []string {
+func (b *Builder) compilerExe(envValue string, def string, filtered bool) []string {
compiler := strings.Fields(envValue)
if len(compiler) == 0 {
compiler = []string{def}
}
+ if filtered {
+ return append(compiler[0:1], filterCompilerFlags(compiler[1:])...)
+ }
return compiler
}
@@ -2285,8 +2290,23 @@ func envList(key, def string) []string {
return strings.Fields(v)
}
+var filterFlags = os.Getenv("CGO_PEDANTIC") == ""
+
+func filterCompilerFlags(flags []string) []string {
+ var newflags []string
+ if !filterFlags {
+ return flags
+ }
+ for _, flag := range flags {
+ if strings.HasPrefix(flag, "-m") {
+ newflags = append(newflags, flag)
+ }
+ }
+ return newflags
+}
+
// CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo.
-func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
+func (b *Builder) CFlags(p *load.Package, filtered bool) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
defaults := "-g -O2"
if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
@@ -2304,6 +2324,13 @@ func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, l
if ldflags, err = buildFlags("LDFLAGS", defaults, p.CgoLDFLAGS, checkLinkerFlags); err != nil {
return
}
+ if filtered {
+ cppflags = filterCompilerFlags(cppflags)
+ cflags = filterCompilerFlags(cflags)
+ cxxflags = filterCompilerFlags(cxxflags)
+ fflags = filterCompilerFlags(fflags)
+ ldflags = filterCompilerFlags(ldflags)
+ }
return
}
@@ -2319,7 +2346,7 @@ var cgoRe = regexp.MustCompile(`[/\\:]`)
func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
p := a.Package
- cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
+ cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p, false)
if err != nil {
return nil, nil, err
}
@@ -2679,7 +2706,7 @@ func (b *Builder) swigIntSize(objdir string) (intsize string, err error) {
// Run SWIG on one SWIG input file.
func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
- cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
+ cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p, false)
if err != nil {
return "", "", err
}