build-psiphon-framework.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/usr/bin/env bash
  2. # This script takes one optional argument: 'private', if private plugins should
  3. # be used. It should be omitted if private plugins are not desired.
  4. if [[ $1 == "private" ]]; then
  5. FORCE_PRIVATE_PLUGINS=true
  6. echo "TRUE"
  7. else
  8. FORCE_PRIVATE_PLUGINS=false
  9. echo "FALSE"
  10. fi
  11. # -x echos commands. -u exits if an unintialized variable is used.
  12. # -e exits if a command returns an error.
  13. set -x -u -e
  14. # Modify this value as we use newer Go versions.
  15. GO_VERSION_REQUIRED="1.9.4"
  16. # Reset the PATH to macOS default. This is mainly so we don't execute the wrong
  17. # gomobile executable.
  18. PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
  19. BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P)
  20. cd ${BASE_DIR}
  21. # The location of the final framework build
  22. BUILD_DIR="${BASE_DIR}/build"
  23. # Ensure go is installed
  24. which go 2>&1 > /dev/null
  25. if [[ $? != 0 ]]; then
  26. echo "Go is not installed in the path, aborting"
  27. exit 1
  28. fi
  29. VALID_IOS_ARCHS="arm64 armv7 armv7s"
  30. VALID_SIMULATOR_ARCHS="x86_64"
  31. FRAMEWORK="Psi"
  32. INTERMEDIATE_OUPUT_DIR="${BASE_DIR}/PsiphonTunnel/PsiphonTunnel"
  33. INTERMEDIATE_OUPUT_FILE="${FRAMEWORK}.framework"
  34. FRAMEWORK_BINARY="${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}/Versions/A/${FRAMEWORK}"
  35. PRIVATE_PLUGINS_TAG=""
  36. if [[ ${FORCE_PRIVATE_PLUGINS} == true ]]; then PRIVATE_PLUGINS_TAG="PRIVATE_PLUGINS"; fi
  37. BUILD_TAGS="IOS ${PRIVATE_PLUGINS_TAG}"
  38. UMBRELLA_FRAMEWORK_XCODE_PROJECT=${BASE_DIR}/PsiphonTunnel/PsiphonTunnel.xcodeproj/
  39. # Exporting these seems necessary for subcommands to pick them up.
  40. export GOPATH=${PWD}/go-ios-build
  41. export PATH=${GOPATH}/bin:${PATH}
  42. # The GOPATH we're using is temporary, so make sure there isn't one from a previous run.
  43. rm -rf ${GOPATH}
  44. GOMOBILE_PINNED_REV=eb9032959f05f108b05721914dfe09cfa0c5131d
  45. GOMOBILE_PATH=${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  46. TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core
  47. PATH=${PATH}:${GOPATH}/bin
  48. mkdir -p ${GOPATH}
  49. if [[ $? != 0 ]]; then
  50. echo "FAILURE: mkdir -p ${GOPATH}"
  51. exit 1
  52. fi
  53. # Symlink the current source directory into GOPATH, so that we're building the
  54. # code in this local repo, rather than pulling from Github and building that.
  55. mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs
  56. if [[ $? != 0 ]]; then
  57. echo "mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs"
  58. exit 1
  59. fi
  60. ln -s "${BASE_DIR}/../.." "${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  61. if [[ $? != 0 ]]; then
  62. echo "ln -s ../.. ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  63. exit 1
  64. fi
  65. mkdir -p ${INTERMEDIATE_OUPUT_DIR}
  66. if [[ $? != 0 ]]; then
  67. echo "FAILURE: mkdir -p ${INTERMEDIATE_OUPUT_DIR}"
  68. exit 1
  69. fi
  70. # arg: binary_path
  71. function strip_architectures() {
  72. valid_archs="${VALID_IOS_ARCHS} ${VALID_SIMULATOR_ARCHS}"
  73. ARCHS="$(lipo -info "$1" | rev | cut -d ':' -f1 | rev)"
  74. for ARCH in "${valid_archs}"; do
  75. if ! [[ "${valid_archs}" == *"$ARCH"* ]]; then
  76. echo "Stripping ARCH ${ARCH} from $1"
  77. lipo -remove "$ARCH" -output "$1" "$1"
  78. if [[ $? != 0 ]]; then
  79. echo "FAILURE: lipo $1"
  80. exit 1
  81. fi
  82. fi
  83. done
  84. return 0
  85. }
  86. # Check Go version
  87. GO_VERSION=$(go version | sed -E -n 's/.*go([0-9]\.[0-9]+(\.[0-9]+)?).*/\1/p')
  88. if [[ ${GO_VERSION} != ${GO_VERSION_REQUIRED} ]]; then
  89. echo "FAILURE: go version mismatch; require ${GO_VERSION_REQUIRED}; got ${GO_VERSION}"
  90. exit 1
  91. fi
  92. # Don't use -u, because this path points to our local repo, and we don't want it overridden.
  93. go get -d -v -tags "${BUILD_TAGS}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  94. if [[ $? != 0 ]]; then
  95. echo "FAILURE: go get -d -v -tags "${BUILD_TAGS}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi"
  96. exit 1
  97. fi
  98. #
  99. # Get and install gomobile, using our pinned revision
  100. #
  101. go get -u golang.org/x/mobile/cmd/gomobile
  102. cd ${GOPATH}/src/golang.org/x/mobile/cmd/gomobile
  103. git checkout master
  104. git checkout -b pinned ${GOMOBILE_PINNED_REV}
  105. go install
  106. ${GOPATH}/bin/gomobile init -v -x
  107. if [[ $? != 0 ]]; then
  108. echo "FAILURE: ${GOPATH}/bin/gomobile init"
  109. exit 1
  110. fi
  111. #
  112. # gomobile bind
  113. #
  114. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  115. BUILDREPO=$(git config --get remote.origin.url)
  116. BUILDREV=$(git rev-parse --short HEAD)
  117. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  118. GOMOBILEVERSION=$(${GOPATH}/bin/gomobile version | perl -ne '/gomobile version (.*?) / && print $1')
  119. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  120. cd ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  121. DEPENDENCIES=$(echo -n "{" && go list -tags "${BUILD_TAGS}" -f '{{range $dep := .Deps}}{{printf "%s\n" $dep}}{{end}}' | xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}' | xargs -I pkg bash -c 'cd $GOPATH/src/pkg && echo -n "\"pkg\":\"$(git rev-parse --short HEAD)\","' | sed 's/,$/}/')
  122. LDFLAGS="\
  123. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=${BUILDDATE} \
  124. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=${BUILDREPO} \
  125. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=${BUILDREV} \
  126. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=${GOVERSION} \
  127. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.gomobileVersion=${GOMOBILEVERSION} \
  128. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=${DEPENDENCIES} \
  129. "
  130. echo ""
  131. echo "Variables for ldflags:"
  132. echo " Build date: ${BUILDDATE}"
  133. echo " Build repo: ${BUILDREPO}"
  134. echo " Build revision: ${BUILDREV}"
  135. echo " Go version: ${GOVERSION}"
  136. echo " Gomobile version: ${GOMOBILEVERSION}"
  137. echo ""
  138. # We're using a generated-code prefix to workaround https://github.com/golang/go/issues/18693
  139. # CGO_CFLAGS_ALLOW is to workaround https://github.com/golang/go/issues/23742 in Go 1.9.4
  140. CGO_CFLAGS_ALLOW="-fmodules|-fblocks" ${GOPATH}/bin/gomobile bind -v -x -target ios -prefix Go -tags="${BUILD_TAGS}" -ldflags="${LDFLAGS}" -o "${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi
  141. rc=$?; if [[ $rc != 0 ]]; then
  142. echo "FAILURE: ${GOPATH}/bin/gomobile bind -target ios -tags="${BUILD_TAGS}" -ldflags="${LDFLAGS}" -o "${INTERMEDIATE_OUPUT_DIR}/${INTERMEDIATE_OUPUT_FILE}" github.com/Psiphon-Labs/psiphon-tunnel-core/MobileLibrary/psi"
  143. exit $rc
  144. fi
  145. strip_architectures "${FRAMEWORK_BINARY}"
  146. #
  147. # Do the outer framework build using Xcode
  148. #
  149. # Clean previous output
  150. rm -rf "${BUILD_DIR}"
  151. rm -rf "${BUILD_DIR}-SIMULATOR"
  152. # Build the outer framework for phones...
  153. xcodebuild clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGN_ENTITLEMENTS="" CODE_SIGNING_ALLOWED="NO" -configuration Release -sdk iphoneos ONLY_ACTIVE_ARCH=NO -project ${UMBRELLA_FRAMEWORK_XCODE_PROJECT} CONFIGURATION_BUILD_DIR="${BUILD_DIR}"
  154. rc=$?; if [[ $rc != 0 ]]; then
  155. echo "FAILURE: xcodebuild iphoneos"
  156. exit $rc
  157. fi
  158. # ...and for the simulator.
  159. xcodebuild clean build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGN_ENTITLEMENTS="" CODE_SIGNING_ALLOWED="NO" -configuration Release -sdk iphonesimulator ARCHS=x86_64 VALID_ARCHS=x86_64 ONLY_ACTIVE_ARCH=NO -project ${UMBRELLA_FRAMEWORK_XCODE_PROJECT} CONFIGURATION_BUILD_DIR="${BUILD_DIR}-SIMULATOR"
  160. rc=$?; if [[ $rc != 0 ]]; then
  161. echo "FAILURE: xcodebuild iphonesimulator"
  162. exit $rc
  163. fi
  164. # Add the simulator x86_64 binary into the main framework binary.
  165. lipo -create "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel" "${BUILD_DIR}-SIMULATOR/PsiphonTunnel.framework/PsiphonTunnel" -output "${BUILD_DIR}/PsiphonTunnel.framework/PsiphonTunnel"
  166. rc=$?; if [[ $rc != 0 ]]; then
  167. echo "FAILURE: lipo create"
  168. exit $rc
  169. fi
  170. # Delete the temporary simulator build files.
  171. rm -rf "${BUILD_DIR}-SIMULATOR"
  172. # Jenkins loses symlinks from the framework directory, which results in a build
  173. # artifact that is invalid to use in an App Store app. Instead, we will zip the
  174. # resulting build and use that as the artifact.
  175. cd "${BUILD_DIR}"
  176. zip --recurse-paths --symlinks build.zip * --exclude "*.DS_Store"
  177. echo "BUILD DONE"