build-darwin.sh 5.5 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.13.7/misc/ios/clangwrap.sh.
  10. GO_VERSION_REQUIRED="1.13.7"
  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 'PKG=pkg && cd $GOPATH/src/$PKG && echo -n "\"$PKG\":\"$(git rev-parse --short HEAD)\","' | sed 's/,$/}/')
  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. CC=${BASE_DIR}/clangwrap.sh \
  90. CXX=${BASE_DIR}/clangwrap.sh \
  91. CGO_LDFLAGS="-arch armv7 -isysroot $(xcrun --sdk iphoneos --show-sdk-path)" \
  92. CGO_CFLAGS=-isysroot$(xcrun --sdk iphoneos --show-sdk-path) \
  93. 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
  94. CC=${BASE_DIR}/clangwrap.sh \
  95. CXX=${BASE_DIR}/clangwrap.sh \
  96. CGO_LDFLAGS="-arch arm64 -isysroot $(xcrun --sdk iphoneos --show-sdk-path)" \
  97. CGO_CFLAGS=-isysroot$(xcrun --sdk iphoneos --show-sdk-path) \
  98. 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
  99. }
  100. build_for_macos () {
  101. MACOS_BUILD_DIR="${BUILD_DIR}/macos"
  102. rm -rf "${MACOS_BUILD_DIR}"
  103. prepare_build darwin
  104. # i386 is deprecated for macOS and does not link
  105. #TARGET_ARCH=386
  106. #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
  107. TARGET_ARCH=amd64
  108. 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
  109. }
  110. cleanup () {
  111. # Remove temporary build artifacts
  112. rm -rf ${TEMP_DIR}
  113. }
  114. TARGET=$1
  115. case $TARGET in
  116. macos)
  117. echo "..Building for MacOS"
  118. build_for_macos
  119. if [ $? != 0 ]; then
  120. exit $?
  121. fi
  122. ;;
  123. ios)
  124. echo "..Building for iOS"
  125. build_for_ios
  126. if [ $? != 0 ]; then
  127. exit $?
  128. fi
  129. ;;
  130. all)
  131. echo "..Building all"
  132. build_for_ios
  133. if [ $? != 0 ]; then
  134. exit $?
  135. fi
  136. build_for_macos
  137. if [ $? != 0 ]; then
  138. exit $?
  139. fi
  140. ;;
  141. *)
  142. echo "..invalid target"
  143. exit 1
  144. ;;
  145. esac
  146. cleanup
  147. echo "BUILD DONE"