build-darwin.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=$BUILDDATE \
  66. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=$BUILDREPO \
  67. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=$BUILDREV \
  68. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=$GOVERSION \
  69. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=$DEPENDENCIES \
  70. "
  71. echo "Variables for ldflags:"
  72. echo " Build date: ${BUILDDATE}"
  73. echo " Build repo: ${BUILDREPO}"
  74. echo " Build revision: ${BUILDREV}"
  75. echo " Go version: ${GOVERSION}"
  76. echo " Dependencies: ${DEPENDENCIES}"
  77. echo ""
  78. }
  79. build_for_ios () {
  80. IOS_BUILD_DIR="${BUILD_DIR}/ios"
  81. rm -rf "${IOS_BUILD_DIR}"
  82. echo "...Getting project dependencies (via go get) for iOS."
  83. cd ${BASE_DIR}
  84. GOOS=darwin go get -d -v -tags "${BUILD_TAGS}" ./...
  85. if [ $? != 0 ]; then
  86. echo "....'go get' failed, exiting"
  87. exit $?
  88. fi
  89. prepare_build darwin
  90. curl https://raw.githubusercontent.com/golang/go/master/misc/ios/clangwrap.sh -o ${TEMP_DIR}/clangwrap.sh
  91. chmod 555 ${TEMP_DIR}/clangwrap.sh
  92. CC=${TEMP_DIR}/clangwrap.sh \
  93. CXX=${TEMP_DIR}/clangwrap.sh \
  94. CGO_LDFLAGS="-arch armv7 -isysroot $(xcrun --sdk iphoneos --show-sdk-path)" \
  95. CGO_CFLAGS=-isysroot$(xcrun --sdk iphoneos --show-sdk-path) \
  96. 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
  97. CC=${TEMP_DIR}/clangwrap.sh \
  98. CXX=${TEMP_DIR}/clangwrap.sh \
  99. CGO_LDFLAGS="-arch arm64 -isysroot $(xcrun --sdk iphoneos --show-sdk-path)" \
  100. CGO_CFLAGS=-isysroot$(xcrun --sdk iphoneos --show-sdk-path) \
  101. 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
  102. }
  103. build_for_macos () {
  104. MACOS_BUILD_DIR="${BUILD_DIR}/macos"
  105. rm -rf "${MACOS_BUILD_DIR}"
  106. echo "...Getting project dependencies (via go get) for MacOS"
  107. cd ${BASE_DIR}
  108. GOOS=darwin go get -d -v -tags "${BUILD_TAGS}" ./...
  109. if [ $? != 0 ]; then
  110. echo "....'go get' failed, exiting"
  111. exit $?
  112. fi
  113. prepare_build darwin
  114. TARGET_ARCH=386
  115. 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
  116. TARGET_ARCH=amd64
  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. }
  119. cleanup () {
  120. # Remove temporary build artifacts
  121. rm -rf ${TEMP_DIR}
  122. }
  123. TARGET=$1
  124. case $TARGET in
  125. macos)
  126. echo "..Building for MacOS"
  127. build_for_macos
  128. if [ $? != 0 ]; then
  129. exit $?
  130. fi
  131. ;;
  132. ios)
  133. echo "..Building for iOS"
  134. build_for_ios
  135. if [ $? != 0 ]; then
  136. exit $?
  137. fi
  138. ;;
  139. all)
  140. echo "..Building all"
  141. build_for_ios
  142. if [ $? != 0 ]; then
  143. exit $?
  144. fi
  145. build_for_macos
  146. if [ $? != 0 ]; then
  147. exit $?
  148. fi
  149. ;;
  150. *)
  151. echo "..invalid target"
  152. exit 1
  153. ;;
  154. esac
  155. cleanup
  156. echo "BUILD DONE"