Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -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 | --- a/src/cmd/go/internal/work/action.go |
| 31 | +++ b/src/cmd/go/internal/work/action.go |
| 32 | @@ -670,6 +670,9 @@ func (b *Builder) addTransitiveLinkDeps( |
| 33 | if p1 == nil || p1.Shlib == "" || haveShlib[filepath.Base(p1.Shlib)] { |
| 34 | continue |
| 35 | } |
| 36 | + if goRootPrecious && (p1.Standard || p1.Goroot) { |
| 37 | + continue |
| 38 | + } |
| 39 | haveShlib[filepath.Base(p1.Shlib)] = true |
| 40 | // TODO(rsc): The use of ModeInstall here is suspect, but if we only do ModeBuild, |
| 41 | // we'll end up building an overall library or executable that depends at runtime |
| 42 | --- a/src/cmd/go/internal/work/build.go |
| 43 | +++ b/src/cmd/go/internal/work/build.go |
| 44 | @@ -167,6 +167,8 @@ See also: go install, go get, go clean. |
| 45 | |
| 46 | const concurrentGCBackendCompilationEnabledByDefault = true |
| 47 | |
| 48 | +var goRootPrecious bool = true |
| 49 | + |
| 50 | func init() { |
| 51 | // break init cycle |
| 52 | CmdBuild.Run = runBuild |
| 53 | @@ -179,6 +181,10 @@ func init() { |
| 54 | |
| 55 | AddBuildFlags(CmdBuild, DefaultBuildFlags) |
| 56 | AddBuildFlags(CmdInstall, DefaultBuildFlags) |
| 57 | + |
| 58 | + if x := os.Getenv("GOROOT_OVERRIDE"); x != "" { |
| 59 | + goRootPrecious = false |
| 60 | + } |
| 61 | } |
| 62 | |
| 63 | // Note that flags consulted by other parts of the code |
| 64 | --- a/src/cmd/go/internal/work/exec.go |
| 65 | +++ b/src/cmd/go/internal/work/exec.go |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 66 | @@ -468,6 +468,23 @@ func (b *Builder) build(a *Action) (err |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 67 | return errors.New("binary-only packages are no longer supported") |
| 68 | } |
| 69 | |
| 70 | + if goRootPrecious && (a.Package.Standard || a.Package.Goroot) { |
| 71 | + _, err := os.Stat(a.Package.Target) |
| 72 | + if err == nil { |
| 73 | + a.built = a.Package.Target |
| 74 | + a.Target = a.Package.Target |
| 75 | + a.buildID = b.fileHash(a.Package.Target) |
| 76 | + a.Package.Stale = false |
| 77 | + a.Package.StaleReason = "GOROOT-resident package" |
| 78 | + return nil |
| 79 | + } |
| 80 | + a.Package.Stale = true |
| 81 | + a.Package.StaleReason = "missing or invalid GOROOT-resident package" |
| 82 | + if b.IsCmdList { |
| 83 | + return nil |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | if err := b.Mkdir(a.Objdir); err != nil { |
| 88 | return err |
| 89 | } |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 90 | @@ -1520,6 +1537,14 @@ func BuildInstallFunc(b *Builder, a *Act |
| 91 | return err |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | + if goRootPrecious && a.Package != nil { |
| 95 | + p := a.Package |
| 96 | + if p.Standard || p.Goroot { |
| 97 | + err := fmt.Errorf("attempting to install package %s into read-only GOROOT", p.ImportPath) |
| 98 | + return err |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | if err := b.Mkdir(a.Objdir); err != nil { |
| 103 | return err |
| 104 | } |