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