]> Cypherpunks repositories - gostls13.git/commitdiff
misc/cgo/testshared: add basic test for -buildmode=shared/-linkshared
authorMichael Hudson-Doyle <michael.hudson@canonical.com>
Mon, 27 Apr 2015 03:00:48 +0000 (15:00 +1200)
committerIan Lance Taylor <iant@golang.org>
Wed, 29 Apr 2015 23:47:47 +0000 (23:47 +0000)
Just a first basic test, I'll extend this to test more but want to get an
opinion on basic approach first.

Change-Id: Idab9ebd7d9960b000b81a01a1e53258bf4bce755
Reviewed-on: https://go-review.googlesource.com/9386
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>

misc/cgo/testshared/src/trivial/trivial.go [new file with mode: 0644]
misc/cgo/testshared/test.bash [new file with mode: 0755]
src/cmd/dist/test.go

diff --git a/misc/cgo/testshared/src/trivial/trivial.go b/misc/cgo/testshared/src/trivial/trivial.go
new file mode 100644 (file)
index 0000000..da29a2c
--- /dev/null
@@ -0,0 +1,4 @@
+package main
+
+func main() {
+}
diff --git a/misc/cgo/testshared/test.bash b/misc/cgo/testshared/test.bash
new file mode 100755 (executable)
index 0000000..0ab68b8
--- /dev/null
@@ -0,0 +1,64 @@
+#!/usr/bin/env bash
+# Copyright 2015 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.
+
+# Test that -buildmode=shared can produce a shared library and that
+# -linkshared can link against it to produce a working executable.
+
+set -eu
+
+export GOPATH="$(pwd)"
+
+die () {
+    echo $@
+    exit 1
+}
+
+# Because go install -buildmode=shared $standard_library_package always
+# installs into $GOROOT, here are some gymnastics to come up with a
+# unique installsuffix to use in this test that we can clean up
+# afterwards.
+rootdir="$(dirname $(go list -f '{{.Target}}' runtime))"
+template="${rootdir}_XXXXXXXX_dynlink"
+std_install_dir=$(mktemp -d "$template")
+
+cleanup () {
+    rm -rf $std_install_dir ./bin/ ./pkg/
+}
+trap cleanup EXIT
+
+mysuffix=$(echo $std_install_dir | sed -e 's/.*_\([^_]*\)_dynlink/\1/')
+
+# This is the smallest set of packages we can link into a shared
+# library. Check they are built into a library with the expected name.
+minpkgs="runtime runtime/cgo sync/atomic"
+soname=libruntime,runtime-cgo,sync-atomic.so
+
+go install -installsuffix="$mysuffix" -buildmode=shared $minpkgs || die "install -buildmode=shared failed"
+
+if [ ! -f "$std_install_dir/$soname" ]; then
+    echo "$std_install_dir/$soname not found!"
+    exit 1
+fi
+
+# The install command should have created a "shlibname" file for each
+# package indicating the name of the shared library containing it.
+for pkg in $minpkgs; do
+    if [ ! -f "$std_install_dir/$pkg.shlibname" ]; then
+        die "no shlibname file for $pkg"
+    fi
+    if [ "$(cat "$std_install_dir/$pkg.shlibname")" != "$soname" ]; then
+        die "shlibname file for $pkg has wrong contents"
+    fi
+done
+
+# Build a trivial program that links against the shared library we
+# just made and check it runs.
+go install -installsuffix="$mysuffix" -linkshared trivial || die "build -linkshared failed"
+./bin/trivial || die "./bin/trivial failed"
+
+# And check that it is actually dynamically linked against the library
+# we hope it is linked against.
+a="$(ldd ./bin/trivial)" || die "ldd ./bin/trivial failed: $a"
+{ echo "$a" | grep -q "$std_install_dir/$soname"; } || die "trivial does not appear to be linked against $soname"
index 7ffe5d6f6291302f06830ac47f686acb920df026..a50322214b3c768a01fc51baea78168ac153534d 100644 (file)
@@ -281,6 +281,9 @@ func (t *tester) registerTests() {
                if t.buildmode("c-shared") {
                        t.registerTest("testcshared", "../misc/cgo/testcshared", "./test.bash")
                }
+               if t.buildmode("shared") {
+                       t.registerTest("testshared", "../misc/cgo/testshared", "./test.bash")
+               }
                if t.gohostos == "linux" && t.goarch == "amd64" {
                        t.registerTest("testasan", "../misc/cgo/testasan", "go", "run", "main.go")
                }
@@ -399,6 +402,12 @@ func (t *tester) buildmode(mode string) bool {
                        return true
                }
                return false
+       case "shared":
+               switch pair {
+               case "linux-amd64":
+                       return true
+               }
+               return false
        default:
                log.Fatal("internal error: unknown buildmode %s", mode)
                return false