aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2022-11-16 17:27:06 -0500
committerGopher Robot <gobot@golang.org>2022-11-17 02:01:30 +0000
commitfdd8f0219d055f16647c8dfcd11e79cce222d200 (patch)
tree7f371f64dcd9b1253fd67897aa9a69172ab065b1 /src
parentb7662047aedc5f2c512911eb59d514ce75b16e18 (diff)
downloadgo-fdd8f0219d055f16647c8dfcd11e79cce222d200.tar.xz
cmd/go: disable cgo by default if DefaultCC is absolute and doesn't exist
Also fix the autocgo test from CL 450739 when DefaultCC is absolute. Change-Id: Ie282a42a1334660225e88680b63b18b7c1ecba2c Reviewed-on: https://go-review.googlesource.com/c/go/+/451219 Auto-Submit: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/go/internal/cfg/cfg.go9
-rw-r--r--src/cmd/go/scriptconds_test.go14
-rw-r--r--src/cmd/go/testdata/script/README2
-rw-r--r--src/cmd/go/testdata/script/autocgo.txt7
4 files changed, 29 insertions, 3 deletions
diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go
index 3257140515..f71fcdaeb9 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 6eb3e0979c..2717dbb4ae 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=<suffix>", 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 58c9170d5d..7b747994c6 100644
--- a/src/cmd/go/testdata/script/README
+++ b/src/cmd/go/testdata/script/README
@@ -372,6 +372,8 @@ The available conditions are:
GOEXPERIMENT <suffix> is enabled
[GOOS:*]
runtime.GOOS == <suffix>
+[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 522eaf46e7..586c80251d 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