Commit 109823ec authored by Andrew Gerrand's avatar Andrew Gerrand

cmd/go: for generate, use build context values for GOOS/GOARCH

Fixes #16120

Change-Id: Ia352558231e00baab5c698e93d7267564c07ec0c
Reviewed-on: https://go-review.googlesource.com/24242Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Andrew Gerrand <adg@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent e1a6e71e
...@@ -14,7 +14,6 @@ import ( ...@@ -14,7 +14,6 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime"
"strconv" "strconv"
"strings" "strings"
) )
...@@ -276,8 +275,8 @@ func isGoGenerate(buf []byte) bool { ...@@ -276,8 +275,8 @@ func isGoGenerate(buf []byte) bool {
// single go:generate command. // single go:generate command.
func (g *Generator) setEnv() { func (g *Generator) setEnv() {
g.env = []string{ g.env = []string{
"GOARCH=" + runtime.GOARCH, "GOARCH=" + buildContext.GOARCH,
"GOOS=" + runtime.GOOS, "GOOS=" + buildContext.GOOS,
"GOFILE=" + g.file, "GOFILE=" + g.file,
"GOLINE=" + strconv.Itoa(g.lineNum), "GOLINE=" + strconv.Itoa(g.lineNum),
"GOPACKAGE=" + g.pkg, "GOPACKAGE=" + g.pkg,
......
...@@ -2920,3 +2920,27 @@ func TestAlwaysLinkSysoFiles(t *testing.T) { ...@@ -2920,3 +2920,27 @@ func TestAlwaysLinkSysoFiles(t *testing.T) {
tg.run("list", "-f", "{{.SysoFiles}}", "syso") tg.run("list", "-f", "{{.SysoFiles}}", "syso")
tg.grepStdout("a.syso", "missing syso file with CGO_ENABLED=0") 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")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment