github.com/sanprasirt/go@v0.0.0-20170607001320-a027466e4b6d/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 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 40 ) 41 42 func init() { 43 BuildToolchainCompiler = func() string { return "missing-compiler" } 44 BuildToolchainLinker = func() string { return "missing-linker" } 45 } 46 47 // An EnvVar is an environment variable Name=Value. 48 type EnvVar struct { 49 Name string 50 Value string 51 } 52 53 // OrigEnv is the original environment of the program at startup. 54 var OrigEnv []string 55 56 // CmdEnv is the new environment for running go tool commands. 57 // User binaries (during go test or go run) are run with OrigEnv, 58 // not CmdEnv. 59 var CmdEnv []EnvVar 60 61 // Global build parameters (used during package load) 62 var ( 63 Goarch string 64 Goos string 65 ExeSuffix string 66 Gopath []string 67 ) 68 69 var ( 70 GOROOT = findGOROOT() 71 GOBIN = os.Getenv("GOBIN") 72 GOROOTbin = filepath.Join(GOROOT, "bin") 73 GOROOTpkg = filepath.Join(GOROOT, "pkg") 74 GOROOTsrc = filepath.Join(GOROOT, "src") 75 76 // Used in envcmd.MkEnv and build ID computations. 77 GOARM = fmt.Sprint(objabi.GOARM) 78 GO386 = objabi.GO386 79 ) 80 81 func findGOROOT() string { 82 if env := os.Getenv("GOROOT"); env != "" { 83 return filepath.Clean(env) 84 } 85 exe, err := os.Executable() 86 if err == nil { 87 exe, err = filepath.Abs(exe) 88 if err == nil { 89 if dir := filepath.Join(exe, "../.."); isGOROOT(dir) { 90 return dir 91 } 92 exe, err = filepath.EvalSymlinks(exe) 93 if err == nil { 94 if dir := filepath.Join(exe, "../.."); isGOROOT(dir) { 95 return dir 96 } 97 } 98 } 99 } 100 return filepath.Clean(runtime.GOROOT()) 101 } 102 103 // isGOROOT reports whether path looks like a GOROOT. 104 // 105 // It does this by looking for the path/pkg/tool directory, 106 // which is necessary for useful operation of the cmd/go tool, 107 // and is not typically present in a GOPATH. 108 func isGOROOT(path string) bool { 109 stat, err := os.Stat(filepath.Join(path, "pkg", "tool")) 110 if err != nil { 111 return false 112 } 113 return stat.IsDir() 114 }