github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/go/internal/envcmd/env.go (about) 1 // Copyright 2012 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 envcmd implements the ``go env'' command. 6 package envcmd 7 8 import ( 9 "encoding/json" 10 "fmt" 11 "os" 12 "path/filepath" 13 "runtime" 14 "strings" 15 16 "cmd/go/internal/base" 17 "cmd/go/internal/cache" 18 "cmd/go/internal/cfg" 19 "cmd/go/internal/load" 20 "cmd/go/internal/modload" 21 "cmd/go/internal/work" 22 ) 23 24 var CmdEnv = &base.Command{ 25 UsageLine: "go env [-json] [var ...]", 26 Short: "print Go environment information", 27 Long: ` 28 Env prints Go environment information. 29 30 By default env prints information as a shell script 31 (on Windows, a batch file). If one or more variable 32 names is given as arguments, env prints the value of 33 each named variable on its own line. 34 35 The -json flag prints the environment in JSON format 36 instead of as a shell script. 37 38 For more about environment variables, see 'go help environment'. 39 `, 40 } 41 42 func init() { 43 CmdEnv.Run = runEnv // break init cycle 44 } 45 46 var envJson = CmdEnv.Flag.Bool("json", false, "") 47 48 func MkEnv() []cfg.EnvVar { 49 var b work.Builder 50 b.Init() 51 52 env := []cfg.EnvVar{ 53 {Name: "GOARCH", Value: cfg.Goarch}, 54 {Name: "GOBIN", Value: cfg.GOBIN}, 55 {Name: "GOCACHE", Value: cache.DefaultDir()}, 56 {Name: "GOEXE", Value: cfg.ExeSuffix}, 57 {Name: "GOFLAGS", Value: os.Getenv("GOFLAGS")}, 58 {Name: "GOHOSTARCH", Value: runtime.GOARCH}, 59 {Name: "GOHOSTOS", Value: runtime.GOOS}, 60 {Name: "GOOS", Value: cfg.Goos}, 61 {Name: "GOPATH", Value: cfg.BuildContext.GOPATH}, 62 {Name: "GOPROXY", Value: os.Getenv("GOPROXY")}, 63 {Name: "GORACE", Value: os.Getenv("GORACE")}, 64 {Name: "GOROOT", Value: cfg.GOROOT}, 65 {Name: "GOTMPDIR", Value: os.Getenv("GOTMPDIR")}, 66 {Name: "GOTOOLDIR", Value: base.ToolDir}, 67 } 68 69 if work.GccgoBin != "" { 70 env = append(env, cfg.EnvVar{Name: "GCCGO", Value: work.GccgoBin}) 71 } else { 72 env = append(env, cfg.EnvVar{Name: "GCCGO", Value: work.GccgoName}) 73 } 74 75 switch cfg.Goarch { 76 case "arm": 77 env = append(env, cfg.EnvVar{Name: "GOARM", Value: cfg.GOARM}) 78 case "386": 79 env = append(env, cfg.EnvVar{Name: "GO386", Value: cfg.GO386}) 80 case "mips", "mipsle": 81 env = append(env, cfg.EnvVar{Name: "GOMIPS", Value: cfg.GOMIPS}) 82 case "mips64", "mips64le": 83 env = append(env, cfg.EnvVar{Name: "GOMIPS64", Value: cfg.GOMIPS64}) 84 } 85 86 cc := cfg.DefaultCC(cfg.Goos, cfg.Goarch) 87 if env := strings.Fields(os.Getenv("CC")); len(env) > 0 { 88 cc = env[0] 89 } 90 cxx := cfg.DefaultCXX(cfg.Goos, cfg.Goarch) 91 if env := strings.Fields(os.Getenv("CXX")); len(env) > 0 { 92 cxx = env[0] 93 } 94 env = append(env, cfg.EnvVar{Name: "CC", Value: cc}) 95 env = append(env, cfg.EnvVar{Name: "CXX", Value: cxx}) 96 97 if cfg.BuildContext.CgoEnabled { 98 env = append(env, cfg.EnvVar{Name: "CGO_ENABLED", Value: "1"}) 99 } else { 100 env = append(env, cfg.EnvVar{Name: "CGO_ENABLED", Value: "0"}) 101 } 102 103 return env 104 } 105 106 func findEnv(env []cfg.EnvVar, name string) string { 107 for _, e := range env { 108 if e.Name == name { 109 return e.Value 110 } 111 } 112 return "" 113 } 114 115 // ExtraEnvVars returns environment variables that should not leak into child processes. 116 func ExtraEnvVars() []cfg.EnvVar { 117 gomod := "" 118 if modload.HasModRoot() { 119 gomod = filepath.Join(modload.ModRoot(), "go.mod") 120 } else if modload.Enabled() { 121 gomod = os.DevNull 122 } 123 return []cfg.EnvVar{ 124 {Name: "GOMOD", Value: gomod}, 125 } 126 } 127 128 // ExtraEnvVarsCostly returns environment variables that should not leak into child processes 129 // but are costly to evaluate. 130 func ExtraEnvVarsCostly() []cfg.EnvVar { 131 var b work.Builder 132 b.Init() 133 cppflags, cflags, cxxflags, fflags, ldflags, err := b.CFlags(&load.Package{}) 134 if err != nil { 135 // Should not happen - b.CFlags was given an empty package. 136 fmt.Fprintf(os.Stderr, "go: invalid cflags: %v\n", err) 137 return nil 138 } 139 cmd := b.GccCmd(".", "") 140 141 return []cfg.EnvVar{ 142 // Note: Update the switch in runEnv below when adding to this list. 143 {Name: "CGO_CFLAGS", Value: strings.Join(cflags, " ")}, 144 {Name: "CGO_CPPFLAGS", Value: strings.Join(cppflags, " ")}, 145 {Name: "CGO_CXXFLAGS", Value: strings.Join(cxxflags, " ")}, 146 {Name: "CGO_FFLAGS", Value: strings.Join(fflags, " ")}, 147 {Name: "CGO_LDFLAGS", Value: strings.Join(ldflags, " ")}, 148 {Name: "PKG_CONFIG", Value: b.PkgconfigCmd()}, 149 {Name: "GOGCCFLAGS", Value: strings.Join(cmd[3:], " ")}, 150 } 151 } 152 153 func runEnv(cmd *base.Command, args []string) { 154 env := cfg.CmdEnv 155 env = append(env, ExtraEnvVars()...) 156 157 // Do we need to call ExtraEnvVarsCostly, which is a bit expensive? 158 // Only if we're listing all environment variables ("go env") 159 // or the variables being requested are in the extra list. 160 needCostly := true 161 if len(args) > 0 { 162 needCostly = false 163 for _, arg := range args { 164 switch arg { 165 case "CGO_CFLAGS", 166 "CGO_CPPFLAGS", 167 "CGO_CXXFLAGS", 168 "CGO_FFLAGS", 169 "CGO_LDFLAGS", 170 "PKG_CONFIG", 171 "GOGCCFLAGS": 172 needCostly = true 173 } 174 } 175 } 176 if needCostly { 177 env = append(env, ExtraEnvVarsCostly()...) 178 } 179 180 if len(args) > 0 { 181 if *envJson { 182 var es []cfg.EnvVar 183 for _, name := range args { 184 e := cfg.EnvVar{Name: name, Value: findEnv(env, name)} 185 es = append(es, e) 186 } 187 printEnvAsJSON(es) 188 } else { 189 for _, name := range args { 190 fmt.Printf("%s\n", findEnv(env, name)) 191 } 192 } 193 return 194 } 195 196 if *envJson { 197 printEnvAsJSON(env) 198 return 199 } 200 201 for _, e := range env { 202 if e.Name != "TERM" { 203 switch runtime.GOOS { 204 default: 205 fmt.Printf("%s=\"%s\"\n", e.Name, e.Value) 206 case "plan9": 207 if strings.IndexByte(e.Value, '\x00') < 0 { 208 fmt.Printf("%s='%s'\n", e.Name, strings.ReplaceAll(e.Value, "'", "''")) 209 } else { 210 v := strings.Split(e.Value, "\x00") 211 fmt.Printf("%s=(", e.Name) 212 for x, s := range v { 213 if x > 0 { 214 fmt.Printf(" ") 215 } 216 fmt.Printf("%s", s) 217 } 218 fmt.Printf(")\n") 219 } 220 case "windows": 221 fmt.Printf("set %s=%s\n", e.Name, e.Value) 222 } 223 } 224 } 225 } 226 227 func printEnvAsJSON(env []cfg.EnvVar) { 228 m := make(map[string]string) 229 for _, e := range env { 230 if e.Name == "TERM" { 231 continue 232 } 233 m[e.Name] = e.Value 234 } 235 enc := json.NewEncoder(os.Stdout) 236 enc.SetIndent("", "\t") 237 if err := enc.Encode(m); err != nil { 238 base.Fatalf("%s", err) 239 } 240 }