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.
5 // Package cfg holds configuration shared by multiple parts
19 // These are general "build flags" used by build and other commands.
21 BuildA
bool // -a flag
22 BuildBuildmode
string // -buildmode flag
23 BuildContext
= build
.Default
24 BuildI
bool // -i flag
25 BuildLdflags
[]string // -ldflags flag
26 BuildLinkshared
bool // -linkshared flag
27 BuildMSan
bool // -msan flag
28 BuildN
bool // -n flag
29 BuildO
string // -o flag
30 BuildP
= runtime
.NumCPU() // -p flag
31 BuildPkgdir
string // -pkgdir flag
32 BuildRace
bool // -race flag
33 BuildToolexec
[]string // -toolexec flag
34 BuildToolchainName
string
35 BuildToolchainCompiler
func() string
36 BuildToolchainLinker
func() string
37 BuildV
bool // -v flag
38 BuildWork
bool // -work flag
39 BuildX
bool // -x flag
43 BuildToolchainCompiler
= func() string { return "missing-compiler" }
44 BuildToolchainLinker
= func() string { return "missing-linker" }
47 // An EnvVar is an environment variable Name=Value.
53 // OrigEnv is the original environment of the program at startup.
56 // CmdEnv is the new environment for running go tool commands.
57 // User binaries (during go test or go run) are run with OrigEnv,
61 // Global build parameters (used during package load)
63 Goarch
= BuildContext
.GOARCH
64 Goos
= BuildContext
.GOOS
66 Gopath
= filepath
.SplitList(BuildContext
.GOPATH
)
70 if Goos
== "windows" {
77 GOBIN
= os
.Getenv("GOBIN")
78 GOROOTbin
= filepath
.Join(GOROOT
, "bin")
79 GOROOTpkg
= filepath
.Join(GOROOT
, "pkg")
80 GOROOTsrc
= filepath
.Join(GOROOT
, "src")
82 // Used in envcmd.MkEnv and build ID computations.
83 GOARM
= fmt
.Sprint(objabi
.GOARM
)
87 // Update build context to use our computed GOROOT.
89 BuildContext
.GOROOT
= GOROOT
90 // Note that we must use runtime.GOOS and runtime.GOARCH here,
91 // as the tool directory does not move based on environment variables.
92 // This matches the initialization of ToolDir in go/build,
93 // except for using GOROOT rather than runtime.GOROOT().
94 if runtime
.Compiler
!= "gccgo" {
95 build
.ToolDir
= filepath
.Join(GOROOT
, "pkg/tool/"+runtime
.GOOS
+"_"+runtime
.GOARCH
)
99 func findGOROOT() string {
100 if env
:= os
.Getenv("GOROOT"); env
!= "" {
101 return filepath
.Clean(env
)
103 if runtime
.Compiler
!= "gccgo" {
104 exe
, err
:= os
.Executable()
106 exe
, err
= filepath
.Abs(exe
)
108 if dir
:= filepath
.Join(exe
, "../.."); isGOROOT(dir
) {
111 exe
, err
= filepath
.EvalSymlinks(exe
)
113 if dir
:= filepath
.Join(exe
, "../.."); isGOROOT(dir
) {
120 return filepath
.Clean(runtime
.GOROOT())
123 // isGOROOT reports whether path looks like a GOROOT.
125 // It does this by looking for the path/pkg/tool directory,
126 // which is necessary for useful operation of the cmd/go tool,
127 // and is not typically present in a GOPATH.
128 func isGOROOT(path
string) bool {
129 stat
, err
:= os
.Stat(filepath
.Join(path
, "pkg", "tool"))