]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: define a build tag for any GOEXPERIMENT which is enabled.
authorDan Scales <danscales@google.com>
Wed, 11 Mar 2020 19:08:03 +0000 (12:08 -0700)
committerDan Scales <danscales@google.com>
Mon, 23 Mar 2020 19:24:26 +0000 (19:24 +0000)
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.

I use this capability in my CL for static lock ranking
(https://go-review.googlesource.com/c/go/+/207619), so that static lock ranking
can be fully enabled as a GOEXPERIMENT, but there is no overhead in the runtime
when the experiment is not enabled.

I added a test in cmd/go/testdata/scripts to make sure the build tags are being
defined properly. In order to implement the test, I needed to provide environment
variable GOEXPSTRING to the test scripts (with its value set from
objabi.Expstring(), so that it can determine the experiments baked into the
toolchain.

I filed https://github.com/golang/go/issues/37937 to make a builder with
GOEXPERIMENT set to 'staticlockranking'. This builder will ensure another variant
of GOEXPERIMENT is being tested regularly for this change, as well as checking
static lock ranking in the runtime.

Change-Id: Ieb4b86107238febd105558c1e639d30cfe57ab5c
Reviewed-on: https://go-review.googlesource.com/c/go/+/222925
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/work/init.go
src/cmd/go/script_test.go
src/cmd/go/testdata/script/README
src/cmd/go/testdata/script/build_tag_goexperiment.txt [new file with mode: 0644]

index e970272954e8bf7c2b3849ec1752415e9d7ed891..a574924c5b3a9b4dcfa5b80b9c300322ebf4981d 100644 (file)
@@ -10,6 +10,7 @@ import (
        "cmd/go/internal/base"
        "cmd/go/internal/cfg"
        "cmd/go/internal/load"
+       "cmd/internal/objabi"
        "cmd/internal/sys"
        "flag"
        "fmt"
@@ -34,6 +35,20 @@ func BuildInit() {
                }
                cfg.BuildPkgdir = p
        }
+
+       // 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. [2:] strips the
+       // leading "X:" from objabi.Expstring().
+       exp := objabi.Expstring()[2:]
+       if exp != "none" {
+               experiments := strings.Split(exp, ",")
+               for _, expt := range experiments {
+                       cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, "goexperiment."+expt)
+               }
+       }
 }
 
 func instrumentInit() {
index 3eb66f9166114aab7cf3b356a81153e077439729..87afb6aec8b419d7e3ded88f0418415c9a023a09 100644 (file)
@@ -30,6 +30,7 @@ import (
        "cmd/go/internal/robustio"
        "cmd/go/internal/txtar"
        "cmd/go/internal/work"
+       "cmd/internal/objabi"
        "cmd/internal/sys"
 )
 
@@ -119,6 +120,7 @@ func (ts *testScript) setup() {
                "GOCACHE=" + testGOCACHE,
                "GODEBUG=" + os.Getenv("GODEBUG"),
                "GOEXE=" + cfg.ExeSuffix,
+               "GOEXPSTRING=" + objabi.Expstring()[2:],
                "GOOS=" + runtime.GOOS,
                "GOPATH=" + filepath.Join(ts.workdir, "gopath"),
                "GOPROXY=" + proxyURL,
index 81b6d9d81455c84790dac2cc7c6d75d2ffe0025c..f4c92e65ab8c5efc0a3e2cc675e8413f4597b003 100644 (file)
@@ -29,6 +29,7 @@ Scripts also have access to these other environment variables:
        GOARCH=<target GOARCH>
        GOCACHE=<actual GOCACHE being used outside the test>
        GOEXE=<executable file suffix: .exe on Windows, empty on other systems>
+       GOEXPSTRING=<value of objabi.Expstring(), from GOEXPERIMENT when toolchain built>
        GOOS=<target GOOS>
        GOPATH=$WORK/gopath
        GOPROXY=<local module proxy serving from cmd/go/testdata/mod>
diff --git a/src/cmd/go/testdata/script/build_tag_goexperiment.txt b/src/cmd/go/testdata/script/build_tag_goexperiment.txt
new file mode 100644 (file)
index 0000000..26ad029
--- /dev/null
@@ -0,0 +1,104 @@
+# compile_ext will fail if the buildtags that are enabled (or not enabled) for the
+# framepointer and fieldtrack experiments are not consistent with the value of
+# GOEXPSTRING (which comes from objabi.Expstring()).
+
+[short] skip
+go run m
+
+-- expt_main.go --
+package main
+
+import (
+       "os"
+       "strings"
+)
+
+func main() {
+  fp()
+  ft()
+}
+
+func hasExpEntry(s string) bool {
+       // script_test.go defines GOEXPSTRING to be the value of
+       // objabi.Expstring(), which gives the enabled experiments baked into the
+       // toolchain.
+       g := os.Getenv("GOEXPSTRING")
+       for _, f := range strings.Split(g, ",") {
+               if f == s {
+                       return true
+               }
+       }
+       return false
+}
+
+-- fp_off.go --
+// +build !goexperiment.framepointer
+
+package main
+
+import (
+       "fmt"
+       "os"
+)
+
+func fp() {
+       if hasExpEntry("framepointer") {
+               fmt.Println("in !framepointer build, but objabi.Expstring() has 'framepointer'")
+               os.Exit(1)
+       }
+}
+
+-- fp_on.go --
+// +build goexperiment.framepointer
+
+package main
+
+import (
+       "fmt"
+       "os"
+)
+
+func fp() {
+       if !hasExpEntry("framepointer") {
+               fmt.Println("in framepointer build, but objabi.Expstring() does not have 'framepointer', is", os.Getenv("GOEXPSTRING"))
+               os.Exit(1)
+       }
+}
+
+-- ft_off.go --
+// +build !goexperiment.fieldtrack
+
+package main
+
+import (
+       "fmt"
+       "os"
+)
+
+func ft() {
+       if hasExpEntry("fieldtrack") {
+               fmt.Println("in !fieldtrack build, but objabi.Expstring() has 'fieldtrack'")
+               os.Exit(1)
+       }
+}
+
+-- ft_on.go --
+// +build goexperiment.fieldtrack
+
+package main
+
+import (
+       "fmt"
+       "os"
+)
+
+func ft() {
+       if !hasExpEntry("fieldtrack") {
+               fmt.Println("in fieldtrack build, but objabi.Expstring() does not have 'fieldtrack', is", os.Getenv("GOEXPSTRING"))
+               os.Exit(1)
+       }
+}
+
+-- go.mod --
+module m
+go 1.14