1 #!/usr/bin/env bash
2
3 # Copyright 2010 The Go Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style
5 # license that can be found in the LICENSE file.
6
7 # This is a small script for executing go binaries on the android platform.
8 #
9 # example:
10 # ./a 5.out foo bar baz
11 #
12 # The script exports the local values of GOARCH, GOTRACEBACK and GOGC
13 # to the android environment.
14 #
15 # Known issues:
16 # The script fails unless the last character output by the program is "\n"
17 #
18 # TODO(kaib): add gdb bridge support
19
20 exp ()
21 {
22 if [ ${!1} ]; then
23 echo "export $1=\"${!1}\"; "
24 fi
25 }
26
27 # adb does not correctly return the exit value of the executed program. use this
28 # wrapper to manually extract the exit value
29 rloc=/data/local/tmp/retval
30 rsize=$(adb shell "ls -l $rloc"|tr -s ' '|cut -d' ' -f4)
31 rcheck=38
32 if [ "$rsize" != "$rcheck" ]; then
33 # echo "debug: retval size incorrect want $rcheck, got $rsize. uploading"
34 echo >/tmp/adb.retval '#!/system/bin/sh
35 "$@"
36 echo RETVAL: $?'
37 adb push /tmp/adb.retval $rloc >/dev/null 2>&1
38 adb shell chmod 755 $rloc
39 fi
40
41 # run the main binary
42 if [ "-g" == "$1" ]; then
43 adb forward tcp:$2 tcp:$2
44 args=$(echo $*| cut -d' ' -f4-)
45 adb push $3 /data/local/tmp/$3 >/dev/null 2>&1
46 adb shell "$(exp GOARCH) $(exp GOTRACEBACK) $(exp GOGC) \
47 gdbserver :$2 /data/local/tmp/retval /data/local/tmp/$3 $args" \
48 2>&1|tr -d '\r' |tee /tmp/adb.out|grep -v RETVAL
49 else
50 if [ "$*" != "$1" ]; then
51 args=$(echo $*| cut -d' ' -f2-)
52 fi
53 adb push $1 /data/local/tmp/$1 >/dev/null 2>&1
54 adb shell "$(exp GOARCH) $(exp GOTRACEBACK) $(exp GOGC) \
55 /data/local/tmp/retval /data/local/tmp/$1 $args" \
56 2>&1|tr -d '\r' |tee /tmp/adb.out|grep -v RETVAL
57 fi
58 exit $(grep RETVAL /tmp/adb.out|tr -d '\n\r'| cut -d' ' -f2)
59
View as plain text