make.bash 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 and android/amd64 is x86_64
  45. 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
  46. if [ $? != 0 ]; then
  47. echo "..'gomobile bind' failed, exiting"
  48. exit $?
  49. fi
  50. mkdir -p build-tmp/psi
  51. unzip -o psi.aar -d build-tmp/psi
  52. yes | cp -f PsiphonTunnel/AndroidManifest.xml build-tmp/psi/AndroidManifest.xml
  53. yes | cp -f PsiphonTunnel/libs/armeabi-v7a/libtun2socks.so build-tmp/psi/jni/armeabi-v7a/libtun2socks.so
  54. yes | cp -f PsiphonTunnel/libs/arm64-v8a/libtun2socks.so build-tmp/psi/jni/arm64-v8a/libtun2socks.so
  55. yes | cp -f PsiphonTunnel/libs/x86/libtun2socks.so build-tmp/psi/jni/x86/libtun2socks.so
  56. yes | cp -f PsiphonTunnel/libs/x86_64/libtun2socks.so build-tmp/psi/jni/x86_64/libtun2socks.so
  57. mkdir -p build-tmp/psi/res/xml
  58. yes | cp -f PsiphonTunnel/ca_psiphon_psiphontunnel_backup_rules.xml build-tmp/psi/res/xml/ca_psiphon_psiphontunnel_backup_rules.xml
  59. 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
  60. if [ $? != 0 ]; then
  61. echo "..'javac' compiling PsiphonTunnel failed, exiting"
  62. exit $?
  63. fi
  64. cd build-tmp
  65. jar uf psi/classes.jar ca/psiphon/*.class
  66. if [ $? != 0 ]; then
  67. echo "..'jar' failed to add classes, exiting"
  68. exit $?
  69. fi
  70. cd -
  71. cd build-tmp/psi
  72. echo -e "-keep class psi.** { *; }\n" >> proguard.txt
  73. zip -r ../../ca.psiphon.aar ./
  74. cd -
  75. rm -rf build-tmp
  76. echo "Done"