1
0
mirror of https://github.com/golang/go synced 2024-11-11 20:01:37 -07:00

internal/buildcfg: refactor GOEXPERIMENT parsing code somewhat

This CL extracts out a ParseGOEXPERIMENT helper function that parses
GOOS/GOARCH/GOEXPERIMENT values and returns active and baseline
experiment flag sets and an error value, without affecting any global
state. This will be used in the subsequent CL for 'go env' support for
GOEXPERIMENT to validate configuration changes.

The existing package initialization for Experiment and
experimentBaseline and also UpdateExperiments are updated to use it as
well.

Change-Id: Ic2ed3fd36d2a6f7f3d8172fccb865e02505c0052
Reviewed-on: https://go-review.googlesource.com/c/go/+/331109
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Matthew Dempsky 2021-06-25 13:24:10 -07:00
parent ed01ceaf48
commit c95464f0ea
2 changed files with 41 additions and 26 deletions

View File

@ -79,7 +79,7 @@ func defaultContext() build.Context {
// The experiments flags are based on GOARCH, so they may // The experiments flags are based on GOARCH, so they may
// need to change. TODO: This should be cleaned up. // need to change. TODO: This should be cleaned up.
buildcfg.UpdateExperiments(ctxt.GOARCH) buildcfg.UpdateExperiments(ctxt.GOOS, ctxt.GOARCH, envOr("GOEXPERIMENT", buildcfg.DefaultGOEXPERIMENT))
ctxt.ToolTags = nil ctxt.ToolTags = nil
for _, exp := range buildcfg.EnabledExperiments() { for _, exp := range buildcfg.EnabledExperiments() {
ctxt.ToolTags = append(ctxt.ToolTags, "goexperiment."+exp) ctxt.ToolTags = append(ctxt.ToolTags, "goexperiment."+exp)

View File

@ -6,7 +6,6 @@ package buildcfg
import ( import (
"fmt" "fmt"
"os"
"reflect" "reflect"
"strings" "strings"
@ -18,20 +17,19 @@ import (
// //
// (This is not necessarily the set of experiments the compiler itself // (This is not necessarily the set of experiments the compiler itself
// was built with.) // was built with.)
var Experiment goexperiment.Flags = parseExperiments(GOARCH) //
var regabiSupported = GOARCH == "amd64" && (GOOS == "android" || GOOS == "linux" || GOOS == "darwin" || GOOS == "windows")
// experimentBaseline specifies the experiment flags that are enabled by // experimentBaseline specifies the experiment flags that are enabled by
// default in the current toolchain. This is, in effect, the "control" // default in the current toolchain. This is, in effect, the "control"
// configuration and any variation from this is an experiment. // configuration and any variation from this is an experiment.
var experimentBaseline = goexperiment.Flags{ var Experiment, experimentBaseline = func() (goexperiment.Flags, goexperiment.Flags) {
RegabiWrappers: regabiSupported, flags, baseline, err := ParseGOEXPERIMENT(GOOS, GOARCH, envOr("GOEXPERIMENT", defaultGOEXPERIMENT))
RegabiG: regabiSupported, if err != nil {
RegabiReflect: regabiSupported, Error = err
RegabiDefer: regabiSupported, }
RegabiArgs: regabiSupported, return flags, baseline
} }()
const DefaultGOEXPERIMENT = defaultGOEXPERIMENT
// FramePointerEnabled enables the use of platform conventions for // FramePointerEnabled enables the use of platform conventions for
// saving frame pointers. // saving frame pointers.
@ -42,16 +40,29 @@ var experimentBaseline = goexperiment.Flags{
// Note: must agree with runtime.framepointer_enabled. // Note: must agree with runtime.framepointer_enabled.
var FramePointerEnabled = GOARCH == "amd64" || GOARCH == "arm64" var FramePointerEnabled = GOARCH == "amd64" || GOARCH == "arm64"
func parseExperiments(goarch string) goexperiment.Flags { // ParseGOEXPERIMENT parses a (GOOS, GOARCH, GOEXPERIMENT)
// configuration tuple and returns the enabled and baseline experiment
// flag sets.
//
// TODO(mdempsky): Move to internal/goexperiment.
func ParseGOEXPERIMENT(goos, goarch, goexp string) (flags, baseline goexperiment.Flags, err error) {
regabiSupported := goarch == "amd64" && (goos == "android" || goos == "linux" || goos == "darwin" || goos == "windows")
baseline = goexperiment.Flags{
RegabiWrappers: regabiSupported,
RegabiG: regabiSupported,
RegabiReflect: regabiSupported,
RegabiDefer: regabiSupported,
RegabiArgs: regabiSupported,
}
// Start with the statically enabled set of experiments. // Start with the statically enabled set of experiments.
flags := experimentBaseline flags = baseline
// Pick up any changes to the baseline configuration from the // Pick up any changes to the baseline configuration from the
// GOEXPERIMENT environment. This can be set at make.bash time // GOEXPERIMENT environment. This can be set at make.bash time
// and overridden at build time. // and overridden at build time.
env := envOr("GOEXPERIMENT", defaultGOEXPERIMENT) if goexp != "" {
if env != "" {
// Create a map of known experiment names. // Create a map of known experiment names.
names := make(map[string]func(bool)) names := make(map[string]func(bool))
rv := reflect.ValueOf(&flags).Elem() rv := reflect.ValueOf(&flags).Elem()
@ -74,7 +85,7 @@ func parseExperiments(goarch string) goexperiment.Flags {
} }
// Parse names. // Parse names.
for _, f := range strings.Split(env, ",") { for _, f := range strings.Split(goexp, ",") {
if f == "" { if f == "" {
continue continue
} }
@ -91,8 +102,8 @@ func parseExperiments(goarch string) goexperiment.Flags {
} }
set, ok := names[f] set, ok := names[f]
if !ok { if !ok {
fmt.Printf("unknown experiment %s\n", f) err = fmt.Errorf("unknown GOEXPERIMENT %s", f)
os.Exit(2) return
} }
set(val) set(val)
} }
@ -108,12 +119,12 @@ func parseExperiments(goarch string) goexperiment.Flags {
} }
// Check regabi dependencies. // Check regabi dependencies.
if flags.RegabiG && !flags.RegabiWrappers { if flags.RegabiG && !flags.RegabiWrappers {
Error = fmt.Errorf("GOEXPERIMENT regabig requires regabiwrappers") err = fmt.Errorf("GOEXPERIMENT regabig requires regabiwrappers")
} }
if flags.RegabiArgs && !(flags.RegabiWrappers && flags.RegabiG && flags.RegabiReflect && flags.RegabiDefer) { if flags.RegabiArgs && !(flags.RegabiWrappers && flags.RegabiG && flags.RegabiReflect && flags.RegabiDefer) {
Error = fmt.Errorf("GOEXPERIMENT regabiargs requires regabiwrappers,regabig,regabireflect,regabidefer") err = fmt.Errorf("GOEXPERIMENT regabiargs requires regabiwrappers,regabig,regabireflect,regabidefer")
} }
return flags return
} }
// expList returns the list of lower-cased experiment names for // expList returns the list of lower-cased experiment names for
@ -169,6 +180,10 @@ func AllExperiments() []string {
// UpdateExperiments updates the Experiment global based on a new GOARCH value. // UpdateExperiments updates the Experiment global based on a new GOARCH value.
// This is only required for cmd/go, which can change GOARCH after // This is only required for cmd/go, which can change GOARCH after
// program startup due to use of "go env -w". // program startup due to use of "go env -w".
func UpdateExperiments(goarch string) { func UpdateExperiments(goos, goarch, goexperiment string) {
Experiment = parseExperiments(goarch) var err error
Experiment, experimentBaseline, err = ParseGOEXPERIMENT(goos, goarch, goexperiment)
if err != nil {
Error = err
}
} }