Source file src/internal/buildcfg/cfg.go

     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 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.
     8  //
     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.
    12  package buildcfg
    13  
    14  import (
    15  	"fmt"
    16  	"os"
    17  	"path/filepath"
    18  	"strings"
    19  )
    20  
    21  var (
    22  	defaultGOROOT string // set by linker
    23  
    24  	GOROOT   = envOr("GOROOT", defaultGOROOT)
    25  	GOARCH   = envOr("GOARCH", defaultGOARCH)
    26  	GOOS     = envOr("GOOS", defaultGOOS)
    27  	GO386    = envOr("GO386", defaultGO386)
    28  	GOAMD64  = goamd64()
    29  	GOARM    = goarm()
    30  	GOMIPS   = gomips()
    31  	GOMIPS64 = gomips64()
    32  	GOPPC64  = goppc64()
    33  	GOWASM   = gowasm()
    34  	GO_LDSO  = defaultGO_LDSO
    35  	Version  = version
    36  )
    37  
    38  // Error is one of the errors found (if any) in the build configuration.
    39  var Error error
    40  
    41  // Check exits the program with a fatal error if Error is non-nil.
    42  func Check() {
    43  	if Error != nil {
    44  		fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), Error)
    45  		os.Exit(2)
    46  	}
    47  }
    48  
    49  func envOr(key, value string) string {
    50  	if x := os.Getenv(key); x != "" {
    51  		return x
    52  	}
    53  	return value
    54  }
    55  
    56  func goamd64() int {
    57  	switch v := envOr("GOAMD64", defaultGOAMD64); v {
    58  	case "v1":
    59  		return 1
    60  	case "v2":
    61  		return 2
    62  	case "v3":
    63  		return 3
    64  	case "v4":
    65  		return 4
    66  	}
    67  	Error = fmt.Errorf("invalid GOAMD64: must be v1, v2, v3, v4")
    68  	return int(defaultGOAMD64[len("v")] - '0')
    69  }
    70  
    71  func goarm() int {
    72  	def := defaultGOARM
    73  	if GOOS == "android" && GOARCH == "arm" {
    74  		// Android arm devices always support GOARM=7.
    75  		def = "7"
    76  	}
    77  	switch v := envOr("GOARM", def); v {
    78  	case "5":
    79  		return 5
    80  	case "6":
    81  		return 6
    82  	case "7":
    83  		return 7
    84  	}
    85  	Error = fmt.Errorf("invalid GOARM: must be 5, 6, 7")
    86  	return int(def[0] - '0')
    87  }
    88  
    89  func gomips() string {
    90  	switch v := envOr("GOMIPS", defaultGOMIPS); v {
    91  	case "hardfloat", "softfloat":
    92  		return v
    93  	}
    94  	Error = fmt.Errorf("invalid GOMIPS: must be hardfloat, softfloat")
    95  	return defaultGOMIPS
    96  }
    97  
    98  func gomips64() string {
    99  	switch v := envOr("GOMIPS64", defaultGOMIPS64); v {
   100  	case "hardfloat", "softfloat":
   101  		return v
   102  	}
   103  	Error = fmt.Errorf("invalid GOMIPS64: must be hardfloat, softfloat")
   104  	return defaultGOMIPS64
   105  }
   106  
   107  func goppc64() int {
   108  	switch v := envOr("GOPPC64", defaultGOPPC64); v {
   109  	case "power8":
   110  		return 8
   111  	case "power9":
   112  		return 9
   113  	}
   114  	Error = fmt.Errorf("invalid GOPPC64: must be power8, power9")
   115  	return int(defaultGOPPC64[len("power")] - '0')
   116  }
   117  
   118  type gowasmFeatures struct {
   119  	SignExt bool
   120  	SatConv bool
   121  }
   122  
   123  func (f gowasmFeatures) String() string {
   124  	var flags []string
   125  	if f.SatConv {
   126  		flags = append(flags, "satconv")
   127  	}
   128  	if f.SignExt {
   129  		flags = append(flags, "signext")
   130  	}
   131  	return strings.Join(flags, ",")
   132  }
   133  
   134  func gowasm() (f gowasmFeatures) {
   135  	for _, opt := range strings.Split(envOr("GOWASM", ""), ",") {
   136  		switch opt {
   137  		case "satconv":
   138  			f.SatConv = true
   139  		case "signext":
   140  			f.SignExt = true
   141  		case "":
   142  			// ignore
   143  		default:
   144  			Error = fmt.Errorf("invalid GOWASM: no such feature %q", opt)
   145  		}
   146  	}
   147  	return
   148  }
   149  
   150  func Getgoextlinkenabled() string {
   151  	return envOr("GO_EXTLINK_ENABLED", defaultGO_EXTLINK_ENABLED)
   152  }
   153  

View as plain text