make.bash 4.4 KB

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