make.bash 4.9 KB

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