Skip to content

Commit 202ef48

Browse files
committed
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/10608 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 117506a commit 202ef48

File tree

3 files changed

+59
-61
lines changed

3 files changed

+59
-61
lines changed

misc/cgo/testso/test.bash

Lines changed: 0 additions & 28 deletions
This file was deleted.

misc/cgo/testso/test.bat

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/cmd/dist/test.go

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,12 @@ func (t *tester) registerTests() {
410410
t.registerTest("testgodefs", "../misc/cgo/testgodefs", "./test.bash")
411411
}
412412
if t.cgoEnabled {
413-
if t.gohostos == "windows" {
413+
if t.cgoTestSOSupported() {
414414
t.tests = append(t.tests, distTest{
415415
name: "testso",
416416
heading: "../misc/cgo/testso",
417-
fn: t.cgoTestSOWindows,
417+
fn: t.cgoTestSO,
418418
})
419-
} else if t.hasBash() && t.goos != "android" && !t.iOS() {
420-
t.registerTest("testso", "../misc/cgo/testso", "./test.bash")
421419
}
422420
if t.supportedBuildmode("c-archive") {
423421
t.registerTest("testcarchive", "../misc/cgo/testcarchive", "./test.bash")
@@ -714,21 +712,67 @@ func (t *tester) cgoTest() error {
714712
return nil
715713
}
716714

717-
func (t *tester) cgoTestSOWindows() error {
718-
cmd := t.dirCmd("misc/cgo/testso", `.\test`)
719-
var buf bytes.Buffer
720-
cmd.Stdout = &buf
721-
cmd.Stderr = &buf
722-
err := cmd.Run()
723-
s := buf.String()
724-
fmt.Println(s)
715+
func (t *tester) cgoTestSOSupported() bool {
716+
if t.goos == "android" || t.iOS() {
717+
// No exec facility on Android or iOS.
718+
return false
719+
}
720+
if t.goos == "ppc64le" || t.goos == "ppc64" {
721+
// External linking not implemented on ppc64 (issue #8912).
722+
return false
723+
}
724+
return true
725+
}
726+
727+
func (t *tester) cgoTestSO() error {
728+
dir := filepath.Join(t.goroot, "misc/cgo/testso")
729+
730+
// build shared object
731+
output, err := exec.Command("go", "env", "CC").Output()
732+
if err != nil {
733+
return fmt.Errorf("Error running go env CC: %v", err)
734+
}
735+
cc := strings.TrimSuffix(string(output), "\n")
736+
if cc == "" {
737+
return errors.New("CC environment variable (go env CC) cannot be empty")
738+
}
739+
output, err = exec.Command("go", "env", "GOGCCFLAGS").Output()
725740
if err != nil {
741+
return fmt.Errorf("Error running go env GOGCCFLAGS: %v", err)
742+
}
743+
gogccflags := strings.Split(strings.TrimSuffix(string(output), "\n"), " ")
744+
745+
ext := "so"
746+
args := append(gogccflags, "-shared")
747+
switch t.goos {
748+
case "darwin":
749+
ext = "dylib"
750+
args = append(args, "-undefined", "suppress", "-flat_namespace")
751+
case "windows":
752+
ext = "dll"
753+
}
754+
sofname := "libcgosotest." + ext
755+
args = append(args, "-o", sofname, "cgoso_c.c")
756+
757+
if err := t.dirCmd(dir, cc, args...).Run(); err != nil {
726758
return err
727759
}
728-
if strings.Contains(s, "FAIL") {
729-
return errors.New("test failed")
760+
defer os.Remove(filepath.Join(dir, sofname))
761+
762+
if err := t.dirCmd(dir, "go", "build", "-o", "main.exe", "main.go").Run(); err != nil {
763+
return err
730764
}
731-
return nil
765+
defer os.Remove(filepath.Join(dir, "main.exe"))
766+
767+
cmd := t.dirCmd(dir, "./main.exe")
768+
if t.goos != "windows" {
769+
s := "LD_LIBRARY_PATH"
770+
if t.goos == "darwin" {
771+
s = "DYLD_LIBRARY_PATH"
772+
}
773+
cmd.Env = mergeEnvLists([]string{s + "=."}, os.Environ())
774+
}
775+
return cmd.Run()
732776
}
733777

734778
func (t *tester) hasBash() bool {

0 commit comments

Comments
 (0)