Skip to content

Commit 52d9e41

Browse files
rscgopherbot
authored andcommitted
cmd/go: automatically disable cgo on systems with no C compiler
The documentation for cgo has always said: > The cgo tool is enabled by default for native builds > on systems where it is expected to work. Following the spirit of that rule, this CL disables cgo by default on systems where $CC is unset and the default C compiler (clang or gcc) is not found in $PATH. This CL makes builds of Go code on systems with no C compiler installed automatically fall back to non-cgo mode. For example, if building a Go program using package net in a stripped down Linux container, that build will now run with cgo disabled, instead of attempting the build with cgo enabled and only succeeding if the right pre-compiled .a files happen to be loaded into the container. This CL makes it safe to drop the pre-compiled .a files from the Go distribution. Systems that don't have a C compiler will simply disable cgo when building new .a files for that system. In general keeping the pre-compiled .a files working in cgo mode on systems without C compilers has had only mixed success due to the precise build cache. Today we have had to disable various checks in the precise build cache so that distributed .a files look up-to-date even if the current machine's C compiler is a different version than the one used when packaging the distribution. Each time we improve precision we have a decent chance of re-invalidating the files. This CL, combined with dropping the .a files entirely, will let us re-enable those checks and ensure that the .a files used in a build actually match the C compiler being used. On macOS, the distributed .a files for cgo-dependent packages have been stale (not actually used by the go command) since the release of Go 1.14 in February 2020, due to CL 216304 setting a CGO_CFLAGS environment variable that won't match the default setting on users machines. (To keep the distributed .a files working, that CL should have instead changed the default in the go command.) The effect is that for the past six Go releases (!!!), the go command has been unable to build basic programs like src/net/http/triv.go on macOS without either disabling cgo or installing Xcode's C compiler. This CL fixes that problem by disabling cgo when there's no C compiler. Now it will once again be possible to build basic programs with just a Go toolchain installed. In the past, disabling cgo on macOS would have resulted in subpar implementations of crypto/x509, net, and os/user, but as of CL 449316 those packages have all been updated to use libc calls directly, so they now provide the same implementation whether or not cgo is enabled. In the past, disabling cgo on macOS would also have made the race detector unusable, but CL 451055 makes the race detector work on macOS even when cgo is disabled. On Windows, none of the standard library uses cgo today, so all the necessary .a files can be rebuilt without a C toolchain, and there is no loss of functionality in the standard library when cgo is disabled. After this CL, the race detector won't work on Windows without a C toolchain installed, but that turns out to be true already: when linking race-enabled programs, even if the Go linker does not invoke the host linker, it still attempts to read some of the host C toolchain's .a files to resolve undefined references. On Unix systems, disabling cgo when a C compiler is not present will mean that builds get the pure Go net resolver, which is used by default even in cgo builds when /etc/resolv.conf is simple enough. It will also mean they get the pure os/user code, which reads /etc/passwd and /etc/group instead of using shared libraries, and therefore it may miss out on other sources of user information such as LDAP. The race detector also will not work without a C compiler. This would be dire except that nearly all Unix systems have a C compiler installed by default, and on those that don't it is trivial to add one. In particular, the vast majority of Go developers running on Linux and other Unix systems will already have a C compiler and will be unaffected. Change-Id: I491e8a022fe3a64022e9dc593850d483a0d353fa Reviewed-on: https://go-review.googlesource.com/c/go/+/450739 Reviewed-by: Bryan Mills <[email protected]> Auto-Submit: Russ Cox <[email protected]> Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 5bf9aeb commit 52d9e41

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/cmd/cgo/doc.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ specified by a -I flag), then "#include <foo/bar.h>" will always find the
119119
local version in preference to any other version.
120120
121121
The cgo tool is enabled by default for native builds on systems where
122-
it is expected to work. It is disabled by default when
123-
cross-compiling. You can control this by setting the CGO_ENABLED
122+
it is expected to work. It is disabled by default when cross-compiling
123+
as well as when the CC environment variable is unset and the default
124+
C compiler (typically gcc or clang) cannot be found on the system PATH.
125+
You can override the default by setting the CGO_ENABLED
124126
environment variable when running the go tool: set it to 1 to enable
125127
the use of cgo, and to 0 to disable it. The go tool will set the
126128
build constraint "cgo" if cgo is enabled. The special import "C"

src/cmd/go/internal/cfg/cfg.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"internal/cfg"
1515
"io"
1616
"os"
17+
"os/exec"
1718
"path/filepath"
1819
"runtime"
1920
"strings"
@@ -148,7 +149,21 @@ func defaultContext() build.Context {
148149
// go/build.Default.GOOS/GOARCH == runtime.GOOS/GOARCH.
149150
// So ctxt.CgoEnabled (== go/build.Default.CgoEnabled) is correct
150151
// as is and can be left unmodified.
151-
// Nothing to do here.
152+
//
153+
// All that said, starting in Go 1.20 we layer one more rule
154+
// on top of the go/build decision: if CC is unset and
155+
// the default C compiler we'd look for is not in the PATH,
156+
// we automatically default cgo to off.
157+
// This makes go builds work automatically on systems
158+
// without a C compiler installed.
159+
if ctxt.CgoEnabled {
160+
if os.Getenv("CC") == "" {
161+
cc := DefaultCC(ctxt.GOOS, ctxt.GOARCH)
162+
if _, err := exec.LookPath(cc); err != nil {
163+
ctxt.CgoEnabled = false
164+
}
165+
}
166+
}
152167
}
153168

154169
ctxt.OpenFile = func(path string) (io.ReadCloser, error) {
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Test automatic setting of CGO_ENABLED based on $CC and what's in $PATH.
2+
3+
[!cgo] skip
4+
[cross] skip
5+
6+
# Assume we're on a system that can enable cgo normally.
7+
env CGO_ENABLED=
8+
go env CGO_ENABLED
9+
stdout 1
10+
11+
# Clearing CC and removing everything but Go from the PATH should disable cgo: no C compiler anymore.
12+
env CC=
13+
env PATH=$GOROOT/bin
14+
go env CGO_ENABLED
15+
stdout 0
16+
17+
# Setting CC should re-enable cgo.
18+
env CC=cc
19+
go env CGO_ENABLED
20+
stdout 1
21+
22+
# So should setting CGO_ENABLED.
23+
env CC=
24+
env CGO_ENABLED=1
25+
go env CGO_ENABLED
26+
stdout 1

0 commit comments

Comments
 (0)