From acd179b49f1fc8d6f7f69e569fb4a56039c725a1 Mon Sep 17 00:00:00 2001
From: Matt Madison <matt@madison.systems>
Date: Sat, 17 Feb 2018 05:24:20 -0800
Subject: [PATCH 3/9] allow GOTOOLDIR to be overridden in the environment

to allow for split host/target build roots

Upstream-Status: Inappropriate [OE specific]

Signed-off-by: Matt Madison <matt@madison.systems>
---
 src/cmd/dist/build.go          | 4 +++-
 src/cmd/go/internal/cfg/cfg.go | 6 +++++-
 src/go/build/build.go          | 2 +-
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
index 49ed80033e..afc615b5c2 100644
--- a/src/cmd/dist/build.go
+++ b/src/cmd/dist/build.go
@@ -220,7 +220,9 @@ func xinit() {
 	workdir = xworkdir()
 	xatexit(rmworkdir)
 
-	tooldir = pathf("%s/pkg/tool/%s_%s", goroot, gohostos, gohostarch)
+	if tooldir = os.Getenv("GOTOOLDIR"); tooldir == "" {
+		tooldir = pathf("%s/pkg/tool/%s_%s", goroot, gohostos, gohostarch)
+	}
 }
 
 // compilerEnv returns a map from "goos/goarch" to the
diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go
index 1de4f0dc79..4f6010d660 100644
--- a/src/cmd/go/internal/cfg/cfg.go
+++ b/src/cmd/go/internal/cfg/cfg.go
@@ -96,7 +96,11 @@ func init() {
 	// as the tool directory does not move based on environment variables.
 	// This matches the initialization of ToolDir in go/build,
 	// except for using GOROOT rather than runtime.GOROOT().
-	build.ToolDir = filepath.Join(GOROOT, "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
+	if s := os.Getenv("GOTOOLDIR"); s != "" {
+		build.ToolDir = filepath.Clean(s)
+	} else {
+		build.ToolDir = filepath.Join(GOROOT, "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
+	}
 }
 
 func findGOROOT() string {
diff --git a/src/go/build/build.go b/src/go/build/build.go
index 68fb423983..81b1b32270 100644
--- a/src/go/build/build.go
+++ b/src/go/build/build.go
@@ -1594,7 +1594,7 @@ func init() {
 }
 
 // ToolDir is the directory containing build tools.
-var ToolDir = filepath.Join(runtime.GOROOT(), "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
+var ToolDir = envOr("GOTOOLDIR", filepath.Join(runtime.GOROOT(), "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH))
 
 // IsLocalImport reports whether the import path is
 // a local import path, like ".", "..", "./foo", or "../foo".
-- 
2.14.1

