@@ -1164,10 +1164,8 @@ func (b *Builder) vet(ctx context.Context, a *Action) error {
1164
1164
return err
1165
1165
}
1166
1166
1167
- env := b .cCompilerEnv ()
1168
- if cfg .BuildToolchainName == "gccgo" {
1169
- env = append (env , "GCCGO=" + BuildToolchain .compiler ())
1170
- }
1167
+ // TODO(rsc): Why do we pass $GCCGO to go vet?
1168
+ env := b .cgoEnv ()
1171
1169
1172
1170
p := a .Package
1173
1171
tool := VetTool
@@ -2110,6 +2108,24 @@ func (b *Builder) cCompilerEnv() []string {
2110
2108
return []string {"TERM=dumb" }
2111
2109
}
2112
2110
2111
+ // cgoEnv returns environment variables to set when running cgo.
2112
+ // Some of these pass through to cgo running the C compiler,
2113
+ // so it includes cCompilerEnv.
2114
+ func (b * Builder ) cgoEnv () []string {
2115
+ b .cgoEnvOnce .Do (func () {
2116
+ cc , err := exec .LookPath (b .ccExe ()[0 ])
2117
+ if err != nil || filepath .Base (cc ) == cc { // reject relative path
2118
+ cc = "/missing-cc"
2119
+ }
2120
+ gccgo := GccgoBin
2121
+ if filepath .Base (gccgo ) == gccgo { // reject relative path
2122
+ gccgo = "/missing-gccgo"
2123
+ }
2124
+ b .cgoEnvCache = append (b .cCompilerEnv (), "CC=" + cc , "GCCGO=" + gccgo )
2125
+ })
2126
+ return b .cgoEnvCache
2127
+ }
2128
+
2113
2129
// mkdir makes the named directory.
2114
2130
func (b * Builder ) Mkdir (dir string ) error {
2115
2131
// Make Mkdir(a.Objdir) a no-op instead of an error when a.Objdir == "".
@@ -2710,13 +2726,13 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
2710
2726
// along to the host linker. At this point in the code, cgoLDFLAGS
2711
2727
// consists of the original $CGO_LDFLAGS (unchecked) and all the
2712
2728
// flags put together from source code (checked).
2713
- cgoenv := b .cCompilerEnv ()
2729
+ cgoenv := b .cgoEnv ()
2714
2730
if len (cgoLDFLAGS ) > 0 {
2715
2731
flags := make ([]string , len (cgoLDFLAGS ))
2716
2732
for i , f := range cgoLDFLAGS {
2717
2733
flags [i ] = strconv .Quote (f )
2718
2734
}
2719
- cgoenv = [] string { "CGO_LDFLAGS=" + strings .Join (flags , " " )}
2735
+ cgoenv = append ( cgoenv , "CGO_LDFLAGS=" + strings .Join (flags , " " ))
2720
2736
}
2721
2737
2722
2738
if cfg .BuildToolchainName == "gccgo" {
@@ -2947,7 +2963,7 @@ func (b *Builder) dynimport(a *Action, p *load.Package, objdir, importGo, cgoExe
2947
2963
if p .Standard && p .ImportPath == "runtime/cgo" {
2948
2964
cgoflags = []string {"-dynlinker" } // record path to dynamic linker
2949
2965
}
2950
- return b .run (a , base .Cwd , p .ImportPath , b .cCompilerEnv (), cfg .BuildToolexec , cgoExe , "-dynpackage" , p .Name , "-dynimport" , dynobj , "-dynout" , importGo , cgoflags )
2966
+ return b .run (a , base .Cwd , p .ImportPath , b .cgoEnv (), cfg .BuildToolexec , cgoExe , "-dynpackage" , p .Name , "-dynimport" , dynobj , "-dynout" , importGo , cgoflags )
2951
2967
}
2952
2968
2953
2969
// Run SWIG on all SWIG input files.
0 commit comments