build-psiphon-framework.sh 8.0 KB

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