From fdad9a0ea659cf2281a0df16b0f69f179605ec9a Mon Sep 17 00:00:00 2001
From: Alex Kube <alexander.j.kube@gmail.com>
Date: Wed, 23 Oct 2019 21:15:37 +0430
Subject: [PATCH 02/11] cmd/go: Allow GOTOOLDIR to be overridden in the
 environment

to allow for split host/target build roots

Adapted to Go 1.13 from patches originally submitted to
the meta/recipes-devtools/go tree by
Matt Madison <matt@madison.systems>.

Upstream-Status: Inappropriate [OE specific]

Signed-off-by: Alexander J Kube <alexander.j.kube@gmail.com>
Signed-off-by: Jose Quaresma <jose.quaresma@foundries.io>
---
 src/cmd/dist/build.go          | 4 +++-
 src/cmd/go/internal/cfg/cfg.go | 6 +++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
index 1f467647f5..b62e518030 100644
--- a/src/cmd/dist/build.go
+++ b/src/cmd/dist/build.go
@@ -280,7 +280,9 @@ func xinit() {
 	}
 	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)
+	}
 
 	goversion := findgoversion()
 	isRelease = strings.HasPrefix(goversion, "release.") || strings.HasPrefix(goversion, "go")
diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go
index 3b9f27e91d..ec043c1530 100644
--- a/src/cmd/go/internal/cfg/cfg.go
+++ b/src/cmd/go/internal/cfg/cfg.go
@@ -248,7 +248,11 @@ func SetGOROOT(goroot string, isTestGo bool) {
 			// This matches the initialization of ToolDir in go/build, except for
 			// using ctxt.GOROOT and the installed GOOS and GOARCH rather than the
 			// GOROOT, GOOS, and GOARCH reported by the runtime package.
-			build.ToolDir = filepath.Join(GOROOTpkg, "tool", installedGOOS+"_"+installedGOARCH)
+	   		if s := os.Getenv("GOTOOLDIR"); s != "" {
+				build.ToolDir = filepath.Clean(s)
+			} else {
+				build.ToolDir = filepath.Join(GOROOTpkg, "tool", installedGOOS+"_"+installedGOARCH)
+			}
 		}
 	}
 }
-- 
2.25.1

