blob: 1b7fec15493a7465f1cb9ba4202935410a023396 [file] [log] [blame]
Hyang-Ah Hana Kim92189a22015-04-16 13:46:58 -04001#!/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
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -04006# For testing Android, this script requires adb to push and run compiled
7# binaries on a target device.
8
Hyang-Ah Hana Kim92189a22015-04-16 13:46:58 -04009set -e
10
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -040011if [ ! -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
15fi
16
17goos=$(go env GOOS)
Hyang-Ah (Hana) Kima4f4a462015-05-12 16:47:40 -040018goarch=$(go env GOARCH)
19
20# Directory where cgo headers and outputs will be installed.
21# The installation directory format varies depending on the platform.
22installdir=pkg/${goos}_${goarch}_testcshared_shared
Srdjan Petroviccc6554f2015-06-16 10:07:45 -070023if [ "${goos}/${goarch}" == "android/arm" ] || [ "${goos}/${goarch}" == "darwin/amd64" ]; then
Hyang-Ah (Hana) Kima4f4a462015-05-12 16:47:40 -040024 installdir=pkg/${goos}_${goarch}_testcshared
25fi
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -040026
27# Temporary directory on the android device.
28androidpath=/data/local/tmp/testcshared-$$
29
Hyang-Ah Hana Kim92189a22015-04-16 13:46:58 -040030function cleanup() {
Mikio Harafe1cecf2015-06-25 11:33:48 +090031 rm -rf libgo.$libext libgo2.$libext libgo.h testp testp2 testp3 pkg
Ian Lance Taylore45aebd2015-05-06 22:06:19 -070032
Hyang-Ah (Hana) Kima4f4a462015-05-12 16:47:40 -040033 rm -rf $(go env GOROOT)/${installdir}
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -040034
Hyang-Ah (Hana) Kima4f4a462015-05-12 16:47:40 -040035 if [ "$goos" == "android" ]; then
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -040036 adb shell rm -rf $androidpath
37 fi
Hyang-Ah Hana Kim92189a22015-04-16 13:46:58 -040038}
39trap cleanup EXIT
40
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -040041if [ "$goos" == "android" ]; then
42 adb shell mkdir -p "$androidpath"
43fi
44
45function run() {
46 case "$goos" in
47 "android")
48 local args=$@
Hyang-Ah (Hana) Kim647026a2015-05-13 17:26:19 -040049 output=$(adb shell "cd ${androidpath}; $@")
Hyang-Ah (Hana) Kima4f4a462015-05-12 16:47:40 -040050 output=$(echo $output|tr -d '\r')
Hyang-Ah Hana Kime9a89b82015-04-24 14:11:29 -040051 case $output in
52 *PASS) echo "PASS";;
53 *) echo "$output";;
54 esac
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -040055 ;;
56 *)
57 echo $(env $@)
58 ;;
59 esac
60}
61
62function binpush() {
63 bin=${1}
64 if [ "$goos" == "android" ]; then
65 adb push "$bin" "${androidpath}/${bin}" 2>/dev/null
66 fi
67}
68
Ian Lance Taylore45aebd2015-05-06 22:06:19 -070069rm -rf pkg
70
71suffix="-installsuffix testcshared"
72
Srdjan Petroviccc6554f2015-06-16 10:07:45 -070073libext="so"
74if [ "$goos" == "darwin" ]; then
75 libext="dylib"
76fi
77
Ian Lance Taylore45aebd2015-05-06 22:06:19 -070078# Create the header files.
79GOPATH=$(pwd) go install -buildmode=c-shared $suffix libgo
80
Srdjan Petroviccc6554f2015-06-16 10:07:45 -070081GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo.$libext src/libgo/libgo.go
82binpush libgo.$libext
Hyang-Ah Hana Kim92189a22015-04-16 13:46:58 -040083
Michael Hudson-Doyle2c2cbb62015-05-21 13:07:19 +120084if [ "$goos" == "linux" ]; then
85 if readelf -d libgo.$libext | grep TEXTREL >/dev/null; then
86 echo "libgo.$libext has TEXTREL set"
87 exit 1
88 fi
89fi
90
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -040091# test0: exported symbols in shared lib are accessible.
Ian Lance Taylore45aebd2015-05-06 22:06:19 -070092# TODO(iant): using _shared here shouldn't really be necessary.
Srdjan Petroviccc6554f2015-06-16 10:07:45 -070093$(go env CC) $(go env GOGCCFLAGS) -I ${installdir} -o testp main0.c libgo.$libext
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -040094binpush testp
Hyang-Ah (Hana) Kima4f4a462015-05-12 16:47:40 -040095
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -040096output=$(run LD_LIBRARY_PATH=. ./testp)
Hyang-Ah Hana Kim92189a22015-04-16 13:46:58 -040097if [ "$output" != "PASS" ]; then
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -040098 echo "FAIL test0 got ${output}"
Hyang-Ah Hana Kim92189a22015-04-16 13:46:58 -040099 exit 1
100fi
101
Srdjan Petroviccc6554f2015-06-16 10:07:45 -0700102# test1: shared library can be dynamically loaded and exported symbols are accessible.
Hyang-Ah Hana Kim92189a22015-04-16 13:46:58 -0400103$(go env CC) $(go env GOGCCFLAGS) -o testp main1.c -ldl
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -0400104binpush testp
Srdjan Petroviccc6554f2015-06-16 10:07:45 -0700105output=$(run ./testp ./libgo.$libext)
Hyang-Ah Hana Kim92189a22015-04-16 13:46:58 -0400106if [ "$output" != "PASS" ]; then
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -0400107 echo "FAIL test1 got ${output}"
Hyang-Ah Hana Kim92189a22015-04-16 13:46:58 -0400108 exit 1
109fi
Ian Lance Taylore589e082015-04-21 10:46:29 -0700110
Srdjan Petroviccc6554f2015-06-16 10:07:45 -0700111# test2: tests libgo2 which does not export any functions.
112GOPATH=$(pwd) go build -buildmode=c-shared $suffix -o libgo2.$libext src/libgo2/libgo2.go
113binpush libgo2.$libext
114linkflags="-Wl,--no-as-needed"
115if [ "$goos" == "darwin" ]; then
116 linkflags=""
117fi
118$(go env CC) $(go env GOGCCFLAGS) -o testp2 main2.c $linkflags libgo2.$libext
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -0400119binpush testp2
120output=$(run LD_LIBRARY_PATH=. ./testp2)
Ian Lance Taylore589e082015-04-21 10:46:29 -0700121if [ "$output" != "PASS" ]; then
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -0400122 echo "FAIL test2 got ${output}"
Ian Lance Taylore589e082015-04-21 10:46:29 -0700123 exit 1
124fi
Hyang-Ah Hana Kim85669792015-04-23 17:27:38 -0400125
126# test3: tests main.main is exported on android.
127if [ "$goos" == "android" ]; then
128 $(go env CC) $(go env GOGCCFLAGS) -o testp3 main3.c -ldl
129 binpush testp3
130 output=$(run ./testp ./libgo.so)
131 if [ "$output" != "PASS" ]; then
132 echo "FAIL test3 got ${output}"
133 exit 1
134 fi
135fi
136echo "ok"