build-psiphon-framework.sh 8.3 KB

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