build-darwin.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env bash
  2. # -x echos commands.
  3. # -e exits if a command returns an error.
  4. set -x -e
  5. # This script takes an optional second argument: 'private', if private plugins should
  6. # be used. It should be omitted if private plugins are not desired.
  7. if [[ $2 == "private" ]]; then
  8. FORCE_PRIVATE_PLUGINS=true
  9. echo "TRUE"
  10. else
  11. FORCE_PRIVATE_PLUGINS=false
  12. echo "FALSE"
  13. fi
  14. # Modify this value as we use newer Go versions.
  15. GO_VERSION_REQUIRED="1.9.6"
  16. BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P)
  17. cd ${BASE_DIR}
  18. # The location of the final build products
  19. BUILD_DIR="${BASE_DIR}/build/darwin"
  20. TEMP_DIR="${BUILD_DIR}/tmp"
  21. # Clean previous output
  22. rm -rf "${BUILD_DIR}"
  23. mkdir -p ${TEMP_DIR}
  24. if [[ $? != 0 ]]; then
  25. echo "FAILURE: mkdir -p ${TEMP_DIR}"
  26. exit 1
  27. fi
  28. # Ensure go is installed
  29. which go 2>&1 > /dev/null
  30. if [[ $? != 0 ]]; then
  31. echo "Go is not installed in the path, aborting"
  32. exit 1
  33. fi
  34. PRIVATE_PLUGINS_TAG=""
  35. if [[ ${FORCE_PRIVATE_PLUGINS} == true ]]; then PRIVATE_PLUGINS_TAG="PRIVATE_PLUGINS"; fi
  36. # Exporting these seems necessary for subcommands to pick them up.
  37. export GOPATH=${TEMP_DIR}/go-darwin-build
  38. export PATH=${GOPATH}/bin:${PATH}
  39. # The GOPATH we're using is temporary, so make sure there isn't one from a previous run.
  40. rm -rf ${GOPATH}
  41. TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core
  42. mkdir -p ${GOPATH}
  43. if [[ $? != 0 ]]; then
  44. echo "FAILURE: mkdir -p ${GOPATH}"
  45. exit 1
  46. fi
  47. # Symlink the current source directory into GOPATH, so that we're building the
  48. # code in this local repo, rather than pulling from Github and building that.
  49. mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs
  50. if [[ $? != 0 ]]; then
  51. echo "mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs"
  52. exit 1
  53. fi
  54. ln -s "${BASE_DIR}/.." "${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  55. if [[ $? != 0 ]]; then
  56. echo "ln -s ../.. ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  57. exit 1
  58. fi
  59. # Check Go version
  60. GO_VERSION=$(go version | sed -E -n 's/.*go([0-9]\.[0-9]+(\.[0-9]+)?).*/\1/p')
  61. if [[ ${GO_VERSION} != ${GO_VERSION_REQUIRED} ]]; then
  62. echo "FAILURE: go version mismatch; require ${GO_VERSION_REQUIRED}; got ${GO_VERSION}"
  63. exit 1
  64. fi
  65. prepare_build () {
  66. # Ensure BUILD* variables reflect the tunnel-core repo
  67. cd ${TUNNEL_CORE_SRC_DIR}
  68. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  69. BUILDREPO=$(git config --get remote.origin.url)
  70. BUILDREV=$(git rev-parse --short HEAD)
  71. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  72. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  73. cd ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary
  74. DEPENDENCIES=$(echo -n "{" && go list -tags "$1" -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/,$/}/')
  75. LDFLAGS="\
  76. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=$BUILDDATE \
  77. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=$BUILDREPO \
  78. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=$BUILDREV \
  79. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=$GOVERSION \
  80. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=$DEPENDENCIES \
  81. "
  82. echo "Variables for ldflags:"
  83. echo " Build date: ${BUILDDATE}"
  84. echo " Build repo: ${BUILDREPO}"
  85. echo " Build revision: ${BUILDREV}"
  86. echo " Go version: ${GOVERSION}"
  87. echo " Dependencies: ${DEPENDENCIES}"
  88. echo ""
  89. }
  90. build_for_ios () {
  91. IOS_BUILD_TAGS="IOS ${PRIVATE_PLUGINS_TAG}"
  92. IOS_BUILD_DIR="${BUILD_DIR}/ios"
  93. rm -rf "${IOS_BUILD_DIR}"
  94. echo "...Getting project dependencies (via go get) for iOS."
  95. cd ${BASE_DIR}
  96. GOOS=darwin go get -d -v -tags "$IOS_BUILD_TAGS" ./...
  97. prepare_build "$IOS_BUILD_TAGS"
  98. if [ $? != 0 ]; then
  99. echo "....'go get' failed, exiting"
  100. exit $?
  101. fi
  102. curl https://raw.githubusercontent.com/golang/go/master/misc/ios/clangwrap.sh -o ${TEMP_DIR}/clangwrap.sh
  103. chmod 555 ${TEMP_DIR}/clangwrap.sh
  104. CC=${TEMP_DIR}/clangwrap.sh \
  105. CXX=${TEMP_DIR}/clangwrap.sh \
  106. CGO_LDFLAGS="-arch armv7 -isysroot $(xcrun --sdk iphoneos --show-sdk-path)" \
  107. CGO_CFLAGS=-isysroot$(xcrun --sdk iphoneos --show-sdk-path) \
  108. CGO_ENABLED=1 GOOS=darwin GOARCH=arm GOARM=7 go build -buildmode=c-archive -ldflags "$LDFLAGS" -tags "${IOS_BUILD_TAGS}" -o ${IOS_BUILD_DIR}/PsiphonTunnel-ios-arm.dylib PsiphonTunnel.go
  109. CC=${TEMP_DIR}/clangwrap.sh \
  110. CXX=${TEMP_DIR}/clangwrap.sh \
  111. CGO_LDFLAGS="-arch arm64 -isysroot $(xcrun --sdk iphoneos --show-sdk-path)" \
  112. CGO_CFLAGS=-isysroot$(xcrun --sdk iphoneos --show-sdk-path) \
  113. CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -buildmode=c-archive -ldflags "$LDFLAGS" -tags "${IOS_BUILD_TAGS}" -o ${IOS_BUILD_DIR}/PsiphonTunnel-ios-arm64.dylib PsiphonTunnel.go
  114. }
  115. build_for_macos () {
  116. MACOS_BUILD_TAGS="${PRIVATE_PLUGINS_TAG}"
  117. MACOS_BUILD_DIR="${BUILD_DIR}/macos"
  118. rm -rf "${MACOS_BUILD_DIR}"
  119. echo "...Getting project dependencies (via go get) for MacOS"
  120. cd ${BASE_DIR}
  121. GOOS=darwin go get -d -v -tags "$MACOS_BUILD_TAGS" ./...
  122. prepare_build "$MACOS_BUILD_TAGS"
  123. if [ $? != 0 ]; then
  124. echo "....'go get' failed, exiting"
  125. exit $?
  126. fi
  127. TARGET_ARCH=386
  128. CGO_ENABLED=1 GOOS=darwin GOARCH="${TARGET_ARCH}" go build -buildmode=c-archive -ldflags "$LDFLAGS" -tags "${MACOS_BUILD_TAGS}" -o "${MACOS_BUILD_DIR}/PsiphonTunnel-macos-${TARGET_ARCH}.dylib" PsiphonTunnel.go
  129. TARGET_ARCH=amd64
  130. CGO_ENABLED=1 GOOS=darwin GOARCH="${TARGET_ARCH}" go build -buildmode=c-archive -ldflags "$LDFLAGS" -tags "${MACOS_BUILD_TAGS}" -o "${MACOS_BUILD_DIR}/PsiphonTunnel-macos-${TARGET_ARCH}.dylib" PsiphonTunnel.go
  131. }
  132. cleanup () {
  133. # Remove temporary build artifacts
  134. rm -rf ${TEMP_DIR}
  135. }
  136. TARGET=$1
  137. case $TARGET in
  138. macos)
  139. echo "..Building for MacOS"
  140. build_for_macos
  141. if [ $? != 0 ]; then
  142. exit $?
  143. fi
  144. ;;
  145. ios)
  146. echo "..Building for iOS"
  147. build_for_ios
  148. if [ $? != 0 ]; then
  149. exit $?
  150. fi
  151. ;;
  152. all)
  153. echo "..Building all"
  154. build_for_ios
  155. if [ $? != 0 ]; then
  156. exit $?
  157. fi
  158. build_for_macos
  159. if [ $? != 0 ]; then
  160. exit $?
  161. fi
  162. ;;
  163. *)
  164. echo "..No selection made, building all"
  165. build_for_ios
  166. if [ $? != 0 ]; then
  167. exit $?
  168. fi
  169. build_for_macos
  170. if [ $? != 0 ]; then
  171. exit $?
  172. fi
  173. ;;
  174. esac
  175. cleanup
  176. echo "BUILD DONE"