github.com/Rookout/GoSDK@v0.1.48/pkg/services/assembler/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.assembler 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  	"runtime"
    19  	"strconv"
    20  	"strings"
    21  )
    22  
    23  var (
    24  	GOROOT   = runtime.GOROOT() 
    25  	GOARCH   = envOr("GOARCH", "")
    26  	GOOS     = envOr("GOOS", "linux")
    27  	GO386    = envOr("GO386", "")
    28  	GOAMD64  = goamd64()
    29  	GOARM    = goarm()
    30  	GOMIPS   = gomips()
    31  	GOMIPS64 = gomips64()
    32  	GOPPC64  = goppc64()
    33  	GOWASM   = gowasm()
    34  	ToolTags = toolTags()
    35  	GO_LDSO  = ""
    36  	Version  = ""
    37  )
    38  
    39  
    40  var Error error
    41  
    42  
    43  func Check() {
    44  	if Error != nil {
    45  		fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), Error)
    46  		os.Exit(2)
    47  	}
    48  }
    49  
    50  func envOr(key, value string) string {
    51  	if x := os.Getenv(key); x != "" {
    52  		return x
    53  	}
    54  	return value
    55  }
    56  
    57  func goamd64() int {
    58  	switch v := envOr("GOAMD64", "v4"); v {
    59  	case "v1":
    60  		return 1
    61  	case "v2":
    62  		return 2
    63  	case "v3":
    64  		return 3
    65  	case "v4":
    66  		return 4
    67  	}
    68  	Error = fmt.Errorf("invalid GOAMD64: must be v1, v2, v3, v4")
    69  	return int("v4"[len("v")] - '0')
    70  }
    71  
    72  func goarm() int {
    73  	def := "7"
    74  	if GOOS == "android" && GOARCH == "arm" {
    75  		
    76  		def = "7"
    77  	}
    78  	switch v := envOr("GOARM", def); v {
    79  	case "5":
    80  		return 5
    81  	case "6":
    82  		return 6
    83  	case "7":
    84  		return 7
    85  	}
    86  	Error = fmt.Errorf("invalid GOARM: must be 5, 6, 7")
    87  	return int(def[0] - '0')
    88  }
    89  
    90  func gomips() string {
    91  	switch v := envOr("GOMIPS", "hardfloat"); v {
    92  	case "hardfloat", "softfloat":
    93  		return v
    94  	}
    95  	Error = fmt.Errorf("invalid GOMIPS: must be hardfloat, softfloat")
    96  	return "hardfloat"
    97  }
    98  
    99  func gomips64() string {
   100  	switch v := envOr("GOMIPS64", "hardfloat"); v {
   101  	case "hardfloat", "softfloat":
   102  		return v
   103  	}
   104  	Error = fmt.Errorf("invalid GOMIPS64: must be hardfloat, softfloat")
   105  	return "hardfloat"
   106  }
   107  
   108  func goppc64() int {
   109  	switch v := envOr("GOPPC64", "power8"); v {
   110  	case "power8":
   111  		return 8
   112  	case "power9":
   113  		return 9
   114  	case "power10":
   115  		return 10
   116  	}
   117  	Error = fmt.Errorf("invalid GOPPC64: must be power8, power9, power10")
   118  	return int("power8"[len("power")] - '0')
   119  }
   120  
   121  type gowasmFeatures struct {
   122  	SatConv bool
   123  	SignExt bool
   124  }
   125  
   126  func (f gowasmFeatures) String() string {
   127  	var flags []string
   128  	if f.SatConv {
   129  		flags = append(flags, "satconv")
   130  	}
   131  	if f.SignExt {
   132  		flags = append(flags, "signext")
   133  	}
   134  	return strings.Join(flags, ",")
   135  }
   136  
   137  func gowasm() (f gowasmFeatures) {
   138  	for _, opt := range strings.Split(envOr("GOWASM", ""), ",") {
   139  		switch opt {
   140  		case "satconv":
   141  			f.SatConv = true
   142  		case "signext":
   143  			f.SignExt = true
   144  		case "":
   145  			
   146  		default:
   147  			Error = fmt.Errorf("invalid GOWASM: no such feature %q", opt)
   148  		}
   149  	}
   150  	return
   151  }
   152  
   153  func Getgoextlinkenabled() string {
   154  	return envOr("GO_EXTLINK_ENABLED", "")
   155  }
   156  
   157  func toolTags() []string {
   158  	tags := experimentTags()
   159  	tags = append(tags, gogoarchTags()...)
   160  	return tags
   161  }
   162  
   163  func experimentTags() []string {
   164  	var list []string
   165  	
   166  	
   167  	
   168  	
   169  	
   170  	for _, exp := range Experiment.Enabled() {
   171  		list = append(list, "goexperiment."+exp)
   172  	}
   173  	return list
   174  }
   175  
   176  
   177  
   178  func GOGOARCH() (name, value string) {
   179  	switch GOARCH {
   180  	case "386":
   181  		return "GO386", GO386
   182  	case "amd64":
   183  		return "GOAMD64", fmt.Sprintf("v%d", GOAMD64)
   184  	case "arm":
   185  		return "GOARM", strconv.Itoa(GOARM)
   186  	case "mips", "mipsle":
   187  		return "GOMIPS", GOMIPS
   188  	case "mips64", "mips64le":
   189  		return "GOMIPS64", GOMIPS64
   190  	case "ppc64", "ppc64le":
   191  		return "GOPPC64", fmt.Sprintf("power%d", GOPPC64)
   192  	case "wasm":
   193  		return "GOWASM", GOWASM.String()
   194  	}
   195  	return "", ""
   196  }
   197  
   198  func gogoarchTags() []string {
   199  	switch GOARCH {
   200  	case "386":
   201  		return []string{GOARCH + "." + GO386}
   202  	case "amd64":
   203  		var list []string
   204  		for i := 1; i <= GOAMD64; i++ {
   205  			list = append(list, fmt.Sprintf("%s.v%d", GOARCH, i))
   206  		}
   207  		return list
   208  	case "arm":
   209  		var list []string
   210  		for i := 5; i <= GOARM; i++ {
   211  			list = append(list, fmt.Sprintf("%s.%d", GOARCH, i))
   212  		}
   213  		return list
   214  	case "mips", "mipsle":
   215  		return []string{GOARCH + "." + GOMIPS}
   216  	case "mips64", "mips64le":
   217  		return []string{GOARCH + "." + GOMIPS64}
   218  	case "ppc64", "ppc64le":
   219  		var list []string
   220  		for i := 8; i <= GOPPC64; i++ {
   221  			list = append(list, fmt.Sprintf("%s.power%d", GOARCH, i))
   222  		}
   223  		return list
   224  	case "wasm":
   225  		var list []string
   226  		if GOWASM.SatConv {
   227  			list = append(list, GOARCH+".satconv")
   228  		}
   229  		if GOWASM.SignExt {
   230  			list = append(list, GOARCH+".signext")
   231  		}
   232  		return list
   233  	}
   234  	return nil
   235  }