github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/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 "fmt" 11 "go/build" 12 "os" 13 "path/filepath" 14 "runtime" 15 16 "cmd/internal/objabi" 17 ) 18 19 // These are general "build flags" used by build and other commands. 20 var ( 21 BuildA bool // -a flag 22 BuildBuildmode string // -buildmode flag 23 BuildContext = build.Default 24 BuildI bool // -i flag 25 BuildLinkshared bool // -linkshared flag 26 BuildMSan bool // -msan flag 27 BuildN bool // -n flag 28 BuildO string // -o flag 29 BuildP = runtime.NumCPU() // -p flag 30 BuildPkgdir string // -pkgdir flag 31 BuildRace bool // -race flag 32 BuildToolexec []string // -toolexec flag 33 BuildToolchainName string 34 BuildToolchainCompiler func() string 35 BuildToolchainLinker func() string 36 BuildV bool // -v flag 37 BuildWork bool // -work flag 38 BuildX bool // -x flag 39 40 //@aghosn added for enclave relocation. 41 Relocencl bool 42 CmdName string // "build", "install", "list", etc. 43 44 DebugActiongraph string // -debug-actiongraph flag (undocumented, unstable) 45 ) 46 47 func init() { 48 BuildToolchainCompiler = func() string { return "missing-compiler" } 49 BuildToolchainLinker = func() string { return "missing-linker" } 50 } 51 52 // An EnvVar is an environment variable Name=Value. 53 type EnvVar struct { 54 Name string 55 Value string 56 } 57 58 // OrigEnv is the original environment of the program at startup. 59 var OrigEnv []string 60 61 // CmdEnv is the new environment for running go tool commands. 62 // User binaries (during go test or go run) are run with OrigEnv, 63 // not CmdEnv. 64 var CmdEnv []EnvVar 65 66 // Global build parameters (used during package load) 67 var ( 68 Goarch = BuildContext.GOARCH 69 Goos = BuildContext.GOOS 70 ExeSuffix string 71 Gopath = filepath.SplitList(BuildContext.GOPATH) 72 ) 73 74 func init() { 75 if Goos == "windows" { 76 ExeSuffix = ".exe" 77 } 78 } 79 80 var ( 81 GOROOT = findGOROOT() 82 GOBIN = os.Getenv("GOBIN") 83 GOROOTbin = filepath.Join(GOROOT, "bin") 84 GOROOTpkg = filepath.Join(GOROOT, "pkg") 85 GOROOTsrc = filepath.Join(GOROOT, "src") 86 GOROOT_FINAL = findGOROOT_FINAL() 87 88 // Used in envcmd.MkEnv and build ID computations. 89 GOARM = fmt.Sprint(objabi.GOARM) 90 GO386 = objabi.GO386 91 GOMIPS = objabi.GOMIPS 92 ) 93 94 // Update build context to use our computed GOROOT. 95 func init() { 96 BuildContext.GOROOT = GOROOT 97 // Note that we must use runtime.GOOS and runtime.GOARCH here, 98 // as the tool directory does not move based on environment variables. 99 // This matches the initialization of ToolDir in go/build, 100 // except for using GOROOT rather than runtime.GOROOT(). 101 build.ToolDir = filepath.Join(GOROOT, "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH) 102 } 103 104 func findGOROOT() string { 105 if env := os.Getenv("GOROOT"); env != "" { 106 return filepath.Clean(env) 107 } 108 def := filepath.Clean(runtime.GOROOT()) 109 exe, err := os.Executable() 110 if err == nil { 111 exe, err = filepath.Abs(exe) 112 if err == nil { 113 if dir := filepath.Join(exe, "../.."); isGOROOT(dir) { 114 // If def (runtime.GOROOT()) and dir are the same 115 // directory, prefer the spelling used in def. 116 if isSameDir(def, dir) { 117 return def 118 } 119 return dir 120 } 121 exe, err = filepath.EvalSymlinks(exe) 122 if err == nil { 123 if dir := filepath.Join(exe, "../.."); isGOROOT(dir) { 124 if isSameDir(def, dir) { 125 return def 126 } 127 return dir 128 } 129 } 130 } 131 } 132 return def 133 } 134 135 func findGOROOT_FINAL() string { 136 def := GOROOT 137 if env := os.Getenv("GOROOT_FINAL"); env != "" { 138 def = filepath.Clean(env) 139 } 140 return def 141 } 142 143 // isSameDir reports whether dir1 and dir2 are the same directory. 144 func isSameDir(dir1, dir2 string) bool { 145 if dir1 == dir2 { 146 return true 147 } 148 info1, err1 := os.Stat(dir1) 149 info2, err2 := os.Stat(dir2) 150 return err1 == nil && err2 == nil && os.SameFile(info1, info2) 151 } 152 153 // isGOROOT reports whether path looks like a GOROOT. 154 // 155 // It does this by looking for the path/pkg/tool directory, 156 // which is necessary for useful operation of the cmd/go tool, 157 // and is not typically present in a GOPATH. 158 func isGOROOT(path string) bool { 159 stat, err := os.Stat(filepath.Join(path, "pkg", "tool")) 160 if err != nil { 161 return false 162 } 163 return stat.IsDir() 164 }