make.bash 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. # - starts the string with a `{`
  19. # - uses the `go list` command and passes it a template string (using the Go template syntax) saying I want all the dependencies of the package in the current directory, printing 1/line via printf
  20. # - pipes to `xargs` to run a command on each line output from the first command
  21. # - uses `go list` with a template string to print the "Import Path" (from just below `$GOPATH/src`) if the package is not part of the standard library
  22. # - pipes to `xargs` again, specifiying `pkg` as the placeholder name for each item being operated on (which is the list of non standard library import paths from the previous step)
  23. # - `xargs` runs a bash script (via `-c`) which changes to each import path in sequence, then echoes out `"<import path>":"<subshell output of getting the short git revision>",`
  24. # - this leaves a trailing `,` at the end, and no close to the JSON object, so simply `sed` replace the comma before the end of the line with `}` and you now have valid JSON
  25. 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/pkg && echo -n "\"pkg\":\"$(git rev-parse --short HEAD)\","' | sed 's/,$/}/')
  26. LDFLAGS="\
  27. -s \
  28. -w \
  29. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=$BUILDDATE \
  30. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=$BUILDREPO \
  31. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=$BUILDREV \
  32. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=$GOVERSION \
  33. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=$DEPENDENCIES \
  34. "
  35. echo "Variables for ldflags:"
  36. echo " Build date: ${BUILDDATE}"
  37. echo " Build repo: ${BUILDREPO}"
  38. echo " Build revision: ${BUILDREV}"
  39. echo " Go version: ${GOVERSION}"
  40. echo " Dependencies: ${DEPENDENCIES}"
  41. echo ""
  42. }
  43. build_for_android () {
  44. TARGET_OS=android
  45. OUTPUT_DIR="${BUILD_DIR}/${TARGET_OS}"
  46. prepare_build android
  47. TARGET_ARCH=arm
  48. ARMV=7
  49. CC="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/arm-linux-androideabi-clang" \
  50. CXX="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/arm-linux-androideabi-clang++" \
  51. GOARM=${ARMV} \
  52. 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
  53. TARGET_ARCH=arm64
  54. CC="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/aarch64-linux-android-clang" \
  55. CXX="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/aarch64-linux-android-clang++" \
  56. 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
  57. }
  58. build_for_linux () {
  59. TARGET_OS=linux
  60. OUTPUT_DIR="${BUILD_DIR}/${TARGET_OS}"
  61. prepare_build linux
  62. TARGET_ARCH=386
  63. # TODO: is "CFLAGS=-m32" required?
  64. CFLAGS=-m32 \
  65. 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
  66. TARGET_ARCH=amd64
  67. 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
  68. }
  69. build_for_windows () {
  70. TARGET_OS=windows
  71. OUTPUT_DIR="${BUILD_DIR}/${TARGET_OS}"
  72. prepare_build windows
  73. TARGET_ARCH=386
  74. CGO_ENABLED=1 \
  75. CGO_LDFLAGS="-static-libgcc -L /usr/i686-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  76. CC=/usr/bin/i686-w64-mingw32-gcc \
  77. 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
  78. TARGET_ARCH=amd64
  79. CGO_ENABLED=1 \
  80. CGO_LDFLAGS="-static-libgcc -L /usr/x86_64-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  81. CC=/usr/bin/x86_64-w64-mingw32-gcc \
  82. 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
  83. }
  84. build_for_ios () {
  85. echo "To build for iOS please use build-darwin.sh"
  86. }
  87. build_for_macos () {
  88. echo "To build for macos please use build-darwin.sh"
  89. }
  90. TARGET=$1
  91. case $TARGET in
  92. windows)
  93. echo "..Building for Windows"
  94. build_for_windows
  95. exit $?
  96. ;;
  97. linux)
  98. echo "..Building for Linux"
  99. build_for_linux
  100. exit $?
  101. ;;
  102. macos)
  103. echo "..Building for MacOS"
  104. build_for_macos
  105. exit $?
  106. ;;
  107. android)
  108. echo "..Building for Android"
  109. build_for_android
  110. exit $?
  111. ;;
  112. ios)
  113. echo "..Building for iOS"
  114. build_for_ios
  115. exit $?
  116. ;;
  117. all)
  118. echo "..Building all"
  119. build_for_windows
  120. if [ $? != 0 ]; then
  121. exit $?
  122. fi
  123. build_for_linux
  124. if [ $? != 0 ]; then
  125. exit $?
  126. fi
  127. build_for_macos
  128. if [ $? != 0 ]; then
  129. exit $?
  130. fi
  131. build_for_android
  132. if [ $? != 0 ]; then
  133. exit $?
  134. fi
  135. build_for_ios
  136. if [ $? != 0 ]; then
  137. exit $?
  138. fi
  139. ;;
  140. *)
  141. echo "..invalid target"
  142. exit 1
  143. ;;
  144. esac
  145. echo "BUILD DONE"