build-psiphon-framework.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.17.9"
  6. # At this time, psiphon-tunnel-core 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. UMBRELLA_FRAMEWORK_XCODE_PROJECT=${BASE_DIR}/PsiphonTunnel/PsiphonTunnel.xcodeproj/
  26. # Exporting these seems necessary for subcommands to pick them up.
  27. export GOPATH=${PWD}/go-ios-build
  28. export PATH=${GOPATH}/bin:${PATH}
  29. # The GOPATH we're using is temporary, so make sure there isn't one from a previous run.
  30. rm -rf "${GOPATH}"
  31. GOMOBILE_PINNED_REV=ce6a79cf6a13dd77095a6f8dbee5f39848fa7da1
  32. GOMOBILE_PATH=${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  33. TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core
  34. PATH=${PATH}:${GOPATH}/bin
  35. mkdir -p "${GOPATH}"
  36. if [[ $? != 0 ]]; then
  37. echo "FAILURE: mkdir -p ${GOPATH}"
  38. exit 1
  39. fi
  40. # Symlink the current source directory into GOPATH, so that we're building the
  41. # code in this local repo, rather than pulling from Github and building that.
  42. mkdir -p "${GOPATH}/src/github.com/Psiphon-Labs"
  43. if [[ $? != 0 ]]; then
  44. echo "mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs"
  45. exit 1
  46. fi
  47. ln -s "${BASE_DIR}/../.." "${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  48. if [[ $? != 0 ]]; then
  49. echo "ln -s ../.. ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  50. exit 1
  51. fi
  52. # Builds Psi.framework library for the given platform.
  53. #
  54. # - Parameter 1: `gomobile bind` -target option value
  55. # - Parameter 2: Variable name where gomobile output will be set to.
  56. function gomobile_build_for_platform() {
  57. # Possible values are "ios" and "simulator"
  58. local TARGETS=$1
  59. echo "Build library for targets ${TARGETS}"
  60. local GOBIND_OUT="${BUILD_DIR}/gobind-framework/Psi.xcframework"
  61. # We're using a generated-code prefix to workaround https://github.com/golang/go/issues/18693
  62. # CGO_CFLAGS_ALLOW is to workaround https://github.com/golang/go/issues/23742 in Go 1.9.4
  63. CGO_CFLAGS_ALLOW="-fmodules|-fblocks" "${GOPATH}"/bin/gomobile bind -v -x \
  64. -target "${TARGETS}" \
  65. -prefix Go \
  66. -tags="${BUILD_TAGS}" \
  67. -ldflags="${LDFLAGS}" \
  68. -o "${GOBIND_OUT}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  69. rc=$?; if [[ $rc != 0 ]]; then
  70. echo "FAILURE: gomobile bind failed".
  71. exit $rc
  72. fi
  73. # Sets parameter $2 to value of GOBIND_OUT.
  74. eval "$2=${GOBIND_OUT}"
  75. }
  76. #
  77. # Check Go version
  78. #
  79. GO_VERSION=$(go version | sed -E -n 's/.*go([0-9]\.[0-9]+(\.[0-9]+)?).*/\1/p')
  80. if [[ ${GO_VERSION} != "${GO_VERSION_REQUIRED}" ]]; then
  81. echo "FAILURE: go version mismatch; require ${GO_VERSION_REQUIRED}; got ${GO_VERSION}"
  82. exit 1
  83. fi
  84. #
  85. # Get and install gomobile, using our pinned revision
  86. #
  87. go get -u golang.org/x/mobile/cmd/gomobile
  88. cd "${GOPATH}"/src/golang.org/x/mobile/cmd/gomobile
  89. git checkout master
  90. git checkout -b pinned ${GOMOBILE_PINNED_REV}
  91. # Patch gomobile to edit a command that assumes modules
  92. mv init.go init.go.orig
  93. sed -e 's/golang.org\/x\/mobile\/cmd\/gobind@latest/golang.org\/x\/mobile\/cmd\/gobind/g' init.go.orig > init.go
  94. go install
  95. "${GOPATH}"/bin/gomobile init -v -x
  96. if [[ $? != 0 ]]; then
  97. echo "FAILURE: ${GOPATH}/bin/gomobile init"
  98. exit 1
  99. fi
  100. # Ensure BUILD* variables reflect the tunnel-core repo
  101. cd "${TUNNEL_CORE_SRC_DIR}"
  102. BUILDINFOFILE="${BASE_DIR}/psiphon-tunnel-core_buildinfo.txt"
  103. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  104. BUILDREPO=$(git config --get remote.origin.url)
  105. BUILDREV=$(git rev-parse --short HEAD)
  106. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  107. GOMOBILEVERSION=$("${GOPATH}"/bin/gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  108. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  109. cd "${GOPATH}"/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  110. 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 'cd $GOPATH/src/$0 && if echo -n "$0" | grep -vEq "^github.com/Psiphon-Labs/psiphon-tunnel-core/" ; then echo -n "\"$0\":\"$(git rev-parse --short HEAD)\"," ; fi' pkg | sed 's/,$//' | tr -d '\n' && echo -n "}")
  111. LDFLAGS="\
  112. -s \
  113. -w \
  114. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=${BUILDDATE} \
  115. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=${BUILDREPO} \
  116. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=${BUILDREV} \
  117. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=${GOVERSION} \
  118. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.gomobileVersion=${GOMOBILEVERSION} \
  119. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=${DEPENDENCIES} \
  120. "
  121. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > "$BUILDINFOFILE"
  122. echo ""
  123. echo "Variables for ldflags:"
  124. echo " Build date: ${BUILDDATE}"
  125. echo " Build repo: ${BUILDREPO}"
  126. echo " Build revision: ${BUILDREV}"
  127. echo " Go version: ${GOVERSION}"
  128. echo " Gomobile version: ${GOMOBILEVERSION}"
  129. echo ""
  130. #
  131. # Clean previous output
  132. #
  133. rm -rf "${BUILD_DIR}"
  134. #
  135. # Builds Psi.xcframework
  136. #
  137. IOS_PSI_FRAMEWORK=""
  138. gomobile_build_for_platform "ios" IOS_PSI_FRAMEWORK
  139. echo "$IOS_PSI_FRAMEWORK"
  140. #
  141. # Copies gobind output Psi.xcframework to the Xcode project
  142. #
  143. rm -rf "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel/Psi.xcframework"
  144. cp -r "${IOS_PSI_FRAMEWORK}" "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel"
  145. #
  146. # Build PsiphonTunnel framework for iOS.
  147. #
  148. IOS_ARCHIVE="${BUILD_DIR}/ios.xcarchive"
  149. xcodebuild clean archive \
  150. -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \
  151. -scheme "PsiphonTunnel" \
  152. -configuration "Release" \
  153. -sdk iphoneos \
  154. -archivePath "${IOS_ARCHIVE}" \
  155. CODE_SIGN_IDENTITY="" \
  156. CODE_SIGNING_REQUIRED="NO" \
  157. CODE_SIGN_ENTITLEMENTS="" \
  158. CODE_SIGNING_ALLOWED="NO" \
  159. STRIP_BITCODE_FROM_COPIED_FILES="NO" \
  160. BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \
  161. ONLY_ACTIVE_ARCH="NO" \
  162. SKIP_INSTALL="NO" \
  163. EXCLUDED_ARCHS="armv7"
  164. # Build PsiphonTunnel framework for simulator.
  165. #
  166. # Note:
  167. # - Excludes 32-bit Intel: EXCLUDED_ARCHS="i386".
  168. # - Excludes ARM Macs: EXCLUDED_ARCHS="arm64".
  169. SIMULATOR_ARCHIVE="${BUILD_DIR}/simulator.xcarchive"
  170. xcodebuild clean archive \
  171. -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \
  172. -scheme "PsiphonTunnel" \
  173. -configuration "Release" \
  174. -sdk iphonesimulator \
  175. -archivePath "${SIMULATOR_ARCHIVE}" \
  176. CODE_SIGN_IDENTITY="" \
  177. CODE_SIGNING_REQUIRED="NO" \
  178. CODE_SIGN_ENTITLEMENTS="" \
  179. CODE_SIGNING_ALLOWED="NO" \
  180. STRIP_BITCODE_FROM_COPIED_FILES="NO" \
  181. BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \
  182. ONLY_ACTIVE_ARCH="NO" \
  183. SKIP_INSTALL="NO" \
  184. EXCLUDED_ARCHS="arm64 i386"
  185. #
  186. # Bundling the generated frameworks into a single PsiphonTunnel.xcframework
  187. #
  188. xcodebuild -create-xcframework \
  189. -framework "${IOS_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \
  190. -debug-symbols "${IOS_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \
  191. -framework "${SIMULATOR_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \
  192. -debug-symbols "${SIMULATOR_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \
  193. -output "${BUILD_DIR}/PsiphonTunnel.xcframework"
  194. # Jenkins loses symlinks from the framework directory, which results in a build
  195. # artifact that is invalid to use in an App Store app. Instead, we will zip the
  196. # resulting build and use that as the artifact.
  197. cd "${BUILD_DIR}"
  198. zip --recurse-paths --symlinks build.zip ./PsiphonTunnel.xcframework --exclude "*.DS_Store"
  199. echo "BUILD DONE"