Commit 202ef487 authored by Alex Brainman's avatar Alex Brainman

cmd/dist: convert testso test into Go

I would like to re-apply reverted http://golang.org/cl/8523.
Reverted tests still fail in some environments (see issue #10360).
It is easier to run tests selectively when in Go.
This CL prepares for the changes.

Updates #10360

Change-Id: Iefeb1d71cb3d1cfa653a6ccd9f6e35686c0c5b24
Reviewed-on: https://go-review.googlesource.com/10608Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 117506aa
#!/usr/bin/env bash
# Copyright 2011 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
set -e
if [ "$(uname -m)" == ppc64 -o "$(uname -m)" == ppc64le ]; then
# External linking not implemented on ppc64
echo "skipping test on ppc64 (issue #8912)"
exit
fi
args=
dyld_envvar=LD_LIBRARY_PATH
ext=so
if [ "$(uname)" == "Darwin" ]; then
args="-undefined suppress -flat_namespace"
dyld_envvar=DYLD_LIBRARY_PATH
ext=dylib
fi
dylib=libcgosotest.$ext
$(go env CC) $(go env GOGCCFLAGS) -shared $args -o $dylib cgoso_c.c
go build main.go
eval "$dyld_envvar"=. ./main
rm -rf $dylib main *.dSYM
:: Copyright 2013 The Go Authors. All rights reserved.
:: Use of this source code is governed by a BSD-style
:: license that can be found in the LICENSE file.
@echo off
gcc -c cgoso_c.c
gcc -shared -o libcgosotest.dll cgoso_c.o
if not exist libcgosotest.dll goto fail
go build main.go
if not exist main.exe goto fail
main.exe
goto :end
:fail
echo FAIL
:end
del /F cgoso_c.o libcgosotest.dll main.exe 2>NUL
......@@ -410,14 +410,12 @@ func (t *tester) registerTests() {
t.registerTest("testgodefs", "../misc/cgo/testgodefs", "./test.bash")
}
if t.cgoEnabled {
if t.gohostos == "windows" {
if t.cgoTestSOSupported() {
t.tests = append(t.tests, distTest{
name: "testso",
heading: "../misc/cgo/testso",
fn: t.cgoTestSOWindows,
fn: t.cgoTestSO,
})
} else if t.hasBash() && t.goos != "android" && !t.iOS() {
t.registerTest("testso", "../misc/cgo/testso", "./test.bash")
}
if t.supportedBuildmode("c-archive") {
t.registerTest("testcarchive", "../misc/cgo/testcarchive", "./test.bash")
......@@ -714,21 +712,67 @@ func (t *tester) cgoTest() error {
return nil
}
func (t *tester) cgoTestSOWindows() error {
cmd := t.dirCmd("misc/cgo/testso", `.\test`)
var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = &buf
err := cmd.Run()
s := buf.String()
fmt.Println(s)
func (t *tester) cgoTestSOSupported() bool {
if t.goos == "android" || t.iOS() {
// No exec facility on Android or iOS.
return false
}
if t.goos == "ppc64le" || t.goos == "ppc64" {
// External linking not implemented on ppc64 (issue #8912).
return false
}
return true
}
func (t *tester) cgoTestSO() error {
dir := filepath.Join(t.goroot, "misc/cgo/testso")
// build shared object
output, err := exec.Command("go", "env", "CC").Output()
if err != nil {
return fmt.Errorf("Error running go env CC: %v", err)
}
cc := strings.TrimSuffix(string(output), "\n")
if cc == "" {
return errors.New("CC environment variable (go env CC) cannot be empty")
}
output, err = exec.Command("go", "env", "GOGCCFLAGS").Output()
if err != nil {
return fmt.Errorf("Error running go env GOGCCFLAGS: %v", err)
}
gogccflags := strings.Split(strings.TrimSuffix(string(output), "\n"), " ")
ext := "so"
args := append(gogccflags, "-shared")
switch t.goos {
case "darwin":
ext = "dylib"
args = append(args, "-undefined", "suppress", "-flat_namespace")
case "windows":
ext = "dll"
}
sofname := "libcgosotest." + ext
args = append(args, "-o", sofname, "cgoso_c.c")
if err := t.dirCmd(dir, cc, args...).Run(); err != nil {
return err
}
if strings.Contains(s, "FAIL") {
return errors.New("test failed")
defer os.Remove(filepath.Join(dir, sofname))
if err := t.dirCmd(dir, "go", "build", "-o", "main.exe", "main.go").Run(); err != nil {
return err
}
return nil
defer os.Remove(filepath.Join(dir, "main.exe"))
cmd := t.dirCmd(dir, "./main.exe")
if t.goos != "windows" {
s := "LD_LIBRARY_PATH"
if t.goos == "darwin" {
s = "DYLD_LIBRARY_PATH"
}
cmd.Env = mergeEnvLists([]string{s + "=."}, os.Environ())
}
return cmd.Run()
}
func (t *tester) hasBash() bool {
......
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