Skip to content

Commit

Permalink
cmd/go: add GOAMD64 environment variable
Browse files Browse the repository at this point in the history
The variable represents the microarchitecture levels for which to compile.
Valid values are v1 (default), v2, v3, v4.

Updates golang#45453
  • Loading branch information
nimelehin committed Sep 13, 2021
1 parent ad97d20 commit 6b6e325
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/cmd/dist/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
goos string
goarm string
go386 string
goamd64 string
gomips string
gomips64 string
goppc64 string
Expand Down Expand Up @@ -145,6 +146,12 @@ func xinit() {
}
go386 = b

b = os.Getenv("GOAMD64")
if b == "" {
b = "v1"
}
goamd64 = b

b = os.Getenv("GOMIPS")
if b == "" {
b = "hardfloat"
Expand Down Expand Up @@ -217,6 +224,7 @@ func xinit() {

// For tools being invoked but also for os.ExpandEnv.
os.Setenv("GO386", go386)
os.Setenv("GOAMD64", goamd64)
os.Setenv("GOARCH", goarch)
os.Setenv("GOARM", goarm)
os.Setenv("GOHOSTARCH", gohostarch)
Expand Down Expand Up @@ -1181,6 +1189,9 @@ func cmdenv() {
if goarch == "386" {
xprintf(format, "GO386", go386)
}
if goarch == "amd64" {
xprintf(format, "GOAMD64", goamd64)
}
if goarch == "mips" || goarch == "mipsle" {
xprintf(format, "GOMIPS", gomips)
}
Expand Down
1 change: 1 addition & 0 deletions src/cmd/dist/buildruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func mkbuildcfg(file string) {
fmt.Fprintf(&buf, "import \"runtime\"\n")
fmt.Fprintln(&buf)
fmt.Fprintf(&buf, "const defaultGO386 = `%s`\n", go386)
fmt.Fprintf(&buf, "const defaultGOAMD64 = `%s`\n", goamd64)
fmt.Fprintf(&buf, "const defaultGOARM = `%s`\n", goarm)
fmt.Fprintf(&buf, "const defaultGOMIPS = `%s`\n", gomips)
fmt.Fprintf(&buf, "const defaultGOMIPS64 = `%s`\n", gomips64)
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/go/alldocs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/cmd/go/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ var (
// Used in envcmd.MkEnv and build ID computations.
GOARM = envOr("GOARM", fmt.Sprint(buildcfg.GOARM))
GO386 = envOr("GO386", buildcfg.GO386)
GOAMD64 = envOr("GOAMD64", fmt.Sprintf("%s%d", "v", buildcfg.GOAMD64))
GOMIPS = envOr("GOMIPS", buildcfg.GOMIPS)
GOMIPS64 = envOr("GOMIPS64", buildcfg.GOMIPS64)
GOPPC64 = envOr("GOPPC64", fmt.Sprintf("%s%d", "power", buildcfg.GOPPC64))
Expand All @@ -289,6 +290,8 @@ func GetArchEnv() (key, val string) {
return "GOARM", GOARM
case "386":
return "GO386", GO386
case "amd64":
return "GOAMD64", GOAMD64
case "mips", "mipsle":
return "GOMIPS", GOMIPS
case "mips64", "mips64le":
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/go/internal/help/helpdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,9 @@ Architecture-specific environment variables:
GO386
For GOARCH=386, how to implement floating point instructions.
Valid values are sse2 (default), softfloat.
GOAMD64
For GOARCH=GOAMD64, the microarchitecture levels for which to compile.
Valid values are v1 (default), v2, v3, v4.
GOMIPS
For GOARCH=mips{,le}, whether to use floating point instructions.
Valid values are hardfloat (default), softfloat.
Expand Down
5 changes: 5 additions & 0 deletions src/cmd/go/internal/work/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ func asmArgs(a *Action, p *load.Package) []interface{} {
args = append(args, "-D", "GO386_"+cfg.GO386)
}

if cfg.Goarch == "amd64" {
// Define GOAMD64_value from cfg.GOAMD64.
args = append(args, "-D", "GOAMD64_"+cfg.GOAMD64)
}

if cfg.Goarch == "mips" || cfg.Goarch == "mipsle" {
// Define GOMIPS_value from cfg.GOMIPS.
args = append(args, "-D", "GOMIPS_"+cfg.GOMIPS)
Expand Down
17 changes: 17 additions & 0 deletions src/internal/buildcfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
GOARCH = envOr("GOARCH", defaultGOARCH)
GOOS = envOr("GOOS", defaultGOOS)
GO386 = envOr("GO386", defaultGO386)
GOAMD64 = goamd64()
GOARM = goarm()
GOMIPS = gomips()
GOMIPS64 = gomips64()
Expand Down Expand Up @@ -52,6 +53,22 @@ func envOr(key, value string) string {
return value
}

func goamd64() int {
def := defaultGOAMD64
switch v := envOr("GOAMD64", def); v {
case "v1":
return 1
case "v2":
return 2
case "v3":
return 3
case "v4":
return 4
}
Error = fmt.Errorf("invalid GOAMD64: must be v1, v2, v3 or v4")
return int(def[len("v")] - '0')
}

func goarm() int {
def := defaultGOARM
if GOOS == "android" && GOARCH == "arm" {
Expand Down
1 change: 1 addition & 0 deletions src/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const KnownEnv = `
GCCGO
GO111MODULE
GO386
GOAMD64
GOARCH
GOARM
GOBIN
Expand Down
2 changes: 1 addition & 1 deletion test/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -1753,7 +1753,7 @@ var (
// are the supported variants.
archVariants = map[string][]string{
"386": {"GO386", "sse2", "softfloat"},
"amd64": {},
"amd64": {"GOAMD64", "v1", "v2", "v3", "v4"},
"arm": {"GOARM", "5", "6", "7"},
"arm64": {},
"mips": {"GOMIPS", "hardfloat", "softfloat"},
Expand Down

0 comments on commit 6b6e325

Please sign in to comment.