build-psiphon-framework.sh 8.0 KB

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