Skip to content

Commit

Permalink
cmd/go: print more env variables in "go env"
Browse files Browse the repository at this point in the history
"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 <[email protected]>
Run-TryBot: Quentin Smith <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
quentinmit committed Oct 18, 2016
1 parent 8b3194a commit 75fef5a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/cmd/go/alldocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 5 additions & 9 deletions src/cmd/go/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions src/cmd/go/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
Expand Down Expand Up @@ -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))
Expand Down
18 changes: 18 additions & 0 deletions src/cmd/go/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
3 changes: 3 additions & 0 deletions src/cmd/go/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 75fef5a

Please sign in to comment.