build-psiphon-framework.sh 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.19.2"
  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. GOMOBILEVERSION=$("${GOPATH}"/bin/gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  121. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  122. cd "${GOPATH}"/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  123. 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 "}")
  124. LDFLAGS="\
  125. -s \
  126. -w \
  127. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=${BUILDDATE} \
  128. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=${BUILDREPO} \
  129. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=${BUILDREV} \
  130. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=${GOVERSION} \
  131. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.gomobileVersion=${GOMOBILEVERSION} \
  132. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=${DEPENDENCIES} \
  133. "
  134. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > "$BUILDINFOFILE"
  135. echo ""
  136. echo "Variables for ldflags:"
  137. echo " Build date: ${BUILDDATE}"
  138. echo " Build repo: ${BUILDREPO}"
  139. echo " Build revision: ${BUILDREV}"
  140. echo " Go version: ${GOVERSION}"
  141. echo " Gomobile version: ${GOMOBILEVERSION}"
  142. echo ""
  143. #
  144. # Clean previous output
  145. #
  146. rm -rf "${BUILD_DIR}"
  147. #
  148. # Builds Psi.xcframework
  149. #
  150. IOS_PSI_FRAMEWORK=""
  151. gomobile_build_for_platform "ios" IOS_PSI_FRAMEWORK
  152. echo "$IOS_PSI_FRAMEWORK"
  153. #
  154. # Copies gobind output Psi.xcframework to the TunnelCore Xcode project
  155. #
  156. rm -rf "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel/Psi.xcframework"
  157. cp -r "${IOS_PSI_FRAMEWORK}" "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel"
  158. #
  159. # Build PsiphonTunnel framework for iOS.
  160. #
  161. IOS_ARCHIVE="${BUILD_DIR}/ios.xcarchive"
  162. xcodebuild clean archive \
  163. -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \
  164. -scheme "PsiphonTunnel" \
  165. -configuration "Release" \
  166. -sdk iphoneos \
  167. -archivePath "${IOS_ARCHIVE}" \
  168. CODE_SIGN_IDENTITY="" \
  169. CODE_SIGNING_REQUIRED="NO" \
  170. CODE_SIGN_ENTITLEMENTS="" \
  171. CODE_SIGNING_ALLOWED="NO" \
  172. STRIP_BITCODE_FROM_COPIED_FILES="NO" \
  173. BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \
  174. ONLY_ACTIVE_ARCH="NO" \
  175. SKIP_INSTALL="NO" \
  176. EXCLUDED_ARCHS="armv7"
  177. # Build PsiphonTunnel framework for simulator.
  178. #
  179. # Note:
  180. # - Excludes 32-bit Intel: EXCLUDED_ARCHS="i386".
  181. SIMULATOR_ARCHIVE="${BUILD_DIR}/simulator.xcarchive"
  182. xcodebuild clean archive \
  183. -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \
  184. -scheme "PsiphonTunnel" \
  185. -configuration "Release" \
  186. -sdk iphonesimulator \
  187. -archivePath "${SIMULATOR_ARCHIVE}" \
  188. CODE_SIGN_IDENTITY="" \
  189. CODE_SIGNING_REQUIRED="NO" \
  190. CODE_SIGN_ENTITLEMENTS="" \
  191. CODE_SIGNING_ALLOWED="NO" \
  192. STRIP_BITCODE_FROM_COPIED_FILES="NO" \
  193. BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \
  194. ONLY_ACTIVE_ARCH="NO" \
  195. SKIP_INSTALL="NO" \
  196. EXCLUDED_ARCHS="i386"
  197. #
  198. # Bundles the generated frameworks (for iOS and simulator) into a single PsiphonTunnel.xcframework
  199. #
  200. xcodebuild -create-xcframework \
  201. -framework "${IOS_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \
  202. -debug-symbols "${IOS_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \
  203. -framework "${SIMULATOR_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \
  204. -debug-symbols "${SIMULATOR_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \
  205. -output "${BUILD_DIR}/PsiphonTunnel.xcframework"
  206. # Jenkins loses symlinks from the framework directory, which results in a build
  207. # artifact that is invalid to use in an App Store app. Instead, we will zip the
  208. # resulting build and use that as the artifact.
  209. cd "${BUILD_DIR}"
  210. zip --recurse-paths --symlinks build.zip ./PsiphonTunnel.xcframework --exclude "*.DS_Store"
  211. echo "BUILD DONE"