github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/cfg/cfg.go (about) 1 // Copyright 2017 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 cfg holds configuration shared by multiple parts 6 // of the go command. 7 package cfg 8 9 import ( 10 "github.com/shogo82148/std/context" 11 "github.com/shogo82148/std/fmt" 12 "github.com/shogo82148/std/go/build" 13 "github.com/shogo82148/std/internal/buildcfg" 14 "github.com/shogo82148/std/io" 15 "github.com/shogo82148/std/runtime" 16 ) 17 18 // Global build parameters (used during package load) 19 var ( 20 Goos = envOr("GOOS", build.Default.GOOS) 21 Goarch = envOr("GOARCH", build.Default.GOARCH) 22 23 ExeSuffix = exeSuffix() 24 25 // ModulesEnabled specifies whether the go command is running 26 // in module-aware mode (as opposed to GOPATH mode). 27 // It is equal to modload.Enabled, but not all packages can import modload. 28 ModulesEnabled bool 29 ) 30 31 // ToolExeSuffix returns the suffix for executables installed 32 // in build.ToolDir. 33 func ToolExeSuffix() string 34 35 // These are general "build flags" used by build and other commands. 36 var ( 37 BuildA bool 38 BuildBuildmode string 39 BuildBuildvcs = "auto" 40 BuildContext = defaultContext() 41 BuildMod string 42 BuildModExplicit bool 43 BuildModReason string 44 BuildLinkshared bool 45 BuildMSan bool 46 BuildASan bool 47 BuildCover bool 48 BuildCoverMode string 49 BuildCoverPkg []string 50 BuildN bool 51 BuildO string 52 BuildP = runtime.GOMAXPROCS(0) 53 BuildPGO string 54 BuildPkgdir string 55 BuildRace bool 56 BuildToolexec []string 57 BuildToolchainName string 58 BuildTrimpath bool 59 BuildV bool 60 BuildWork bool 61 BuildX bool 62 63 ModCacheRW bool 64 ModFile string 65 66 CmdName string 67 68 DebugActiongraph string 69 DebugTrace string 70 DebugRuntimeTrace string 71 72 // GoPathError is set when GOPATH is not set. it contains an 73 // explanation why GOPATH is unset. 74 GoPathError string 75 ) 76 77 // SetGOROOT sets GOROOT and associated variables to the given values. 78 // 79 // If isTestGo is true, build.ToolDir is set based on the TESTGO_GOHOSTOS and 80 // TESTGO_GOHOSTARCH environment variables instead of runtime.GOOS and 81 // runtime.GOARCH. 82 func SetGOROOT(goroot string, isTestGo bool) 83 84 // Experiment configuration. 85 var ( 86 // RawGOEXPERIMENT is the GOEXPERIMENT value set by the user. 87 RawGOEXPERIMENT = envOr("GOEXPERIMENT", buildcfg.DefaultGOEXPERIMENT) 88 // CleanGOEXPERIMENT is the minimal GOEXPERIMENT value needed to reproduce the 89 // experiments enabled by RawGOEXPERIMENT. 90 CleanGOEXPERIMENT = RawGOEXPERIMENT 91 92 Experiment *buildcfg.ExperimentFlags 93 ExperimentErr error 94 ) 95 96 // An EnvVar is an environment variable Name=Value. 97 type EnvVar struct { 98 Name string 99 Value string 100 } 101 102 // OrigEnv is the original environment of the program at startup. 103 var OrigEnv []string 104 105 // CmdEnv is the new environment for running go tool commands. 106 // User binaries (during go test or go run) are run with OrigEnv, 107 // not CmdEnv. 108 var CmdEnv []EnvVar 109 110 // EnvFile returns the name of the Go environment configuration file. 111 func EnvFile() (string, error) 112 113 // Getenv gets the value for the configuration key. 114 // It consults the operating system environment 115 // and then the go/env file. 116 // If Getenv is called for a key that cannot be set 117 // in the go/env file (for example GODEBUG), it panics. 118 // This ensures that CanGetenv is accurate, so that 119 // 'go env -w' stays in sync with what Getenv can retrieve. 120 func Getenv(key string) string 121 122 // CanGetenv reports whether key is a valid go/env configuration key. 123 func CanGetenv(key string) bool 124 125 var ( 126 GOROOT string 127 128 // Either empty or produced by filepath.Join(GOROOT, …). 129 GOROOTbin string 130 GOROOTpkg string 131 GOROOTsrc string 132 133 GOBIN = Getenv("GOBIN") 134 GOMODCACHE = envOr("GOMODCACHE", gopathDir("pkg/mod")) 135 136 // Used in envcmd.MkEnv and build ID computations. 137 GOARM = envOr("GOARM", fmt.Sprint(buildcfg.GOARM)) 138 GOARM64 = envOr("GOARM64", fmt.Sprint(buildcfg.GOARM64)) 139 GO386 = envOr("GO386", buildcfg.GO386) 140 GOAMD64 = envOr("GOAMD64", fmt.Sprintf("%s%d", "v", buildcfg.GOAMD64)) 141 GOMIPS = envOr("GOMIPS", buildcfg.GOMIPS) 142 GOMIPS64 = envOr("GOMIPS64", buildcfg.GOMIPS64) 143 GOPPC64 = envOr("GOPPC64", fmt.Sprintf("%s%d", "power", buildcfg.GOPPC64)) 144 GORISCV64 = envOr("GORISCV64", fmt.Sprintf("rva%du64", buildcfg.GORISCV64)) 145 GOWASM = envOr("GOWASM", fmt.Sprint(buildcfg.GOWASM)) 146 147 GOPROXY = envOr("GOPROXY", "") 148 GOSUMDB = envOr("GOSUMDB", "") 149 GOPRIVATE = Getenv("GOPRIVATE") 150 GONOPROXY = envOr("GONOPROXY", GOPRIVATE) 151 GONOSUMDB = envOr("GONOSUMDB", GOPRIVATE) 152 GOINSECURE = Getenv("GOINSECURE") 153 GOVCS = Getenv("GOVCS") 154 ) 155 156 var SumdbDir = gopathDir("pkg/sumdb") 157 158 // GetArchEnv returns the name and setting of the 159 // GOARCH-specific architecture environment variable. 160 // If the current architecture has no GOARCH-specific variable, 161 // GetArchEnv returns empty key and value. 162 func GetArchEnv() (key, val string) 163 164 // WithBuildXWriter returns a Context in which BuildX output is written 165 // to given io.Writer. 166 func WithBuildXWriter(ctx context.Context, xLog io.Writer) context.Context 167 168 // BuildXWriter returns nil if BuildX is false, or 169 // the writer to which BuildX output should be written otherwise. 170 func BuildXWriter(ctx context.Context) (io.Writer, bool)