| 1 |
// Copyright 2021 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package buildcfg |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"reflect" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"internal/goexperiment" |
| 13 |
) |
| 14 |
|
| 15 |
// Experiment contains the toolchain experiments enabled for the |
| 16 |
// current build. |
| 17 |
// |
| 18 |
// (This is not necessarily the set of experiments the compiler itself |
| 19 |
// was built with.) |
| 20 |
// |
| 21 |
// experimentBaseline specifies the experiment flags that are enabled by |
| 22 |
// default in the current toolchain. This is, in effect, the "control" |
| 23 |
// configuration and any variation from this is an experiment. |
| 24 |
var Experiment, experimentBaseline = func() (goexperiment.Flags, goexperiment.Flags) { |
| 25 |
flags, baseline, err := ParseGOEXPERIMENT(GOOS, GOARCH, envOr("GOEXPERIMENT", defaultGOEXPERIMENT)) |
| 26 |
if err != nil { |
| 27 |
Error = err |
| 28 |
} |
| 29 |
return flags, baseline |
| 30 |
}() |
| 31 |
|
| 32 |
const DefaultGOEXPERIMENT = defaultGOEXPERIMENT |
| 33 |
|
| 34 |
// FramePointerEnabled enables the use of platform conventions for |
| 35 |
// saving frame pointers. |
| 36 |
// |
| 37 |
// This used to be an experiment, but now it's always enabled on |
| 38 |
// platforms that support it. |
| 39 |
// |
| 40 |
// Note: must agree with runtime.framepointer_enabled. |
| 41 |
var FramePointerEnabled = GOARCH == "amd64" || GOARCH == "arm64" |
| 42 |
|
| 43 |
// ParseGOEXPERIMENT parses a (GOOS, GOARCH, GOEXPERIMENT) |
| 44 |
// configuration tuple and returns the enabled and baseline experiment |
| 45 |
// flag sets. |
| 46 |
// |
| 47 |
// TODO(mdempsky): Move to internal/goexperiment. |
| 48 |
func ParseGOEXPERIMENT(goos, goarch, goexp string) (flags, baseline goexperiment.Flags, err error) { |
| 49 |
regabiSupported := false |
| 50 |
|
| 51 |
baseline = goexperiment.Flags{ |
| 52 |
RegabiWrappers: regabiSupported, |
| 53 |
RegabiReflect: regabiSupported, |
| 54 |
RegabiArgs: regabiSupported, |
| 55 |
PacerRedesign: true, |
| 56 |
} |
| 57 |
|
| 58 |
// Start with the statically enabled set of experiments. |
| 59 |
flags = baseline |
| 60 |
|
| 61 |
// Pick up any changes to the baseline configuration from the |
| 62 |
// GOEXPERIMENT environment. This can be set at make.bash time |
| 63 |
// and overridden at build time. |
| 64 |
if goexp != "" { |
| 65 |
// Create a map of known experiment names. |
| 66 |
names := make(map[string]func(bool)) |
| 67 |
rv := reflect.ValueOf(&flags).Elem() |
| 68 |
rt := rv.Type() |
| 69 |
for i := 0; i < rt.NumField(); i++ { |
| 70 |
field := rv.Field(i) |
| 71 |
names[strings.ToLower(rt.Field(i).Name)] = field.SetBool |
| 72 |
} |
| 73 |
|
| 74 |
// "regabi" is an alias for all working regabi |
| 75 |
// subexperiments, and not an experiment itself. Doing |
| 76 |
// this as an alias make both "regabi" and "noregabi" |
| 77 |
// do the right thing. |
| 78 |
names["regabi"] = func(v bool) { |
| 79 |
flags.RegabiWrappers = v |
| 80 |
flags.RegabiReflect = v |
| 81 |
flags.RegabiArgs = v |
| 82 |
} |
| 83 |
|
| 84 |
// Parse names. |
| 85 |
for _, f := range strings.Split(goexp, ",") { |
| 86 |
if f == "" { |
| 87 |
continue |
| 88 |
} |
| 89 |
if f == "none" { |
| 90 |
// GOEXPERIMENT=none disables all experiment flags. |
| 91 |
// This is used by cmd/dist, which doesn't know how |
| 92 |
// to build with any experiment flags. |
| 93 |
flags = goexperiment.Flags{} |
| 94 |
continue |
| 95 |
} |
| 96 |
val := true |
| 97 |
if strings.HasPrefix(f, "no") { |
| 98 |
f, val = f[2:], false |
| 99 |
} |
| 100 |
set, ok := names[f] |
| 101 |
if !ok { |
| 102 |
err = fmt.Errorf("unknown GOEXPERIMENT %s", f) |
| 103 |
return |
| 104 |
} |
| 105 |
set(val) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// regabi is always enabled on amd64. |
| 110 |
if goarch == "amd64" { |
| 111 |
flags.RegabiWrappers = true |
| 112 |
flags.RegabiReflect = true |
| 113 |
flags.RegabiArgs = true |
| 114 |
} |
| 115 |
// regabi is only supported on amd64, arm64, ppc64 and ppc64le. |
| 116 |
if !regabiSupported { |
| 117 |
flags.RegabiReflect = false |
| 118 |
flags.RegabiArgs = false |
| 119 |
} |
| 120 |
// Check regabi dependencies. |
| 121 |
if flags.RegabiArgs && !(flags.RegabiWrappers && flags.RegabiReflect) { |
| 122 |
err = fmt.Errorf("GOEXPERIMENT regabiargs requires regabiwrappers,regabireflect") |
| 123 |
} |
| 124 |
return |
| 125 |
} |
| 126 |
|
| 127 |
// expList returns the list of lower-cased experiment names for |
| 128 |
// experiments that differ from base. base may be nil to indicate no |
| 129 |
// experiments. If all is true, then include all experiment flags, |
| 130 |
// regardless of base. |
| 131 |
func expList(exp, base *goexperiment.Flags, all bool) []string { |
| 132 |
var list []string |
| 133 |
rv := reflect.ValueOf(exp).Elem() |
| 134 |
var rBase reflect.Value |
| 135 |
if base != nil { |
| 136 |
rBase = reflect.ValueOf(base).Elem() |
| 137 |
} |
| 138 |
rt := rv.Type() |
| 139 |
for i := 0; i < rt.NumField(); i++ { |
| 140 |
name := strings.ToLower(rt.Field(i).Name) |
| 141 |
val := rv.Field(i).Bool() |
| 142 |
baseVal := false |
| 143 |
if base != nil { |
| 144 |
baseVal = rBase.Field(i).Bool() |
| 145 |
} |
| 146 |
if all || val != baseVal { |
| 147 |
if val { |
| 148 |
list = append(list, name) |
| 149 |
} else { |
| 150 |
list = append(list, "no"+name) |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
return list |
| 155 |
} |
| 156 |
|
| 157 |
// GOEXPERIMENT is a comma-separated list of enabled or disabled |
| 158 |
// experiments that differ from the baseline experiment configuration. |
| 159 |
// GOEXPERIMENT is exactly what a user would set on the command line |
| 160 |
// to get the set of enabled experiments. |
| 161 |
func GOEXPERIMENT() string { |
| 162 |
goexp := strings.Join(expList(&Experiment, &experimentBaseline, false), ",") |
| 163 |
if goexp == "" && DefaultGOEXPERIMENT != "" { |
| 164 |
goexp = "," // non-empty to override DefaultGOEXPERIMENT |
| 165 |
} |
| 166 |
return goexp |
| 167 |
} |
| 168 |
|
| 169 |
// EnabledExperiments returns a list of enabled experiments, as |
| 170 |
// lower-cased experiment names. |
| 171 |
func EnabledExperiments() []string { |
| 172 |
return expList(&Experiment, nil, false) |
| 173 |
} |
| 174 |
|
| 175 |
// AllExperiments returns a list of all experiment settings. |
| 176 |
// Disabled experiments appear in the list prefixed by "no". |
| 177 |
func AllExperiments() []string { |
| 178 |
return expList(&Experiment, nil, true) |
| 179 |
} |
| 180 |
|
| 181 |
// UpdateExperiments updates the Experiment global based on a new GOARCH value. |
| 182 |
// This is only required for cmd/go, which can change GOARCH after |
| 183 |
// program startup due to use of "go env -w". |
| 184 |
func UpdateExperiments(goos, goarch, goexperiment string) { |
| 185 |
var err error |
| 186 |
Experiment, experimentBaseline, err = ParseGOEXPERIMENT(goos, goarch, goexperiment) |
| 187 |
if err != nil { |
| 188 |
Error = err |
| 189 |
} |
| 190 |
} |