make.bash 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. # Don't use '-u' to force updates because the docker builds always pull
  10. # the latest versions. Outside of Docker, be aware that these dependencies
  11. # will not be overridden w/ new versions if they already exist in $GOPATH
  12. GOOS=android go get -d -v -tags "${BUILD_TAGS}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  13. if [ $? != 0 ]; then
  14. echo "..'go get -d -v github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon' failed, exiting"
  15. exit $?
  16. fi
  17. BUILDDATE=$(date --iso-8601=seconds)
  18. BUILDREPO=$(git config --get remote.origin.url)
  19. BUILDREV=$(git rev-parse --short HEAD)
  20. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  21. GOMOBILEVERSION=$(gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  22. # - starts the string with a `{`
  23. # - 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
  24. # - pipes to `xargs` to run a command on each line output from the first command
  25. # - 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
  26. # - 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)
  27. # - `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>",`
  28. # - 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
  29. 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/,$/}/')
  30. LDFLAGS="\
  31. -s \
  32. -w \
  33. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=$BUILDDATE \
  34. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=$BUILDREPO \
  35. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=$BUILDREV \
  36. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=$GOVERSION \
  37. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.gomobileVersion=$GOMOBILEVERSION \
  38. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=$DEPENDENCIES \
  39. "
  40. echo "Variables for ldflags:"
  41. echo " Build date: ${BUILDDATE}"
  42. echo " Build repo: ${BUILDREPO}"
  43. echo " Build revision: ${BUILDREV}"
  44. echo " Go version: ${GOVERSION}"
  45. echo " Gomobile version: ${GOMOBILEVERSION}"
  46. echo " Dependencies: ${DEPENDENCIES}"
  47. echo ""
  48. # Note: android/386 is x86 and android/amd64 is x86_64
  49. gomobile bind -v -x -target=android/arm,android/arm64,android/386,android/amd64 -tags="${BUILD_TAGS}" -ldflags="$LDFLAGS" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  50. if [ $? != 0 ]; then
  51. echo "..'gomobile bind' failed, exiting"
  52. exit $?
  53. fi
  54. mkdir -p build-tmp/psi
  55. unzip -o psi.aar -d build-tmp/psi
  56. yes | cp -f PsiphonTunnel/AndroidManifest.xml build-tmp/psi/AndroidManifest.xml
  57. yes | cp -f PsiphonTunnel/libs/armeabi-v7a/libtun2socks.so build-tmp/psi/jni/armeabi-v7a/libtun2socks.so
  58. yes | cp -f PsiphonTunnel/libs/arm64-v8a/libtun2socks.so build-tmp/psi/jni/arm64-v8a/libtun2socks.so
  59. yes | cp -f PsiphonTunnel/libs/x86/libtun2socks.so build-tmp/psi/jni/x86/libtun2socks.so
  60. yes | cp -f PsiphonTunnel/libs/x86_64/libtun2socks.so build-tmp/psi/jni/x86_64/libtun2socks.so
  61. javac -d build-tmp -bootclasspath $ANDROID_HOME/platforms/android-23/android.jar -source 1.7 -target 1.7 -classpath build-tmp/psi/classes.jar:$ANDROID_HOME/platforms/android-23/optional/org.apache.http.legacy.jar PsiphonTunnel/PsiphonTunnel.java
  62. if [ $? != 0 ]; then
  63. echo "..'javac' compiling PsiphonTunnel failed, exiting"
  64. exit $?
  65. fi
  66. cd build-tmp
  67. jar uf psi/classes.jar ca/psiphon/*.class
  68. if [ $? != 0 ]; then
  69. echo "..'jar' failed to add classes, exiting"
  70. exit $?
  71. fi
  72. cd -
  73. cd build-tmp/psi
  74. echo -e "-keep class psi.** { *; }\n" >> proguard.txt
  75. zip -r ../../ca.psiphon.aar ./
  76. cd -
  77. rm -rf build-tmp
  78. echo "Done"