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.
5 // Package buildcfg provides access to the build configuration
6 // described by the current environment. It is for use by build tools
7 // such as cmd/go or cmd/compile and for setting up go/build's Default context.
9 // Note that it does NOT provide access to the build configuration used to
10 // build the currently-running binary. For that, use runtime.GOOS etc
11 // as well as internal/goexperiment.
22 defaultGOROOT string // set by linker
24 GOROOT = envOr("GOROOT", defaultGOROOT)
25 GOARCH = envOr("GOARCH", defaultGOARCH)
26 GOOS = envOr("GOOS", defaultGOOS)
27 GO386 = envOr("GO386", defaultGO386)
33 GO_LDSO = defaultGO_LDSO
37 // Error is one of the errors found (if any) in the build configuration.
40 // Check exits the program with a fatal error if Error is non-nil.
43 fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), Error)
48 func envOr(key, value string) string {
49 if x := os.Getenv(key); x != "" {
57 if GOOS == "android" && GOARCH == "arm" {
58 // Android arm devices always support GOARM=7.
61 switch v := envOr("GOARM", def); v {
69 Error = fmt.Errorf("invalid GOARM: must be 5, 6, 7")
70 return int(def[0] - '0')
73 func gomips() string {
74 switch v := envOr("GOMIPS", defaultGOMIPS); v {
75 case "hardfloat", "softfloat":
78 Error = fmt.Errorf("invalid GOMIPS: must be hardfloat, softfloat")
82 func gomips64() string {
83 switch v := envOr("GOMIPS64", defaultGOMIPS64); v {
84 case "hardfloat", "softfloat":
87 Error = fmt.Errorf("invalid GOMIPS64: must be hardfloat, softfloat")
88 return defaultGOMIPS64
92 switch v := envOr("GOPPC64", defaultGOPPC64); v {
98 Error = fmt.Errorf("invalid GOPPC64: must be power8, power9")
99 return int(defaultGOPPC64[len("power")] - '0')
102 type gowasmFeatures struct {
107 func (f gowasmFeatures) String() string {
110 flags = append(flags, "satconv")
113 flags = append(flags, "signext")
115 return strings.Join(flags, ",")
118 func gowasm() (f gowasmFeatures) {
119 for _, opt := range strings.Split(envOr("GOWASM", ""), ",") {
128 Error = fmt.Errorf("invalid GOWASM: no such feature %q", opt)
134 func Getgoextlinkenabled() string {
135 return envOr("GO_EXTLINK_ENABLED", defaultGO_EXTLINK_ENABLED)