make.bash 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/usr/bin/env bash
  2. set -e -u -x
  3. if [ ! -f make.bash ]; then
  4. echo "make.bash must be run from $GOPATH/src/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary"
  5. exit 1
  6. fi
  7. # $2, if specified, is go build tags
  8. if [ -z ${2+x} ]; then BUILD_TAGS=""; else BUILD_TAGS="$2"; fi
  9. export GOCACHE=/tmp
  10. BUILD_DIR=build
  11. if [ ! -d ${BUILD_DIR} ]; then
  12. mkdir ${BUILD_DIR}
  13. fi
  14. prepare_build () {
  15. BUILDDATE=$(date --iso-8601=seconds)
  16. BUILDREPO=$(git config --get remote.origin.url)
  17. BUILDREV=$(git rev-parse --short HEAD)
  18. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  19. LDFLAGS="\
  20. -s \
  21. -w \
  22. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=$BUILDDATE \
  23. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=$BUILDREPO \
  24. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=$BUILDREV \
  25. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=$GOVERSION \
  26. "
  27. echo "Variables for ldflags:"
  28. echo " Build date: ${BUILDDATE}"
  29. echo " Build repo: ${BUILDREPO}"
  30. echo " Build revision: ${BUILDREV}"
  31. echo " Go version: ${GOVERSION}"
  32. echo ""
  33. }
  34. build_for_android () {
  35. TARGET_OS=android
  36. OUTPUT_DIR="${BUILD_DIR}/${TARGET_OS}"
  37. prepare_build android
  38. # Required workaround for a !PSIPHON_DISABLE_INPROXY dependency:
  39. # https://github.com/wlynxg/anet/tree/5501d401a269290292909e6cc75f105571f97cfa?tab=readme-ov-file#how-to-build-with-go-1230-or-later
  40. #
  41. # TODO: conditional on !PSIPHON_DISABLE_INPROXY build tag?
  42. ANDROID_LDFLAGS="-checklinkname=0 $LDFLAGS"
  43. TARGET_ARCH=arm
  44. ARMV=7
  45. CC="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/arm-linux-androideabi-clang" \
  46. CXX="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/arm-linux-androideabi-clang++" \
  47. GOARM=${ARMV} \
  48. GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$ANDROID_LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}${ARMV}/libpsiphontunnel.so" PsiphonTunnel.go
  49. TARGET_ARCH=arm64
  50. CC="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/aarch64-linux-android-clang" \
  51. CXX="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/aarch64-linux-android-clang++" \
  52. GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$ANDROID_LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}/libpsiphontunnel.so" PsiphonTunnel.go
  53. }
  54. build_for_linux () {
  55. TARGET_OS=linux
  56. OUTPUT_DIR="${BUILD_DIR}/${TARGET_OS}"
  57. prepare_build linux
  58. TARGET_ARCH=386
  59. # TODO: is "CFLAGS=-m32" required?
  60. CFLAGS=-m32 \
  61. GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}/libpsiphontunnel.so" PsiphonTunnel.go
  62. TARGET_ARCH=amd64
  63. GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}/libpsiphontunnel.so" PsiphonTunnel.go
  64. }
  65. build_for_windows () {
  66. TARGET_OS=windows
  67. OUTPUT_DIR="${BUILD_DIR}/${TARGET_OS}"
  68. prepare_build windows
  69. TARGET_ARCH=386
  70. CGO_ENABLED=1 \
  71. CGO_LDFLAGS="-static-libgcc -L /usr/i686-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  72. CC=/usr/bin/i686-w64-mingw32-gcc \
  73. GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}/psiphontunnel.dll" PsiphonTunnel.go
  74. TARGET_ARCH=amd64
  75. CGO_ENABLED=1 \
  76. CGO_LDFLAGS="-static-libgcc -L /usr/x86_64-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  77. CC=/usr/bin/x86_64-w64-mingw32-gcc \
  78. GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -buildmode=c-shared -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o "${OUTPUT_DIR}/${TARGET_ARCH}/psiphontunnel.dll" PsiphonTunnel.go
  79. }
  80. build_for_ios () {
  81. echo "To build for iOS please use build-darwin.sh"
  82. }
  83. build_for_macos () {
  84. echo "To build for macos please use build-darwin.sh"
  85. }
  86. TARGET=$1
  87. case $TARGET in
  88. windows)
  89. echo "..Building for Windows"
  90. build_for_windows
  91. exit $?
  92. ;;
  93. linux)
  94. echo "..Building for Linux"
  95. build_for_linux
  96. exit $?
  97. ;;
  98. macos)
  99. echo "..Building for MacOS"
  100. build_for_macos
  101. exit $?
  102. ;;
  103. android)
  104. echo "..Building for Android"
  105. build_for_android
  106. exit $?
  107. ;;
  108. ios)
  109. echo "..Building for iOS"
  110. build_for_ios
  111. exit $?
  112. ;;
  113. all)
  114. echo "..Building all"
  115. build_for_windows
  116. if [ $? != 0 ]; then
  117. exit $?
  118. fi
  119. build_for_linux
  120. if [ $? != 0 ]; then
  121. exit $?
  122. fi
  123. build_for_macos
  124. if [ $? != 0 ]; then
  125. exit $?
  126. fi
  127. build_for_android
  128. if [ $? != 0 ]; then
  129. exit $?
  130. fi
  131. build_for_ios
  132. if [ $? != 0 ]; then
  133. exit $?
  134. fi
  135. ;;
  136. *)
  137. echo "..invalid target"
  138. exit 1
  139. ;;
  140. esac
  141. echo "BUILD DONE"