blob: 38b28b987716e0040dd3cfa352c30cfa35caad30 [file] [log] [blame]
Andrew Geissler6aa7eec2023-03-03 12:41:14 -06001From efab470498bb0a30ee2d00455a0c8c10459f6347 Mon Sep 17 00:00:00 2001
Andrew Geissler82c905d2020-04-13 13:39:40 -05002From: Alex Kube <alexander.j.kube@gmail.com>
3Date: Wed, 23 Oct 2019 21:18:56 +0430
Andrew Geissler6aa7eec2023-03-03 12:41:14 -06004Subject: [PATCH 6/9] cmd/go: make GOROOT precious by default
Andrew Geissler82c905d2020-04-13 13:39:40 -05005
6Upstream-Status: Inappropriate [OE specific]
7
8The go build tool normally rebuilds whatever it detects is
9stale. This can be a problem when GOROOT is intended to
10be read-only and the go runtime has been built as a shared
11library, since we don't want every application to be rebuilding
12the shared runtime - particularly in cross-build/packaging
13setups, since that would lead to 'abi mismatch' runtime errors.
14
15This patch prevents the install and linkshared actions from
16installing to GOROOT unless overridden with the GOROOT_OVERRIDE
17environment variable.
18
19Adapted to Go 1.13 from patches originally submitted to
20the meta/recipes-devtools/go tree by
21Matt Madison <matt@madison.systems>.
22
23Signed-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
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060030diff --git a/src/cmd/go/internal/work/action.go b/src/cmd/go/internal/work/action.go
31index 8beb134..68a8cfe 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -050032--- a/src/cmd/go/internal/work/action.go
33+++ b/src/cmd/go/internal/work/action.go
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060034@@ -718,6 +718,9 @@ func (b *Builder) addTransitiveLinkDeps(a, a1 *Action, shlib string) {
Andrew Geissler82c905d2020-04-13 13:39:40 -050035 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
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060044diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go
45index 2f2860a..8cc6166 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -050046--- a/src/cmd/go/internal/work/build.go
47+++ b/src/cmd/go/internal/work/build.go
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060048@@ -217,6 +217,8 @@ See also: go install, go get, go clean.
Andrew Geissler82c905d2020-04-13 13:39:40 -050049
50 const concurrentGCBackendCompilationEnabledByDefault = true
51
52+var goRootPrecious bool = true
53+
54 func init() {
55 // break init cycle
56 CmdBuild.Run = runBuild
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060057@@ -230,6 +232,10 @@ func init() {
58 AddCoverFlags(CmdBuild, nil)
59 AddCoverFlags(CmdInstall, nil)
60 }
Andrew Geissler82c905d2020-04-13 13:39:40 -050061+
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
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060068diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
69index 7e4fcb3..d83b31b 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -050070--- a/src/cmd/go/internal/work/exec.go
71+++ b/src/cmd/go/internal/work/exec.go
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060072@@ -527,6 +527,23 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
Andrew Geissler82c905d2020-04-13 13:39:40 -050073 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 }
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060096@@ -1624,6 +1641,14 @@ func (b *Builder) linkShared(ctx context.Context, a *Action) (err error) {
Andrew Geisslerc9f78652020-09-18 14:11:35 -050097 return err
Andrew Geissler82c905d2020-04-13 13:39:40 -050098 }
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 }
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600111--
1122.30.2
113