github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/internal/buildcfg/cfg.go (about)

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