Skip to content

Commit d04b07f

Browse files
aykevldeadprogram
authored andcommitted
compileopts: always enable CGo
CGo is needed for the rp2040 and for macOS (GOOS=darwin), without it these targets just won't work. And there really isn't a benefit from disabling CGo: we don't need any external linkers for example. This avoids a somewhat common issue of people having CGO_ENABLED=0 somewhere in their environment and not understanding why things don't work. See for example: #3450
1 parent 7486277 commit d04b07f

File tree

3 files changed

+3
-16
lines changed

3 files changed

+3
-16
lines changed

compileopts/config.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,6 @@ func (c *Config) BuildTags() []string {
8787
return tags
8888
}
8989

90-
// CgoEnabled returns true if (and only if) CGo is enabled. It is true by
91-
// default and false if CGO_ENABLED is set to "0".
92-
func (c *Config) CgoEnabled() bool {
93-
return goenv.Get("CGO_ENABLED") == "1"
94-
}
95-
9690
// GC returns the garbage collection strategy in use on this platform. Valid
9791
// values are "none", "leaking", "conservative" and "precise".
9892
func (c *Config) GC() string {

goenv/goenv.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,8 @@ func Get(name string) string {
142142
}
143143
return filepath.Join(dir, "tinygo")
144144
case "CGO_ENABLED":
145-
val := os.Getenv("CGO_ENABLED")
146-
if val == "1" || val == "0" {
147-
return val
148-
}
149-
// Default to enabling CGo.
145+
// Always enable CGo. It is required by a number of targets, including
146+
// macOS and the rp2040.
150147
return "1"
151148
case "TINYGOROOT":
152149
return sourceDir()

loader/list.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,8 @@ func List(config *compileopts.Config, extraArgs, pkgs []string) (*exec.Cmd, erro
2222
args = append(args, "-tags", strings.Join(config.BuildTags(), " "))
2323
}
2424
args = append(args, pkgs...)
25-
cgoEnabled := "0"
26-
if config.CgoEnabled() {
27-
cgoEnabled = "1"
28-
}
2925
cmd := exec.Command(filepath.Join(goenv.Get("GOROOT"), "bin", "go"), args...)
30-
cmd.Env = append(os.Environ(), "GOROOT="+goroot, "GOOS="+config.GOOS(), "GOARCH="+config.GOARCH(), "CGO_ENABLED="+cgoEnabled)
26+
cmd.Env = append(os.Environ(), "GOROOT="+goroot, "GOOS="+config.GOOS(), "GOARCH="+config.GOARCH(), "CGO_ENABLED=1")
3127
if config.Options.Directory != "" {
3228
cmd.Dir = config.Options.Directory
3329
}

0 commit comments

Comments
 (0)