]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go, cmd/compile: match tool versions
authorDavid Crawshaw <crawshaw@golang.org>
Thu, 4 May 2017 17:58:27 +0000 (13:58 -0400)
committerDavid Crawshaw <crawshaw@golang.org>
Thu, 4 May 2017 20:19:48 +0000 (20:19 +0000)
This change passes runtime.Version from the go tool to the compiler.
If the versions do not match, the compilation fails.
The result is a go tool from one GOROOT will complain loudly if it
is invoked with a different GOROOT value.

Only release versions are checked, so that when developing Go
you can still use "go install cmd/go" and "go install cmd/compile"
separately.

Fixes #19064

Change-Id: I17e184d07d3c1092b1d9af53ba55ed3ecf67791d
Reviewed-on: https://go-review.googlesource.com/42595
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/compile/internal/gc/main.go
src/cmd/go/go_test.go
src/cmd/go/internal/work/build.go
src/cmd/go/internal/work/testgo.go [new file with mode: 0644]

index 495baebcc4c1991ba9a08beb0fcb3cb52e8c6bda..ce84024174345fe5654d9bfa193e3d42633797eb 100644 (file)
@@ -222,6 +222,8 @@ func Main(archInit func(*Arch)) {
        flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to `file`")
        flag.StringVar(&memprofile, "memprofile", "", "write memory profile to `file`")
        flag.Int64Var(&memprofilerate, "memprofilerate", 0, "set runtime.MemProfileRate to `rate`")
+       var goversion string
+       flag.StringVar(&goversion, "goversion", "", "required version of the runtime")
        flag.StringVar(&traceprofile, "traceprofile", "", "write an execution trace to `file`")
        flag.StringVar(&blockprofile, "blockprofile", "", "write block profile to `file`")
        flag.StringVar(&mutexprofile, "mutexprofile", "", "write mutex profile to `file`")
@@ -242,6 +244,11 @@ func Main(archInit func(*Arch)) {
                usage()
        }
 
+       if goversion != "" && goversion != runtime.Version() {
+               fmt.Printf("compile: version %q does not match go tool version %q\n", runtime.Version(), goversion)
+               Exit(2)
+       }
+
        thearch.LinkArch.Init(Ctxt)
 
        if outfile == "" {
index 49614a9bef7943c573251538dc80e8465617fa3a..930df005df01b214a46596e3b576e7433a2159f1 100644 (file)
@@ -4013,3 +4013,14 @@ func TestExecutableGOROOT(t *testing.T) {
                t.Fatalf("%s env GOROOT = %q, want %q", symGoTool, got, want)
        }
 }
+
+func TestNeedVersion(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.parallel()
+       tg.tempFile("goversion.go", `package main; func main() {}`)
+       path := tg.path("goversion.go")
+       tg.setenv("TESTGO_VERSION", "go1.testgo")
+       tg.runFail("run", path)
+       tg.grepStderr("compile", "does not match go tool version")
+}
index 2f903adf3e3cdf02084d15535362895548d69b78..4e181933a75e8677f3a37bbc0e697bb2bc65b678 100644 (file)
@@ -392,8 +392,13 @@ func BuildModeInit() {
                        cfg.BuildContext.InstallSuffix += codegenArg[1:]
                }
        }
+       if strings.HasPrefix(runtimeVersion, "go1") {
+               buildGcflags = append(buildGcflags, "-goversion", runtimeVersion)
+       }
 }
 
+var runtimeVersion = runtime.Version()
+
 func runBuild(cmd *base.Command, args []string) {
        InstrumentInit()
        BuildModeInit()
diff --git a/src/cmd/go/internal/work/testgo.go b/src/cmd/go/internal/work/testgo.go
new file mode 100644 (file)
index 0000000..3e623c6
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This file contains extra hooks for testing the go command.
+
+// +build testgo
+
+package work
+
+import "os"
+
+func init() {
+       if v := os.Getenv("TESTGO_VERSION"); v != "" {
+               runtimeVersion = v
+       }
+}