@@ -13,7 +13,6 @@ import (
13
13
"go/build"
14
14
"internal/buildcfg"
15
15
"internal/cfg"
16
- "internal/platform"
17
16
"io"
18
17
"os"
19
18
"path/filepath"
@@ -128,14 +127,16 @@ func defaultContext() build.Context {
128
127
ctxt .ToolTags = save
129
128
130
129
// The go/build rule for whether cgo is enabled is:
131
- // 1. If $CGO_ENABLED is set, respect it.
132
- // 2. Otherwise, if this is a cross-compile, disable cgo.
133
- // 3. Otherwise, use built-in default for GOOS/GOARCH.
130
+ // 1. If $CGO_ENABLED is set, respect it.
131
+ // 2. Otherwise, if this is a cross-compile, disable cgo.
132
+ // 3. Otherwise, use built-in default for GOOS/GOARCH.
133
+ //
134
134
// Recreate that logic here with the new GOOS/GOARCH setting.
135
- if v := Getenv ("CGO_ENABLED" ); v == "0" || v == "1" {
136
- ctxt .CgoEnabled = v [0 ] == '1'
137
- } else if ctxt .GOOS != runtime .GOOS || ctxt .GOARCH != runtime .GOARCH {
138
- ctxt .CgoEnabled = false
135
+ // We need to run steps 2 and 3 to determine what the default value
136
+ // of CgoEnabled would be for computing CGOChanged.
137
+ defaultCgoEnabled := ctxt .CgoEnabled
138
+ if ctxt .GOOS != runtime .GOOS || ctxt .GOARCH != runtime .GOARCH {
139
+ defaultCgoEnabled = false
139
140
} else {
140
141
// Use built-in default cgo setting for GOOS/GOARCH.
141
142
// Note that ctxt.GOOS/GOARCH are derived from the preference list
@@ -162,17 +163,16 @@ func defaultContext() build.Context {
162
163
if os .Getenv ("CC" ) == "" {
163
164
cc := DefaultCC (ctxt .GOOS , ctxt .GOARCH )
164
165
if _ , err := LookPath (cc ); err != nil {
165
- ctxt . CgoEnabled = false
166
+ defaultCgoEnabled = false
166
167
}
167
168
}
168
169
}
169
170
}
170
- CGOChanged = ctxt .CgoEnabled != func () bool {
171
- if runtime .GOARCH == ctxt .GOARCH && runtime .GOOS == ctxt .GOOS {
172
- return platform .CgoSupported (ctxt .GOOS , ctxt .GOARCH )
173
- }
174
- return false
175
- }()
171
+ ctxt .CgoEnabled = defaultCgoEnabled
172
+ if v := Getenv ("CGO_ENABLED" ); v == "0" || v == "1" {
173
+ ctxt .CgoEnabled = v [0 ] == '1'
174
+ }
175
+ CGOChanged = ctxt .CgoEnabled != defaultCgoEnabled
176
176
177
177
ctxt .OpenFile = func (path string ) (io.ReadCloser , error ) {
178
178
return fsys .Open (path )
0 commit comments