From 75fef5a0f60bcace88a8d7470df2d85d8eee048f Mon Sep 17 00:00:00 2001 From: Quentin Smith Date: Mon, 17 Oct 2016 18:40:18 -0400 Subject: [PATCH] cmd/go: print more env variables in "go env" "go env" previously only printed a subset of the documented environment variables; now it includes everything, such as GO386 and CGO_*. This also fixes the CGO_CFLAGS environment variable to always have the same default. According to iant@ and confirmed by testing, cgo can now understand the default value of CGO_CFLAGS. Fixes #17191. Change-Id: Icf75055446dd250b6256ef1139e9ce848f4a9d3b Reviewed-on: https://go-review.googlesource.com/31330 TryBot-Result: Gobot Gobot Run-TryBot: Quentin Smith Reviewed-by: Ian Lance Taylor --- src/cmd/go/alldocs.go | 3 +++ src/cmd/go/build.go | 14 +++++--------- src/cmd/go/env.go | 26 ++++++++++++++++++++++++++ src/cmd/go/go_test.go | 18 ++++++++++++++++++ src/cmd/go/help.go | 3 +++ 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index c719c88648..5741c9f51e 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -1100,6 +1100,9 @@ // CGO_CXXFLAGS // Flags that cgo will pass to the compiler when compiling // C++ code. +// CGO_FFLAGS +// Flags that cgo will pass to the compiler when compiling +// Fortran code. // CGO_LDFLAGS // Flags that cgo will pass to the compiler when linking. // CXX diff --git a/src/cmd/go/build.go b/src/cmd/go/build.go index 5c317cef81..4ff4a980fc 100644 --- a/src/cmd/go/build.go +++ b/src/cmd/go/build.go @@ -3188,11 +3188,8 @@ func envList(key, def string) []string { } // Return the flags to use when invoking the C, C++ or Fortran compilers, or cgo. -func (b *builder) cflags(p *Package, def bool) (cppflags, cflags, cxxflags, fflags, ldflags []string) { - var defaults string - if def { - defaults = "-g -O2" - } +func (b *builder) cflags(p *Package) (cppflags, cflags, cxxflags, fflags, ldflags []string) { + defaults := "-g -O2" cppflags = stringList(envList("CGO_CPPFLAGS", ""), p.CgoCPPFLAGS) cflags = stringList(envList("CGO_CFLAGS", defaults), p.CgoCFLAGS) @@ -3205,8 +3202,7 @@ func (b *builder) cflags(p *Package, def bool) (cppflags, cflags, cxxflags, ffla var cgoRe = regexp.MustCompile(`[/\\:]`) func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) { - cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS := b.cflags(p, true) - _, cgoexeCFLAGS, _, _, _ := b.cflags(p, false) + cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS := b.cflags(p) cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...) cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...) // If we are compiling Objective-C code, then we need to link against libobjc @@ -3284,7 +3280,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi cgoflags = append(cgoflags, "-exportheader="+obj+"_cgo_install.h") } - if err := b.run(p.Dir, p.ImportPath, cgoenv, buildToolExec, cgoExe, "-objdir", obj, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoexeCFLAGS, cgofiles); err != nil { + if err := b.run(p.Dir, p.ImportPath, cgoenv, buildToolExec, cgoExe, "-objdir", obj, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil { return nil, nil, err } outGo = append(outGo, gofiles...) @@ -3602,7 +3598,7 @@ func (b *builder) swigIntSize(obj string) (intsize string, err error) { // Run SWIG on one SWIG input file. func (b *builder) swigOne(p *Package, file, obj string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) { - cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _ := b.cflags(p, true) + cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _ := b.cflags(p) var cflags []string if cxx { cflags = stringList(cgoCPPFLAGS, pcCFLAGS, cgoCXXFLAGS) diff --git a/src/cmd/go/env.go b/src/cmd/go/env.go index 8aaaf46329..04c54e635d 100644 --- a/src/cmd/go/env.go +++ b/src/cmd/go/env.go @@ -49,6 +49,19 @@ func mkEnv() []envVar { {"TERM", "dumb"}, } + if gccgoBin != "" { + env = append(env, envVar{"GCCGO", gccgoBin}) + } else { + env = append(env, envVar{"GCCGO", gccgoName}) + } + + switch goarch { + case "arm": + env = append(env, envVar{"GOARM", os.Getenv("GOARM")}) + case "386": + env = append(env, envVar{"GO386", os.Getenv("GO386")}) + } + if goos != "plan9" { cmd := b.gccCmd(".") env = append(env, envVar{"CC", cmd[0]}) @@ -77,6 +90,19 @@ func findEnv(env []envVar, name string) string { func runEnv(cmd *Command, args []string) { env := mkEnv() + // Add these environment variables here so they do not leak + // into child processes. + var b builder + b.init() + cppflags, cflags, cxxflags, fflags, ldflags := b.cflags(&Package{}) + env = append(env, + envVar{"PKG_CONFIG", b.pkgconfigCmd()}, + envVar{"CGO_CFLAGS", strings.Join(cflags, " ")}, + envVar{"CGO_CPPFLAGS", strings.Join(cppflags, " ")}, + envVar{"CGO_CXXFLAGS", strings.Join(cxxflags, " ")}, + envVar{"CGO_FFLAGS", strings.Join(fflags, " ")}, + envVar{"CGO_LDFLAGS", strings.Join(ldflags, " ")}, + ) if len(args) > 0 { for _, name := range args { fmt.Printf("%s\n", findEnv(env, name)) diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index 4839b9bcbb..5ee7f04ade 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -2992,3 +2992,21 @@ func TestGoGetUpdateWithWildcard(t *testing.T) { const notExpectedPkgPath = "src/github.com/tmwh/go-get-issue-14450-c-dependency/e" tg.mustNotExist(tg.path(notExpectedPkgPath)) } + +func TestGoEnv(t *testing.T) { + tg := testgo(t) + defer tg.cleanup() + tg.setenv("GOARCH", "arm") + tg.run("env", "GOARCH") + tg.grepStdout("^arm$", "GOARCH not honored") + + tg.run("env", "GCCGO") + tg.grepStdout(".", "GCCGO unexpectedly empty") + + tg.run("env", "CGO_CFLAGS") + tg.grepStdout(".", "default CGO_CFLAGS unexpectedly empty") + + tg.setenv("CGO_CFLAGS", "-foobar") + tg.run("env", "CGO_CFLAGS") + tg.grepStdout("^-foobar$", "CGO_CFLAGS not honored") +} diff --git a/src/cmd/go/help.go b/src/cmd/go/help.go index 25e33e70a7..34fef7aca3 100644 --- a/src/cmd/go/help.go +++ b/src/cmd/go/help.go @@ -466,6 +466,9 @@ Environment variables for use with cgo: CGO_CXXFLAGS Flags that cgo will pass to the compiler when compiling C++ code. + CGO_FFLAGS + Flags that cgo will pass to the compiler when compiling + Fortran code. CGO_LDFLAGS Flags that cgo will pass to the compiler when linking. CXX