0007-cmd-go-make-GOROOT-precious-by-default.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. From 9ba507e076c744f4d394418e4a849e68cd426a4a Mon Sep 17 00:00:00 2001
  2. From: Alex Kube <alexander.j.kube@gmail.com>
  3. Date: Wed, 23 Oct 2019 21:18:56 +0430
  4. Subject: [PATCH 7/9] cmd/go: make GOROOT precious by default
  5. Upstream-Status: Inappropriate [OE specific]
  6. The go build tool normally rebuilds whatever it detects is
  7. stale. This can be a problem when GOROOT is intended to
  8. be read-only and the go runtime has been built as a shared
  9. library, since we don't want every application to be rebuilding
  10. the shared runtime - particularly in cross-build/packaging
  11. setups, since that would lead to 'abi mismatch' runtime errors.
  12. This patch prevents the install and linkshared actions from
  13. installing to GOROOT unless overridden with the GOROOT_OVERRIDE
  14. environment variable.
  15. Adapted to Go 1.13 from patches originally submitted to
  16. the meta/recipes-devtools/go tree by
  17. Matt Madison <matt@madison.systems>.
  18. Signed-off-by: Alexander J Kube <alexander.j.kube@gmail.com>
  19. ---
  20. src/cmd/go/internal/work/action.go | 3 +++
  21. src/cmd/go/internal/work/build.go | 6 ++++++
  22. src/cmd/go/internal/work/exec.go | 25 +++++++++++++++++++++++++
  23. 3 files changed, 34 insertions(+)
  24. diff --git a/src/cmd/go/internal/work/action.go b/src/cmd/go/internal/work/action.go
  25. index 33b7818..7617b4c 100644
  26. --- a/src/cmd/go/internal/work/action.go
  27. +++ b/src/cmd/go/internal/work/action.go
  28. @@ -662,6 +662,9 @@ func (b *Builder) addTransitiveLinkDeps(a, a1 *Action, shlib string) {
  29. if p1 == nil || p1.Shlib == "" || haveShlib[filepath.Base(p1.Shlib)] {
  30. continue
  31. }
  32. + if goRootPrecious && (p1.Standard || p1.Goroot) {
  33. + continue
  34. + }
  35. haveShlib[filepath.Base(p1.Shlib)] = true
  36. // TODO(rsc): The use of ModeInstall here is suspect, but if we only do ModeBuild,
  37. // we'll end up building an overall library or executable that depends at runtime
  38. diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go
  39. index 9305b2d..6560317 100644
  40. --- a/src/cmd/go/internal/work/build.go
  41. +++ b/src/cmd/go/internal/work/build.go
  42. @@ -155,6 +155,8 @@ See also: go install, go get, go clean.
  43. const concurrentGCBackendCompilationEnabledByDefault = true
  44. +var goRootPrecious bool = true
  45. +
  46. func init() {
  47. // break init cycle
  48. CmdBuild.Run = runBuild
  49. @@ -167,6 +169,10 @@ func init() {
  50. AddBuildFlags(CmdBuild)
  51. AddBuildFlags(CmdInstall)
  52. +
  53. + if x := os.Getenv("GOROOT_OVERRIDE"); x != "" {
  54. + goRootPrecious = false
  55. + }
  56. }
  57. // Note that flags consulted by other parts of the code
  58. diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
  59. index ccebaf8..59450d7 100644
  60. --- a/src/cmd/go/internal/work/exec.go
  61. +++ b/src/cmd/go/internal/work/exec.go
  62. @@ -455,6 +455,23 @@ func (b *Builder) build(a *Action) (err error) {
  63. return errors.New("binary-only packages are no longer supported")
  64. }
  65. + if goRootPrecious && (a.Package.Standard || a.Package.Goroot) {
  66. + _, err := os.Stat(a.Package.Target)
  67. + if err == nil {
  68. + a.built = a.Package.Target
  69. + a.Target = a.Package.Target
  70. + a.buildID = b.fileHash(a.Package.Target)
  71. + a.Package.Stale = false
  72. + a.Package.StaleReason = "GOROOT-resident package"
  73. + return nil
  74. + }
  75. + a.Package.Stale = true
  76. + a.Package.StaleReason = "missing or invalid GOROOT-resident package"
  77. + if b.IsCmdList {
  78. + return nil
  79. + }
  80. + }
  81. +
  82. if err := b.Mkdir(a.Objdir); err != nil {
  83. return err
  84. }
  85. @@ -1499,6 +1516,14 @@ func BuildInstallFunc(b *Builder, a *Action) (err error) {
  86. return nil
  87. }
  88. + if goRootPrecious && a.Package != nil {
  89. + p := a.Package
  90. + if p.Standard || p.Goroot {
  91. + err := fmt.Errorf("attempting to install package %s into read-only GOROOT", p.ImportPath)
  92. + return err
  93. + }
  94. + }
  95. +
  96. if err := b.Mkdir(a.Objdir); err != nil {
  97. return err
  98. }
  99. --
  100. 2.17.1 (Apple Git-112)