make.bash 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, psiphon-tunnel-core 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. # DEPENDENCIES
  18. #
  19. # - this script produces a JSON object listing all Go package dependencies,
  20. # excluding packages under github.com/Psiphon-Labs/psiphon-tunnel-core/
  21. # (thus also excluding vendored packages) which will all have the same rev
  22. # as BUILDREV
  23. #
  24. # - starts the string with a `{` and ends with a `}`
  25. #
  26. # - uses the `go list` command and passes it a template string (using the Go
  27. # template syntax) saying I want all the dependencies of the package in the
  28. # current directory, printing 1/line via printf
  29. #
  30. # - pipes to `xargs` to run a command on each line output from the first
  31. # command and uses `go list` with a template string to print the "Import
  32. # Path" (from just below `$GOPATH/src`) if the package is not part of the
  33. # standard library
  34. #
  35. # - pipes to `xargs` again, specifiying `pkg` as the placeholder name for each
  36. # item being operated on (which is the list of non standard library import
  37. # paths from the previous step); `xargs` runs a bash script (via `-c`) which
  38. # changes to each import path in sequence, then echoes out, after the
  39. # exclusion check, `"<import path>":"<subshell output of getting the short
  40. # git revision>",`
  41. #
  42. # - for non-empty dependency lists, the last command leaves a trailing `,\n` at
  43. # the end, so use `sed` and `tr` to remove the suffix.
  44. #
  45. 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/$0 && if echo -n "$0" | grep -vEq "^github.com/Psiphon-Labs/psiphon-tunnel-core/" ; then echo -n "\"$0\":\"$(git rev-parse --short HEAD)\"," ; fi' pkg | sed 's/,$//' | tr -d '\n' && echo -n "}")
  46. LDFLAGS="\
  47. -s \
  48. -w \
  49. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=$BUILDDATE \
  50. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=$BUILDREPO \
  51. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=$BUILDREV \
  52. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=$GOVERSION \
  53. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.gomobileVersion=$GOMOBILEVERSION \
  54. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=$DEPENDENCIES \
  55. "
  56. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > $BUILDINFOFILE
  57. echo "Variables for ldflags:"
  58. echo " Build date: ${BUILDDATE}"
  59. echo " Build repo: ${BUILDREPO}"
  60. echo " Build revision: ${BUILDREV}"
  61. echo " Go version: ${GOVERSION}"
  62. echo " Gomobile version: ${GOMOBILEVERSION}"
  63. echo " Dependencies: ${DEPENDENCIES}"
  64. echo ""
  65. # Note: android/386 is x86, which is used on both x86 and x86_64 Android
  66. # devices. We are excluding the android/amd64, x86_64, ABI as it causes a
  67. # crash in Android x86_64 emulators: "seccomp prevented call to disallowed
  68. # x86_64 system call 22". x86/linux syscall 22 is pipe.
  69. #
  70. # In Android seccomp config, pipe is permitted only for 32-bit platforms:
  71. # https://android.googlesource.com/platform/bionic/+/2b499046f10487802bfbaaf4429160595d08b22c/libc/SECCOMP_WHITELIST_APP.TXT#7.
  72. #
  73. # The Go syscall.Pipe on linux(android)/amd64 is the disallowed pipe:
  74. # https://github.com/golang/go/blob/release-branch.go1.14/src/syscall/syscall_linux_amd64.go#L115-L126
  75. #
  76. # A potential future fix is to use the allowed pipe2,
  77. # https://android.googlesource.com/platform/bionic/+/2b499046f10487802bfbaaf4429160595d08b22c/libc/SYSCALLS.TXT#129,
  78. # which is what linux(android)/arm64 uses, for example:
  79. # https://github.com/golang/go/blob/release-branch.go1.14/src/syscall/syscall_linux_arm64.go#L150-L159.
  80. 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
  81. if [ $? != 0 ]; then
  82. echo "..'gomobile bind' failed, exiting"
  83. exit $?
  84. fi
  85. mkdir -p build-tmp/psi
  86. unzip -o psi.aar -d build-tmp/psi
  87. yes | cp -f PsiphonTunnel/AndroidManifest.xml build-tmp/psi/AndroidManifest.xml
  88. yes | cp -f PsiphonTunnel/libs/armeabi-v7a/libtun2socks.so build-tmp/psi/jni/armeabi-v7a/libtun2socks.so
  89. yes | cp -f PsiphonTunnel/libs/arm64-v8a/libtun2socks.so build-tmp/psi/jni/arm64-v8a/libtun2socks.so
  90. yes | cp -f PsiphonTunnel/libs/x86/libtun2socks.so build-tmp/psi/jni/x86/libtun2socks.so
  91. mkdir -p build-tmp/psi/res/xml
  92. yes | cp -f PsiphonTunnel/ca_psiphon_psiphontunnel_backup_rules.xml build-tmp/psi/res/xml/ca_psiphon_psiphontunnel_backup_rules.xml
  93. javac -d build-tmp -bootclasspath $ANDROID_HOME/platforms/android-$ANDROID_PLATFORM_VERSION/android.jar -source 1.8 -target 1.8 -classpath build-tmp/psi/classes.jar PsiphonTunnel/PsiphonTunnel.java
  94. if [ $? != 0 ]; then
  95. echo "..'javac' compiling PsiphonTunnel failed, exiting"
  96. exit $?
  97. fi
  98. cd build-tmp
  99. jar uf psi/classes.jar ca/psiphon/*.class
  100. if [ $? != 0 ]; then
  101. echo "..'jar' failed to add classes, exiting"
  102. exit $?
  103. fi
  104. cd -
  105. cd build-tmp/psi
  106. echo -e "-keep class psi.** { *; }\n-keep class ca.psiphon.** { *; }\n" >> proguard.txt
  107. rm -f ../../ca.psiphon.aar
  108. zip -r ../../ca.psiphon.aar ./
  109. cd -
  110. rm -rf build-tmp
  111. echo "Done"