build-psiphon-framework.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.11"
  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. -iosversion "10.0" \
  66. -prefix Go \
  67. -tags="${BUILD_TAGS}" \
  68. -ldflags="${LDFLAGS}" \
  69. -o "${GOBIND_OUT}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  70. rc=$?; if [[ $rc != 0 ]]; then
  71. echo "FAILURE: gomobile bind failed".
  72. exit $rc
  73. fi
  74. # Sets parameter $2 to value of GOBIND_OUT.
  75. eval "$2=${GOBIND_OUT}"
  76. }
  77. #
  78. # Check Go version
  79. #
  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. #
  86. # Get and install gomobile, using our pinned revision
  87. #
  88. go get -u golang.org/x/mobile/cmd/gomobile
  89. cd "${GOPATH}"/src/golang.org/x/mobile/cmd/gomobile
  90. git checkout master
  91. git checkout -b pinned ${GOMOBILE_PINNED_REV}
  92. # Patch gomobile to edit a command that assumes modules
  93. mv init.go init.go.orig
  94. sed -e 's/golang.org\/x\/mobile\/cmd\/gobind@latest/golang.org\/x\/mobile\/cmd\/gobind/g' init.go.orig > init.go
  95. go install
  96. "${GOPATH}"/bin/gomobile init -v -x
  97. if [[ $? != 0 ]]; then
  98. echo "FAILURE: ${GOPATH}/bin/gomobile init"
  99. exit 1
  100. fi
  101. # Ensure BUILD* variables reflect the tunnel-core repo
  102. cd "${TUNNEL_CORE_SRC_DIR}"
  103. BUILDINFOFILE="${BASE_DIR}/psiphon-tunnel-core_buildinfo.txt"
  104. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  105. BUILDREPO=$(git config --get remote.origin.url)
  106. BUILDREV=$(git rev-parse --short HEAD)
  107. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  108. GOMOBILEVERSION=$("${GOPATH}"/bin/gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  109. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  110. cd "${GOPATH}"/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  111. 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 "}")
  112. LDFLAGS="\
  113. -s \
  114. -w \
  115. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=${BUILDDATE} \
  116. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=${BUILDREPO} \
  117. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=${BUILDREV} \
  118. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=${GOVERSION} \
  119. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.gomobileVersion=${GOMOBILEVERSION} \
  120. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=${DEPENDENCIES} \
  121. "
  122. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > "$BUILDINFOFILE"
  123. echo ""
  124. echo "Variables for ldflags:"
  125. echo " Build date: ${BUILDDATE}"
  126. echo " Build repo: ${BUILDREPO}"
  127. echo " Build revision: ${BUILDREV}"
  128. echo " Go version: ${GOVERSION}"
  129. echo " Gomobile version: ${GOMOBILEVERSION}"
  130. echo ""
  131. #
  132. # Clean previous output
  133. #
  134. rm -rf "${BUILD_DIR}"
  135. #
  136. # Builds Psi.xcframework
  137. #
  138. IOS_PSI_FRAMEWORK=""
  139. gomobile_build_for_platform "ios" IOS_PSI_FRAMEWORK
  140. echo "$IOS_PSI_FRAMEWORK"
  141. #
  142. # Copies gobind output Psi.xcframework to the Xcode project
  143. #
  144. rm -rf "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel/Psi.xcframework"
  145. cp -r "${IOS_PSI_FRAMEWORK}" "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel"
  146. #
  147. # Build PsiphonTunnel framework for iOS.
  148. #
  149. IOS_ARCHIVE="${BUILD_DIR}/ios.xcarchive"
  150. xcodebuild clean archive \
  151. -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \
  152. -scheme "PsiphonTunnel" \
  153. -configuration "Release" \
  154. -sdk iphoneos \
  155. -archivePath "${IOS_ARCHIVE}" \
  156. CODE_SIGN_IDENTITY="" \
  157. CODE_SIGNING_REQUIRED="NO" \
  158. CODE_SIGN_ENTITLEMENTS="" \
  159. CODE_SIGNING_ALLOWED="NO" \
  160. STRIP_BITCODE_FROM_COPIED_FILES="NO" \
  161. BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \
  162. ONLY_ACTIVE_ARCH="NO" \
  163. SKIP_INSTALL="NO" \
  164. EXCLUDED_ARCHS="armv7"
  165. # Build PsiphonTunnel framework for simulator.
  166. #
  167. # Note:
  168. # - Excludes 32-bit Intel: EXCLUDED_ARCHS="i386".
  169. # - Excludes ARM Macs: EXCLUDED_ARCHS="arm64".
  170. SIMULATOR_ARCHIVE="${BUILD_DIR}/simulator.xcarchive"
  171. xcodebuild clean archive \
  172. -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \
  173. -scheme "PsiphonTunnel" \
  174. -configuration "Release" \
  175. -sdk iphonesimulator \
  176. -archivePath "${SIMULATOR_ARCHIVE}" \
  177. CODE_SIGN_IDENTITY="" \
  178. CODE_SIGNING_REQUIRED="NO" \
  179. CODE_SIGN_ENTITLEMENTS="" \
  180. CODE_SIGNING_ALLOWED="NO" \
  181. STRIP_BITCODE_FROM_COPIED_FILES="NO" \
  182. BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \
  183. ONLY_ACTIVE_ARCH="NO" \
  184. SKIP_INSTALL="NO" \
  185. EXCLUDED_ARCHS="arm64 i386"
  186. #
  187. # Bundling the generated frameworks into a single PsiphonTunnel.xcframework
  188. #
  189. xcodebuild -create-xcframework \
  190. -framework "${IOS_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \
  191. -debug-symbols "${IOS_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \
  192. -framework "${SIMULATOR_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \
  193. -debug-symbols "${SIMULATOR_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \
  194. -output "${BUILD_DIR}/PsiphonTunnel.xcframework"
  195. # Jenkins loses symlinks from the framework directory, which results in a build
  196. # artifact that is invalid to use in an App Store app. Instead, we will zip the
  197. # resulting build and use that as the artifact.
  198. cd "${BUILD_DIR}"
  199. zip --recurse-paths --symlinks build.zip ./PsiphonTunnel.xcframework --exclude "*.DS_Store"
  200. echo "BUILD DONE"