build-psiphon-framework.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/env bash
  2. # This script takes one optional argument: 'private', if private plugins should
  3. # be used. It should be omitted if private plugins are not desired.
  4. if [[ $1 == "private" ]]; then
  5. FORCE_PRIVATE_PLUGINS=true
  6. echo "TRUE"
  7. else
  8. FORCE_PRIVATE_PLUGINS=false
  9. echo "FALSE"
  10. fi
  11. # -x echos commands. -u exits if an unintialized variable is used.
  12. # -e exits if a command returns an error.
  13. set -x -u -e
  14. # Reset the PATH to macOS default. This is mainly so we don't execute the wrong
  15. # gomobile executable.
  16. PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
  17. BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P)
  18. cd ${BASE_DIR}
  19. # The location of the final framework build
  20. BUILD_DIR="${BASE_DIR}/build"
  21. # Ensure go is installed
  22. which go 2>&1 > /dev/null
  23. if [[ $? != 0 ]]; then
  24. echo "Go is not installed in the path, aborting"
  25. exit 1
  26. fi
  27. VALID_IOS_ARCHS="arm64 armv7 armv7s"
  28. VALID_SIMULATOR_ARCHS="x86_64"
  29. FRAMEWORK="Psi"
  30. INTERMEDIATE_OUPUT_DIR="${BASE_DIR}/PsiphonTunnel/PsiphonTunnel"
  31. INTERMEDIATE_OUPUT_FILE="${FRAMEWORK}.framework"
  32. FRAMEWORK_BINARY="${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}/Versions/A/${FRAMEWORK}"
  33. # The "OPENSSL" tag enables support of OpenSSL for use by IndistinguishableTLS.
  34. PRIVATE_PLUGINS_TAG=""
  35. if [[ ${FORCE_PRIVATE_PLUGINS} == true ]]; then PRIVATE_PLUGINS_TAG="PRIVATE_PLUGINS"; fi
  36. BUILD_TAGS="OPENSSL IOS ${PRIVATE_PLUGINS_TAG}"
  37. LIBSSL=${BASE_DIR}/OpenSSL-for-iPhone/lib/libssl.a
  38. LIBCRYPTO=${BASE_DIR}/OpenSSL-for-iPhone/lib/libcrypto.a
  39. OPENSSL_INCLUDE=${BASE_DIR}/OpenSSL-for-iPhone/include/
  40. UMBRELLA_FRAMEWORK_XCODE_PROJECT=${BASE_DIR}/PsiphonTunnel/PsiphonTunnel.xcodeproj/
  41. TRUSTED_ROOT_CA_FILE=${BASE_DIR}/PsiphonTunnel/PsiphonTunnel/rootCAs.txt
  42. # Download trustedroot CAs off curl website, see https://curl.haxx.se/docs/caextract.html for details
  43. curl -o $TRUSTED_ROOT_CA_FILE https://curl.haxx.se/ca/cacert.pem
  44. if [[ $? != 0 ]]; then
  45. echo "FAILURE: curl -o $TRUSTED_ROOT_CA_FILE https://curl.haxx.se/ca/cacert.pem"
  46. exit 1
  47. fi
  48. # Exporting these seems necessary for subcommands to pick them up.
  49. export GOPATH=${PWD}/go-ios-build
  50. export PATH=${GOPATH}/bin:${PATH}
  51. # The GOPATH we're using is temporary, so make sure there isn't one from a previous run.
  52. rm -rf ${GOPATH}
  53. # When updating the pinned rev, you will have to manually delete go-ios-build
  54. GOMOBILE_PINNED_REV=a0f998b2d8c7ee81ddbead9202dd5e0184a998ad
  55. GOMOBILE_PATH=${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  56. TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core
  57. OPENSSL_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Inc/openssl
  58. PATH=${PATH}:${GOPATH}/bin
  59. mkdir -p ${GOPATH}
  60. if [[ $? != 0 ]]; then
  61. echo "FAILURE: mkdir -p ${GOPATH}"
  62. exit 1
  63. fi
  64. # Symlink the current source directory into GOPATH, so that we're building the
  65. # code in this local repo, rather than pulling from Github and building that.
  66. mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs
  67. if [[ $? != 0 ]]; then
  68. echo "mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs"
  69. exit 1
  70. fi
  71. ln -s "${BASE_DIR}/../.." "${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  72. if [[ $? != 0 ]]; then
  73. echo "ln -s ../.. ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  74. exit 1
  75. fi
  76. mkdir -p ${INTERMEDIATE_OUPUT_DIR}
  77. if [[ $? != 0 ]]; then
  78. echo "FAILURE: mkdir -p ${INTERMEDIATE_OUPUT_DIR}"
  79. exit 1
  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. #
  112. # Get and install gomobile, using our pinned revision
  113. #
  114. go get -u golang.org/x/mobile/cmd/gomobile
  115. cd ${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  116. git checkout master
  117. git checkout -b pinned ${GOMOBILE_PINNED_REV}
  118. go install
  119. ${GOPATH}/bin/gomobile init -v
  120. if [[ $? != 0 ]]; then
  121. echo "FAILURE: ${GOPATH}/bin/gomobile init -v"
  122. exit 1
  123. fi
  124. #
  125. # gomobile bind
  126. #
  127. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  128. BUILDREPO=$(git config --get remote.origin.url)
  129. BUILDREV=$(git rev-parse --short HEAD)
  130. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  131. GOMOBILEVERSION=$(${GOPATH}/bin/gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  132. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  133. cd ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  134. 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/,$/}/')
  135. LDFLAGS="\
  136. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=${BUILDDATE} \
  137. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=${BUILDREPO} \
  138. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=${BUILDREV} \
  139. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=${GOVERSION} \
  140. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.gomobileVersion=${GOMOBILEVERSION} \
  141. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=${DEPENDENCIES} \
  142. "
  143. echo ""
  144. echo "Variables for ldflags:"
  145. echo " Build date: ${BUILDDATE}"
  146. echo " Build repo: ${BUILDREPO}"
  147. echo " Build revision: ${BUILDREV}"
  148. echo " Go version: ${GOVERSION}"
  149. echo " Gomobile version: ${GOMOBILEVERSION}"
  150. echo ""
  151. # Patch source files to build on Darwin
  152. IOS_CGO_BUILD_FLAGS='// #cgo darwin CFLAGS: -I'"${OPENSSL_INCLUDE}"'\
  153. // #cgo darwin LDFLAGS:'"${LIBSSL}"'\
  154. // #cgo darwin LDFLAGS:'"${LIBCRYPTO}"''
  155. LC_ALL=C sed -i -- "s|// #cgo pkg-config: libssl|${IOS_CGO_BUILD_FLAGS}|" "${OPENSSL_SRC_DIR}/build.go"
  156. # We're using a generated-code prefix to workaround https://github.com/golang/go/issues/18693
  157. ${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
  158. rc=$?; if [[ $rc != 0 ]]; then
  159. 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"
  160. exit $rc
  161. fi
  162. strip_architectures "${FRAMEWORK_BINARY}"
  163. #
  164. # Do the outer framework build using Xcode
  165. #
  166. # Clean previous output
  167. rm -rf "${BUILD_DIR}"
  168. rm -rf "${BUILD_DIR}-SIMULATOR"
  169. # Build the outer framework for phones...
  170. 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}"
  171. rc=$?; if [[ $rc != 0 ]]; then
  172. echo "FAILURE: xcodebuild iphoneos"
  173. exit $rc
  174. fi
  175. # ...and for the simulator.
  176. 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"
  177. rc=$?; if [[ $rc != 0 ]]; then
  178. echo "FAILURE: xcodebuild iphonesimulator"
  179. exit $rc
  180. fi
  181. # Add the simulator x86_64 binary into the main framework binary.
  182. lipo -create "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel" "${BUILD_DIR}-SIMULATOR/PsiphonTunnel.framework/PsiphonTunnel" -output "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel"
  183. rc=$?; if [[ $rc != 0 ]]; then
  184. echo "FAILURE: lipo create"
  185. exit $rc
  186. fi
  187. # Delete the temporary simulator build files.
  188. rm -rf "${BUILD_DIR}-SIMULATOR"
  189. # Jenkins loses symlinks from the framework directory, which results in a build
  190. # artifact that is invalid to use in an App Store app. Instead, we will zip the
  191. # resulting build and use that as the artifact.
  192. cd "${BUILD_DIR}"
  193. zip --recurse-paths --symlinks build.zip * --exclude "*.DS_Store"
  194. echo "BUILD DONE"