@@ -87,16 +87,6 @@ func defaultContext() build.Context {
87
87
88
88
ctxt .JoinPath = filepath .Join // back door to say "do not use go command"
89
89
90
- ctxt .GOROOT = findGOROOT ()
91
- if runtime .Compiler != "gccgo" {
92
- // Note that we must use runtime.GOOS and runtime.GOARCH here,
93
- // as the tool directory does not move based on environment
94
- // variables. This matches the initialization of ToolDir in
95
- // go/build, except for using ctxt.GOROOT rather than
96
- // runtime.GOROOT.
97
- build .ToolDir = filepath .Join (ctxt .GOROOT , "pkg/tool/" + runtime .GOOS + "_" + runtime .GOARCH )
98
- }
99
-
100
90
// Override defaults computed in go/build with defaults
101
91
// from go environment configuration file, if known.
102
92
ctxt .GOPATH = envOr ("GOPATH" , gopath (ctxt ))
@@ -146,10 +136,36 @@ func defaultContext() build.Context {
146
136
}
147
137
148
138
func init () {
139
+ SetGOROOT (findGOROOT ())
149
140
BuildToolchainCompiler = func () string { return "missing-compiler" }
150
141
BuildToolchainLinker = func () string { return "missing-linker" }
151
142
}
152
143
144
+ func SetGOROOT (goroot string ) {
145
+ BuildContext .GOROOT = goroot
146
+
147
+ GOROOT = goroot
148
+ if goroot == "" {
149
+ GOROOTbin = ""
150
+ GOROOTpkg = ""
151
+ GOROOTsrc = ""
152
+ } else {
153
+ GOROOTbin = filepath .Join (goroot , "bin" )
154
+ GOROOTpkg = filepath .Join (goroot , "pkg" )
155
+ GOROOTsrc = filepath .Join (goroot , "src" )
156
+ }
157
+ GOROOT_FINAL = findGOROOT_FINAL (goroot )
158
+
159
+ if runtime .Compiler != "gccgo" && goroot != "" {
160
+ // Note that we must use runtime.GOOS and runtime.GOARCH here,
161
+ // as the tool directory does not move based on environment
162
+ // variables. This matches the initialization of ToolDir in
163
+ // go/build, except for using BuildContext.GOROOT rather than
164
+ // runtime.GOROOT.
165
+ build .ToolDir = filepath .Join (goroot , "pkg/tool/" + runtime .GOOS + "_" + runtime .GOARCH )
166
+ }
167
+ }
168
+
153
169
// Experiment configuration.
154
170
var (
155
171
// RawGOEXPERIMENT is the GOEXPERIMENT value set by the user.
@@ -279,12 +295,12 @@ func CanGetenv(key string) bool {
279
295
}
280
296
281
297
var (
282
- GOROOT = BuildContext .GOROOT
298
+ GOROOT string
299
+ GOROOTbin string
300
+ GOROOTpkg string
301
+ GOROOTsrc string
302
+ GOROOT_FINAL string
283
303
GOBIN = Getenv ("GOBIN" )
284
- GOROOTbin = filepath .Join (GOROOT , "bin" )
285
- GOROOTpkg = filepath .Join (GOROOT , "pkg" )
286
- GOROOTsrc = filepath .Join (GOROOT , "src" )
287
- GOROOT_FINAL = findGOROOT_FINAL ()
288
304
GOMODCACHE = envOr ("GOMODCACHE" , gopathDir ("pkg/mod" ))
289
305
290
306
// Used in envcmd.MkEnv and build ID computations.
@@ -386,10 +402,10 @@ func findGOROOT() string {
386
402
return def
387
403
}
388
404
389
- func findGOROOT_FINAL () string {
405
+ func findGOROOT_FINAL (goroot string ) string {
390
406
// $GOROOT_FINAL is only for use during make.bash
391
407
// so it is not settable using go/env, so we use os.Getenv here.
392
- def := GOROOT
408
+ def := goroot
393
409
if env := os .Getenv ("GOROOT_FINAL" ); env != "" {
394
410
def = filepath .Clean (env )
395
411
}
0 commit comments