build-psiphon-framework.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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="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. 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. # Gomobile messes up the build tags by quoting them incorrectly. We'll hack a fix for it.
  109. # First do a grep to see if this code is still there (or has been fixed upstream).
  110. grep -q 'strconv.Quote' ./build.go
  111. if [[ $? != 0 ]]; then
  112. echo "Upstream gomobile code has changed, breaking hacks."
  113. exit 1
  114. fi
  115. # Then do the hack-fix-replacement.
  116. perl -i -pe 's/"-tags="\+strconv\.Quote\(strings.Join\(ctx\.BuildTags, ","\)\),/"-tags",strings.Join(ctx.BuildTags, " "),/g' ./build.go
  117. # We also need to remove the now-unused strconv import.
  118. perl -i -pe 's/"strconv"//g' ./build.go
  119. # Gomobile's iOS code puts an *additional* build tags flag at the end of the command line. This
  120. # overrides any existing build tags and messes up our builds. So we'll hack a fix for that, too.
  121. # First do a grep to see if this code is still there (or has been fixed upstream).
  122. grep -q '"-tags=ios",' ./bind_iosapp.go
  123. if [[ $? != 0 ]]; then
  124. echo "Upstream gomobile code has changed, breaking hacks."
  125. exit 1
  126. fi
  127. # Then do the hack-fix-replacement.
  128. perl -i -pe 's/(.+)"-tags=ios",(.+)/\tctx.BuildTags = append(ctx.BuildTags, "ios")\n\1\2/g' ./bind_iosapp.go
  129. go install
  130. ${GOPATH}/bin/gomobile init -v
  131. if [[ $? != 0 ]]; then
  132. echo "FAILURE: ${GOPATH}/bin/gomobile init -v"
  133. exit 1
  134. fi
  135. #
  136. # gomobile bind
  137. #
  138. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  139. BUILDREPO=$(git config --get remote.origin.url)
  140. BUILDREV=$(git rev-parse --short HEAD)
  141. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  142. GOMOBILEVERSION=$(${GOPATH}/bin/gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  143. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  144. cd ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  145. 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/,$/}/')
  146. LDFLAGS="\
  147. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=${BUILDDATE} \
  148. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=${BUILDREPO} \
  149. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=${BUILDREV} \
  150. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=${GOVERSION} \
  151. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.gomobileVersion=${GOMOBILEVERSION} \
  152. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=${DEPENDENCIES} \
  153. "
  154. echo ""
  155. echo "Variables for ldflags:"
  156. echo " Build date: ${BUILDDATE}"
  157. echo " Build repo: ${BUILDREPO}"
  158. echo " Build revision: ${BUILDREV}"
  159. echo " Go version: ${GOVERSION}"
  160. echo " Gomobile version: ${GOMOBILEVERSION}"
  161. echo ""
  162. # Patch source files to build on Darwin
  163. IOS_CGO_BUILD_FLAGS='// #cgo darwin CFLAGS: -I'"${OPENSSL_INCLUDE}"'\
  164. // #cgo darwin LDFLAGS:'"${LIBSSL}"'\
  165. // #cgo darwin LDFLAGS:'"${LIBCRYPTO}"''
  166. LC_ALL=C sed -i -- "s|// #cgo pkg-config: libssl|${IOS_CGO_BUILD_FLAGS}|" "${OPENSSL_SRC_DIR}/build.go"
  167. ${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
  168. rc=$?; if [[ $rc != 0 ]]; then
  169. 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"
  170. exit $rc
  171. fi
  172. strip_architectures "${FRAMEWORK_BINARY}"
  173. #
  174. # Do the outer framework build using Xcode
  175. #
  176. # Clean previous output
  177. rm -rf "${BUILD_DIR}"
  178. rm -rf "${BUILD_DIR}-SIMULATOR"
  179. # Build the outer framework for phones...
  180. 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}"
  181. rc=$?; if [[ $rc != 0 ]]; then
  182. echo "FAILURE: xcodebuild iphoneos"
  183. exit $rc
  184. fi
  185. # ...and for the simulator.
  186. 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"
  187. rc=$?; if [[ $rc != 0 ]]; then
  188. echo "FAILURE: xcodebuild iphonesimulator"
  189. exit $rc
  190. fi
  191. # Add the simulator x86_64 binary into the main framework binary.
  192. lipo -create "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel" "${BUILD_DIR}-SIMULATOR/PsiphonTunnel.framework/PsiphonTunnel" -output "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel"
  193. rc=$?; if [[ $rc != 0 ]]; then
  194. echo "FAILURE: lipo create"
  195. exit $rc
  196. fi
  197. # Delete the temporary simulator build files.
  198. rm -rf "${BUILD_DIR}-SIMULATOR"
  199. # Jenkins loses symlinks from the framework directory, which results in a build
  200. # artifact that is invalid to use in an App Store app. Instead, we will zip the
  201. # resulting build and use that as the artifact.
  202. cd "${BUILD_DIR}"
  203. zip --recurse-paths --symlinks build.zip * --exclude "*.DS_Store"
  204. echo "BUILD DONE"