summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/go/go-1.10/0007-cmd-go-make-GOROOT-precious-by-default.patch
blob: 72427627bc41110297268c35d595701ed32d5a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
From f98aa287941417226a6e4f78759f8a5e19732cde Mon Sep 17 00:00:00 2001
From: Matt Madison <matt@madison.systems>
Date: Fri, 2 Mar 2018 06:00:20 -0800
Subject: [PATCH 7/9] cmd/go: make GOROOT precious by default

The go build tool normally rebuilds whatever it detects is
stale.  This can be a problem when GOROOT is intended to
be read-only and the go runtime has been built as a shared
library, since we don't want every application to be rebuilding
the shared runtime - particularly in cross-build/packaging
setups, since that would lead to 'abi mismatch' runtime errors.

This patch prevents the install and linkshared actions from
installing to GOROOT unless overridden with the GOROOT_OVERRIDE
environment variable.

Upstream-Status: Inappropriate [OE specific]

Signed-off-by: Matt Madison <matt@madison.systems>
---
 src/cmd/go/internal/work/action.go |  3 +++
 src/cmd/go/internal/work/build.go  |  5 +++++
 src/cmd/go/internal/work/exec.go   | 25 +++++++++++++++++++++++++
 3 files changed, 33 insertions(+)

diff --git a/src/cmd/go/internal/work/action.go b/src/cmd/go/internal/work/action.go
index 9f1f8f8a50..a382880474 100644
--- a/src/cmd/go/internal/work/action.go
+++ b/src/cmd/go/internal/work/action.go
@@ -563,6 +563,9 @@ func (b *Builder) addTransitiveLinkDeps(a, a1 *Action, shlib string) {
 			if p1 == nil || p1.Shlib == "" || haveShlib[filepath.Base(p1.Shlib)] {
 				continue
 			}
+			if goRootPrecious && (p1.Standard || p1.Goroot) {
+				continue
+			}
 			haveShlib[filepath.Base(p1.Shlib)] = true
 			// TODO(rsc): The use of ModeInstall here is suspect, but if we only do ModeBuild,
 			// we'll end up building an overall library or executable that depends at runtime
diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go
index 57b7b00879..e2ba95420e 100644
--- a/src/cmd/go/internal/work/build.go
+++ b/src/cmd/go/internal/work/build.go
@@ -143,6 +143,7 @@ See also: go install, go get, go clean.
 }
 
 const concurrentGCBackendCompilationEnabledByDefault = true
+var goRootPrecious bool = true
 
 func init() {
 	// break init cycle
@@ -156,6 +157,10 @@ func init() {
 
 	AddBuildFlags(CmdBuild)
 	AddBuildFlags(CmdInstall)
+
+	if x := os.Getenv("GOROOT_OVERRIDE"); x != "" {
+		goRootPrecious = false
+	}
 }
 
 // Note that flags consulted by other parts of the code
diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
index b0f6b45647..c8f266a8ad 100644
--- a/src/cmd/go/internal/work/exec.go
+++ b/src/cmd/go/internal/work/exec.go
@@ -371,6 +371,23 @@ func (b *Builder) build(a *Action) (err error) {
 		return fmt.Errorf("missing or invalid binary-only package")
 	}
 
+	if goRootPrecious && (a.Package.Standard || a.Package.Goroot) {
+		_, err := os.Stat(a.Package.Target)
+		if err == nil {
+			a.built = a.Package.Target
+			a.Target = a.Package.Target
+			a.buildID = b.fileHash(a.Package.Target)
+			a.Package.Stale = false
+			a.Package.StaleReason = "GOROOT-resident package"
+			return nil
+		}
+		if b.ComputeStaleOnly {
+			a.Package.Stale = true
+			a.Package.StaleReason = "missing or invalid GOROOT-resident package"
+			return nil
+		}
+	}
+
 	if err := b.Mkdir(a.Objdir); err != nil {
 		return err
 	}
@@ -1097,6 +1114,14 @@ func BuildInstallFunc(b *Builder, a *Action) (err error) {
 		return nil
 	}
 
+	if goRootPrecious && a.Package != nil {
+		p := a.Package
+		if p.Standard || p.Goroot {
+			err := fmt.Errorf("attempting to install package %s into read-only GOROOT", p.ImportPath)
+			return err
+		}
+	}
+
 	if err := b.Mkdir(a.Objdir); err != nil {
 		return err
 	}
-- 
2.14.1