From: Ian Lance Taylor Date: Fri, 14 Jul 2017 15:22:30 +0000 (-0700) Subject: cmd/go: update BuildContext.GOROOT and build.Tooldir with computed GOROOT X-Git-Tag: go1.9rc1~66 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7914369e64190ce22e6fd6bba685e3f82338875a;p=gostls13.git 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/cmd/dist/test.go b/src/cmd/dist/test.go index a2a5126d2d..84d30a4a92 100644 --- a/src/cmd/dist/test.go +++ b/src/cmd/dist/test.go @@ -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 diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index 65cc9a221c..b3ad1ce71e 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -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) diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index 3a64af35b1..0ea327f8bc 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -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.