diff --git a/src/cmd/compile/internal/gc/main.go b/src/cmd/compile/internal/gc/main.go index 495baebcc4c19..ce84024174345 100644 --- a/src/cmd/compile/internal/gc/main.go +++ b/src/cmd/compile/internal/gc/main.go @@ -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 == "" { diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index 49614a9bef794..930df005df01b 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -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") +} diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index 2f903adf3e3cd..4e181933a75e8 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -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 index 0000000000000..3e623c662100d --- /dev/null +++ b/src/cmd/go/internal/work/testgo.go @@ -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 + } +}