build-psiphon-framework.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/env bash
  2. set -x
  3. # Reset the PATH to macOS default. This is mainly so we don't execute the wrong
  4. # gomobile executable.
  5. PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
  6. BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P)
  7. cd ${BASE_DIR}
  8. # The location of the final framework build
  9. BUILD_DIR="${BASE_DIR}/build"
  10. # Ensure go is installed
  11. which go 2>&1 > /dev/null
  12. if [ $? -ne 0 ]; then
  13. echo "Go is not installed in the path, aborting"
  14. exit 1
  15. fi
  16. VALID_IOS_ARCHS="arm64 armv7 armv7s"
  17. VALID_SIMULATOR_ARCHS="x86_64"
  18. FRAMEWORK="Psi"
  19. INTERMEDIATE_OUPUT_DIR="${BASE_DIR}/PsiphonTunnel/PsiphonTunnel"
  20. INTERMEDIATE_OUPUT_FILE="${FRAMEWORK}.framework"
  21. FRAMEWORK_BINARY="${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}/Versions/A/${FRAMEWORK}"
  22. LIBSSL=${BASE_DIR}/OpenSSL-for-iPhone/lib/libssl.a
  23. LIBCRYPTO=${BASE_DIR}/OpenSSL-for-iPhone/lib/libcrypto.a
  24. OPENSSL_INCLUDE=${BASE_DIR}/OpenSSL-for-iPhone/include/
  25. UMBRELLA_FRAMEWORK_XCODE_PROJECT=${BASE_DIR}/PsiphonTunnel/PsiphonTunnel.xcodeproj/
  26. TRUSTED_ROOT_CA_FILE=${BASE_DIR}/PsiphonTunnel/PsiphonTunnel/rootCAs.txt
  27. # Download trustedroot CAs off curl website, see https://curl.haxx.se/docs/caextract.html for details
  28. curl -o $TRUSTED_ROOT_CA_FILE https://curl.haxx.se/ca/cacert.pem
  29. rc=$?; if [[ $rc != 0 ]]; then
  30. echo "FAILURE: curl -o $TRUSTED_ROOT_CA_FILE https://curl.haxx.se/ca/cacert.pem"
  31. exit $rc
  32. fi
  33. # Exporting these seems necessary for subcommands to pick them up.
  34. export GOPATH=${PWD}/go-ios-build
  35. export PATH=${GOPATH}/bin:${PATH}
  36. # The GOPATH we're using is temporary, so make sure there isn't one from a previous run.
  37. rm -rf ${GOPATH}
  38. # When updating the pinned rev, you will have to manually delete go-ios-build
  39. GOMOBILE_PINNED_REV=aa9922ad4c79ee8a56cd45bf433f2aa943712b09
  40. GOMOBILE_PATH=${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  41. IOS_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-ios
  42. TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core
  43. OPENSSL_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Inc/openssl
  44. PATH=${PATH}:${GOPATH}/bin
  45. mkdir -p ${GOPATH}
  46. rc=$?; if [[ $rc != 0 ]]; then
  47. echo "FAILURE: mkdir -p ${GOPATH}"
  48. exit $rc
  49. fi
  50. # Symlink the current source directory into GOPATH, so that we're building the
  51. # code in this local repo, rather than pulling from Github and building that.
  52. mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs
  53. rc=$?; if [[ $rc != 0 ]]; then
  54. echo "mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs"
  55. exit $rc
  56. fi
  57. ln -s "${BASE_DIR}/../.." "${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  58. rc=$?; if [[ $rc != 0 ]]; then
  59. echo "ln -s ../.. ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  60. exit $rc
  61. fi
  62. mkdir -p ${INTERMEDIATE_OUPUT_DIR}
  63. rc=$?; if [[ $rc != 0 ]]; then
  64. echo "FAILURE: mkdir -p ${INTERMEDIATE_OUPUT_DIR}"
  65. exit $rc
  66. fi
  67. if [ ! -e ${IOS_SRC_DIR} ]; then
  68. echo "iOS source directory (${IOS_SRC_DIR}) not found, creating link"
  69. mkdir -p $(dirname ${IOS_SRC_DIR})
  70. ln -s $(pwd -P) $IOS_SRC_DIR
  71. if [ $? -ne 0 ]; then
  72. echo "..Could not create symlink, aborting"
  73. exit 1
  74. fi
  75. fi
  76. # arg: binary_path
  77. function strip_architectures() {
  78. valid_archs="${VALID_IOS_ARCHS} ${VALID_SIMULATOR_ARCHS}"
  79. ARCHS="$(lipo -info "$1" | rev | cut -d ':' -f1 | rev)"
  80. for ARCH in "${valid_archs}"; do
  81. if ! [[ "${valid_archs}" == *"$ARCH"* ]]; then
  82. echo "Stripping ARCH ${ARCH} from $1"
  83. lipo -remove "$ARCH" -output "$1" "$1"
  84. rc=$?; if [[ $rc != 0 ]]; then
  85. echo "FAILURE: lipo $1"
  86. exit $rc
  87. fi
  88. fi
  89. done
  90. return 0
  91. }
  92. cd OpenSSL-for-iPhone && ./build-libssl.sh; cd -
  93. strip_architectures "${LIBSSL}"
  94. strip_architectures "${LIBCRYPTO}"
  95. go get -d -u -v github.com/Psiphon-Inc/openssl
  96. rc=$?; if [[ $rc != 0 ]]; then
  97. echo "FAILURE: go get -d -u -v github.com/Psiphon-Inc/openssl"
  98. exit $rc
  99. fi
  100. # Don't use -u, because this path points to our local repo, and we don't want it overridden.
  101. go get -d -v github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  102. rc=$?; if [[ $rc != 0 ]]; then
  103. echo "FAILURE: go get -d -v github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi"
  104. exit $rc
  105. fi
  106. function check_pinned_version() {
  107. echo "Checking for gomobile revision: '${GOMOBILE_PINNED_REV}'"
  108. if [ -e ${GOMOBILE_PATH} ]; then
  109. echo "..Gomobile path found"
  110. cd ${GOMOBILE_PATH}
  111. CURRENT_REVISION=$(git rev-parse HEAD)
  112. if [ ${CURRENT_REVISION} != ${GOMOBILE_PINNED_REV} ]; then
  113. echo "..Current revision '${CURRENT_REVISION}' does not match"
  114. return 1
  115. else
  116. echo "..Current revision matches"
  117. return 0
  118. fi
  119. else
  120. echo "Could not find GOMOBILE_PATH (${GOMOBILE_PATH})"
  121. return 1
  122. fi
  123. }
  124. check_pinned_version
  125. if [ $? -ne 0 ]; then
  126. go get -u golang.org/x/mobile/cmd/gomobile
  127. cd ${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  128. git checkout master
  129. git branch -d pinned
  130. git checkout -b pinned ${GOMOBILE_PINNED_REV}
  131. go install
  132. ${GOPATH}/bin/gomobile init -v
  133. if [ $? -ne 0 ]; then
  134. echo "FAILURE: ${GOPATH}/bin/gomobile init -v"
  135. exit 1
  136. fi
  137. check_pinned_version
  138. if [ $? -ne 0 ]; then
  139. echo "gomobile not found, aborting"
  140. exit 1
  141. fi
  142. fi
  143. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  144. BUILDREPO=$(git config --get remote.origin.url)
  145. BUILDREV=$(git rev-parse --short HEAD)
  146. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  147. GOMOBILEVERSION=$(${GOPATH}/bin/gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  148. LDFLAGS="\
  149. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=${BUILDDATE} \
  150. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=${BUILDREPO} \
  151. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=${BUILDREV} \
  152. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=${GOVERSION} \
  153. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.gomobileVersion=${GOMOBILEVERSION} \
  154. "
  155. echo ""
  156. echo "Variables for ldflags:"
  157. echo " Build date: ${BUILDDATE}"
  158. echo " Build repo: ${BUILDREPO}"
  159. echo " Build revision: ${BUILDREV}"
  160. echo " Go version: ${GOVERSION}"
  161. echo " Gomobile version: ${GOMOBILEVERSION}"
  162. echo ""
  163. # Patch source files to build on Darwin
  164. IOS_CGO_BUILD_FLAGS='// #cgo darwin CFLAGS: -I'"${OPENSSL_INCLUDE}"'\
  165. // #cgo darwin LDFLAGS:'"${LIBSSL}"'\
  166. // #cgo darwin LDFLAGS:'"${LIBCRYPTO}"''
  167. LC_ALL=C sed -i -- "s|// #cgo pkg-config: libssl|${IOS_CGO_BUILD_FLAGS}|" "${OPENSSL_SRC_DIR}/build.go"
  168. ${GOPATH}/bin/gomobile bind -target ios -ldflags="${LDFLAGS}" -o "${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  169. rc=$?; if [[ $rc != 0 ]]; then
  170. echo "FAILURE: ${GOPATH}/bin/gomobile bind -target ios -ldflags="${LDFLAGS}" -o "${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi"
  171. exit $rc
  172. fi
  173. strip_architectures "${FRAMEWORK_BINARY}"
  174. #
  175. # Do the outer framework build using Xcode
  176. #
  177. # Clean previous output
  178. rm -rf "${BUILD_DIR}"
  179. rm -rf "${BUILD_DIR}-SIMULATOR"
  180. # Build the outer framework for phones...
  181. xcodebuild clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO -configuration Release -sdk iphoneos ONLY_ACTIVE_ARCH=NO -project ${UMBRELLA_FRAMEWORK_XCODE_PROJECT} CONFIGURATION_BUILD_DIR="${BUILD_DIR}"
  182. rc=$?; if [[ $rc != 0 ]]; then
  183. echo "FAILURE: xcodebuild iphoneos"
  184. exit $rc
  185. fi
  186. # ...and for the simulator.
  187. xcodebuild clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=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"
  188. rc=$?; if [[ $rc != 0 ]]; then
  189. echo "FAILURE: xcodebuild iphonesimulator"
  190. exit $rc
  191. fi
  192. # Add the simulator x86_64 binary into the main framework binary.
  193. lipo -create "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel" "${BUILD_DIR}-SIMULATOR/PsiphonTunnel.framework/PsiphonTunnel" -output "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel"
  194. rc=$?; if [[ $rc != 0 ]]; then
  195. echo "FAILURE: lipo create"
  196. exit $rc
  197. fi
  198. # Delete the temporary simulator build files.
  199. rm -rf "${BUILD_DIR}-SIMULATOR"
  200. echo "BUILD DONE"