build-psiphon-framework.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 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. LDFLAGS="\
  108. -s \
  109. -w \
  110. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=${BUILDDATE} \
  111. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=${BUILDREPO} \
  112. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=${BUILDREV} \
  113. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=${GOVERSION} \
  114. "
  115. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > "$BUILDINFOFILE"
  116. echo ""
  117. echo "Variables for ldflags:"
  118. echo " Build date: ${BUILDDATE}"
  119. echo " Build repo: ${BUILDREPO}"
  120. echo " Build revision: ${BUILDREV}"
  121. echo " Go version: ${GOVERSION}"
  122. echo ""
  123. #
  124. # Clean previous output
  125. #
  126. rm -rf "${BUILD_DIR}"
  127. #
  128. # Builds Psi.xcframework
  129. #
  130. IOS_PSI_FRAMEWORK=""
  131. gomobile_build_for_platform "ios" IOS_PSI_FRAMEWORK
  132. echo "$IOS_PSI_FRAMEWORK"
  133. #
  134. # Copies gobind output Psi.xcframework to the Xcode project
  135. #
  136. rm -rf "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel/Psi.xcframework"
  137. cp -r "${IOS_PSI_FRAMEWORK}" "${BASE_DIR}/PsiphonTunnel/PsiphonTunnel"
  138. #
  139. # Build PsiphonTunnel framework for iOS.
  140. #
  141. IOS_ARCHIVE="${BUILD_DIR}/ios.xcarchive"
  142. xcodebuild clean archive \
  143. -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \
  144. -scheme "PsiphonTunnel" \
  145. -configuration "Release" \
  146. -sdk iphoneos \
  147. -archivePath "${IOS_ARCHIVE}" \
  148. CODE_SIGN_IDENTITY="" \
  149. CODE_SIGNING_REQUIRED="NO" \
  150. CODE_SIGN_ENTITLEMENTS="" \
  151. CODE_SIGNING_ALLOWED="NO" \
  152. STRIP_BITCODE_FROM_COPIED_FILES="NO" \
  153. BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \
  154. ONLY_ACTIVE_ARCH="NO" \
  155. SKIP_INSTALL="NO" \
  156. EXCLUDED_ARCHS="armv7"
  157. # Build PsiphonTunnel framework for simulator.
  158. #
  159. # Note:
  160. # - Excludes 32-bit Intel: EXCLUDED_ARCHS="i386".
  161. # - Excludes ARM Macs: EXCLUDED_ARCHS="arm64".
  162. SIMULATOR_ARCHIVE="${BUILD_DIR}/simulator.xcarchive"
  163. xcodebuild clean archive \
  164. -project "${UMBRELLA_FRAMEWORK_XCODE_PROJECT}" \
  165. -scheme "PsiphonTunnel" \
  166. -configuration "Release" \
  167. -sdk iphonesimulator \
  168. -archivePath "${SIMULATOR_ARCHIVE}" \
  169. CODE_SIGN_IDENTITY="" \
  170. CODE_SIGNING_REQUIRED="NO" \
  171. CODE_SIGN_ENTITLEMENTS="" \
  172. CODE_SIGNING_ALLOWED="NO" \
  173. STRIP_BITCODE_FROM_COPIED_FILES="NO" \
  174. BUILD_LIBRARY_FOR_DISTRIBUTION="YES" \
  175. ONLY_ACTIVE_ARCH="NO" \
  176. SKIP_INSTALL="NO" \
  177. EXCLUDED_ARCHS="arm64 i386"
  178. #
  179. # Bundling the generated frameworks into a single PsiphonTunnel.xcframework
  180. #
  181. xcodebuild -create-xcframework \
  182. -framework "${IOS_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \
  183. -debug-symbols "${IOS_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \
  184. -framework "${SIMULATOR_ARCHIVE}/Products/Library/Frameworks/PsiphonTunnel.framework" \
  185. -debug-symbols "${SIMULATOR_ARCHIVE}/dSYMs/PsiphonTunnel.framework.dSYM" \
  186. -output "${BUILD_DIR}/PsiphonTunnel.xcframework"
  187. # Jenkins loses symlinks from the framework directory, which results in a build
  188. # artifact that is invalid to use in an App Store app. Instead, we will zip the
  189. # resulting build and use that as the artifact.
  190. cd "${BUILD_DIR}"
  191. zip --recurse-paths --symlinks build.zip ./PsiphonTunnel.xcframework --exclude "*.DS_Store"
  192. echo "BUILD DONE"