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>
diff --git a/misc/cgo/testso/test.bash b/misc/cgo/testso/test.bash
deleted file mode 100755
index dce9efc..0000000
--- a/misc/cgo/testso/test.bash
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/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
diff --git a/misc/cgo/testso/test.bat b/misc/cgo/testso/test.bat
deleted file mode 100644
index 7bbabea..0000000
--- a/misc/cgo/testso/test.bat
+++ /dev/null
@@ -1,18 +0,0 @@
-:: 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
diff --git a/src/cmd/dist/test.go b/src/cmd/dist/test.go
index a6a8e01..8246dbb 100644
--- a/src/cmd/dist/test.go
+++ b/src/cmd/dist/test.go
@@ -410,14 +410,12 @@
 		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 @@
 	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 {