build-psiphon-framework.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #!/usr/bin/env bash
  2. # build-psiphon-framework.sh builds a PsiphonTunnel.xcframework bundle
  3. # to be used by the Objective-C tunnel-core users.
  4. #
  5. # The build script performs the following tasks:
  6. # 1. Creates a new Go environment and installs (vendored) gomobile.
  7. # 2. Builds Objective-C tunnel-core library (Psi.xcframework) using `gomobile bind`.
  8. # 3. Copies Psi.xcframework into the PsiphonTunnel Xcode project.
  9. # 4. Builds PsiphonTunnel.framework for iOS (arm64) and simulators (x86_64 and arm64).
  10. # 5. Assembles the iOS and simulator PsiphonTunnel.framework packages
  11. # into a single PsiphonTunnel.xcframework bundle.
  12. set -e -u -x
  13. if [ -z ${1+x} ]; then BUILD_TAGS=""; else BUILD_TAGS="$1"; fi
  14. # Modify this value as we use newer Go versions.
  15. GO_VERSION_REQUIRED="1.21.9"
  16. # At this time, psiphon-tunnel-core doesn't support modules
  17. export GO111MODULE=off
  18. # Reset the PATH to macOS default. This is mainly so we don't execute the wrong
  19. # gomobile executable.
  20. PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
  21. # $GOROOT/bin allows build automation to provide various Go versions dynamically.
  22. # As gomobile would be installed at $GOPATH/bin, there is minimal risk that
  23. # adding $GOROOT/bin will run an unexpected gomobile binary.
  24. PATH=$GOROOT/bin:$PATH
  25. BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P)
  26. cd "${BASE_DIR}"
  27. # The location of the final framework build
  28. BUILD_DIR="${BASE_DIR}/build"
  29. # Ensure go is installed
  30. which go 2>&1 > /dev/null
  31. if [[ $? != 0 ]]; then
  32. echo "Go is not installed in the path, aborting"
  33. exit 1
  34. fi
  35. # PsiphonTunnel Xcode project imports the framework built by gomobile,
  36. # and defines the interface for Objective-C tunnel-core users.
  37. UMBRELLA_FRAMEWORK_XCODE_PROJECT=${BASE_DIR}/PsiphonTunnel/PsiphonTunnel.xcodeproj/
  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. # gomobile library is vendored
  44. GOMOBILE_PATH=${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  45. TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core
  46. PATH=${PATH}:${GOPATH}/bin
  47. mkdir -p "${GOPATH}"
  48. if [[ $? != 0 ]]; then
  49. echo "FAILURE: mkdir -p ${GOPATH}"
  50. exit 1
  51. fi
  52. # Symlink the current source directory into GOPATH, so that we're building the
  53. # code in this local repo, rather than pulling from Github and building that.
  54. mkdir -p "${GOPATH}/src/github.com/Psiphon-Labs"
  55. if [[ $? != 0 ]]; then
  56. echo "mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs"
  57. exit 1
  58. fi
  59. ln -s "${BASE_DIR}/../.." "${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  60. if [[ $? != 0 ]]; then
  61. echo "ln -s ../.. ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  62. exit 1
  63. fi
  64. # Builds Psi.xcframework library for the given platform.
  65. # Psi.xcframework is the glue code between Go and Objective-C.
  66. #
  67. # - Parameter 1: `gomobile bind` -target option value
  68. # - Parameter 2: Variable name where gomobile output (Psi.xcframework) will be set to.
  69. function gomobile_build_for_platform() {
  70. # Possible values are "ios" and "simulator"
  71. local TARGETS=$1
  72. echo "Build library for targets ${TARGETS}"
  73. local GOBIND_OUT="${BUILD_DIR}/gobind-framework/Psi.xcframework"
  74. # We're using a generated-code prefix to workaround https://github.com/golang/go/issues/18693
  75. # CGO_CFLAGS_ALLOW is to workaround https://github.com/golang/go/issues/23742 in Go 1.9.4
  76. CGO_CFLAGS_ALLOW="-fmodules|-fblocks" "${GOPATH}"/bin/gomobile bind -v -x \
  77. -target "${TARGETS}" \
  78. -iosversion "10.0" \
  79. -prefix Go \
  80. -tags="${BUILD_TAGS}" \
  81. -ldflags="${LDFLAGS}" \
  82. -o "${GOBIND_OUT}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  83. rc=$?; if [[ $rc != 0 ]]; then
  84. echo "FAILURE: gomobile bind failed".
  85. exit $rc
  86. fi
  87. # Sets parameter $2 to value of GOBIND_OUT.
  88. eval "$2=${GOBIND_OUT}"
  89. }
  90. #
  91. # Check Go version
  92. #
  93. GO_VERSION=$(go version | sed -E -n 's/.*go([0-9]\.[0-9]+(\.[0-9]+)?).*/\1/p')
  94. if [[ ${GO_VERSION} != "${GO_VERSION_REQUIRED}" ]]; then
  95. echo "FAILURE: go version mismatch; require ${GO_VERSION_REQUIRED}; got ${GO_VERSION}"
  96. exit 1
  97. fi
  98. #
  99. # Copies vendored gomobile into the local GOPATH.
  100. #
  101. mkdir -p "${GOPATH}/src/golang.org/x"
  102. cp -R "${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/go-mobile" "${GOPATH}/src/golang.org/x/mobile"
  103. cd "${GOPATH}/src/golang.org/x/mobile/cmd/gomobile"
  104. # Patch gomobile to edit a command that assumes modules
  105. mv init.go init.go.orig
  106. sed -e 's/golang.org\/x\/mobile\/cmd\/gobind@latest/golang.org\/x\/mobile\/cmd\/gobind/g' init.go.orig > init.go
  107. go install
  108. "${GOPATH}"/bin/gomobile init -v -x
  109. if [[ $? != 0 ]]; then
  110. echo "FAILURE: ${GOPATH}/bin/gomobile init"
  111. exit 1
  112. fi
  113. # Ensure BUILD* variables reflect the tunnel-core repo
  114. cd "${TUNNEL_CORE_SRC_DIR}"
  115. BUILDINFOFILE="${BASE_DIR}/psiphon-tunnel-core_buildinfo.txt"
  116. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  117. BUILDREPO=$(git config --get remote.origin.url)
  118. BUILDREV=$(git rev-parse --short HEAD)
  119. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  120. LDFLAGS="\
  121. -s \
  122. -w \
  123. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=${BUILDDATE} \
  124. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=${BUILDREPO} \
  125. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=${BUILDREV} \
  126. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=${GOVERSION} \
  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 ""
  136. #
  137. # Clean previous output
  138. #
  139. rm -rf "${BUILD_DIR}"
  140. #
  141. # Builds Psi.xcframework
  142. #
  143. IOS_PSI_FRAMEWORK=""
  144. gomobile_build_for_platform "ios" IOS_PSI_FRAMEWORK
  145. echo "$IOS_PSI_FRAMEWORK"
  146. #
  147. # Copies gobind output Psi.xcframework to the TunnelCore Xcode project
  148. #
  149. rm -rf "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel/Psi.xcframework"
  150. cp -r "${IOS_PSI_FRAMEWORK}" "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel"
  151. #
  152. # Build PsiphonTunnel framework for iOS.
  153. #
  154. IOS_ARCHIVE="${BUILD_DIR}/ios.xcarchive"
  155. xcodebuild clean archive \
  156. -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \
  157. -scheme "PsiphonTunnel" \
  158. -configuration "Release" \
  159. -sdk iphoneos \
  160. -archivePath "${IOS_ARCHIVE}" \
  161. CODE_SIGN_IDENTITY="" \
  162. CODE_SIGNING_REQUIRED="NO" \
  163. CODE_SIGN_ENTITLEMENTS="" \
  164. CODE_SIGNING_ALLOWED="NO" \
  165. STRIP_BITCODE_FROM_COPIED_FILES="NO" \
  166. BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \
  167. ONLY_ACTIVE_ARCH="NO" \
  168. SKIP_INSTALL="NO" \
  169. EXCLUDED_ARCHS="armv7"
  170. # Build PsiphonTunnel framework for simulator.
  171. #
  172. # Note:
  173. # - Excludes 32-bit Intel: EXCLUDED_ARCHS="i386".
  174. SIMULATOR_ARCHIVE="${BUILD_DIR}/simulator.xcarchive"
  175. xcodebuild clean archive \
  176. -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \
  177. -scheme "PsiphonTunnel" \
  178. -configuration "Release" \
  179. -sdk iphonesimulator \
  180. -archivePath "${SIMULATOR_ARCHIVE}" \
  181. CODE_SIGN_IDENTITY="" \
  182. CODE_SIGNING_REQUIRED="NO" \
  183. CODE_SIGN_ENTITLEMENTS="" \
  184. CODE_SIGNING_ALLOWED="NO" \
  185. STRIP_BITCODE_FROM_COPIED_FILES="NO" \
  186. BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \
  187. ONLY_ACTIVE_ARCH="NO" \
  188. SKIP_INSTALL="NO" \
  189. EXCLUDED_ARCHS="i386"
  190. #
  191. # Bundles the generated frameworks (for iOS and simulator) into a single PsiphonTunnel.xcframework
  192. #
  193. xcodebuild -create-xcframework \
  194. -framework "${IOS_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \
  195. -debug-symbols "${IOS_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \
  196. -framework "${SIMULATOR_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \
  197. -debug-symbols "${SIMULATOR_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \
  198. -output "${BUILD_DIR}/PsiphonTunnel.xcframework"
  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 ./PsiphonTunnel.xcframework --exclude "*.DS_Store"
  204. echo "BUILD DONE"