build-darwin.sh 5.3 KB

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