build-darwin.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.24.4/misc/ios/clangwrap.sh
  10. GO_VERSION_REQUIRED="1.24.4"
  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. cd ${GOPATH}/src/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary
  66. LDFLAGS="\
  67. -s \
  68. -w \
  69. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=$BUILDDATE \
  70. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=$BUILDREPO \
  71. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=$BUILDREV \
  72. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=$GOVERSION \
  73. "
  74. echo "Variables for ldflags:"
  75. echo " Build date: ${BUILDDATE}"
  76. echo " Build repo: ${BUILDREPO}"
  77. echo " Build revision: ${BUILDREV}"
  78. echo " Go version: ${GOVERSION}"
  79. echo ""
  80. }
  81. build_for_ios () {
  82. IOS_BUILD_DIR="${BUILD_DIR}/ios"
  83. rm -rf "${IOS_BUILD_DIR}"
  84. prepare_build darwin
  85. # As of Go 1.15, "ios/arm" is no longer supported: https://golang.org/doc/go1.15#darwin
  86. CC=${BASE_DIR}/clangwrap.sh \
  87. CXX=${BASE_DIR}/clangwrap.sh \
  88. CGO_LDFLAGS="-arch arm64 -isysroot $(xcrun --sdk iphoneos --show-sdk-path)" \
  89. CGO_CFLAGS=-isysroot$(xcrun --sdk iphoneos --show-sdk-path) \
  90. 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
  91. }
  92. build_for_macos () {
  93. MACOS_BUILD_DIR="${BUILD_DIR}/macos"
  94. rm -rf "${MACOS_BUILD_DIR}"
  95. prepare_build darwin
  96. # i386 is deprecated for macOS and does not link
  97. #TARGET_ARCH=386
  98. #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
  99. TARGET_ARCH=amd64
  100. 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
  101. }
  102. cleanup () {
  103. # Remove temporary build artifacts
  104. rm -rf ${TEMP_DIR}
  105. }
  106. TARGET=$1
  107. case $TARGET in
  108. macos)
  109. echo "..Building for MacOS"
  110. build_for_macos
  111. if [ $? != 0 ]; then
  112. exit $?
  113. fi
  114. ;;
  115. ios)
  116. echo "..Building for iOS"
  117. build_for_ios
  118. if [ $? != 0 ]; then
  119. exit $?
  120. fi
  121. ;;
  122. all)
  123. echo "..Building all"
  124. build_for_ios
  125. if [ $? != 0 ]; then
  126. exit $?
  127. fi
  128. build_for_macos
  129. if [ $? != 0 ]; then
  130. exit $?
  131. fi
  132. ;;
  133. *)
  134. echo "..invalid target"
  135. exit 1
  136. ;;
  137. esac
  138. cleanup
  139. echo "BUILD DONE"