0010-cmd-go-make-content-based-hash-generation-less-pedan.patch 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. Upstream-Status: Inappropriate [OE specific]
  6. Go 1.10's build tool now uses content-based hashes to
  7. determine when something should be built or re-built.
  8. This same mechanism is used to maintain a built-artifact
  9. cache for speeding up builds.
  10. However, the hashes it generates include information that
  11. doesn't work well with OE, nor with using a shared runtime
  12. library.
  13. First, it embeds path names to source files, unless
  14. building within GOROOT. This prevents the building
  15. of a package in GOPATH for later staging into GOROOT.
  16. This patch adds support for the environment variable
  17. GOPATH_OMIT_IN_ACTIONID. If present, path name
  18. embedding is disabled.
  19. Second, if cgo is enabled, the build ID for cgo-related
  20. packages will include the current value of the environment
  21. variables for invoking the compiler (CC, CXX, FC) and
  22. any CGO_xxFLAGS variables. Only if the settings used
  23. during a compilation exactly match, character for character,
  24. the values used for compiling runtime/cgo or any other
  25. cgo-enabled package being imported, will the tool
  26. decide that the imported package is up-to-date.
  27. This is done to help ensure correctness, but is overly
  28. simplistic and effectively prevents the reuse of built
  29. artifacts that use cgo (or shared runtime, which includes
  30. runtime/cgo).
  31. This patch filters out all compiler flags except those
  32. beginning with '-m'. The default behavior can be restored
  33. by setting the CGO_PEDANTIC environment variable.
  34. Adapted to Go 1.13 from patches originally submitted to
  35. the meta/recipes-devtools/go tree by
  36. Matt Madison <matt@madison.systems>.
  37. Signed-off-by: Alexander J Kube <alexander.j.kube@gmail.com>
  38. ---
  39. src/cmd/go/internal/envcmd/env.go | 2 +-
  40. src/cmd/go/internal/work/exec.go | 66 ++++++++++++++++++++++---------
  41. 2 files changed, 49 insertions(+), 19 deletions(-)
  42. --- a/src/cmd/go/internal/envcmd/env.go
  43. +++ b/src/cmd/go/internal/envcmd/env.go
  44. @@ -154,7 +154,7 @@ func ExtraEnvVars() []cfg.EnvVar {
  45. func ExtraEnvVarsCostly() []cfg.EnvVar {
  46. var b work.Builder
  47. b.Init()
  48. - cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{})
  49. + cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{}, false)
  50. if err != nil {
  51. // Should not happen - b.CFlags was given an empty package.
  52. fmt.Fprintf(os.Stderr, "go: invalid cflags: %v\n", err)
  53. --- a/src/cmd/go/internal/work/exec.go
  54. +++ b/src/cmd/go/internal/work/exec.go
  55. @@ -32,6 +32,8 @@ import (
  56. "time"
  57. )
  58. +var omitGopath = os.Getenv("GOPATH_OMIT_IN_ACTIONID") != ""
  59. +
  60. // actionList returns the list of actions in the dag rooted at root
  61. // as visited in a depth-first post-order traversal.
  62. func actionList(root *Action) []*Action {
  63. @@ -205,7 +207,7 @@ func (b *Builder) buildActionID(a *Actio
  64. // but it does not hide the exact value of $GOPATH.
  65. // Include the full dir in that case.
  66. // Assume b.WorkDir is being trimmed properly.
  67. - if !p.Goroot && !strings.HasPrefix(p.Dir, b.WorkDir) {
  68. + if !p.Goroot && !omitGopath && !strings.HasPrefix(p.Dir, b.WorkDir) {
  69. fmt.Fprintf(h, "dir %s\n", p.Dir)
  70. }
  71. fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
  72. @@ -219,13 +221,13 @@ func (b *Builder) buildActionID(a *Actio
  73. }
  74. if len(p.CgoFiles)+len(p.SwigFiles) > 0 {
  75. fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo"))
  76. - cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p)
  77. - fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(), cppflags, cflags, ldflags)
  78. + cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p, true)
  79. + fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(true), cppflags, cflags, ldflags)
  80. if len(p.CXXFiles)+len(p.SwigFiles) > 0 {
  81. - fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(), cxxflags)
  82. + fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(true), cxxflags)
  83. }
  84. if len(p.FFiles) > 0 {
  85. - fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(), fflags)
  86. + fmt.Fprintf(h, "FC=%q %q\n", b.fcExe(true), fflags)
  87. }
  88. // TODO(rsc): Should we include the SWIG version or Fortran/GCC/G++/Objective-C compiler versions?
  89. }
  90. @@ -2254,33 +2256,48 @@ var (
  91. // gccCmd returns a gcc command line prefix
  92. // defaultCC is defined in zdefaultcc.go, written by cmd/dist.
  93. func (b *Builder) GccCmd(incdir, workdir string) []string {
  94. - return b.compilerCmd(b.ccExe(), incdir, workdir)
  95. + return b.compilerCmd(b.ccExe(false), incdir, workdir)
  96. }
  97. // gxxCmd returns a g++ command line prefix
  98. // defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
  99. func (b *Builder) GxxCmd(incdir, workdir string) []string {
  100. - return b.compilerCmd(b.cxxExe(), incdir, workdir)
  101. + return b.compilerCmd(b.cxxExe(false), incdir, workdir)
  102. }
  103. // gfortranCmd returns a gfortran command line prefix.
  104. func (b *Builder) gfortranCmd(incdir, workdir string) []string {
  105. - return b.compilerCmd(b.fcExe(), incdir, workdir)
  106. + return b.compilerCmd(b.fcExe(false), incdir, workdir)
  107. }
  108. // ccExe returns the CC compiler setting without all the extra flags we add implicitly.
  109. -func (b *Builder) ccExe() []string {
  110. - return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch))
  111. +func (b *Builder) ccExe(filtered bool) []string {
  112. + return b.compilerExe(origCC, cfg.DefaultCC(cfg.Goos, cfg.Goarch), filtered)
  113. }
  114. // cxxExe returns the CXX compiler setting without all the extra flags we add implicitly.
  115. -func (b *Builder) cxxExe() []string {
  116. - return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch))
  117. +func (b *Builder) cxxExe(filtered bool) []string {
  118. + return b.compilerExe(origCXX, cfg.DefaultCXX(cfg.Goos, cfg.Goarch), filtered)
  119. }
  120. // fcExe returns the FC compiler setting without all the extra flags we add implicitly.
  121. -func (b *Builder) fcExe() []string {
  122. - return b.compilerExe(cfg.Getenv("FC"), "gfortran")
  123. +func (b *Builder) fcExe(filtered bool) []string {
  124. + return b.compilerExe(os.Getenv("FC"), "gfortran", filtered)
  125. +}
  126. +
  127. +var filterFlags = os.Getenv("CGO_PEDANTIC") == ""
  128. +
  129. +func filterCompilerFlags(flags []string) []string {
  130. + var newflags []string
  131. + if !filterFlags {
  132. + return flags
  133. + }
  134. + for _, flag := range flags {
  135. + if strings.HasPrefix(flag, "-m") {
  136. + newflags = append(newflags, flag)
  137. + }
  138. + }
  139. + return newflags
  140. }
  141. // compilerExe returns the compiler to use given an
  142. @@ -2289,11 +2306,16 @@ func (b *Builder) fcExe() []string {
  143. // of the compiler but can have additional arguments if they
  144. // were present in the environment value.
  145. // For example if CC="gcc -DGOPHER" then the result is ["gcc", "-DGOPHER"].
  146. -func (b *Builder) compilerExe(envValue string, def string) []string {
  147. +func (b *Builder) compilerExe(envValue string, def string, filtered bool) []string {
  148. compiler := strings.Fields(envValue)
  149. if len(compiler) == 0 {
  150. compiler = []string{def}
  151. }
  152. +
  153. + if filtered {
  154. + return append(compiler[0:1], filterCompilerFlags(compiler[1:])...)
  155. + }
  156. +
  157. return compiler
  158. }
  159. @@ -2454,7 +2476,7 @@ func envList(key, def string) []string {
  160. }
  161. // CFlags returns the flags to use when invoking the C, C++ or Fortran compilers, or cgo.
  162. -func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
  163. +func (b *Builder) CFlags(p *load.Package, filtered bool) (cppflags, cflags, cxxflags, fflags, ldflags []string, err error) {
  164. defaults := "-g -O2"
  165. if cppflags, err = buildFlags("CPPFLAGS", "", p.CgoCPPFLAGS, checkCompilerFlags); err != nil {
  166. @@ -2473,6 +2495,14 @@ func (b *Builder) CFlags(p *load.Package
  167. return
  168. }
  169. + if filtered {
  170. + cppflags = filterCompilerFlags(cppflags)
  171. + cflags = filterCompilerFlags(cflags)
  172. + cxxflags = filterCompilerFlags(cxxflags)
  173. + fflags = filterCompilerFlags(fflags)
  174. + ldflags = filterCompilerFlags(ldflags)
  175. + }
  176. +
  177. return
  178. }
  179. @@ -2487,7 +2517,7 @@ var cgoRe = lazyregexp.New(`[/\\:]`)
  180. func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
  181. p := a.Package
  182. - cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p)
  183. + cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS, err := b.CFlags(p, false)
  184. if err != nil {
  185. return nil, nil, err
  186. }
  187. @@ -2846,7 +2876,7 @@ func (b *Builder) swigIntSize(objdir str
  188. // Run SWIG on one SWIG input file.
  189. func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
  190. - cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)
  191. + cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p, false)
  192. if err != nil {
  193. return "", "", err
  194. }