From a13ae484e41139094505d2834437e9262a5315f7 Mon Sep 17 00:00:00 2001
From: Alex Kube <alexander.j.kube@gmail.com>
Date: Wed, 23 Oct 2019 21:14:22 +0430
Subject: [PATCH 2/9] 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 <matt@madison.systems>.

Signed-off-by: Alexander J Kube <alexander.j.kube@gmail.com>
---
 src/cmd/go/internal/envcmd/env.go |  2 +-
 src/cmd/go/internal/work/exec.go  | 66 ++++++++++++++++++++++---------
 2 files changed, 49 insertions(+), 19 deletions(-)

--- a/src/cmd/go/internal/envcmd/env.go
+++ b/src/cmd/go/internal/envcmd/env.go
@@ -156,7 +156,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)
--- a/src/cmd/go/internal/work/exec.go
+++ b/src/cmd/go/internal/work/exec.go
@@ -32,6 +32,8 @@ import (
 	"time"
 )
 
+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 {
@@ -208,7 +210,7 @@ func (b *Builder) buildActionID(a *Actio
 	// 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)
@@ -224,13 +226,13 @@ func (b *Builder) buildActionID(a *Actio
 	}
 	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?
 	}
@@ -2228,33 +2230,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
@@ -2263,11 +2280,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 = []string{def}
 	}
+
+	if filtered {
+		return append(compiler[0:1], filterCompilerFlags(compiler[1:])...)
+	}
+
 	return compiler
 }
 
@@ -2428,7 +2450,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 {
@@ -2447,6 +2469,14 @@ func (b *Builder) CFlags(p *load.Package
 		return
 	}
 
+	if filtered {
+		cppflags = filterCompilerFlags(cppflags)
+		cflags = filterCompilerFlags(cflags)
+		cxxflags = filterCompilerFlags(cxxflags)
+		fflags = filterCompilerFlags(fflags)
+		ldflags = filterCompilerFlags(ldflags)
+	}
+
 	return
 }
 
@@ -2461,7 +2491,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
 	}
@@ -2820,7 +2850,7 @@ func (b *Builder) swigIntSize(objdir str
 
 // 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
 	}
