1From 9ba507e076c744f4d394418e4a849e68cd426a4a Mon Sep 17 00:00:00 2001
2From: Alex Kube <alexander.j.kube@gmail.com>
3Date: Wed, 23 Oct 2019 21:18:56 +0430
4Subject: [PATCH 7/9] cmd/go: make GOROOT precious by default
5
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
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
66@@ -468,6 +468,23 @@ func (b *Builder) build(a *Action) (err
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 	}
90@@ -1520,6 +1537,14 @@ func BuildInstallFunc(b *Builder, a *Act
91 		return err
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 	}
105