make.bash 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env bash
  2. set -e -u -x
  3. if [ ! -f make.bash ]; then
  4. echo "make.bash must be run from $GOPATH/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/Android"
  5. exit 1
  6. fi
  7. # $1, if specified, is go build tags
  8. if [ -z ${1+x} ]; then BUILD_TAGS=""; else BUILD_TAGS="$1"; fi
  9. # At this time, gomobile doesn't support modules
  10. export GO111MODULE=off
  11. BUILDINFOFILE="psiphon-tunnel-core_buildinfo.txt"
  12. BUILDDATE=$(date --iso-8601=seconds)
  13. BUILDREPO=$(git config --get remote.origin.url)
  14. BUILDREV=$(git rev-parse --short HEAD)
  15. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  16. GOMOBILEVERSION=$(gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  17. # - starts the string with a `{`
  18. # - uses the `go list` command and passes it a template string (using the Go template syntax) saying I want all the dependencies of the package in the current directory, printing 1/line via printf
  19. # - pipes to `xargs` to run a command on each line output from the first command
  20. # - uses `go list` with a template string to print the "Import Path" (from just below `$GOPATH/src`) if the package is not part of the standard library
  21. # - pipes to `xargs` again, specifiying `pkg` as the placeholder name for each item being operated on (which is the list of non standard library import paths from the previous step)
  22. # - `xargs` runs a bash script (via `-c`) which changes to each import path in sequence, then echoes out `"<import path>":"<subshell output of getting the short git revision>",`
  23. # - this leaves a trailing `,` at the end, and no close to the JSON object, so simply `sed` replace the comma before the end of the line with `}` and you now have valid JSON
  24. DEPENDENCIES=$(cd ../psi && echo -n "{" && GOOS=android go list -tags "${BUILD_TAGS}" -f '{{range $dep := .Deps}}{{printf "%s\n" $dep}}{{end}}' | GOOS=android xargs go list -tags "${BUILD_TAGS}" -f '{{if not .Standard}}{{.ImportPath}}{{end}}' | xargs -I pkg bash -c 'cd $GOPATH/src/pkg && echo -n "\"pkg\":\"$(git rev-parse --short HEAD)\","' | sed 's/,$/}/')
  25. LDFLAGS="\
  26. -s \
  27. -w \
  28. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=$BUILDDATE \
  29. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=$BUILDREPO \
  30. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=$BUILDREV \
  31. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=$GOVERSION \
  32. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.gomobileVersion=$GOMOBILEVERSION \
  33. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=$DEPENDENCIES \
  34. "
  35. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > $BUILDINFOFILE
  36. echo "Variables for ldflags:"
  37. echo " Build date: ${BUILDDATE}"
  38. echo " Build repo: ${BUILDREPO}"
  39. echo " Build revision: ${BUILDREV}"
  40. echo " Go version: ${GOVERSION}"
  41. echo " Gomobile version: ${GOMOBILEVERSION}"
  42. echo " Dependencies: ${DEPENDENCIES}"
  43. echo ""
  44. # Note: android/386 is x86, which is used on both x86 and x86_64 Android
  45. # devices. We are excluding the android/amd64, x86_64, ABI as it causes a
  46. # crash in Android x86_64 emulators: "seccomp prevented call to disallowed
  47. # x86_64 system call 22". x86/linux syscall 22 is pipe.
  48. #
  49. # In Android seccomp config, pipe is permitted only for 32-bit platforms:
  50. # https://android.googlesource.com/platform/bionic/+/2b499046f10487802bfbaaf4429160595d08b22c/libc/SECCOMP_WHITELIST_APP.TXT#7.
  51. #
  52. # The Go syscall.Pipe on linux(android)/amd64 is the disallowed pipe:
  53. # https://github.com/golang/go/blob/release-branch.go1.14/src/syscall/syscall_linux_amd64.go#L115-L126
  54. #
  55. # A potential future fix is to use the allowed pipe2,
  56. # https://android.googlesource.com/platform/bionic/+/2b499046f10487802bfbaaf4429160595d08b22c/libc/SYSCALLS.TXT#129,
  57. # which is what linux(android)/arm64 uses, for example:
  58. # https://github.com/golang/go/blob/release-branch.go1.14/src/syscall/syscall_linux_arm64.go#L150-L159.
  59. gomobile bind -v -x -target=android/arm,android/arm64,android/386 -tags="${BUILD_TAGS}" -ldflags="$LDFLAGS" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  60. if [ $? != 0 ]; then
  61. echo "..'gomobile bind' failed, exiting"
  62. exit $?
  63. fi
  64. mkdir -p build-tmp/psi
  65. unzip -o psi.aar -d build-tmp/psi
  66. yes | cp -f PsiphonTunnel/AndroidManifest.xml build-tmp/psi/AndroidManifest.xml
  67. yes | cp -f PsiphonTunnel/libs/armeabi-v7a/libtun2socks.so build-tmp/psi/jni/armeabi-v7a/libtun2socks.so
  68. yes | cp -f PsiphonTunnel/libs/arm64-v8a/libtun2socks.so build-tmp/psi/jni/arm64-v8a/libtun2socks.so
  69. yes | cp -f PsiphonTunnel/libs/x86/libtun2socks.so build-tmp/psi/jni/x86/libtun2socks.so
  70. mkdir -p build-tmp/psi/res/xml
  71. yes | cp -f PsiphonTunnel/ca_psiphon_psiphontunnel_backup_rules.xml build-tmp/psi/res/xml/ca_psiphon_psiphontunnel_backup_rules.xml
  72. javac -d build-tmp -bootclasspath $ANDROID_HOME/platforms/android-23/android.jar -source 1.8 -target 1.8 -classpath build-tmp/psi/classes.jar PsiphonTunnel/PsiphonTunnel.java
  73. if [ $? != 0 ]; then
  74. echo "..'javac' compiling PsiphonTunnel failed, exiting"
  75. exit $?
  76. fi
  77. cd build-tmp
  78. jar uf psi/classes.jar ca/psiphon/*.class
  79. if [ $? != 0 ]; then
  80. echo "..'jar' failed to add classes, exiting"
  81. exit $?
  82. fi
  83. cd -
  84. cd build-tmp/psi
  85. echo -e "-keep class psi.** { *; }\n" >> proguard.txt
  86. zip -r ../../ca.psiphon.aar ./
  87. cd -
  88. rm -rf build-tmp
  89. echo "Done"