]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: update BuildContext.GOROOT and build.Tooldir with computed GOROOT
authorIan Lance Taylor <iant@golang.org>
Fri, 14 Jul 2017 15:22:30 +0000 (08:22 -0700)
committerIan Lance Taylor <iant@golang.org>
Fri, 14 Jul 2017 20:07:08 +0000 (20:07 +0000)
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: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/dist/test.go
src/cmd/go/internal/cfg/cfg.go
src/cmd/go/internal/work/build.go

index a2a5126d2d189fd90375de984d10f5adb29cc625..84d30a4a920d7e6834e0a11306e48f0925430014 100644 (file)
@@ -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
index 65cc9a221c166431a1b3016f427bfcd9f5bff25f..b3ad1ce71ec1906d0ffc6c70596bccb0c592b2a6 100644 (file)
@@ -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)
index 3a64af35b1298ddb59f3c22e53d299195fcb471e..0ea327f8bc6896182efdb2ea4ef28c0d41d1c9e0 100644 (file)
@@ -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.