1
0
mirror of https://github.com/golang/go synced 2024-09-29 21:24:30 -06:00

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 <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
Bryan C. Mills 2023-03-09 16:17:30 -05:00 committed by Gopher Robot
parent 7dbd6de7d4
commit c9389a5849

View 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)