build-psiphon-framework.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/usr/bin/env bash
  2. # This script takes one optional argument: 'private', if private plugins should
  3. # be used. It should be omitted if private plugins are not desired.
  4. if [[ $1 == "private" ]]; then
  5. FORCE_PRIVATE_PLUGINS=true
  6. echo "TRUE"
  7. else
  8. FORCE_PRIVATE_PLUGINS=false
  9. echo "FALSE"
  10. fi
  11. # -x echos commands. -u exits if an unintialized variable is used.
  12. # -e exits if a command returns an error.
  13. set -x -u -e
  14. # Reset the PATH to macOS default. This is mainly so we don't execute the wrong
  15. # gomobile executable.
  16. PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
  17. BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P)
  18. cd ${BASE_DIR}
  19. # The location of the final framework build
  20. BUILD_DIR="${BASE_DIR}/build"
  21. # Ensure go is installed
  22. which go 2>&1 > /dev/null
  23. if [[ $? != 0 ]]; then
  24. echo "Go is not installed in the path, aborting"
  25. exit 1
  26. fi
  27. VALID_IOS_ARCHS="arm64 armv7 armv7s"
  28. VALID_SIMULATOR_ARCHS="x86_64"
  29. FRAMEWORK="Psi"
  30. INTERMEDIATE_OUPUT_DIR="${BASE_DIR}/PsiphonTunnel/PsiphonTunnel"
  31. INTERMEDIATE_OUPUT_FILE="${FRAMEWORK}.framework"
  32. FRAMEWORK_BINARY="${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}/Versions/A/${FRAMEWORK}"
  33. PRIVATE_PLUGINS_TAG=""
  34. if [[ ${FORCE_PRIVATE_PLUGINS} == true ]]; then PRIVATE_PLUGINS_TAG="PRIVATE_PLUGINS"; fi
  35. BUILD_TAGS="IOS ${PRIVATE_PLUGINS_TAG}"
  36. UMBRELLA_FRAMEWORK_XCODE_PROJECT=${BASE_DIR}/PsiphonTunnel/PsiphonTunnel.xcodeproj/
  37. TRUSTED_ROOT_CA_FILE=${BASE_DIR}/PsiphonTunnel/PsiphonTunnel/rootCAs.txt
  38. # Download trustedroot CAs off curl website, see https://curl.haxx.se/docs/caextract.html for details
  39. curl -o $TRUSTED_ROOT_CA_FILE https://curl.haxx.se/ca/cacert.pem
  40. if [[ $? != 0 ]]; then
  41. echo "FAILURE: curl -o $TRUSTED_ROOT_CA_FILE https://curl.haxx.se/ca/cacert.pem"
  42. exit 1
  43. fi
  44. # Exporting these seems necessary for subcommands to pick them up.
  45. export GOPATH=${PWD}/go-ios-build
  46. export PATH=${GOPATH}/bin:${PATH}
  47. # The GOPATH we're using is temporary, so make sure there isn't one from a previous run.
  48. rm -rf ${GOPATH}
  49. GOMOBILE_PINNED_REV=eb9032959f05f108b05721914dfe09cfa0c5131d
  50. GOMOBILE_PATH=${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  51. TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core
  52. PATH=${PATH}:${GOPATH}/bin
  53. mkdir -p ${GOPATH}
  54. if [[ $? != 0 ]]; then
  55. echo "FAILURE: mkdir -p ${GOPATH}"
  56. exit 1
  57. fi
  58. # Symlink the current source directory into GOPATH, so that we're building the
  59. # code in this local repo, rather than pulling from Github and building that.
  60. mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs
  61. if [[ $? != 0 ]]; then
  62. echo "mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs"
  63. exit 1
  64. fi
  65. ln -s "${BASE_DIR}/../.." "${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  66. if [[ $? != 0 ]]; then
  67. echo "ln -s ../.. ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  68. exit 1
  69. fi
  70. mkdir -p ${INTERMEDIATE_OUPUT_DIR}
  71. if [[ $? != 0 ]]; then
  72. echo "FAILURE: mkdir -p ${INTERMEDIATE_OUPUT_DIR}"
  73. exit 1
  74. fi
  75. # arg: binary_path
  76. function strip_architectures() {
  77. valid_archs="${VALID_IOS_ARCHS} ${VALID_SIMULATOR_ARCHS}"
  78. ARCHS="$(lipo -info "$1" | rev | cut -d ':' -f1 | rev)"
  79. for ARCH in "${valid_archs}"; do
  80. if ! [[ "${valid_archs}" == *"$ARCH"* ]]; then
  81. echo "Stripping ARCH ${ARCH} from $1"
  82. lipo -remove "$ARCH" -output "$1" "$1"
  83. if [[ $? != 0 ]]; then
  84. echo "FAILURE: lipo $1"
  85. exit 1
  86. fi
  87. fi
  88. done
  89. return 0
  90. }
  91. # Don't use -u, because this path points to our local repo, and we don't want it overridden.
  92. go get -d -v -tags "${BUILD_TAGS}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  93. if [[ $? != 0 ]]; then
  94. echo "FAILURE: go get -d -v -tags "${BUILD_TAGS}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi"
  95. exit 1
  96. fi
  97. #
  98. # Get and install gomobile, using our pinned revision
  99. #
  100. go get -u golang.org/x/mobile/cmd/gomobile
  101. cd ${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  102. git checkout master
  103. git checkout -b pinned ${GOMOBILE_PINNED_REV}
  104. go install
  105. ${GOPATH}/bin/gomobile init -v -x
  106. if [[ $? != 0 ]]; then
  107. echo "FAILURE: ${GOPATH}/bin/gomobile init"
  108. exit 1
  109. fi
  110. #
  111. # gomobile bind
  112. #
  113. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  114. BUILDREPO=$(git config --get remote.origin.url)
  115. BUILDREV=$(git rev-parse --short HEAD)
  116. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  117. GOMOBILEVERSION=$(${GOPATH}/bin/gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  118. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  119. cd ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  120. DEPENDENCIES=$(echo -n "{" && go list -tags "${BUILD_TAGS}" -f '{{range $dep := .Deps}}{{printf "%s\n" $dep}}{{end}}' | xargs go list -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/,$/}/')
  121. LDFLAGS="\
  122. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=${BUILDDATE} \
  123. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=${BUILDREPO} \
  124. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=${BUILDREV} \
  125. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=${GOVERSION} \
  126. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.gomobileVersion=${GOMOBILEVERSION} \
  127. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=${DEPENDENCIES} \
  128. "
  129. echo ""
  130. echo "Variables for ldflags:"
  131. echo " Build date: ${BUILDDATE}"
  132. echo " Build repo: ${BUILDREPO}"
  133. echo " Build revision: ${BUILDREV}"
  134. echo " Go version: ${GOVERSION}"
  135. echo " Gomobile version: ${GOMOBILEVERSION}"
  136. echo ""
  137. # We're using a generated-code prefix to workaround https://github.com/golang/go/issues/18693
  138. ${GOPATH}/bin/gomobile bind -v -x -target ios -prefix Go -tags="${BUILD_TAGS}" -ldflags="${LDFLAGS}" -o "${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  139. rc=$?; if [[ $rc != 0 ]]; then
  140. echo "FAILURE: ${GOPATH}/bin/gomobile bind -target ios -tags="${BUILD_TAGS}" -ldflags="${LDFLAGS}" -o "${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi"
  141. exit $rc
  142. fi
  143. strip_architectures "${FRAMEWORK_BINARY}"
  144. #
  145. # Do the outer framework build using Xcode
  146. #
  147. # Clean previous output
  148. rm -rf "${BUILD_DIR}"
  149. rm -rf "${BUILD_DIR}-SIMULATOR"
  150. # Build the outer framework for phones...
  151. xcodebuild clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGN_ENTITLEMENTS="" CODE_SIGNING_ALLOWED="NO" -configuration Release -sdk iphoneos ONLY_ACTIVE_ARCH=NO -project ${UMBRELLA_FRAMEWORK_XCODE_PROJECT} CONFIGURATION_BUILD_DIR="${BUILD_DIR}"
  152. rc=$?; if [[ $rc != 0 ]]; then
  153. echo "FAILURE: xcodebuild iphoneos"
  154. exit $rc
  155. fi
  156. # ...and for the simulator.
  157. xcodebuild clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGN_ENTITLEMENTS="" CODE_SIGNING_ALLOWED="NO" -configuration Release -sdk iphonesimulator ARCHS=x86_64 VALID_ARCHS=x86_64 ONLY_ACTIVE_ARCH=NO -project ${UMBRELLA_FRAMEWORK_XCODE_PROJECT} CONFIGURATION_BUILD_DIR="${BUILD_DIR}-SIMULATOR"
  158. rc=$?; if [[ $rc != 0 ]]; then
  159. echo "FAILURE: xcodebuild iphonesimulator"
  160. exit $rc
  161. fi
  162. # Add the simulator x86_64 binary into the main framework binary.
  163. lipo -create "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel" "${BUILD_DIR}-SIMULATOR/PsiphonTunnel.framework/PsiphonTunnel" -output "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel"
  164. rc=$?; if [[ $rc != 0 ]]; then
  165. echo "FAILURE: lipo create"
  166. exit $rc
  167. fi
  168. # Delete the temporary simulator build files.
  169. rm -rf "${BUILD_DIR}-SIMULATOR"
  170. # Jenkins loses symlinks from the framework directory, which results in a build
  171. # artifact that is invalid to use in an App Store app. Instead, we will zip the
  172. # resulting build and use that as the artifact.
  173. cd "${BUILD_DIR}"
  174. zip --recurse-paths --symlinks build.zip * --exclude "*.DS_Store"
  175. echo "BUILD DONE"