build-psiphon-framework.sh 8.3 KB

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