blob: 29ef947abd6fb6e6a15d30fce650b89f3de9d727 [file] [log] [blame]
Brad Bishop19323692019-04-05 15:28:33 -04001From 7cc60b3887be2d5674b9f5d422d022976cf205e5 Mon Sep 17 00:00:00 2001
2From: Matt Madison <matt@madison.systems>
3Date: Fri, 2 Mar 2018 06:00:20 -0800
4Subject: [PATCH] cmd/go: make GOROOT precious by default
5
6The go build tool normally rebuilds whatever it detects is
7stale. This can be a problem when GOROOT is intended to
8be read-only and the go runtime has been built as a shared
9library, since we don't want every application to be rebuilding
10the shared runtime - particularly in cross-build/packaging
11setups, since that would lead to 'abi mismatch' runtime errors.
12
13This patch prevents the install and linkshared actions from
14installing to GOROOT unless overridden with the GOROOT_OVERRIDE
15environment variable.
16
17Upstream-Status: Inappropriate [OE specific]
18
19Signed-off-by: Matt Madison <matt@madison.systems>
20
21---
22 src/cmd/go/internal/work/action.go | 3 +++
23 src/cmd/go/internal/work/build.go | 5 +++++
24 src/cmd/go/internal/work/exec.go | 25 +++++++++++++++++++++++++
25 3 files changed, 33 insertions(+)
26
27Index: go/src/cmd/go/internal/work/action.go
28===================================================================
29--- go.orig/src/cmd/go/internal/work/action.go
30+++ go/src/cmd/go/internal/work/action.go
31@@ -600,6 +600,9 @@ func (b *Builder) addTransitiveLinkDeps(
32 if p1 == nil || p1.Shlib == "" || haveShlib[filepath.Base(p1.Shlib)] {
33 continue
34 }
35+ if goRootPrecious && (p1.Standard || p1.Goroot) {
36+ continue
37+ }
38 haveShlib[filepath.Base(p1.Shlib)] = true
39 // TODO(rsc): The use of ModeInstall here is suspect, but if we only do ModeBuild,
40 // we'll end up building an overall library or executable that depends at runtime
41Index: go/src/cmd/go/internal/work/build.go
42===================================================================
43--- go.orig/src/cmd/go/internal/work/build.go
44+++ go/src/cmd/go/internal/work/build.go
45@@ -147,6 +147,7 @@ See also: go install, go get, go clean.
46 }
47
48 const concurrentGCBackendCompilationEnabledByDefault = true
49+var goRootPrecious bool = true
50
51 func init() {
52 // break init cycle
53@@ -160,6 +161,10 @@ func init() {
54
55 AddBuildFlags(CmdBuild)
56 AddBuildFlags(CmdInstall)
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
64Index: go/src/cmd/go/internal/work/exec.go
65===================================================================
66--- go.orig/src/cmd/go/internal/work/exec.go
67+++ go/src/cmd/go/internal/work/exec.go
68@@ -436,6 +436,23 @@ func (b *Builder) build(a *Action) (err
69 return fmt.Errorf("missing or invalid binary-only package; expected file %q", a.Package.Target)
70 }
71
72+ if goRootPrecious && (a.Package.Standard || a.Package.Goroot) {
73+ _, err := os.Stat(a.Package.Target)
74+ if err == nil {
75+ a.built = a.Package.Target
76+ a.Target = a.Package.Target
77+ a.buildID = b.fileHash(a.Package.Target)
78+ a.Package.Stale = false
79+ a.Package.StaleReason = "GOROOT-resident package"
80+ return nil
81+ }
82+ a.Package.Stale = true
83+ a.Package.StaleReason = "missing or invalid GOROOT-resident package"
84+ if b.IsCmdList {
85+ return nil
86+ }
87+ }
88+
89 if err := b.Mkdir(a.Objdir); err != nil {
90 return err
91 }
92@@ -1438,6 +1455,14 @@ func BuildInstallFunc(b *Builder, a *Act
93 return nil
94 }
95
96+ if goRootPrecious && a.Package != nil {
97+ p := a.Package
98+ if p.Standard || p.Goroot {
99+ err := fmt.Errorf("attempting to install package %s into read-only GOROOT", p.ImportPath)
100+ return err
101+ }
102+ }
103+
104 if err := b.Mkdir(a.Objdir); err != nil {
105 return err
106 }