github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/misc/cgo/testcshared/test.bash (about) 1 #!/usr/bin/env bash 2 # Copyright 2015 The Go Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style 4 # license that can be found in the LICENSE file. 5 6 # For testing Android, this script requires adb to push and run compiled 7 # binaries on a target device. 8 9 set -e 10 11 if [ ! -f src/libgo/libgo.go ]; then 12 cwd=$(pwd) 13 echo 'misc/cgo/testcshared/test.bash is running in $cwd' 1>&2 14 exit 1 15 fi 16 17 goos=$(go env GOOS) 18 goarch=$(go env GOARCH) 19 20 # Directory where cgo headers and outputs will be installed. 21 # The installation directory format varies depending on the platform. 22 installdir=pkg/${goos}_${goarch}_testcshared_shared 23 if [ "${goos}/${goarch}" == "darwin/amd64" ]; then 24 installdir=pkg/${goos}_${goarch}_testcshared 25 fi 26 27 # Temporary directory on the android device. 28 androidpath=/data/local/tmp/testcshared-$$ 29 30 function cleanup() { 31 rm -f libgo.$libext libgo2.$libext libgo4.$libext libgo.h libgo4.h 32 rm -f testp testp2 testp3 testp4 33 rm -rf pkg $(go env GOROOT)/${installdir} 34 35 if [ "$goos" == "android" ]; then 36 adb shell rm -rf $androidpath 37 fi 38 } 39 trap cleanup EXIT 40 41 if [ "$goos" == "android" ]; then 42 adb shell mkdir -p "$androidpath" 43 fi 44 45 function run() { 46 case "$goos" in 47 "android") 48 local args=$@ 49 output=$(adb shell "cd ${androidpath}; $@") 50 output=$(echo $output|tr -d '\r') 51 case $output in 52 *PASS) echo "PASS";; 53 *) echo "$output";; 54 esac 55 ;; 56 *) 57 echo $(env $@) 58 ;; 59 esac 60 } 61 62 function binpush() { 63 bin=${1} 64 if [ "$goos" == "android" ]; then 65 adb push "$bin" "${androidpath}/${bin}" 2>/dev/null 66 fi 67 } 68 69 rm -rf pkg 70 71 suffix="-installsuffix testcshared" 72 73 libext="so" 74 if [ "$goos" == "darwin" ]; then 75 libext="dylib" 76 fi 77 78 # Create the header files. 79 GOPATH=$(pwd) go install -buildmode=c-shared $suffix libgo 80 81 GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo.$libext src/libgo/libgo.go 82 binpush libgo.$libext 83 84 if [ "$goos" == "linux" ] || [ "$goos" == "android" ] ; then 85 if readelf -d libgo.$libext | grep TEXTREL >/dev/null; then 86 echo "libgo.$libext has TEXTREL set" 87 exit 1 88 fi 89 fi 90 91 GOGCCFLAGS=$(go env GOGCCFLAGS) 92 if [ "$goos" == "android" ]; then 93 GOGCCFLAGS="${GOGCCFLAGS} -pie" 94 fi 95 96 status=0 97 98 # test0: exported symbols in shared lib are accessible. 99 # TODO(iant): using _shared here shouldn't really be necessary. 100 $(go env CC) ${GOGCCFLAGS} -I ${installdir} -o testp main0.c libgo.$libext 101 binpush testp 102 103 output=$(run LD_LIBRARY_PATH=. ./testp) 104 if [ "$output" != "PASS" ]; then 105 echo "FAIL test0 got ${output}" 106 status=1 107 fi 108 109 # test1: shared library can be dynamically loaded and exported symbols are accessible. 110 $(go env CC) ${GOGCCFLAGS} -o testp main1.c -ldl 111 binpush testp 112 output=$(run ./testp ./libgo.$libext) 113 if [ "$output" != "PASS" ]; then 114 echo "FAIL test1 got ${output}" 115 status=1 116 fi 117 118 # test2: tests libgo2 which does not export any functions. 119 GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo2.$libext libgo2 120 binpush libgo2.$libext 121 linkflags="-Wl,--no-as-needed" 122 if [ "$goos" == "darwin" ]; then 123 linkflags="" 124 fi 125 $(go env CC) ${GOGCCFLAGS} -o testp2 main2.c $linkflags libgo2.$libext 126 binpush testp2 127 output=$(run LD_LIBRARY_PATH=. ./testp2) 128 if [ "$output" != "PASS" ]; then 129 echo "FAIL test2 got ${output}" 130 status=1 131 fi 132 133 # test3: tests main.main is exported on android. 134 if [ "$goos" == "android" ]; then 135 $(go env CC) ${GOGCCFLAGS} -o testp3 main3.c -ldl 136 binpush testp3 137 output=$(run ./testp ./libgo.so) 138 if [ "$output" != "PASS" ]; then 139 echo "FAIL test3 got ${output}" 140 status=1 141 fi 142 fi 143 144 # test4: tests signal handlers 145 GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo4.$libext libgo4 146 binpush libgo4.$libext 147 $(go env CC) ${GOGCCFLAGS} -pthread -o testp4 main4.c -ldl 148 binpush testp4 149 output=$(run ./testp4 ./libgo4.$libext 2>&1) 150 if test "$output" != "PASS"; then 151 echo "FAIL test4 got ${output}" 152 if test "$goos" != "android"; then 153 echo "re-running test4 in verbose mode" 154 ./testp4 ./libgo4.$libext verbose 155 fi 156 status=1 157 fi 158 159 if test $status = 0; then 160 echo "ok" 161 fi 162 163 exit $status