build-psiphon-framework.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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:/usr/local/go/bin
  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=""
  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=a0f998b2d8c7ee81ddbead9202dd5e0184a998ad
  45. GOMOBILE_PATH=${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  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. # 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. if [[ $? != 0 ]]; then
  80. echo "FAILURE: lipo $1"
  81. exit 1
  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 -tags "${BUILD_TAGS}" github.com/Psiphon-Inc/openssl
  91. if [[ $? != 0 ]]; then
  92. echo "FAILURE: go get -d -u -v -tags "${BUILD_TAGS}" github.com/Psiphon-Inc/openssl"
  93. exit 1
  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 -tags "${BUILD_TAGS}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  97. if [[ $? != 0 ]]; then
  98. echo "FAILURE: go get -d -v -tags "${BUILD_TAGS}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi"
  99. exit 1
  100. fi
  101. #
  102. # Get and install gomobile, using our pinned revision
  103. #
  104. go get -u golang.org/x/mobile/cmd/gomobile
  105. cd ${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  106. git checkout master
  107. git checkout -b pinned ${GOMOBILE_PINNED_REV}
  108. go install
  109. ${GOPATH}/bin/gomobile init -v
  110. if [[ $? != 0 ]]; then
  111. echo "FAILURE: ${GOPATH}/bin/gomobile init -v"
  112. exit 1
  113. fi
  114. #
  115. # gomobile bind
  116. #
  117. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  118. BUILDREPO=$(git config --get remote.origin.url)
  119. BUILDREV=$(git rev-parse --short HEAD)
  120. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  121. GOMOBILEVERSION=$(${GOPATH}/bin/gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  122. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  123. cd ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  124. DEPENDENCIES=$(echo -n "{" && go list -tags "${BUILD_TAGS}" -f '{{range $dep := .Deps}}{{printf "%s\n" $dep}}{{end}}' | xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}' | xargs -I pkg bash -c 'cd $GOPATH/src/pkg && echo -n "\"pkg\":\"$(git rev-parse --short HEAD)\","' | sed 's/,$/}/')
  125. LDFLAGS="\
  126. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=${BUILDDATE} \
  127. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=${BUILDREPO} \
  128. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=${BUILDREV} \
  129. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=${GOVERSION} \
  130. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.gomobileVersion=${GOMOBILEVERSION} \
  131. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=${DEPENDENCIES} \
  132. "
  133. echo ""
  134. echo "Variables for ldflags:"
  135. echo " Build date: ${BUILDDATE}"
  136. echo " Build repo: ${BUILDREPO}"
  137. echo " Build revision: ${BUILDREV}"
  138. echo " Go version: ${GOVERSION}"
  139. echo " Gomobile version: ${GOMOBILEVERSION}"
  140. echo ""
  141. # Patch source files to build on Darwin
  142. IOS_CGO_BUILD_FLAGS='// #cgo darwin CFLAGS: -I'"${OPENSSL_INCLUDE}"'\
  143. // #cgo darwin LDFLAGS:'"${LIBSSL}"'\
  144. // #cgo darwin LDFLAGS:'"${LIBCRYPTO}"''
  145. LC_ALL=C sed -i -- "s|// #cgo pkg-config: libssl|${IOS_CGO_BUILD_FLAGS}|" "${OPENSSL_SRC_DIR}/build.go"
  146. # We're using a generated-code prefix to workaround https://github.com/golang/go/issues/18693
  147. ${GOPATH}/bin/gomobile bind -v -x -target ios -prefix Go -tags="${BUILD_TAGS}" -ldflags="${LDFLAGS}" -o "${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  148. rc=$?; if [[ $rc != 0 ]]; then
  149. echo "FAILURE: ${GOPATH}/bin/gomobile bind -target ios -tags="${BUILD_TAGS}" -ldflags="${LDFLAGS}" -o "${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi"
  150. exit $rc
  151. fi
  152. strip_architectures "${FRAMEWORK_BINARY}"
  153. #
  154. # Do the outer framework build using Xcode
  155. #
  156. # Clean previous output
  157. rm -rf "${BUILD_DIR}"
  158. rm -rf "${BUILD_DIR}-SIMULATOR"
  159. # Build the outer framework for phones...
  160. xcodebuild clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGN_ENTITLEMENTS="" CODE_SIGNING_ALLOWED="NO" -configuration Release -sdk iphoneos ONLY_ACTIVE_ARCH=NO -project ${UMBRELLA_FRAMEWORK_XCODE_PROJECT} CONFIGURATION_BUILD_DIR="${BUILD_DIR}"
  161. rc=$?; if [[ $rc != 0 ]]; then
  162. echo "FAILURE: xcodebuild iphoneos"
  163. exit $rc
  164. fi
  165. # ...and for the simulator.
  166. xcodebuild clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGN_ENTITLEMENTS="" CODE_SIGNING_ALLOWED="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"
  167. rc=$?; if [[ $rc != 0 ]]; then
  168. echo "FAILURE: xcodebuild iphonesimulator"
  169. exit $rc
  170. fi
  171. # Add the simulator x86_64 binary into the main framework binary.
  172. lipo -create "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel" "${BUILD_DIR}-SIMULATOR/PsiphonTunnel.framework/PsiphonTunnel" -output "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel"
  173. rc=$?; if [[ $rc != 0 ]]; then
  174. echo "FAILURE: lipo create"
  175. exit $rc
  176. fi
  177. # Delete the temporary simulator build files.
  178. rm -rf "${BUILD_DIR}-SIMULATOR"
  179. # Jenkins loses symlinks from the framework directory, which results in a build
  180. # artifact that is invalid to use in an App Store app. Instead, we will zip the
  181. # resulting build and use that as the artifact.
  182. cd "${BUILD_DIR}"
  183. zip --recurse-paths --symlinks build.zip * --exclude "*.DS_Store"
  184. echo "BUILD DONE"