]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: for generate, use build context values for GOOS/GOARCH
authorAndrew Gerrand <adg@golang.org>
Mon, 20 Jun 2016 03:30:04 +0000 (13:30 +1000)
committerAndrew Gerrand <adg@golang.org>
Mon, 20 Jun 2016 23:55:43 +0000 (23:55 +0000)
Fixes #16120

Change-Id: Ia352558231e00baab5c698e93d7267564c07ec0c
Reviewed-on: https://go-review.googlesource.com/24242
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Andrew Gerrand <adg@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/go/generate.go
src/cmd/go/go_test.go

index 749e28c24d3f6df06880e5cd057592bf81b9ab43..3c6065e89aafa75f57835862e3bdc2d338e43250 100644 (file)
@@ -14,7 +14,6 @@ import (
        "os/exec"
        "path/filepath"
        "regexp"
-       "runtime"
        "strconv"
        "strings"
 )
@@ -276,8 +275,8 @@ func isGoGenerate(buf []byte) bool {
 // single go:generate command.
 func (g *Generator) setEnv() {
        g.env = []string{
-               "GOARCH=" + runtime.GOARCH,
-               "GOOS=" + runtime.GOOS,
+               "GOARCH=" + buildContext.GOARCH,
+               "GOOS=" + buildContext.GOOS,
                "GOFILE=" + g.file,
                "GOLINE=" + strconv.Itoa(g.lineNum),
                "GOPACKAGE=" + g.pkg,
index a6c70d97b6f7cce3fed4e09b0f1397647fd199a7..0529d7fb311e53a92897237748174f5d055079e0 100644 (file)
@@ -2920,3 +2920,27 @@ func TestAlwaysLinkSysoFiles(t *testing.T) {
        tg.run("list", "-f", "{{.SysoFiles}}", "syso")
        tg.grepStdout("a.syso", "missing syso file with CGO_ENABLED=0")
 }
+
+// Issue 16120.
+func TestGenerateUsesBuildContext(t *testing.T) {
+       if runtime.GOOS == "windows" {
+               t.Skip("this test won't run under Windows")
+       }
+
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.parallel()
+       tg.tempDir("src/gen")
+       tg.tempFile("src/gen/gen.go", "package gen\n//go:generate echo $GOOS $GOARCH\n")
+       tg.setenv("GOPATH", tg.path("."))
+
+       tg.setenv("GOOS", "linux")
+       tg.setenv("GOARCH", "amd64")
+       tg.run("generate", "gen")
+       tg.grepStdout("linux amd64", "unexpected GOOS/GOARCH combination")
+
+       tg.setenv("GOOS", "darwin")
+       tg.setenv("GOARCH", "386")
+       tg.run("generate", "gen")
+       tg.grepStdout("darwin 386", "unexpected GOOS/GOARCH combination")
+}