Skip to content

Commit 75fef5a

Browse files
committed
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 <[email protected]> Run-TryBot: Quentin Smith <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 8b3194a commit 75fef5a

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

src/cmd/go/alldocs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,9 @@
11001100
// CGO_CXXFLAGS
11011101
// Flags that cgo will pass to the compiler when compiling
11021102
// C++ code.
1103+
// CGO_FFLAGS
1104+
// Flags that cgo will pass to the compiler when compiling
1105+
// Fortran code.
11031106
// CGO_LDFLAGS
11041107
// Flags that cgo will pass to the compiler when linking.
11051108
// CXX

src/cmd/go/build.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3188,11 +3188,8 @@ func envList(key, def string) []string {
31883188
}
31893189

31903190
// Return the flags to use when invoking the C, C++ or Fortran compilers, or cgo.
3191-
func (b *builder) cflags(p *Package, def bool) (cppflags, cflags, cxxflags, fflags, ldflags []string) {
3192-
var defaults string
3193-
if def {
3194-
defaults = "-g -O2"
3195-
}
3191+
func (b *builder) cflags(p *Package) (cppflags, cflags, cxxflags, fflags, ldflags []string) {
3192+
defaults := "-g -O2"
31963193

31973194
cppflags = stringList(envList("CGO_CPPFLAGS", ""), p.CgoCPPFLAGS)
31983195
cflags = stringList(envList("CGO_CFLAGS", defaults), p.CgoCFLAGS)
@@ -3205,8 +3202,7 @@ func (b *builder) cflags(p *Package, def bool) (cppflags, cflags, cxxflags, ffla
32053202
var cgoRe = regexp.MustCompile(`[/\\:]`)
32063203

32073204
func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
3208-
cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS := b.cflags(p, true)
3209-
_, cgoexeCFLAGS, _, _, _ := b.cflags(p, false)
3205+
cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS := b.cflags(p)
32103206
cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...)
32113207
cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...)
32123208
// 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
32843280
cgoflags = append(cgoflags, "-exportheader="+obj+"_cgo_install.h")
32853281
}
32863282

3287-
if err := b.run(p.Dir, p.ImportPath, cgoenv, buildToolExec, cgoExe, "-objdir", obj, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoexeCFLAGS, cgofiles); err != nil {
3283+
if err := b.run(p.Dir, p.ImportPath, cgoenv, buildToolExec, cgoExe, "-objdir", obj, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
32883284
return nil, nil, err
32893285
}
32903286
outGo = append(outGo, gofiles...)
@@ -3602,7 +3598,7 @@ func (b *builder) swigIntSize(obj string) (intsize string, err error) {
36023598

36033599
// Run SWIG on one SWIG input file.
36043600
func (b *builder) swigOne(p *Package, file, obj string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
3605-
cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _ := b.cflags(p, true)
3601+
cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _ := b.cflags(p)
36063602
var cflags []string
36073603
if cxx {
36083604
cflags = stringList(cgoCPPFLAGS, pcCFLAGS, cgoCXXFLAGS)

src/cmd/go/env.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ func mkEnv() []envVar {
4949
{"TERM", "dumb"},
5050
}
5151

52+
if gccgoBin != "" {
53+
env = append(env, envVar{"GCCGO", gccgoBin})
54+
} else {
55+
env = append(env, envVar{"GCCGO", gccgoName})
56+
}
57+
58+
switch goarch {
59+
case "arm":
60+
env = append(env, envVar{"GOARM", os.Getenv("GOARM")})
61+
case "386":
62+
env = append(env, envVar{"GO386", os.Getenv("GO386")})
63+
}
64+
5265
if goos != "plan9" {
5366
cmd := b.gccCmd(".")
5467
env = append(env, envVar{"CC", cmd[0]})
@@ -77,6 +90,19 @@ func findEnv(env []envVar, name string) string {
7790

7891
func runEnv(cmd *Command, args []string) {
7992
env := mkEnv()
93+
// Add these environment variables here so they do not leak
94+
// into child processes.
95+
var b builder
96+
b.init()
97+
cppflags, cflags, cxxflags, fflags, ldflags := b.cflags(&Package{})
98+
env = append(env,
99+
envVar{"PKG_CONFIG", b.pkgconfigCmd()},
100+
envVar{"CGO_CFLAGS", strings.Join(cflags, " ")},
101+
envVar{"CGO_CPPFLAGS", strings.Join(cppflags, " ")},
102+
envVar{"CGO_CXXFLAGS", strings.Join(cxxflags, " ")},
103+
envVar{"CGO_FFLAGS", strings.Join(fflags, " ")},
104+
envVar{"CGO_LDFLAGS", strings.Join(ldflags, " ")},
105+
)
80106
if len(args) > 0 {
81107
for _, name := range args {
82108
fmt.Printf("%s\n", findEnv(env, name))

src/cmd/go/go_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,3 +2992,21 @@ func TestGoGetUpdateWithWildcard(t *testing.T) {
29922992
const notExpectedPkgPath = "src/github.com/tmwh/go-get-issue-14450-c-dependency/e"
29932993
tg.mustNotExist(tg.path(notExpectedPkgPath))
29942994
}
2995+
2996+
func TestGoEnv(t *testing.T) {
2997+
tg := testgo(t)
2998+
defer tg.cleanup()
2999+
tg.setenv("GOARCH", "arm")
3000+
tg.run("env", "GOARCH")
3001+
tg.grepStdout("^arm$", "GOARCH not honored")
3002+
3003+
tg.run("env", "GCCGO")
3004+
tg.grepStdout(".", "GCCGO unexpectedly empty")
3005+
3006+
tg.run("env", "CGO_CFLAGS")
3007+
tg.grepStdout(".", "default CGO_CFLAGS unexpectedly empty")
3008+
3009+
tg.setenv("CGO_CFLAGS", "-foobar")
3010+
tg.run("env", "CGO_CFLAGS")
3011+
tg.grepStdout("^-foobar$", "CGO_CFLAGS not honored")
3012+
}

src/cmd/go/help.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ Environment variables for use with cgo:
466466
CGO_CXXFLAGS
467467
Flags that cgo will pass to the compiler when compiling
468468
C++ code.
469+
CGO_FFLAGS
470+
Flags that cgo will pass to the compiler when compiling
471+
Fortran code.
469472
CGO_LDFLAGS
470473
Flags that cgo will pass to the compiler when linking.
471474
CXX

0 commit comments

Comments
 (0)