1 From 7cc60b3887be2d5674b9f5d422d022976cf205e5 Mon Sep 17 00:00:00 2001
2 From: Matt Madison <matt@madison.systems>
3 Date: Fri, 2 Mar 2018 06:00:20 -0800
4 Subject: [PATCH] cmd/go: make GOROOT precious by default
6 The go build tool normally rebuilds whatever it detects is
7 stale. This can be a problem when GOROOT is intended to
8 be read-only and the go runtime has been built as a shared
9 library, since we don't want every application to be rebuilding
10 the shared runtime - particularly in cross-build/packaging
11 setups, since that would lead to 'abi mismatch' runtime errors.
13 This patch prevents the install and linkshared actions from
14 installing to GOROOT unless overridden with the GOROOT_OVERRIDE
17 Upstream-Status: Inappropriate [OE specific]
19 Signed-off-by: Matt Madison <matt@madison.systems>
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(+)
27 Index: 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)] {
35 + if goRootPrecious && (p1.Standard || p1.Goroot) {
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
41 Index: 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.
48 const concurrentGCBackendCompilationEnabledByDefault = true
49 +var goRootPrecious bool = true
53 @@ -160,6 +161,10 @@ func init() {
55 AddBuildFlags(CmdBuild)
56 AddBuildFlags(CmdInstall)
58 + if x := os.Getenv("GOROOT_OVERRIDE"); x != "" {
59 + goRootPrecious = false
63 // Note that flags consulted by other parts of the code
64 Index: 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)
72 + if goRootPrecious && (a.Package.Standard || a.Package.Goroot) {
73 + _, err := os.Stat(a.Package.Target)
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"
82 + a.Package.Stale = true
83 + a.Package.StaleReason = "missing or invalid GOROOT-resident package"
89 if err := b.Mkdir(a.Objdir); err != nil {
92 @@ -1438,6 +1455,14 @@ func BuildInstallFunc(b *Builder, a *Act
96 + if goRootPrecious && a.Package != nil {
98 + if p.Standard || p.Goroot {
99 + err := fmt.Errorf("attempting to install package %s into read-only GOROOT", p.ImportPath)
104 if err := b.Mkdir(a.Objdir); err != nil {