Skip to content

Commit

Permalink
go/build: add GO$GOARCH-based ToolTags
Browse files Browse the repository at this point in the history
Implement proposal golang#45454, providing build tags based on the
sub-architecture information in the GO$GOARCH variable
(for example, GOARM for GOARCH=arm).

For example, when GOAMD64=v2, the additional build tags
amd64.v1 and amd64.v2 are defined to be true.

Fixes golang#45454.

Change-Id: I7be56060d47fc61843b97fd8a78498e8202c1ee7
Reviewed-on: https://go-review.googlesource.com/c/go/+/421434
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
Reviewed-by: Cuong Manh Le <[email protected]>
Auto-Submit: Russ Cox <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
  • Loading branch information
rsc authored and jproberts committed Aug 10, 2022
1 parent 03a632b commit e9c869e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 12 deletions.
11 changes: 8 additions & 3 deletions src/cmd/go/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ func defaultContext() build.Context {
ctxt.GOOS = Goos
ctxt.GOARCH = Goarch

// ToolTags are based on GOEXPERIMENT, which we will parse and
// initialize later.
ctxt.ToolTags = nil
// Clear the GOEXPERIMENT-based tool tags, which we will recompute later.
var save []string
for _, tag := range ctxt.ToolTags {
if !strings.HasPrefix(tag, "goexperiment.") {
save = append(save, tag)
}
}
ctxt.ToolTags = save

// The go/build rule for whether cgo is enabled is:
// 1. If $CGO_ENABLED is set, respect it.
Expand Down
45 changes: 45 additions & 0 deletions src/cmd/go/testdata/script/tooltags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
env GOARCH=amd64
env GOAMD64=v3
go list -f '{{context.ToolTags}}'
stdout 'amd64.v1 amd64.v2 amd64.v3'

env GOARCH=arm
env GOARM=6
go list -f '{{context.ToolTags}}'
stdout 'arm.5 arm.6'

env GOARCH=mips
env GOMIPS=hardfloat
go list -f '{{context.ToolTags}}'
stdout 'mips.hardfloat'

env GOARCH=mips64
env GOMIPS=hardfloat
go list -f '{{context.ToolTags}}'
stdout 'mips64.hardfloat'

env GOARCH=ppc64
env GOPPC64=power9
go list -f '{{context.ToolTags}}'
stdout 'ppc64.power8 ppc64.power9'

env GOARCH=ppc64le
env GOPPC64=power9
go list -f '{{context.ToolTags}}'
stdout 'ppc64le.power8 ppc64le.power9'

env GOARCH=386
env GO386=sse2
go list -f '{{context.ToolTags}}'
stdout '386.sse2'

env GOARCH=wasm
env GOWASM=satconv
go list -f '{{context.ToolTags}}'
stdout 'wasm.satconv'

-- go.mod --
module m

-- p.go --
package p
9 changes: 1 addition & 8 deletions src/go/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,8 @@ func defaultContext() Context {
}
c.GOPATH = envOr("GOPATH", defaultGOPATH())
c.Compiler = runtime.Compiler
c.ToolTags = append(c.ToolTags, buildcfg.ToolTags...)

// For each experiment that has been enabled in the toolchain, define a
// build tag with the same name but prefixed by "goexperiment." which can be
// used for compiling alternative files for the experiment. This allows
// changes for the experiment, like extra struct fields in the runtime,
// without affecting the base non-experiment code at all.
for _, exp := range buildcfg.Experiment.Enabled() {
c.ToolTags = append(c.ToolTags, "goexperiment."+exp)
}
defaultToolTags = append([]string{}, c.ToolTags...) // our own private copy

// Each major Go release in the Go 1.x series adds a new
Expand Down
61 changes: 60 additions & 1 deletion src/internal/buildcfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
GOMIPS64 = gomips64()
GOPPC64 = goppc64()
GOWASM = gowasm()
ToolTags = toolTags()
GO_LDSO = defaultGO_LDSO
Version = version
)
Expand Down Expand Up @@ -115,8 +116,8 @@ func goppc64() int {
}

type gowasmFeatures struct {
SignExt bool
SatConv bool
SignExt bool
}

func (f gowasmFeatures) String() string {
Expand Down Expand Up @@ -149,3 +150,61 @@ func gowasm() (f gowasmFeatures) {
func Getgoextlinkenabled() string {
return envOr("GO_EXTLINK_ENABLED", defaultGO_EXTLINK_ENABLED)
}

func toolTags() []string {
tags := experimentTags()
tags = append(tags, gogoarchTags()...)
return tags
}

func experimentTags() []string {
var list []string
// For each experiment that has been enabled in the toolchain, define a
// build tag with the same name but prefixed by "goexperiment." which can be
// used for compiling alternative files for the experiment. This allows
// changes for the experiment, like extra struct fields in the runtime,
// without affecting the base non-experiment code at all.
for _, exp := range Experiment.Enabled() {
list = append(list, "goexperiment."+exp)
}
return list
}

func gogoarchTags() []string {
switch GOARCH {
case "386":
return []string{GOARCH + "." + GO386}
case "amd64":
var list []string
for i := 1; i <= GOAMD64; i++ {
list = append(list, fmt.Sprintf("%s.v%d", GOARCH, i))
}
return list
case "arm":
var list []string
for i := 5; i <= GOARM; i++ {
list = append(list, fmt.Sprintf("%s.%d", GOARCH, i))
}
return list
case "mips", "mipsle":
return []string{GOARCH + "." + GOMIPS}
case "mips64", "mips64le":
return []string{GOARCH + "." + GOMIPS64}
case "ppc64", "ppc64le":
var list []string
for i := 8; i <= GOPPC64; i++ {
list = append(list, fmt.Sprintf("%s.power%d", GOARCH, i))
}
return list
case "wasm":
var list []string
if GOWASM.SatConv {
list = append(list, GOARCH+".satconv")
}
if GOWASM.SignExt {
list = append(list, GOARCH+".signext")
}
return list
}
return nil
}

0 comments on commit e9c869e

Please sign in to comment.