]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: use only the configured C compiler in TestCGOLTO
authorBryan C. Mills <bcmills@google.com>
Thu, 9 Mar 2023 21:17:30 +0000 (16:17 -0500)
committerGopher Robot <gobot@golang.org>
Thu, 9 Mar 2023 22:13:02 +0000 (22:13 +0000)
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 <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/cmd/link/cgo_test.go

index 4393c3fa8019a60b75940cf71c857f571a98185a..52db70e1ad8d4c16ce9c21c4239236e4caa9baf2 100644 (file)
@@ -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)