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