build-darwin.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. # Note:
  7. # clangwrap.sh needs to be updated when the Go version changes.
  8. # The last version was:
  9. # https://github.com/golang/go/blob/go1.17.1/misc/ios/clangwrap.sh
  10. # - with a patch to lower -mios-version-min to 7.0
  11. GO_VERSION_REQUIRED="1.17.1"
  12. # At this time, we don't support modules
  13. export GO111MODULE=off
  14. BASE_DIR=$(cd "$(dirname "$0")" ; pwd -P)
  15. cd ${BASE_DIR}
  16. # The location of the final build products
  17. BUILD_DIR="${BASE_DIR}/build/darwin"
  18. TEMP_DIR="${BUILD_DIR}/tmp"
  19. # Clean previous output
  20. rm -rf "${BUILD_DIR}"
  21. mkdir -p ${TEMP_DIR}
  22. if [[ $? != 0 ]]; then
  23. echo "FAILURE: mkdir -p ${TEMP_DIR}"
  24. exit 1
  25. fi
  26. # Ensure go is installed
  27. which go 2>&1 > /dev/null
  28. if [[ $? != 0 ]]; then
  29. echo "Go is not installed in the path, aborting"
  30. exit 1
  31. fi
  32. # Exporting these seems necessary for subcommands to pick them up.
  33. export GOPATH=${TEMP_DIR}/go-darwin-build
  34. export PATH=${GOPATH}/bin:${PATH}
  35. # The GOPATH we're using is temporary, so make sure there isn't one from a previous run.
  36. rm -rf ${GOPATH}
  37. TUNNEL_CORE_SRC_DIR=${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core
  38. mkdir -p ${GOPATH}
  39. if [[ $? != 0 ]]; then
  40. echo "FAILURE: mkdir -p ${GOPATH}"
  41. exit 1
  42. fi
  43. # Symlink the current source directory into GOPATH, so that we're building the
  44. # code in this local repo, rather than pulling from Github and building that.
  45. mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs
  46. if [[ $? != 0 ]]; then
  47. echo "mkdir -p ${GOPATH}/src/github.com/Psiphon-Labs"
  48. exit 1
  49. fi
  50. ln -s "${BASE_DIR}/.." "${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  51. if [[ $? != 0 ]]; then
  52. echo "ln -s ../.. ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core"
  53. exit 1
  54. fi
  55. # Check Go version
  56. GO_VERSION=$(go version | sed -E -n 's/.*go([0-9]\.[0-9]+(\.[0-9]+)?).*/\1/p')
  57. if [[ ${GO_VERSION} != ${GO_VERSION_REQUIRED} ]]; then
  58. echo "FAILURE: go version mismatch; require ${GO_VERSION_REQUIRED}; got ${GO_VERSION}"
  59. exit 1
  60. fi
  61. prepare_build () {
  62. # Ensure BUILD* variables reflect the tunnel-core repo
  63. cd ${TUNNEL_CORE_SRC_DIR}
  64. BUILDDATE=$(date +%Y-%m-%dT%H:%M:%S%z)
  65. BUILDREPO=$(git config --get remote.origin.url)
  66. BUILDREV=$(git rev-parse --short HEAD)
  67. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  68. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  69. cd ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary
  70. 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 'cd $GOPATH/src/$0 && if echo -n "$0" | grep -vEq "^github.com/Psiphon-Labs/psiphon-tunnel-core/" ; then echo -n "\"$0\":\"$(git rev-parse --short HEAD)\"," ; fi' pkg | sed 's/,$//' | tr -d '\n' && echo -n "}")
  71. LDFLAGS="\
  72. -s \
  73. -w \
  74. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=$BUILDDATE \
  75. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=$BUILDREPO \
  76. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=$BUILDREV \
  77. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=$GOVERSION \
  78. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=$DEPENDENCIES \
  79. "
  80. echo "Variables for ldflags:"
  81. echo " Build date: ${BUILDDATE}"
  82. echo " Build repo: ${BUILDREPO}"
  83. echo " Build revision: ${BUILDREV}"
  84. echo " Go version: ${GOVERSION}"
  85. echo " Dependencies: ${DEPENDENCIES}"
  86. echo ""
  87. }
  88. build_for_ios () {
  89. IOS_BUILD_DIR="${BUILD_DIR}/ios"
  90. rm -rf "${IOS_BUILD_DIR}"
  91. prepare_build darwin
  92. # As of Go 1.15, "ios/arm" is no longer supported: https://golang.org/doc/go1.15#darwin
  93. CC=${BASE_DIR}/clangwrap.sh \
  94. CXX=${BASE_DIR}/clangwrap.sh \
  95. CGO_LDFLAGS="-arch arm64 -isysroot $(xcrun --sdk iphoneos --show-sdk-path)" \
  96. CGO_CFLAGS=-isysroot$(xcrun --sdk iphoneos --show-sdk-path) \
  97. CGO_ENABLED=1 GOOS=ios GOARCH=arm64 go build -buildmode=c-archive -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o ${IOS_BUILD_DIR}/arm64/libpsiphontunnel.a PsiphonTunnel.go
  98. }
  99. build_for_macos () {
  100. MACOS_BUILD_DIR="${BUILD_DIR}/macos"
  101. rm -rf "${MACOS_BUILD_DIR}"
  102. prepare_build darwin
  103. # i386 is deprecated for macOS and does not link
  104. #TARGET_ARCH=386
  105. #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
  106. TARGET_ARCH=amd64
  107. 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
  108. }
  109. cleanup () {
  110. # Remove temporary build artifacts
  111. rm -rf ${TEMP_DIR}
  112. }
  113. TARGET=$1
  114. case $TARGET in
  115. macos)
  116. echo "..Building for MacOS"
  117. build_for_macos
  118. if [ $? != 0 ]; then
  119. exit $?
  120. fi
  121. ;;
  122. ios)
  123. echo "..Building for iOS"
  124. build_for_ios
  125. if [ $? != 0 ]; then
  126. exit $?
  127. fi
  128. ;;
  129. all)
  130. echo "..Building all"
  131. build_for_ios
  132. if [ $? != 0 ]; then
  133. exit $?
  134. fi
  135. build_for_macos
  136. if [ $? != 0 ]; then
  137. exit $?
  138. fi
  139. ;;
  140. *)
  141. echo "..invalid target"
  142. exit 1
  143. ;;
  144. esac
  145. cleanup
  146. echo "BUILD DONE"