From c403b45995c5daa6747ac4d95b39bc9a6feb2cda Mon Sep 17 00:00:00 2001 From: Alex Kube Date: Wed, 23 Oct 2019 21:14:22 +0430 Subject: [PATCH] cmd/go: make content-based hash generation less pedantic Upstream-Status: Inappropriate [OE specific] 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. Adapted to Go 1.13 from patches originally submitted to the meta/recipes-devtools/go tree by Matt Madison . Signed-off-by: Alexander J Kube --- src/cmd/go/internal/envcmd/env.go | 2 +- src/cmd/go/internal/work/exec.go | 66 ++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 20d0587..ff6f0d8 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -160,7 +160,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 5a225fb..a37872e 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -38,6 +38,8 @@ import ( "cmd/go/internal/trace" ) +var omitGopath = os.Getenv("GOPATH_OMIT_IN_ACTIONID") != "" + // actionList returns the list of actions in the dag rooted at root // as visited in a depth-first post-order traversal. func actionList(root *Action) []*Action { @@ -229,7 +231,7 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID { // Assume b.WorkDir is being trimmed properly. // When -trimpath is used with a package built from the module cache, // use the module path and version instead of the directory. - if !p.Goroot && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) { + if !p.Goroot && !omitGopath && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) { fmt.Fprintf(h, "dir %s\n", p.Dir) } else if cfg.BuildTrimpath && p.Module != nil { fmt.Fprintf(h, "module %s@%s\n", p.Module.Path, p.Module.Version) @@ -248,9 +250,9 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID { } if len(p.CgoFiles)+len(p.SwigFiles)+len(p.SwigCXXFiles) > 0 { fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo")) - cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p) + cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p, true) - ccExe := b.ccExe() + ccExe := b.ccExe(true) fmt.Fprintf(h, "CC=%q %q %q %q\n", ccExe, cppflags, cflags, ldflags) // Include the C compiler tool ID so that if the C // compiler changes we rebuild the package. @@ -263,14 +265,14 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID { } } if len(p.CXXFiles)+len(p.SwigCXXFiles) > 0 { - cxxExe := b.cxxExe() + cxxExe := b.cxxExe(true) fmt.Fprintf(h, "CXX=%q %q\n", cxxExe, cxxflags) if cxxID, err := b.gccToolID(cxxExe[0], "c++"); err == nil { fmt.Fprintf(h, "CXX ID=%q\n", cxxID) } } if len(p.FFiles) > 0 { - fcExe := b.fcExe() + fcExe := b.fcExe(true) fmt.Fprintf(h, "FC=%q %q\n", fcExe, fflags) if fcID, err := b.gccToolID(fcExe[0], "f95"); err == nil { fmt.Fprintf(h, "FC ID=%q\n", fcID) @@ -2438,33 +2440,48 @@ 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(cfg.Getenv("FC"), "gfortran") +func (b *Builder) fcExe(filtered bool) []string { + return b.compilerExe(os.Getenv("FC"), "gfortran", filtered) +} + +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 } // compilerExe returns the compiler to use given an @@ -2473,11 +2490,16 @@ 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 = strings.Fields(def) } + + if filtered { + return append(compiler[0:1], filterCompilerFlags(compiler[1:])...) + } + return compiler } @@ -2667,7 +2689,7 @@ func envList(key, def string) []string { } // 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 { @@ -2686,6 +2708,14 @@ func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, l return } + if filtered { + cppflags = filterCompilerFlags(cppflags) + cflags = filterCompilerFlags(cflags) + cxxflags = filterCompilerFlags(cxxflags) + fflags = filterCompilerFlags(fflags) + ldflags = filterCompilerFlags(ldflags) + } + return } @@ -2700,7 +2730,7 @@ var cgoRe = lazyregexp.New(`[/\\:]`) 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 } @@ -3151,7 +3181,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 } -- 2.20.1