a 1.7 KB

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