From c9389a5849d65b74287cd2746a94748d6d64cb44 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Thu, 9 Mar 2023 16:17:30 -0500 Subject: [PATCH] cmd/link: use only the configured C compiler in TestCGOLTO The test had been assuming that any 'gcc' or 'clang' command found in $PATH could be used to compile cgo dependencies for the target GOARCH and GOOS. That assumption does not hold in general: for example, the GOARCH/GOOS configuration may be cross-compiling, which will cause the test to fail if the native 'gcc' and/or 'clang' is not configured for the target architecture. Instead, leave the 'CC' variable unset and assume only that the user has configured it appropriate to the environment in which they are running the test. For #58829. Change-Id: I9a1269ae3e0b4af281702114dabba844953f74bd Reviewed-on: https://go-review.googlesource.com/c/go/+/475155 TryBot-Result: Gopher Robot Run-TryBot: Bryan Mills Auto-Submit: Bryan Mills Reviewed-by: Cherry Mui --- src/cmd/link/cgo_test.go | 41 +++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/cmd/link/cgo_test.go b/src/cmd/link/cgo_test.go index 4393c3fa80..52db70e1ad 100644 --- a/src/cmd/link/cgo_test.go +++ b/src/cmd/link/cgo_test.go @@ -6,11 +6,10 @@ package main import ( "bytes" - "fmt" "internal/testenv" "os" - "os/exec" "path/filepath" + "strconv" "testing" ) @@ -21,12 +20,26 @@ func TestCGOLTO(t *testing.T) { t.Parallel() - for _, cc := range []string{"gcc", "clang"} { - for test := 0; test < 2; test++ { - t.Run(fmt.Sprintf("%s-%d", cc, test), func(t *testing.T) { - testCGOLTO(t, cc, test) - }) + goEnv := func(arg string) string { + cmd := testenv.Command(t, testenv.GoToolPath(t), "env", arg) + cmd.Stderr = new(bytes.Buffer) + + line, err := cmd.Output() + if err != nil { + t.Fatalf("%v: %v\n%s", cmd, err, cmd.Stderr) } + out := string(bytes.TrimSpace(line)) + t.Logf("%v: %q", cmd, out) + return out + } + + cc := goEnv("CC") + cgoCflags := goEnv("CGO_CFLAGS") + + for test := 0; test < 2; test++ { + t.Run(strconv.Itoa(test), func(t *testing.T) { + testCGOLTO(t, cc, cgoCflags, test) + }) } } @@ -79,13 +92,9 @@ func main() { } ` -func testCGOLTO(t *testing.T, cc string, test int) { +func testCGOLTO(t *testing.T, cc, cgoCflags string, test int) { t.Parallel() - if _, err := exec.LookPath(cc); err != nil { - t.Skipf("no %s compiler", cc) - } - dir := t.TempDir() writeTempFile := func(name, contents string) { @@ -108,12 +117,10 @@ func testCGOLTO(t *testing.T, cc string, test int) { cmd := testenv.Command(t, testenv.GoToolPath(t), "build") cmd.Dir = dir - cmd.Env = append(os.Environ(), - "CC="+cc, - "CGO_CFLAGS=-flto", - ) + cgoCflags += " -flto" + cmd.Env = append(cmd.Environ(), "CGO_CFLAGS="+cgoCflags) - t.Log("go build") + t.Logf("CGO_CFLAGS=%q %v", cgoCflags, cmd) out, err := cmd.CombinedOutput() t.Logf("%s", out)