2015-04-16 11:46:58 -06:00
|
|
|
#!/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.
|
|
|
|
|
2015-04-23 15:27:38 -06:00
|
|
|
# For testing Android, this script requires adb to push and run compiled
|
|
|
|
# binaries on a target device.
|
|
|
|
|
2015-04-16 11:46:58 -06:00
|
|
|
set -e
|
|
|
|
|
2015-04-23 15:27:38 -06:00
|
|
|
if [ ! -f src/libgo/libgo.go ]; then
|
|
|
|
cwd=$(pwd)
|
2015-08-16 06:59:23 -06:00
|
|
|
echo "misc/cgo/testcshared/test.bash is running in $cwd" 1>&2
|
2015-04-23 15:27:38 -06:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
goos=$(go env GOOS)
|
2015-05-12 14:47:40 -06:00
|
|
|
goarch=$(go env GOARCH)
|
2016-01-02 07:26:13 -07:00
|
|
|
goroot=$(go env GOROOT)
|
|
|
|
if [ ! -d "$goroot" ]; then
|
2016-04-03 05:43:27 -06:00
|
|
|
echo 'misc/cgo/testcshared/test.bash cannot find GOROOT' 1>&2
|
2016-01-04 13:08:03 -07:00
|
|
|
echo '$GOROOT:' "$GOROOT" 1>&2
|
|
|
|
echo 'go env GOROOT:' "$goroot" 1>&2
|
2016-01-02 07:26:13 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
2015-05-12 14:47:40 -06:00
|
|
|
|
|
|
|
# Directory where cgo headers and outputs will be installed.
|
|
|
|
# The installation directory format varies depending on the platform.
|
|
|
|
installdir=pkg/${goos}_${goarch}_testcshared_shared
|
2016-01-10 22:23:51 -07:00
|
|
|
if [ "${goos}" == "darwin" ]; then
|
2015-05-12 14:47:40 -06:00
|
|
|
installdir=pkg/${goos}_${goarch}_testcshared
|
|
|
|
fi
|
2015-04-23 15:27:38 -06:00
|
|
|
|
|
|
|
# Temporary directory on the android device.
|
|
|
|
androidpath=/data/local/tmp/testcshared-$$
|
|
|
|
|
2015-04-16 11:46:58 -06:00
|
|
|
function cleanup() {
|
2015-12-18 16:29:51 -07:00
|
|
|
rm -f libgo.$libext libgo2.$libext libgo4.$libext libgo5.$libext
|
|
|
|
rm -f libgo.h libgo4.h libgo5.h
|
|
|
|
rm -f testp testp2 testp3 testp4 testp5
|
2016-01-02 07:26:13 -07:00
|
|
|
rm -rf pkg "${goroot}/${installdir}"
|
2015-04-23 15:27:38 -06:00
|
|
|
|
2015-05-12 14:47:40 -06:00
|
|
|
if [ "$goos" == "android" ]; then
|
2016-01-02 07:26:13 -07:00
|
|
|
adb shell rm -rf "$androidpath"
|
2015-04-23 15:27:38 -06:00
|
|
|
fi
|
2015-04-16 11:46:58 -06:00
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
2015-04-23 15:27:38 -06:00
|
|
|
if [ "$goos" == "android" ]; then
|
|
|
|
adb shell mkdir -p "$androidpath"
|
|
|
|
fi
|
|
|
|
|
|
|
|
function run() {
|
|
|
|
case "$goos" in
|
|
|
|
"android")
|
|
|
|
local args=$@
|
2015-05-13 15:26:19 -06:00
|
|
|
output=$(adb shell "cd ${androidpath}; $@")
|
2015-05-12 14:47:40 -06:00
|
|
|
output=$(echo $output|tr -d '\r')
|
2015-04-24 12:11:29 -06:00
|
|
|
case $output in
|
|
|
|
*PASS) echo "PASS";;
|
|
|
|
*) echo "$output";;
|
|
|
|
esac
|
2015-04-23 15:27:38 -06:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo $(env $@)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
function binpush() {
|
|
|
|
bin=${1}
|
|
|
|
if [ "$goos" == "android" ]; then
|
|
|
|
adb push "$bin" "${androidpath}/${bin}" 2>/dev/null
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-05-06 23:06:19 -06:00
|
|
|
rm -rf pkg
|
|
|
|
|
|
|
|
suffix="-installsuffix testcshared"
|
|
|
|
|
2015-06-16 11:07:45 -06:00
|
|
|
libext="so"
|
|
|
|
if [ "$goos" == "darwin" ]; then
|
|
|
|
libext="dylib"
|
|
|
|
fi
|
|
|
|
|
2015-05-06 23:06:19 -06:00
|
|
|
# Create the header files.
|
|
|
|
GOPATH=$(pwd) go install -buildmode=c-shared $suffix libgo
|
|
|
|
|
2015-06-16 11:07:45 -06:00
|
|
|
GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo.$libext src/libgo/libgo.go
|
|
|
|
binpush libgo.$libext
|
2015-04-16 11:46:58 -06:00
|
|
|
|
2015-10-16 12:19:13 -06:00
|
|
|
if [ "$goos" == "linux" ] || [ "$goos" == "android" ] ; then
|
2015-05-20 19:07:19 -06:00
|
|
|
if readelf -d libgo.$libext | grep TEXTREL >/dev/null; then
|
|
|
|
echo "libgo.$libext has TEXTREL set"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2015-10-13 11:58:11 -06:00
|
|
|
GOGCCFLAGS=$(go env GOGCCFLAGS)
|
|
|
|
if [ "$goos" == "android" ]; then
|
|
|
|
GOGCCFLAGS="${GOGCCFLAGS} -pie"
|
|
|
|
fi
|
|
|
|
|
2015-12-16 13:16:17 -07:00
|
|
|
status=0
|
|
|
|
|
2015-04-23 15:27:38 -06:00
|
|
|
# test0: exported symbols in shared lib are accessible.
|
2015-05-06 23:06:19 -06:00
|
|
|
# TODO(iant): using _shared here shouldn't really be necessary.
|
2015-10-13 11:58:11 -06:00
|
|
|
$(go env CC) ${GOGCCFLAGS} -I ${installdir} -o testp main0.c libgo.$libext
|
2015-04-23 15:27:38 -06:00
|
|
|
binpush testp
|
2015-05-12 14:47:40 -06:00
|
|
|
|
2015-04-23 15:27:38 -06:00
|
|
|
output=$(run LD_LIBRARY_PATH=. ./testp)
|
2015-04-16 11:46:58 -06:00
|
|
|
if [ "$output" != "PASS" ]; then
|
2015-04-23 15:27:38 -06:00
|
|
|
echo "FAIL test0 got ${output}"
|
2015-12-16 13:16:17 -07:00
|
|
|
status=1
|
2015-04-16 11:46:58 -06:00
|
|
|
fi
|
|
|
|
|
2015-06-16 11:07:45 -06:00
|
|
|
# test1: shared library can be dynamically loaded and exported symbols are accessible.
|
2015-10-13 11:58:11 -06:00
|
|
|
$(go env CC) ${GOGCCFLAGS} -o testp main1.c -ldl
|
2015-04-23 15:27:38 -06:00
|
|
|
binpush testp
|
2015-06-16 11:07:45 -06:00
|
|
|
output=$(run ./testp ./libgo.$libext)
|
2015-04-16 11:46:58 -06:00
|
|
|
if [ "$output" != "PASS" ]; then
|
2015-04-23 15:27:38 -06:00
|
|
|
echo "FAIL test1 got ${output}"
|
2015-12-16 13:16:17 -07:00
|
|
|
status=1
|
2015-04-16 11:46:58 -06:00
|
|
|
fi
|
2015-04-21 11:46:29 -06:00
|
|
|
|
2015-06-16 11:07:45 -06:00
|
|
|
# test2: tests libgo2 which does not export any functions.
|
2015-10-28 15:57:30 -06:00
|
|
|
GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo2.$libext libgo2
|
2015-06-16 11:07:45 -06:00
|
|
|
binpush libgo2.$libext
|
|
|
|
linkflags="-Wl,--no-as-needed"
|
|
|
|
if [ "$goos" == "darwin" ]; then
|
|
|
|
linkflags=""
|
|
|
|
fi
|
2015-10-13 11:58:11 -06:00
|
|
|
$(go env CC) ${GOGCCFLAGS} -o testp2 main2.c $linkflags libgo2.$libext
|
2015-04-23 15:27:38 -06:00
|
|
|
binpush testp2
|
|
|
|
output=$(run LD_LIBRARY_PATH=. ./testp2)
|
2015-04-21 11:46:29 -06:00
|
|
|
if [ "$output" != "PASS" ]; then
|
2015-04-23 15:27:38 -06:00
|
|
|
echo "FAIL test2 got ${output}"
|
2015-12-16 13:16:17 -07:00
|
|
|
status=1
|
2015-04-21 11:46:29 -06:00
|
|
|
fi
|
2015-04-23 15:27:38 -06:00
|
|
|
|
|
|
|
# test3: tests main.main is exported on android.
|
|
|
|
if [ "$goos" == "android" ]; then
|
2015-10-13 11:58:11 -06:00
|
|
|
$(go env CC) ${GOGCCFLAGS} -o testp3 main3.c -ldl
|
2015-04-23 15:27:38 -06:00
|
|
|
binpush testp3
|
|
|
|
output=$(run ./testp ./libgo.so)
|
|
|
|
if [ "$output" != "PASS" ]; then
|
|
|
|
echo "FAIL test3 got ${output}"
|
2015-12-16 13:16:17 -07:00
|
|
|
status=1
|
2015-04-23 15:27:38 -06:00
|
|
|
fi
|
|
|
|
fi
|
2015-12-16 13:16:17 -07:00
|
|
|
|
|
|
|
# test4: tests signal handlers
|
|
|
|
GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo4.$libext libgo4
|
|
|
|
binpush libgo4.$libext
|
|
|
|
$(go env CC) ${GOGCCFLAGS} -pthread -o testp4 main4.c -ldl
|
|
|
|
binpush testp4
|
|
|
|
output=$(run ./testp4 ./libgo4.$libext 2>&1)
|
|
|
|
if test "$output" != "PASS"; then
|
|
|
|
echo "FAIL test4 got ${output}"
|
|
|
|
if test "$goos" != "android"; then
|
|
|
|
echo "re-running test4 in verbose mode"
|
|
|
|
./testp4 ./libgo4.$libext verbose
|
|
|
|
fi
|
|
|
|
status=1
|
|
|
|
fi
|
|
|
|
|
2015-12-18 16:29:51 -07:00
|
|
|
# test5: tests signal handlers with os/signal.Notify
|
|
|
|
GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo5.$libext libgo5
|
|
|
|
binpush libgo5.$libext
|
|
|
|
$(go env CC) ${GOGCCFLAGS} -pthread -o testp5 main5.c -ldl
|
|
|
|
binpush testp5
|
|
|
|
output=$(run ./testp5 ./libgo5.$libext 2>&1)
|
|
|
|
if test "$output" != "PASS"; then
|
|
|
|
echo "FAIL test5 got ${output}"
|
|
|
|
if test "$goos" != "android"; then
|
|
|
|
echo "re-running test5 in verbose mode"
|
|
|
|
./testp5 ./libgo5.$libext verbose
|
|
|
|
fi
|
|
|
|
status=1
|
|
|
|
fi
|
|
|
|
|
2015-12-16 13:16:17 -07:00
|
|
|
if test $status = 0; then
|
|
|
|
echo "ok"
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit $status
|