Commit 0a82ed5d authored by Elias Naur's avatar Elias Naur

misc/cgo/testcarchive: re-enable c-archive test on iOS and Android

The c-archive test were recently converted from shell script to Go.
Unfortunately, it also lost the ability to target iOS and Android
that lack C compilers and require exec wrappers.

Compile the c-archive test for the host and run it with the target
GOOS/GOARCH environment. Change the test to rely on go env GOOS
and go env GOARCH instead of runtime.GOOS and runtime.GOARCH.

Fixes #8345

Change-Id: I290ace2f7e96b87c55d99492feb7d660140dcb32
Reviewed-on: https://go-review.googlesource.com/21102Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 0104a31b
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"syscall" "syscall"
"testing" "testing"
...@@ -21,7 +20,7 @@ import ( ...@@ -21,7 +20,7 @@ import (
// Program to run. // Program to run.
var bin []string var bin []string
// C compiler wiht args (from $(go env CC) $(go env GOGCCFLAGS)). // C compiler with args (from $(go env CC) $(go env GOGCCFLAGS)).
var cc []string var cc []string
// An environment with GOPATH=$(pwd). // An environment with GOPATH=$(pwd).
...@@ -30,27 +29,21 @@ var gopathEnv []string ...@@ -30,27 +29,21 @@ var gopathEnv []string
// ".exe" on Windows. // ".exe" on Windows.
var exeSuffix string var exeSuffix string
var GOOS, GOARCH string
func init() { func init() {
bin = []string{"./testp"} bin = []string{"./testp"}
execScript := "go_" + runtime.GOOS + "_" + runtime.GOARCH + "_exec" GOOS = goEnv("GOOS")
GOARCH = goEnv("GOARCH")
execScript := "go_" + GOOS + "_" + GOARCH + "_exec"
if executor, err := exec.LookPath(execScript); err == nil { if executor, err := exec.LookPath(execScript); err == nil {
bin = []string{executor, "./testp"} bin = []string{executor, "./testp"}
} }
out, err := exec.Command("go", "env", "CC").Output() ccOut := goEnv("CC")
if err != nil { cc = []string{string(ccOut)}
fmt.Fprintf(os.Stderr, "go env CC failed:\n%s", err)
fmt.Fprintf(os.Stderr, "%s", err.(*exec.ExitError).Stderr)
os.Exit(2)
}
cc = []string{strings.TrimSpace(string(out))}
out, err = exec.Command("go", "env", "GOGCCFLAGS").Output() out := goEnv("GOGCCFLAGS")
if err != nil {
fmt.Fprintf(os.Stderr, "go env GOGCCFLAGS failed:\n%s", err)
fmt.Fprintf(os.Stderr, "%s", err.(*exec.ExitError).Stderr)
os.Exit(2)
}
quote := '\000' quote := '\000'
start := 0 start := 0
lastSpace := true lastSpace := true
...@@ -83,14 +76,14 @@ func init() { ...@@ -83,14 +76,14 @@ func init() {
cc = append(cc, s[start:]) cc = append(cc, s[start:])
} }
if runtime.GOOS == "darwin" { if GOOS == "darwin" {
cc = append(cc, "-Wl,-no_pie") cc = append(cc, "-Wl,-no_pie")
// For Darwin/ARM. // For Darwin/ARM.
// TODO(crawshaw): can we do better? // TODO(crawshaw): can we do better?
cc = append(cc, []string{"-framework", "CoreFoundation", "-framework", "Foundation"}...) cc = append(cc, []string{"-framework", "CoreFoundation", "-framework", "Foundation"}...)
} }
cc = append(cc, "-I", filepath.Join("pkg", runtime.GOOS+"_"+runtime.GOARCH)) cc = append(cc, "-I", filepath.Join("pkg", GOOS+"_"+GOARCH))
// Build an environment with GOPATH=$(pwd) // Build an environment with GOPATH=$(pwd)
env := os.Environ() env := os.Environ()
...@@ -108,11 +101,21 @@ func init() { ...@@ -108,11 +101,21 @@ func init() {
n = append(n, "GOPATH="+dir) n = append(n, "GOPATH="+dir)
gopathEnv = n gopathEnv = n
if runtime.GOOS == "windows" { if GOOS == "windows" {
exeSuffix = ".exe" exeSuffix = ".exe"
} }
} }
func goEnv(key string) string {
out, err := exec.Command("go", "env", key).Output()
if err != nil {
fmt.Fprintf(os.Stderr, "go env %s failed:\n%s", key, err)
fmt.Fprintf(os.Stderr, "%s", err.(*exec.ExitError).Stderr)
os.Exit(2)
}
return strings.TrimSpace(string(out))
}
func TestInstall(t *testing.T) { func TestInstall(t *testing.T) {
defer func() { defer func() {
os.Remove("libgo.a") os.Remove("libgo.a")
...@@ -128,7 +131,7 @@ func TestInstall(t *testing.T) { ...@@ -128,7 +131,7 @@ func TestInstall(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
ccArgs := append(cc, "-o", "testp"+exeSuffix, "main.c", filepath.Join("pkg", runtime.GOOS+"_"+runtime.GOARCH, "libgo.a")) ccArgs := append(cc, "-o", "testp"+exeSuffix, "main.c", filepath.Join("pkg", GOOS+"_"+GOARCH, "libgo.a"))
if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil { if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
t.Logf("%s", out) t.Logf("%s", out)
t.Fatal(err) t.Fatal(err)
...@@ -187,11 +190,11 @@ func TestInstall(t *testing.T) { ...@@ -187,11 +190,11 @@ func TestInstall(t *testing.T) {
} }
func TestEarlySignalHandler(t *testing.T) { func TestEarlySignalHandler(t *testing.T) {
switch runtime.GOOS { switch GOOS {
case "darwin": case "darwin":
switch runtime.GOARCH { switch GOARCH {
case "arm", "arm64": case "arm", "arm64":
t.Skipf("skipping on %s/%s; see https://golang.org/issue/13701", runtime.GOOS, runtime.GOARCH) t.Skipf("skipping on %s/%s; see https://golang.org/issue/13701", GOOS, GOARCH)
} }
case "windows": case "windows":
t.Skip("skipping signal test on Windows") t.Skip("skipping signal test on Windows")
...@@ -345,7 +348,7 @@ func TestSignalForwardingExternal(t *testing.T) { ...@@ -345,7 +348,7 @@ func TestSignalForwardingExternal(t *testing.T) {
} }
func TestOsSignal(t *testing.T) { func TestOsSignal(t *testing.T) {
switch runtime.GOOS { switch GOOS {
case "windows": case "windows":
t.Skip("skipping signal test on Windows") t.Skip("skipping signal test on Windows")
} }
...@@ -377,7 +380,7 @@ func TestOsSignal(t *testing.T) { ...@@ -377,7 +380,7 @@ func TestOsSignal(t *testing.T) {
} }
func TestSigaltstack(t *testing.T) { func TestSigaltstack(t *testing.T) {
switch runtime.GOOS { switch GOOS {
case "windows": case "windows":
t.Skip("skipping signal test on Windows") t.Skip("skipping signal test on Windows")
} }
...@@ -417,7 +420,7 @@ echo "testar" > PWD/testar.ran ...@@ -417,7 +420,7 @@ echo "testar" > PWD/testar.ran
` `
func TestExtar(t *testing.T) { func TestExtar(t *testing.T) {
switch runtime.GOOS { switch GOOS {
case "windows": case "windows":
t.Skip("skipping signal test on Windows") t.Skip("skipping signal test on Windows")
} }
......
...@@ -496,10 +496,8 @@ func (t *tester) registerTests() { ...@@ -496,10 +496,8 @@ func (t *tester) registerTests() {
}, },
}) })
} }
if t.supportedBuildmode("c-archive") && t.goos != "android" && !t.iOS() { if t.supportedBuildmode("c-archive") {
// TODO(elias.naur): reenable on android and iOS t.registerHostTest("testcarchive", "misc/cgo/testcarchive", "carchive_test.go")
// golang.org/issue/8345
t.registerTest("testcarchive", "../misc/cgo/testcarchive", "go", "test", "carchive_test.go")
} }
if t.supportedBuildmode("c-shared") { if t.supportedBuildmode("c-shared") {
t.registerTest("testcshared", "../misc/cgo/testcshared", "./test.bash") t.registerTest("testcshared", "../misc/cgo/testcshared", "./test.bash")
...@@ -693,6 +691,27 @@ func (t *tester) supportedBuildmode(mode string) bool { ...@@ -693,6 +691,27 @@ func (t *tester) supportedBuildmode(mode string) bool {
} }
} }
func (t *tester) registerHostTest(name, dirBanner, pkg string) {
t.tests = append(t.tests, distTest{
name: name,
heading: dirBanner,
fn: func(dt *distTest) error {
return t.runHostTest(dirBanner, pkg)
},
})
}
func (t *tester) runHostTest(dirBanner, pkg string) error {
env := mergeEnvLists([]string{"GOARCH=" + t.gohostarch, "GOOS=" + t.gohostos}, os.Environ())
defer os.Remove(filepath.Join(t.goroot, dirBanner, "test.test"))
cmd := t.dirCmd(dirBanner, "go", "test", t.tags(), "-c", "-o", "test.test", pkg)
cmd.Env = env
if err := cmd.Run(); err != nil {
return err
}
return t.dirCmd(dirBanner, "./test.test").Run()
}
func (t *tester) cgoTest(dt *distTest) error { func (t *tester) cgoTest(dt *distTest) error {
env := mergeEnvLists([]string{"GOTRACEBACK=2"}, os.Environ()) env := mergeEnvLists([]string{"GOTRACEBACK=2"}, os.Environ())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment