libgo: update to go1.9
[official-gcc.git] / libgo / go / cmd / go / internal / cfg / cfg.go
blob8257a0e511685d7e67355c66965f66e2815bbaa6
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
6 // of the go command.
7 package cfg
9 import (
10 "fmt"
11 "go/build"
12 "os"
13 "path/filepath"
14 "runtime"
16 "cmd/internal/objabi"
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
42 func init() {
43 BuildToolchainCompiler = func() string { return "missing-compiler" }
44 BuildToolchainLinker = func() string { return "missing-linker" }
47 // An EnvVar is an environment variable Name=Value.
48 type EnvVar struct {
49 Name string
50 Value string
53 // OrigEnv is the original environment of the program at startup.
54 var OrigEnv []string
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
61 // Global build parameters (used during package load)
62 var (
63 Goarch = BuildContext.GOARCH
64 Goos = BuildContext.GOOS
65 ExeSuffix string
66 Gopath = filepath.SplitList(BuildContext.GOPATH)
69 func init() {
70 if Goos == "windows" {
71 ExeSuffix = ".exe"
75 var (
76 GOROOT = findGOROOT()
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)
84 GO386 = objabi.GO386
87 // Update build context to use our computed GOROOT.
88 func init() {
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()
105 if err == nil {
106 exe, err = filepath.Abs(exe)
107 if err == nil {
108 if dir := filepath.Join(exe, "../.."); isGOROOT(dir) {
109 return dir
111 exe, err = filepath.EvalSymlinks(exe)
112 if err == nil {
113 if dir := filepath.Join(exe, "../.."); isGOROOT(dir) {
114 return 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"))
130 if err != nil {
131 return false
133 return stat.IsDir()