]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add GOOS and GOARCH to generate
authorRob Pike <r@golang.org>
Mon, 25 Aug 2014 18:35:55 +0000 (11:35 -0700)
committerRob Pike <r@golang.org>
Mon, 25 Aug 2014 18:35:55 +0000 (11:35 -0700)
Fixes test failure in build, probably a good idea anyway.

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/131210043

src/cmd/go/generate.go
src/cmd/go/generate_test.go

index 34b10314d2dbddce2ac3bb603d11f536d2767048..5859e9eefd9bfd6147b24b8fb5cd377f41d22a81 100644 (file)
@@ -13,6 +13,7 @@ import (
        "os"
        "os/exec"
        "path/filepath"
+       "runtime"
        "strconv"
        "strings"
        "unicode"
@@ -48,6 +49,10 @@ quoted string appears a single argument to the generator.
 
 Go generate sets several variables when it runs the generator:
 
+       $GOARCH
+               The execution architecture (arm, amd64, etc.)
+       $GOOS
+               The execution operating system (linux, windows, etc.)
        $GOFILE
                The base name of the file.
        $GOPACKAGE
@@ -287,6 +292,10 @@ func (g *Generator) expandEnv(word string) string {
                envVar := word[i+1 : i+w]
                var sub string
                switch envVar {
+               case "GOARCH":
+                       sub = runtime.GOARCH
+               case "GOOS":
+                       sub = runtime.GOOS
                case "GOFILE":
                        sub = g.file
                case "GOPACKAGE":
@@ -332,7 +341,13 @@ func (g *Generator) exec(words []string) {
        cmd.Stderr = os.Stderr
        // Run the command in the package directory.
        cmd.Dir = g.dir
-       cmd.Env = mergeEnvLists([]string{"GOFILE=" + g.file, "GOPACKAGE=" + g.pkg}, os.Environ())
+       env := []string{
+               "GOARCH=" + runtime.GOARCH,
+               "GOOS=" + runtime.GOOS,
+               "GOFILE=" + g.file,
+               "GOPACKAGE=" + g.pkg,
+       }
+       cmd.Env = mergeEnvLists(env, os.Environ())
        err := cmd.Run()
        if err != nil {
                g.errorf("running %q: %s", words[0], err)
index 6be8157636303b09dab0f1eba5feedd500e0a9f1..881a8fe9b60b822166721a5b55c9e69024afd5cb 100644 (file)
@@ -20,7 +20,10 @@ var splitTests = []splitTest{
        {"x", []string{"x"}},
        {" a b\tc ", []string{"a", "b", "c"}},
        {` " a " `, []string{" a "}},
+       {"$GOARCH", []string{runtime.GOARCH}},
+       {"$GOOS", []string{runtime.GOOS}},
        {"$GOFILE", []string{"proc.go"}},
+       {"$GOPACKAGE", []string{"sys"}},
        {"a $XXNOTDEFINEDXX b", []string{"a", "", "b"}},
        {"/$XXNOTDEFINED/", []string{"//"}},
        {"$GOARCH", []string{runtime.GOARCH}},