build-psiphon-framework.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/usr/bin/env bash
  2. set -e -u -x
  3. if [ -z ${1+x} ]; then BUILD_TAGS=""; else BUILD_TAGS="$1"; fi
  4. # Modify this value as we use newer Go versions.
  5. GO_VERSION_REQUIRED="1.13.7"
  6. # At this time, gomobile doesn't support modules
  7. export GO111MODULE=off
  8. # Reset the PATH to macOS default. This is mainly so we don't execute the wrong
  9. # gomobile executable.
  10. PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
  11. # $GOROOT/bin allows build automation to provide various Go versions dynamically.
  12. # As gomobile would be installed at $GOPATH/bin, there is minimal risk that
  13. # adding $GOROOT/bin will run an unexpected gomobile binary.
  14. PATH=$GOROOT/bin:$PATH
  15. BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P)
  16. cd ${BASE_DIR}
  17. # The location of the final framework build
  18. BUILD_DIR="${BASE_DIR}/build"
  19. # Ensure go is installed
  20. which go 2>&1 > /dev/null
  21. if [[ $? != 0 ]]; then
  22. echo "Go is not installed in the path, aborting"
  23. exit 1
  24. fi
  25. VALID_IOS_ARCHS="arm64 armv7 armv7s"
  26. VALID_SIMULATOR_ARCHS="x86_64"
  27. FRAMEWORK="Psi"
  28. INTERMEDIATE_OUPUT_DIR="${BASE_DIR}/PsiphonTunnel/PsiphonTunnel"
  29. INTERMEDIATE_OUPUT_FILE="${FRAMEWORK}.framework"
  30. FRAMEWORK_BINARY="${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}/Versions/A/${FRAMEWORK}"
  31. UMBRELLA_FRAMEWORK_XCODE_PROJECT=${BASE_DIR}/PsiphonTunnel/PsiphonTunnel.xcodeproj/
  32. # Exporting these seems necessary for subcommands to pick them up.
  33. export GOPATH=${PWD}/go-ios-build
  34. export PATH=${GOPATH}/bin:${PATH}
  35. # The GOPATH we're using is temporary, so make sure there isn't one from a previous run.
  36. rm -rf ${GOPATH}
  37. GOMOBILE_PINNED_REV=92f3b9caf7ba8f4f9c10074225afcba0cba47a62
  38. GOMOBILE_PATH=${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  39. TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core
  40. PATH=${PATH}:${GOPATH}/bin
  41. mkdir -p ${GOPATH}
  42. if [[ $? != 0 ]]; then
  43. echo "FAILURE: mkdir -p ${GOPATH}"
  44. exit 1
  45. fi
  46. # Symlink the current source directory into GOPATH, so that we're building the
  47. # code in this local repo, rather than pulling from Github and building that.
  48. mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs
  49. if [[ $? != 0 ]]; then
  50. echo "mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs"
  51. exit 1
  52. fi
  53. ln -s "${BASE_DIR}/../.." "${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  54. if [[ $? != 0 ]]; then
  55. echo "ln -s ../.. ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  56. exit 1
  57. fi
  58. mkdir -p ${INTERMEDIATE_OUPUT_DIR}
  59. if [[ $? != 0 ]]; then
  60. echo "FAILURE: mkdir -p ${INTERMEDIATE_OUPUT_DIR}"
  61. exit 1
  62. fi
  63. # arg: binary_path
  64. function strip_architectures() {
  65. valid_archs="${VALID_IOS_ARCHS} ${VALID_SIMULATOR_ARCHS}"
  66. ARCHS="$(lipo -info "$1" | rev | cut -d ':' -f1 | rev)"
  67. for ARCH in "${valid_archs}"; do
  68. if ! [[ "${valid_archs}" == *"$ARCH"* ]]; then
  69. echo "Stripping ARCH ${ARCH} from $1"
  70. lipo -remove "$ARCH" -output "$1" "$1"
  71. if [[ $? != 0 ]]; then
  72. echo "FAILURE: lipo $1"
  73. exit 1
  74. fi
  75. fi
  76. done
  77. return 0
  78. }
  79. # Check Go version
  80. GO_VERSION=$(go version | sed -E -n 's/.*go([0-9]\.[0-9]+(\.[0-9]+)?).*/\1/p')
  81. if [[ ${GO_VERSION} != ${GO_VERSION_REQUIRED} ]]; then
  82. echo "FAILURE: go version mismatch; require ${GO_VERSION_REQUIRED}; got ${GO_VERSION}"
  83. exit 1
  84. fi
  85. # Don't use -u, because this path points to our local repo, and we don't want it overridden.
  86. go get -d -v -tags "${BUILD_TAGS}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  87. if [[ $? != 0 ]]; then
  88. echo "FAILURE: go get -d -v -tags "${BUILD_TAGS}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi"
  89. exit 1
  90. fi
  91. #
  92. # Get and install gomobile, using our pinned revision
  93. #
  94. go get -u golang.org/x/mobile/cmd/gomobile
  95. cd ${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  96. git checkout master
  97. git checkout -b pinned ${GOMOBILE_PINNED_REV}
  98. go install
  99. ${GOPATH}/bin/gomobile init -v -x
  100. if [[ $? != 0 ]]; then
  101. echo "FAILURE: ${GOPATH}/bin/gomobile init"
  102. exit 1
  103. fi
  104. #
  105. # gomobile bind
  106. #
  107. # Ensure BUILD* variables reflect the tunnel-core repo
  108. cd ${TUNNEL_CORE_SRC_DIR}
  109. BUILDINFOFILE="${BASE_DIR}/psiphon-tunnel-core_buildinfo.txt"
  110. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  111. BUILDREPO=$(git config --get remote.origin.url)
  112. BUILDREV=$(git rev-parse --short HEAD)
  113. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  114. GOMOBILEVERSION=$(${GOPATH}/bin/gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  115. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  116. cd ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  117. DEPENDENCIES=$(echo -n "{" && GOOS=darwin go list -tags "${BUILD_TAGS}" -f '{{range $dep := .Deps}}{{printf "%s\n" $dep}}{{end}}' | GOOS=darwin xargs go list -tags "${BUILD_TAGS}" -f '{{if not .Standard}}{{.ImportPath}}{{end}}' | xargs -I pkg bash -c 'PKG=pkg && cd $GOPATH/src/$PKG && echo -n "\"$PKG\":\"$(git rev-parse --short HEAD)\","' | sed 's/,$/}/')
  118. LDFLAGS="\
  119. -s \
  120. -w \
  121. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=${BUILDDATE} \
  122. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=${BUILDREPO} \
  123. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=${BUILDREV} \
  124. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=${GOVERSION} \
  125. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.gomobileVersion=${GOMOBILEVERSION} \
  126. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=${DEPENDENCIES} \
  127. "
  128. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > $BUILDINFOFILE
  129. echo ""
  130. echo "Variables for ldflags:"
  131. echo " Build date: ${BUILDDATE}"
  132. echo " Build repo: ${BUILDREPO}"
  133. echo " Build revision: ${BUILDREV}"
  134. echo " Go version: ${GOVERSION}"
  135. echo " Gomobile version: ${GOMOBILEVERSION}"
  136. echo ""
  137. # We're using a generated-code prefix to workaround https://github.com/golang/go/issues/18693
  138. # CGO_CFLAGS_ALLOW is to workaround https://github.com/golang/go/issues/23742 in Go 1.9.4
  139. CGO_CFLAGS_ALLOW="-fmodules|-fblocks" ${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
  140. rc=$?; if [[ $rc != 0 ]]; then
  141. 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"
  142. exit $rc
  143. fi
  144. strip_architectures "${FRAMEWORK_BINARY}"
  145. #
  146. # Do the outer framework build using Xcode
  147. #
  148. # Clean previous output
  149. rm -rf "${BUILD_DIR}"
  150. rm -rf "${BUILD_DIR}-SIMULATOR"
  151. # Build the outer framework for phones...
  152. 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}"
  153. rc=$?; if [[ $rc != 0 ]]; then
  154. echo "FAILURE: xcodebuild iphoneos"
  155. exit $rc
  156. fi
  157. # ...and for the simulator.
  158. 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"
  159. rc=$?; if [[ $rc != 0 ]]; then
  160. echo "FAILURE: xcodebuild iphonesimulator"
  161. exit $rc
  162. fi
  163. # Add the simulator x86_64 binary into the main framework binary.
  164. lipo -create "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel" "${BUILD_DIR}-SIMULATOR/PsiphonTunnel.framework/PsiphonTunnel" -output "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel"
  165. rc=$?; if [[ $rc != 0 ]]; then
  166. echo "FAILURE: lipo create"
  167. exit $rc
  168. fi
  169. # Delete the temporary simulator build files.
  170. rm -rf "${BUILD_DIR}-SIMULATOR"
  171. # Jenkins loses symlinks from the framework directory, which results in a build
  172. # artifact that is invalid to use in an App Store app. Instead, we will zip the
  173. # resulting build and use that as the artifact.
  174. cd "${BUILD_DIR}"
  175. zip --recurse-paths --symlinks build.zip * --exclude "*.DS_Store"
  176. echo "BUILD DONE"