diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index 32571405150..f71fcdaeb99 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -159,7 +159,14 @@ func defaultContext() build.Context { if ctxt.CgoEnabled { if os.Getenv("CC") == "" { cc := DefaultCC(ctxt.GOOS, ctxt.GOARCH) - if _, err := exec.LookPath(cc); err != nil { + if filepath.IsAbs(cc) { + if _, err := os.Stat(cc); os.IsNotExist(err) { + // The default CC is an absolute path that doesn't exist. + // (Perhaps make.bash was run on a system with a C compiler + // installed, and the current system doesn't have it there.) + ctxt.CgoEnabled = false + } + } else if _, err := exec.LookPath(cc); err != nil { ctxt.CgoEnabled = false } } diff --git a/src/cmd/go/scriptconds_test.go b/src/cmd/go/scriptconds_test.go index 6eb3e0979c6..2717dbb4ae5 100644 --- a/src/cmd/go/scriptconds_test.go +++ b/src/cmd/go/scriptconds_test.go @@ -5,6 +5,7 @@ package main_test import ( + "cmd/go/internal/cfg" "cmd/go/internal/script" "cmd/go/internal/script/scripttest" "errors" @@ -34,6 +35,7 @@ func scriptConditions() map[string]script.Cond { return script.OnceCondition(summary, func() (bool, error) { return f(), nil }) } + add("abscc", script.Condition("default $CC path is absolute and exists", defaultCCIsAbsolute)) add("asan", sysCondition("-asan", platform.ASanSupported, true)) add("buildmode", script.PrefixCondition("go supports -buildmode=", hasBuildmode)) add("case-sensitive", script.OnceCondition("$WORK filesystem is case-sensitive", isCaseSensitive)) @@ -55,6 +57,18 @@ func scriptConditions() map[string]script.Cond { return conds } +func defaultCCIsAbsolute(s *script.State) (bool, error) { + GOOS, _ := s.LookupEnv("GOOS") + GOARCH, _ := s.LookupEnv("GOARCH") + defaultCC := cfg.DefaultCC(GOOS, GOARCH) + if filepath.IsAbs(defaultCC) { + if _, err := os.Stat(defaultCC); err == nil { + return true, nil + } + } + return false, nil +} + func isMismatchedGoroot(s *script.State) (bool, error) { gorootFinal, _ := s.LookupEnv("GOROOT_FINAL") if gorootFinal == "" { diff --git a/src/cmd/go/testdata/script/README b/src/cmd/go/testdata/script/README index 58c9170d5d7..7b747994c62 100644 --- a/src/cmd/go/testdata/script/README +++ b/src/cmd/go/testdata/script/README @@ -372,6 +372,8 @@ The available conditions are: GOEXPERIMENT is enabled [GOOS:*] runtime.GOOS == +[abscc] + default $CC path is absolute and exists [asan] GOOS/GOARCH supports -asan [buildmode:*] diff --git a/src/cmd/go/testdata/script/autocgo.txt b/src/cmd/go/testdata/script/autocgo.txt index 522eaf46e7c..586c80251da 100644 --- a/src/cmd/go/testdata/script/autocgo.txt +++ b/src/cmd/go/testdata/script/autocgo.txt @@ -8,11 +8,14 @@ env CGO_ENABLED= go env CGO_ENABLED stdout 1 -# Clearing CC and removing everything but Go from the PATH should disable cgo: no C compiler anymore. +# Clearing CC and removing everything but Go from the PATH should usually +# disable cgo: no C compiler anymore (unless the baked-in defaultCC is an +# absolute path and exists. env CC= env PATH=$GOROOT/bin go env CGO_ENABLED -stdout 0 +[!abscc] stdout 0 +[abscc] stdout 1 # Setting CC should re-enable cgo. env CC=cc