build-darwin.sh 5.8 KB

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