]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: disable cgo by default if DefaultCC is absolute and doesn't exist
authorBryan C. Mills <bcmills@google.com>
Wed, 16 Nov 2022 22:27:06 +0000 (17:27 -0500)
committerGopher Robot <gobot@golang.org>
Thu, 17 Nov 2022 02:01:30 +0000 (02:01 +0000)
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>
src/cmd/go/internal/cfg/cfg.go
src/cmd/go/scriptconds_test.go
src/cmd/go/testdata/script/README
src/cmd/go/testdata/script/autocgo.txt

index 3257140515030b593b5b1f0e57c1dd724ded9a72..f71fcdaeb99ade0c2971a36bf557ae346d91ebd1 100644 (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
                                }
                        }
index 6eb3e0979c629e6773080f1e1e055d3e8fc7b1a1..2717dbb4ae53dfa14acef7b19e9c6ecc9a61f7e0 100644 (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 == "" {
index 58c9170d5d7a26db5209ab89cffa84302d3086ff..7b747994c62adeaf7eba3dfeec554cadf1ce34b4 100644 (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:*]
index 522eaf46e7c0b4849d449528a4190aeb30623120..586c80251da3196433bcb4adce706b59cbd54ec6 100644 (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