1
0
mirror of https://github.com/golang/go synced 2024-11-17 05:54:46 -07:00

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>
This commit is contained in:
Bryan C. Mills 2022-11-16 17:27:06 -05:00 committed by Gopher Robot
parent b7662047ae
commit fdd8f0219d
4 changed files with 29 additions and 3 deletions

View File

@ -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
}
}

View File

@ -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 == "" {

View File

@ -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:*]

View File

@ -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