| 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 |
"bytes" |
| 11 |
"context" |
| 12 |
"fmt" |
| 13 |
"go/build" |
| 14 |
"internal/buildcfg" |
| 15 |
"internal/cfg" |
| 16 |
"internal/platform" |
| 17 |
"io" |
| 18 |
"io/fs" |
| 19 |
"os" |
| 20 |
"path/filepath" |
| 21 |
"runtime" |
| 22 |
"strings" |
| 23 |
"sync" |
| 24 |
"time" |
| 25 |
|
| 26 |
"cmd/go/internal/fsys" |
| 27 |
"cmd/internal/pathcache" |
| 28 |
) |
| 29 |
|
| 30 |
// Global build parameters (used during package load) |
| 31 |
var ( |
| 32 |
Goos = envOr("GOOS", build.Default.GOOS) |
| 33 |
Goarch = envOr("GOARCH", build.Default.GOARCH) |
| 34 |
|
| 35 |
ExeSuffix = exeSuffix() |
| 36 |
|
| 37 |
// ModulesEnabled specifies whether the go command is running |
| 38 |
// in module-aware mode (as opposed to GOPATH mode). |
| 39 |
// It is equal to modload.Enabled, but not all packages can import modload. |
| 40 |
ModulesEnabled bool |
| 41 |
) |
| 42 |
|
| 43 |
func exeSuffix() string { |
| 44 |
if Goos == "windows" { |
| 45 |
return ".exe" |
| 46 |
} |
| 47 |
return "" |
| 48 |
} |
| 49 |
|
| 50 |
// Configuration for tools installed to GOROOT/bin. |
| 51 |
// Normally these match runtime.GOOS and runtime.GOARCH, |
| 52 |
// but when testing a cross-compiled cmd/go they will |
| 53 |
// indicate the GOOS and GOARCH of the installed cmd/go |
| 54 |
// rather than the test binary. |
| 55 |
var ( |
| 56 |
installedGOOS string |
| 57 |
installedGOARCH string |
| 58 |
) |
| 59 |
|
| 60 |
// ToolExeSuffix returns the suffix for executables installed |
| 61 |
// in build.ToolDir. |
| 62 |
func ToolExeSuffix() string { |
| 63 |
if installedGOOS == "windows" { |
| 64 |
return ".exe" |
| 65 |
} |
| 66 |
return "" |
| 67 |
} |
| 68 |
|
| 69 |
// These are general "build flags" used by build and other commands. |
| 70 |
var ( |
| 71 |
BuildA bool // -a flag |
| 72 |
BuildBuildmode string // -buildmode flag |
| 73 |
BuildBuildvcs = "auto" // -buildvcs flag: "true", "false", or "auto" |
| 74 |
BuildContext = defaultContext() |
| 75 |
BuildMod string // -mod flag |
| 76 |
BuildModExplicit bool // whether -mod was set explicitly |
| 77 |
BuildModReason string // reason -mod was set, if set by default |
| 78 |
BuildLinkshared bool // -linkshared flag |
| 79 |
BuildMSan bool // -msan flag |
| 80 |
BuildASan bool // -asan flag |
| 81 |
BuildCover bool // -cover flag |
| 82 |
BuildCoverMode string // -covermode flag |
| 83 |
BuildCoverPkg []string // -coverpkg flag |
| 84 |
BuildJSON bool // -json flag |
| 85 |
BuildN bool // -n flag |
| 86 |
BuildO string // -o flag |
| 87 |
BuildP = runtime.GOMAXPROCS(0) // -p flag |
| 88 |
BuildPGO string // -pgo flag |
| 89 |
BuildPkgdir string // -pkgdir flag |
| 90 |
BuildRace bool // -race flag |
| 91 |
BuildToolexec []string // -toolexec flag |
| 92 |
BuildToolchainName string |
| 93 |
BuildToolchainCompiler func() string |
| 94 |
BuildToolchainLinker func() string |
| 95 |
BuildTrimpath bool // -trimpath flag |
| 96 |
BuildV bool // -v flag |
| 97 |
BuildWork bool // -work flag |
| 98 |
BuildX bool // -x flag |
| 99 |
|
| 100 |
ModCacheRW bool // -modcacherw flag |
| 101 |
ModFile string // -modfile flag |
| 102 |
|
| 103 |
CmdName string // "build", "install", "list", "mod tidy", etc. |
| 104 |
|
| 105 |
DebugActiongraph string // -debug-actiongraph flag (undocumented, unstable) |
| 106 |
DebugTrace string // -debug-trace flag |
| 107 |
DebugRuntimeTrace string // -debug-runtime-trace flag (undocumented, unstable) |
| 108 |
|
| 109 |
// GoPathError is set when GOPATH is not set. it contains an |
| 110 |
// explanation why GOPATH is unset. |
| 111 |
GoPathError string |
| 112 |
GOPATHChanged bool |
| 113 |
CGOChanged bool |
| 114 |
) |
| 115 |
|
| 116 |
func defaultContext() build.Context { |
| 117 |
ctxt := build.Default |
| 118 |
|
| 119 |
ctxt.JoinPath = filepath.Join // back door to say "do not use go command" |
| 120 |
|
| 121 |
// Override defaults computed in go/build with defaults |
| 122 |
// from go environment configuration file, if known. |
| 123 |
ctxt.GOPATH, GOPATHChanged = EnvOrAndChanged("GOPATH", gopath(ctxt)) |
| 124 |
ctxt.GOOS = Goos |
| 125 |
ctxt.GOARCH = Goarch |
| 126 |
|
| 127 |
// Clear the GOEXPERIMENT-based tool tags, which we will recompute later. |
| 128 |
var save []string |
| 129 |
for _, tag := range ctxt.ToolTags { |
| 130 |
if !strings.HasPrefix(tag, "goexperiment.") { |
| 131 |
save = append(save, tag) |
| 132 |
} |
| 133 |
} |
| 134 |
ctxt.ToolTags = save |
| 135 |
|
| 136 |
// The go/build rule for whether cgo is enabled is: |
| 137 |
// 1. If $CGO_ENABLED is set, respect it. |
| 138 |
// 2. Otherwise, if this is a cross-compile, disable cgo. |
| 139 |
// 3. Otherwise, use built-in default for GOOS/GOARCH. |
| 140 |
// |
| 141 |
// Recreate that logic here with the new GOOS/GOARCH setting. |
| 142 |
// We need to run steps 2 and 3 to determine what the default value |
| 143 |
// of CgoEnabled would be for computing CGOChanged. |
| 144 |
defaultCgoEnabled := false |
| 145 |
if buildcfg.DefaultCGO_ENABLED == "1" { |
| 146 |
defaultCgoEnabled = true |
| 147 |
} else if buildcfg.DefaultCGO_ENABLED == "0" { |
| 148 |
} else if runtime.GOARCH == ctxt.GOARCH && runtime.GOOS == ctxt.GOOS { |
| 149 |
defaultCgoEnabled = platform.CgoSupported(ctxt.GOOS, ctxt.GOARCH) |
| 150 |
// Use built-in default cgo setting for GOOS/GOARCH. |
| 151 |
// Note that ctxt.GOOS/GOARCH are derived from the preference list |
| 152 |
// (1) environment, (2) go/env file, (3) runtime constants, |
| 153 |
// while go/build.Default.GOOS/GOARCH are derived from the preference list |
| 154 |
// (1) environment, (2) runtime constants. |
| 155 |
// |
| 156 |
// We know ctxt.GOOS/GOARCH == runtime.GOOS/GOARCH; |
| 157 |
// no matter how that happened, go/build.Default will make the |
| 158 |
// same decision (either the environment variables are set explicitly |
| 159 |
// to match the runtime constants, or else they are unset, in which |
| 160 |
// case go/build falls back to the runtime constants), so |
| 161 |
// go/build.Default.GOOS/GOARCH == runtime.GOOS/GOARCH. |
| 162 |
// So ctxt.CgoEnabled (== go/build.Default.CgoEnabled) is correct |
| 163 |
// as is and can be left unmodified. |
| 164 |
// |
| 165 |
// All that said, starting in Go 1.20 we layer one more rule |
| 166 |
// on top of the go/build decision: if CC is unset and |
| 167 |
// the default C compiler we'd look for is not in the PATH, |
| 168 |
// we automatically default cgo to off. |
| 169 |
// This makes go builds work automatically on systems |
| 170 |
// without a C compiler installed. |
| 171 |
if ctxt.CgoEnabled { |
| 172 |
if os.Getenv("CC") == "" { |
| 173 |
cc := DefaultCC(ctxt.GOOS, ctxt.GOARCH) |
| 174 |
if _, err := pathcache.LookPath(cc); err != nil { |
| 175 |
defaultCgoEnabled = false |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
ctxt.CgoEnabled = defaultCgoEnabled |
| 181 |
if v := Getenv("CGO_ENABLED"); v == "0" || v == "1" { |
| 182 |
ctxt.CgoEnabled = v[0] == '1' |
| 183 |
} |
| 184 |
CGOChanged = ctxt.CgoEnabled != defaultCgoEnabled |
| 185 |
|
| 186 |
ctxt.OpenFile = func(path string) (io.ReadCloser, error) { |
| 187 |
return fsys.Open(path) |
| 188 |
} |
| 189 |
ctxt.ReadDir = func(path string) ([]fs.FileInfo, error) { |
| 190 |
// Convert []fs.DirEntry to []fs.FileInfo using dirInfo. |
| 191 |
dirs, err := fsys.ReadDir(path) |
| 192 |
infos := make([]fs.FileInfo, len(dirs)) |
| 193 |
for i, dir := range dirs { |
| 194 |
infos[i] = &dirInfo{dir} |
| 195 |
} |
| 196 |
return infos, err |
| 197 |
} |
| 198 |
ctxt.IsDir = func(path string) bool { |
| 199 |
isDir, err := fsys.IsDir(path) |
| 200 |
return err == nil && isDir |
| 201 |
} |
| 202 |
|
| 203 |
return ctxt |
| 204 |
} |
| 205 |
|
| 206 |
func init() { |
| 207 |
SetGOROOT(Getenv("GOROOT"), false) |
| 208 |
} |
| 209 |
|
| 210 |
// ForceHost forces GOOS and GOARCH to runtime.GOOS and runtime.GOARCH. |
| 211 |
// This is used by go tool to build tools for the go command's own |
| 212 |
// GOOS and GOARCH. |
| 213 |
func ForceHost() { |
| 214 |
Goos = runtime.GOOS |
| 215 |
Goarch = runtime.GOARCH |
| 216 |
ExeSuffix = exeSuffix() |
| 217 |
GO386 = buildcfg.DefaultGO386 |
| 218 |
GOAMD64 = buildcfg.DefaultGOAMD64 |
| 219 |
GOARM = buildcfg.DefaultGOARM |
| 220 |
GOARM64 = buildcfg.DefaultGOARM64 |
| 221 |
GOMIPS = buildcfg.DefaultGOMIPS |
| 222 |
GOMIPS64 = buildcfg.DefaultGOMIPS64 |
| 223 |
GOPPC64 = buildcfg.DefaultGOPPC64 |
| 224 |
GORISCV64 = buildcfg.DefaultGORISCV64 |
| 225 |
GOWASM = "" |
| 226 |
|
| 227 |
// Recompute the build context using Goos and Goarch to |
| 228 |
// set the correct value for ctx.CgoEnabled. |
| 229 |
BuildContext = defaultContext() |
| 230 |
// Call SetGOROOT to properly set the GOROOT on the new context. |
| 231 |
SetGOROOT(Getenv("GOROOT"), false) |
| 232 |
// Recompute experiments: the settings determined depend on GOOS and GOARCH. |
| 233 |
// This will also update the BuildContext's tool tags to include the new |
| 234 |
// experiment tags. |
| 235 |
computeExperiment() |
| 236 |
} |
| 237 |
|
| 238 |
// SetGOROOT sets GOROOT and associated variables to the given values. |
| 239 |
// |
| 240 |
// If isTestGo is true, build.ToolDir is set based on the TESTGO_GOHOSTOS and |
| 241 |
// TESTGO_GOHOSTARCH environment variables instead of runtime.GOOS and |
| 242 |
// runtime.GOARCH. |
| 243 |
func SetGOROOT(goroot string, isTestGo bool) { |
| 244 |
BuildContext.GOROOT = goroot |
| 245 |
|
| 246 |
GOROOT = goroot |
| 247 |
if goroot == "" { |
| 248 |
GOROOTbin = "" |
| 249 |
GOROOTpkg = "" |
| 250 |
GOROOTsrc = "" |
| 251 |
} else { |
| 252 |
GOROOTbin = filepath.Join(goroot, "bin") |
| 253 |
GOROOTpkg = filepath.Join(goroot, "pkg") |
| 254 |
GOROOTsrc = filepath.Join(goroot, "src") |
| 255 |
} |
| 256 |
|
| 257 |
installedGOOS = runtime.GOOS |
| 258 |
installedGOARCH = runtime.GOARCH |
| 259 |
if isTestGo { |
| 260 |
if testOS := os.Getenv("TESTGO_GOHOSTOS"); testOS != "" { |
| 261 |
installedGOOS = testOS |
| 262 |
} |
| 263 |
if testArch := os.Getenv("TESTGO_GOHOSTARCH"); testArch != "" { |
| 264 |
installedGOARCH = testArch |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
if runtime.Compiler != "gccgo" { |
| 269 |
if goroot == "" { |
| 270 |
build.ToolDir = "" |
| 271 |
} else { |
| 272 |
// Note that we must use the installed OS and arch here: the tool |
| 273 |
// directory does not move based on environment variables, and even if we |
| 274 |
// are testing a cross-compiled cmd/go all of the installed packages and |
| 275 |
// tools would have been built using the native compiler and linker (and |
| 276 |
// would spuriously appear stale if we used a cross-compiled compiler and |
| 277 |
// linker). |
| 278 |
// |
| 279 |
// This matches the initialization of ToolDir in go/build, except for |
| 280 |
// using ctxt.GOROOT and the installed GOOS and GOARCH rather than the |
| 281 |
// GOROOT, GOOS, and GOARCH reported by the runtime package. |
| 282 |
build.ToolDir = filepath.Join(GOROOTpkg, "tool", installedGOOS+"_"+installedGOARCH) |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
// Experiment configuration. |
| 288 |
var ( |
| 289 |
// RawGOEXPERIMENT is the GOEXPERIMENT value set by the user. |
| 290 |
RawGOEXPERIMENT = envOr("GOEXPERIMENT", buildcfg.DefaultGOEXPERIMENT) |
| 291 |
// CleanGOEXPERIMENT is the minimal GOEXPERIMENT value needed to reproduce the |
| 292 |
// experiments enabled by RawGOEXPERIMENT. |
| 293 |
CleanGOEXPERIMENT = RawGOEXPERIMENT |
| 294 |
|
| 295 |
Experiment *buildcfg.ExperimentFlags |
| 296 |
ExperimentErr error |
| 297 |
) |
| 298 |
|
| 299 |
func init() { |
| 300 |
computeExperiment() |
| 301 |
} |
| 302 |
|
| 303 |
func computeExperiment() { |
| 304 |
Experiment, ExperimentErr = buildcfg.ParseGOEXPERIMENT(Goos, Goarch, RawGOEXPERIMENT) |
| 305 |
if ExperimentErr != nil { |
| 306 |
return |
| 307 |
} |
| 308 |
|
| 309 |
// GOEXPERIMENT is valid, so convert it to canonical form. |
| 310 |
CleanGOEXPERIMENT = Experiment.String() |
| 311 |
|
| 312 |
// Add build tags based on the experiments in effect. |
| 313 |
exps := Experiment.Enabled() |
| 314 |
expTags := make([]string, 0, len(exps)+len(BuildContext.ToolTags)) |
| 315 |
for _, exp := range exps { |
| 316 |
expTags = append(expTags, "goexperiment."+exp) |
| 317 |
} |
| 318 |
BuildContext.ToolTags = append(expTags, BuildContext.ToolTags...) |
| 319 |
} |
| 320 |
|
| 321 |
// An EnvVar is an environment variable Name=Value. |
| 322 |
type EnvVar struct { |
| 323 |
Name string |
| 324 |
Value string |
| 325 |
Changed bool // effective Value differs from default |
| 326 |
} |
| 327 |
|
| 328 |
// OrigEnv is the original environment of the program at startup. |
| 329 |
var OrigEnv []string |
| 330 |
|
| 331 |
// CmdEnv is the new environment for running go tool commands. |
| 332 |
// User binaries (during go test or go run) are run with OrigEnv, |
| 333 |
// not CmdEnv. |
| 334 |
var CmdEnv []EnvVar |
| 335 |
|
| 336 |
var envCache struct { |
| 337 |
once sync.Once |
| 338 |
m map[string]string |
| 339 |
goroot map[string]string |
| 340 |
} |
| 341 |
|
| 342 |
// EnvFile returns the name of the Go environment configuration file, |
| 343 |
// and reports whether the effective value differs from the default. |
| 344 |
func EnvFile() (string, bool, error) { |
| 345 |
if file := os.Getenv("GOENV"); file != "" { |
| 346 |
if file == "off" { |
| 347 |
return "", false, fmt.Errorf("GOENV=off") |
| 348 |
} |
| 349 |
return file, true, nil |
| 350 |
} |
| 351 |
dir, err := os.UserConfigDir() |
| 352 |
if err != nil { |
| 353 |
return "", false, err |
| 354 |
} |
| 355 |
if dir == "" { |
| 356 |
return "", false, fmt.Errorf("missing user-config dir") |
| 357 |
} |
| 358 |
return filepath.Join(dir, "go/env"), false, nil |
| 359 |
} |
| 360 |
|
| 361 |
func initEnvCache() { |
| 362 |
envCache.m = make(map[string]string) |
| 363 |
envCache.goroot = make(map[string]string) |
| 364 |
if file, _, _ := EnvFile(); file != "" { |
| 365 |
readEnvFile(file, "user") |
| 366 |
} |
| 367 |
goroot := findGOROOT(envCache.m["GOROOT"]) |
| 368 |
if goroot != "" { |
| 369 |
readEnvFile(filepath.Join(goroot, "go.env"), "GOROOT") |
| 370 |
} |
| 371 |
|
| 372 |
// Save the goroot for func init calling SetGOROOT, |
| 373 |
// and also overwrite anything that might have been in go.env. |
| 374 |
// It makes no sense for GOROOT/go.env to specify |
| 375 |
// a different GOROOT. |
| 376 |
envCache.m["GOROOT"] = goroot |
| 377 |
} |
| 378 |
|
| 379 |
func readEnvFile(file string, source string) { |
| 380 |
if file == "" { |
| 381 |
return |
| 382 |
} |
| 383 |
data, err := os.ReadFile(file) |
| 384 |
if err != nil { |
| 385 |
return |
| 386 |
} |
| 387 |
|
| 388 |
for len(data) > 0 { |
| 389 |
// Get next line. |
| 390 |
line := data |
| 391 |
i := bytes.IndexByte(data, '\n') |
| 392 |
if i >= 0 { |
| 393 |
line, data = line[:i], data[i+1:] |
| 394 |
} else { |
| 395 |
data = nil |
| 396 |
} |
| 397 |
|
| 398 |
i = bytes.IndexByte(line, '=') |
| 399 |
if i < 0 || line[0] < 'A' || 'Z' < line[0] { |
| 400 |
// Line is missing = (or empty) or a comment or not a valid env name. Ignore. |
| 401 |
// This should not happen in the user file, since the file should be maintained almost |
| 402 |
// exclusively by "go env -w", but better to silently ignore than to make |
| 403 |
// the go command unusable just because somehow the env file has |
| 404 |
// gotten corrupted. |
| 405 |
// In the GOROOT/go.env file, we expect comments. |
| 406 |
continue |
| 407 |
} |
| 408 |
key, val := line[:i], line[i+1:] |
| 409 |
|
| 410 |
if source == "GOROOT" { |
| 411 |
envCache.goroot[string(key)] = string(val) |
| 412 |
// In the GOROOT/go.env file, do not overwrite fields loaded from the user's go/env file. |
| 413 |
if _, ok := envCache.m[string(key)]; ok { |
| 414 |
continue |
| 415 |
} |
| 416 |
} |
| 417 |
envCache.m[string(key)] = string(val) |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
// Getenv gets the value for the configuration key. |
| 422 |
// It consults the operating system environment |
| 423 |
// and then the go/env file. |
| 424 |
// If Getenv is called for a key that cannot be set |
| 425 |
// in the go/env file (for example GODEBUG), it panics. |
| 426 |
// This ensures that CanGetenv is accurate, so that |
| 427 |
// 'go env -w' stays in sync with what Getenv can retrieve. |
| 428 |
func Getenv(key string) string { |
| 429 |
if !CanGetenv(key) { |
| 430 |
switch key { |
| 431 |
case "CGO_TEST_ALLOW", "CGO_TEST_DISALLOW", "CGO_test_ALLOW", "CGO_test_DISALLOW": |
| 432 |
// used by internal/work/security_test.go; allow |
| 433 |
default: |
| 434 |
panic("internal error: invalid Getenv " + key) |
| 435 |
} |
| 436 |
} |
| 437 |
val := os.Getenv(key) |
| 438 |
if val != "" { |
| 439 |
return val |
| 440 |
} |
| 441 |
envCache.once.Do(initEnvCache) |
| 442 |
return envCache.m[key] |
| 443 |
} |
| 444 |
|
| 445 |
// CanGetenv reports whether key is a valid go/env configuration key. |
| 446 |
func CanGetenv(key string) bool { |
| 447 |
envCache.once.Do(initEnvCache) |
| 448 |
if _, ok := envCache.m[key]; ok { |
| 449 |
// Assume anything in the user file or go.env file is valid. |
| 450 |
return true |
| 451 |
} |
| 452 |
return strings.Contains(cfg.KnownEnv, "\t"+key+"\n") |
| 453 |
} |
| 454 |
|
| 455 |
var ( |
| 456 |
GOROOT string |
| 457 |
|
| 458 |
// Either empty or produced by filepath.Join(GOROOT, …). |
| 459 |
GOROOTbin string |
| 460 |
GOROOTpkg string |
| 461 |
GOROOTsrc string |
| 462 |
|
| 463 |
GOBIN = Getenv("GOBIN") |
| 464 |
GOCACHEPROG, GOCACHEPROGChanged = EnvOrAndChanged("GOCACHEPROG", "") |
| 465 |
GOMODCACHE, GOMODCACHEChanged = EnvOrAndChanged("GOMODCACHE", gopathDir("pkg/mod")) |
| 466 |
|
| 467 |
// Used in envcmd.MkEnv and build ID computations. |
| 468 |
GOARM64, goARM64Changed = EnvOrAndChanged("GOARM64", buildcfg.DefaultGOARM64) |
| 469 |
GOARM, goARMChanged = EnvOrAndChanged("GOARM", buildcfg.DefaultGOARM) |
| 470 |
GO386, go386Changed = EnvOrAndChanged("GO386", buildcfg.DefaultGO386) |
| 471 |
GOAMD64, goAMD64Changed = EnvOrAndChanged("GOAMD64", buildcfg.DefaultGOAMD64) |
| 472 |
GOMIPS, goMIPSChanged = EnvOrAndChanged("GOMIPS", buildcfg.DefaultGOMIPS) |
| 473 |
GOMIPS64, goMIPS64Changed = EnvOrAndChanged("GOMIPS64", buildcfg.DefaultGOMIPS64) |
| 474 |
GOPPC64, goPPC64Changed = EnvOrAndChanged("GOPPC64", buildcfg.DefaultGOPPC64) |
| 475 |
GORISCV64, goRISCV64Changed = EnvOrAndChanged("GORISCV64", buildcfg.DefaultGORISCV64) |
| 476 |
GOWASM, goWASMChanged = EnvOrAndChanged("GOWASM", fmt.Sprint(buildcfg.GOWASM)) |
| 477 |
|
| 478 |
GOFIPS140, GOFIPS140Changed = EnvOrAndChanged("GOFIPS140", buildcfg.DefaultGOFIPS140) |
| 479 |
GOPROXY, GOPROXYChanged = EnvOrAndChanged("GOPROXY", "") |
| 480 |
GOSUMDB, GOSUMDBChanged = EnvOrAndChanged("GOSUMDB", "") |
| 481 |
GOPRIVATE = Getenv("GOPRIVATE") |
| 482 |
GONOPROXY, GONOPROXYChanged = EnvOrAndChanged("GONOPROXY", GOPRIVATE) |
| 483 |
GONOSUMDB, GONOSUMDBChanged = EnvOrAndChanged("GONOSUMDB", GOPRIVATE) |
| 484 |
GOINSECURE = Getenv("GOINSECURE") |
| 485 |
GOVCS = Getenv("GOVCS") |
| 486 |
GOAUTH, GOAUTHChanged = EnvOrAndChanged("GOAUTH", "netrc") |
| 487 |
) |
| 488 |
|
| 489 |
// EnvOrAndChanged returns the environment variable value |
| 490 |
// and reports whether it differs from the default value. |
| 491 |
func EnvOrAndChanged(name, def string) (v string, changed bool) { |
| 492 |
val := Getenv(name) |
| 493 |
if val != "" { |
| 494 |
v = val |
| 495 |
if g, ok := envCache.goroot[name]; ok { |
| 496 |
changed = val != g |
| 497 |
} else { |
| 498 |
changed = val != def |
| 499 |
} |
| 500 |
return v, changed |
| 501 |
} |
| 502 |
return def, false |
| 503 |
} |
| 504 |
|
| 505 |
var SumdbDir = gopathDir("pkg/sumdb") |
| 506 |
|
| 507 |
// GetArchEnv returns the name and setting of the |
| 508 |
// GOARCH-specific architecture environment variable. |
| 509 |
// If the current architecture has no GOARCH-specific variable, |
| 510 |
// GetArchEnv returns empty key and value. |
| 511 |
func GetArchEnv() (key, val string, changed bool) { |
| 512 |
switch Goarch { |
| 513 |
case "arm": |
| 514 |
return "GOARM", GOARM, goARMChanged |
| 515 |
case "arm64": |
| 516 |
return "GOARM64", GOARM64, goARM64Changed |
| 517 |
case "386": |
| 518 |
return "GO386", GO386, go386Changed |
| 519 |
case "amd64": |
| 520 |
return "GOAMD64", GOAMD64, goAMD64Changed |
| 521 |
case "mips", "mipsle": |
| 522 |
return "GOMIPS", GOMIPS, goMIPSChanged |
| 523 |
case "mips64", "mips64le": |
| 524 |
return "GOMIPS64", GOMIPS64, goMIPS64Changed |
| 525 |
case "ppc64", "ppc64le": |
| 526 |
return "GOPPC64", GOPPC64, goPPC64Changed |
| 527 |
case "riscv64": |
| 528 |
return "GORISCV64", GORISCV64, goRISCV64Changed |
| 529 |
case "wasm": |
| 530 |
return "GOWASM", GOWASM, goWASMChanged |
| 531 |
} |
| 532 |
return "", "", false |
| 533 |
} |
| 534 |
|
| 535 |
// envOr returns Getenv(key) if set, or else def. |
| 536 |
func envOr(key, def string) string { |
| 537 |
val := Getenv(key) |
| 538 |
if val == "" { |
| 539 |
val = def |
| 540 |
} |
| 541 |
return val |
| 542 |
} |
| 543 |
|
| 544 |
// There is a copy of findGOROOT, isSameDir, and isGOROOT in |
| 545 |
// x/tools/cmd/godoc/goroot.go. |
| 546 |
// Try to keep them in sync for now. |
| 547 |
|
| 548 |
// findGOROOT returns the GOROOT value, using either an explicitly |
| 549 |
// provided environment variable, a GOROOT that contains the current |
| 550 |
// os.Executable value, or else the GOROOT that the binary was built |
| 551 |
// with from runtime.GOROOT(). |
| 552 |
// |
| 553 |
// There is a copy of this code in x/tools/cmd/godoc/goroot.go. |
| 554 |
func findGOROOT(env string) string { |
| 555 |
if env == "" { |
| 556 |
// Not using Getenv because findGOROOT is called |
| 557 |
// to find the GOROOT/go.env file. initEnvCache |
| 558 |
// has passed in the setting from the user go/env file. |
| 559 |
env = os.Getenv("GOROOT") |
| 560 |
} |
| 561 |
if env != "" { |
| 562 |
return filepath.Clean(env) |
| 563 |
} |
| 564 |
def := "" |
| 565 |
if r := runtime.GOROOT(); r != "" { |
| 566 |
def = filepath.Clean(r) |
| 567 |
} |
| 568 |
if runtime.Compiler == "gccgo" { |
| 569 |
// gccgo has no real GOROOT, and it certainly doesn't |
| 570 |
// depend on the executable's location. |
| 571 |
return def |
| 572 |
} |
| 573 |
|
| 574 |
// canonical returns a directory path that represents |
| 575 |
// the same directory as dir, |
| 576 |
// preferring the spelling in def if the two are the same. |
| 577 |
canonical := func(dir string) string { |
| 578 |
if isSameDir(def, dir) { |
| 579 |
return def |
| 580 |
} |
| 581 |
return dir |
| 582 |
} |
| 583 |
|
| 584 |
exe, err := os.Executable() |
| 585 |
if err == nil { |
| 586 |
exe, err = filepath.Abs(exe) |
| 587 |
if err == nil { |
| 588 |
// cmd/go may be installed in GOROOT/bin or GOROOT/bin/GOOS_GOARCH, |
| 589 |
// depending on whether it was cross-compiled with a different |
| 590 |
// GOHOSTOS (see https://go.dev/issue/62119). Try both. |
| 591 |
if dir := filepath.Join(exe, "../.."); isGOROOT(dir) { |
| 592 |
return canonical(dir) |
| 593 |
} |
| 594 |
if dir := filepath.Join(exe, "../../.."); isGOROOT(dir) { |
| 595 |
return canonical(dir) |
| 596 |
} |
| 597 |
|
| 598 |
// Depending on what was passed on the command line, it is possible |
| 599 |
// that os.Executable is a symlink (like /usr/local/bin/go) referring |
| 600 |
// to a binary installed in a real GOROOT elsewhere |
| 601 |
// (like /usr/lib/go/bin/go). |
| 602 |
// Try to find that GOROOT by resolving the symlinks. |
| 603 |
exe, err = filepath.EvalSymlinks(exe) |
| 604 |
if err == nil { |
| 605 |
if dir := filepath.Join(exe, "../.."); isGOROOT(dir) { |
| 606 |
return canonical(dir) |
| 607 |
} |
| 608 |
if dir := filepath.Join(exe, "../../.."); isGOROOT(dir) { |
| 609 |
return canonical(dir) |
| 610 |
} |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
return def |
| 615 |
} |
| 616 |
|
| 617 |
// isSameDir reports whether dir1 and dir2 are the same directory. |
| 618 |
func isSameDir(dir1, dir2 string) bool { |
| 619 |
if dir1 == dir2 { |
| 620 |
return true |
| 621 |
} |
| 622 |
info1, err1 := os.Stat(dir1) |
| 623 |
info2, err2 := os.Stat(dir2) |
| 624 |
return err1 == nil && err2 == nil && os.SameFile(info1, info2) |
| 625 |
} |
| 626 |
|
| 627 |
// isGOROOT reports whether path looks like a GOROOT. |
| 628 |
// |
| 629 |
// It does this by looking for the path/pkg/tool directory, |
| 630 |
// which is necessary for useful operation of the cmd/go tool, |
| 631 |
// and is not typically present in a GOPATH. |
| 632 |
// |
| 633 |
// There is a copy of this code in x/tools/cmd/godoc/goroot.go. |
| 634 |
func isGOROOT(path string) bool { |
| 635 |
stat, err := os.Stat(filepath.Join(path, "pkg", "tool")) |
| 636 |
if err != nil { |
| 637 |
return false |
| 638 |
} |
| 639 |
return stat.IsDir() |
| 640 |
} |
| 641 |
|
| 642 |
func gopathDir(rel string) string { |
| 643 |
list := filepath.SplitList(BuildContext.GOPATH) |
| 644 |
if len(list) == 0 || list[0] == "" { |
| 645 |
return "" |
| 646 |
} |
| 647 |
return filepath.Join(list[0], rel) |
| 648 |
} |
| 649 |
|
| 650 |
// Keep consistent with go/build.defaultGOPATH. |
| 651 |
func gopath(ctxt build.Context) string { |
| 652 |
if len(ctxt.GOPATH) > 0 { |
| 653 |
return ctxt.GOPATH |
| 654 |
} |
| 655 |
env := "HOME" |
| 656 |
if runtime.GOOS == "windows" { |
| 657 |
env = "USERPROFILE" |
| 658 |
} else if runtime.GOOS == "plan9" { |
| 659 |
env = "home" |
| 660 |
} |
| 661 |
if home := os.Getenv(env); home != "" { |
| 662 |
def := filepath.Join(home, "go") |
| 663 |
if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) { |
| 664 |
GoPathError = "cannot set GOROOT as GOPATH" |
| 665 |
} |
| 666 |
return "" |
| 667 |
} |
| 668 |
GoPathError = fmt.Sprintf("%s is not set", env) |
| 669 |
return "" |
| 670 |
} |
| 671 |
|
| 672 |
// WithBuildXWriter returns a Context in which BuildX output is written |
| 673 |
// to given io.Writer. |
| 674 |
func WithBuildXWriter(ctx context.Context, xLog io.Writer) context.Context { |
| 675 |
return context.WithValue(ctx, buildXContextKey{}, xLog) |
| 676 |
} |
| 677 |
|
| 678 |
type buildXContextKey struct{} |
| 679 |
|
| 680 |
// BuildXWriter returns nil if BuildX is false, or |
| 681 |
// the writer to which BuildX output should be written otherwise. |
| 682 |
func BuildXWriter(ctx context.Context) (io.Writer, bool) { |
| 683 |
if !BuildX { |
| 684 |
return nil, false |
| 685 |
} |
| 686 |
if v := ctx.Value(buildXContextKey{}); v != nil { |
| 687 |
return v.(io.Writer), true |
| 688 |
} |
| 689 |
return os.Stderr, true |
| 690 |
} |
| 691 |
|
| 692 |
// A dirInfo implements fs.FileInfo from fs.DirEntry. |
| 693 |
// We know that go/build doesn't use the non-DirEntry parts, |
| 694 |
// so we can panic instead of doing difficult work. |
| 695 |
type dirInfo struct { |
| 696 |
dir fs.DirEntry |
| 697 |
} |
| 698 |
|
| 699 |
func (d *dirInfo) Name() string { return d.dir.Name() } |
| 700 |
func (d *dirInfo) IsDir() bool { return d.dir.IsDir() } |
| 701 |
func (d *dirInfo) Mode() fs.FileMode { return d.dir.Type() } |
| 702 |
|
| 703 |
func (d *dirInfo) Size() int64 { panic("dirInfo.Size") } |
| 704 |
func (d *dirInfo) ModTime() time.Time { panic("dirInfo.ModTime") } |
| 705 |
func (d *dirInfo) Sys() any { panic("dirInfo.Sys") } |