Source file src/cmd/go/internal/cfg/cfg.go

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

View as plain text