diff --git a/compileopts/config.go b/compileopts/config.go index 197253e4..cbd0bd1d 100644 --- a/compileopts/config.go +++ b/compileopts/config.go @@ -65,6 +65,12 @@ func (c *Config) BuildTags() []string { return tags } +// CgoEnabled returns true if (and only if) CGo is enabled. It is true by +// default and false if CGO_ENABLED is set to "0". +func (c *Config) CgoEnabled() bool { + return goenv.Get("CGO_ENABLED") == "1" +} + // GC returns the garbage collection strategy in use on this platform. Valid // values are "none", "leaking", and "conservative". func (c *Config) GC() string { diff --git a/compiler/compiler.go b/compiler/compiler.go index 4010a8bc..1217d3d3 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -193,7 +193,7 @@ func (c *Compiler) Compile(mainPath string) []error { GOOS: c.GOOS(), GOROOT: goenv.Get("GOROOT"), GOPATH: goenv.Get("GOPATH"), - CgoEnabled: true, + CgoEnabled: c.CgoEnabled(), UseAllFiles: false, Compiler: "gc", // must be one of the recognized compilers BuildTags: c.BuildTags(), @@ -203,7 +203,7 @@ func (c *Compiler) Compile(mainPath string) []error { GOOS: c.GOOS(), GOROOT: goenv.Get("TINYGOROOT"), GOPATH: overlayGopath, - CgoEnabled: true, + CgoEnabled: c.CgoEnabled(), UseAllFiles: false, Compiler: "gc", // must be one of the recognized compilers BuildTags: c.BuildTags(), diff --git a/goenv/goenv.go b/goenv/goenv.go index c02d35df..fb5e353d 100644 --- a/goenv/goenv.go +++ b/goenv/goenv.go @@ -18,6 +18,7 @@ var Keys = []string{ "GOROOT", "GOPATH", "GOCACHE", + "CGO_ENABLED", "TINYGOROOT", } @@ -57,6 +58,13 @@ func Get(name string) string { panic("could not find cache dir: " + err.Error()) } return filepath.Join(dir, "tinygo") + case "CGO_ENABLED": + val := os.Getenv("CGO_ENABLED") + if val == "1" || val == "0" { + return val + } + // Default to enabling CGo. + return "1" case "TINYGOROOT": return sourceDir() default: