]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: add GO$GOARCH-based ToolTags
authorRuss Cox <rsc@golang.org>
Thu, 4 Aug 2022 20:44:29 +0000 (16:44 -0400)
committerGopher Robot <gobot@golang.org>
Mon, 8 Aug 2022 14:39:20 +0000 (14:39 +0000)
Implement proposal #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 #45454.

Change-Id: I7be56060d47fc61843b97fd8a78498e8202c1ee7
Reviewed-on: https://go-review.googlesource.com/c/go/+/421434
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Auto-Submit: Russ Cox <rsc@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/go/internal/cfg/cfg.go
src/cmd/go/testdata/script/tooltags.txt [new file with mode: 0644]
src/go/build/build.go
src/internal/buildcfg/cfg.go

index c6ddfe55d586030303e31edde4ccf2e13644bf7e..84abb276064075b688bf7ad60b8c3cb0034417bf 100644 (file)
@@ -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.
diff --git a/src/cmd/go/testdata/script/tooltags.txt b/src/cmd/go/testdata/script/tooltags.txt
new file mode 100644 (file)
index 0000000..ef7c715
--- /dev/null
@@ -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
index dfb37b8f3435a68f2ec1b2d808c2a1cee4c313b9..b914b67d1d31b878c80eda2389331acf96675e0a 100644 (file)
@@ -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
index 1066d0c18961e88d1e919d4a1e48940fcdd4b5ee..1cc3d3dcd0bca0fd51e051e3d73a9e53c961fbbb 100644 (file)
@@ -30,6 +30,7 @@ var (
        GOMIPS64 = gomips64()
        GOPPC64  = goppc64()
        GOWASM   = gowasm()
+       ToolTags = toolTags()
        GO_LDSO  = defaultGO_LDSO
        Version  = version
 )
@@ -115,8 +116,8 @@ func goppc64() int {
 }
 
 type gowasmFeatures struct {
-       SignExt bool
        SatConv bool
+       SignExt bool
 }
 
 func (f gowasmFeatures) String() string {
@@ -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
+}