Commit 7914369e authored by Ian Lance Taylor's avatar Ian Lance Taylor

cmd/go: update BuildContext.GOROOT and build.Tooldir with computed GOROOT

This is necessary to make a relocated GOROOT work correctly.

Fixes #20997

Change-Id: I18624bd2e109721066cd9e4a887a12583ab79f5d
Reviewed-on: https://go-review.googlesource.com/48550
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 4dbcacda
......@@ -434,6 +434,38 @@ func (t *tester) registerTests() {
})
}
// On the builders only, test that a moved GOROOT still works.
if os.Getenv("GO_BUILDER_NAME") != "" {
t.tests = append(t.tests, distTest{
name: "moved_goroot",
heading: "moved GOROOT",
fn: func(dt *distTest) error {
t.runPending(dt)
moved := t.goroot + "-moved"
if err := os.Rename(t.goroot, moved); err != nil {
return err
}
// Run `go test fmt` in the moved GOROOT.
cmd := exec.Command(filepath.Join(moved, "bin", "go"), "test", "fmt")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Don't set GOROOT in the environment.
for _, e := range os.Environ() {
if !strings.HasPrefix(e, "GOROOT=") {
cmd.Env = append(cmd.Env, e)
}
}
err := cmd.Run()
if rerr := os.Rename(moved, t.goroot); rerr != nil {
log.Fatalf("failed to restore GOROOT: %v", rerr)
}
return err
},
})
}
// Test that internal linking of standard packages does not
// require libgcc. This ensures that we can install a Go
// release on a system that does not have a C compiler
......
......@@ -60,12 +60,18 @@ var CmdEnv []EnvVar
// Global build parameters (used during package load)
var (
Goarch string
Goos string
Goarch = BuildContext.GOARCH
Goos = BuildContext.GOOS
ExeSuffix string
Gopath []string
Gopath = filepath.SplitList(BuildContext.GOPATH)
)
func init() {
if Goos == "windows" {
ExeSuffix = ".exe"
}
}
var (
GOROOT = findGOROOT()
GOBIN = os.Getenv("GOBIN")
......@@ -78,6 +84,16 @@ var (
GO386 = objabi.GO386
)
// Update build context to use our computed GOROOT.
func init() {
BuildContext.GOROOT = GOROOT
// Note that we must use runtime.GOOS and runtime.GOARCH here,
// 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)
}
func findGOROOT() string {
if env := os.Getenv("GOROOT"); env != "" {
return filepath.Clean(env)
......
......@@ -643,16 +643,6 @@ func InstallPackages(args []string, forGet bool) {
}
}
func init() {
cfg.Goarch = cfg.BuildContext.GOARCH
cfg.Goos = cfg.BuildContext.GOOS
if cfg.Goos == "windows" {
cfg.ExeSuffix = ".exe"
}
cfg.Gopath = filepath.SplitList(cfg.BuildContext.GOPATH)
}
// A Builder holds global state about a build.
// It does not hold per-package state, because we
// build packages in parallel, and the builder is shared.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment