make.bash 5.1 KB

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