build-psiphon-framework.sh 8.2 KB

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