make.bash 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.buildDate=$BUILDDATE \
  30. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=$BUILDREPO \
  31. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=$BUILDREV \
  32. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=$GOVERSION \
  33. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.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. echo "...Getting project dependencies (via go get) for Android."
  47. GOOS=android go get -d -v -tags "${BUILD_TAGS}" ./...
  48. if [ $? != 0 ]; then
  49. echo "....'go get' failed, exiting"
  50. exit $?
  51. fi
  52. prepare_build android
  53. TARGET_ARCH=arm
  54. ARMV=7
  55. CC="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/arm-linux-androideabi-clang" \
  56. CXX="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/arm-linux-androideabi-clang++" \
  57. GOARM=${ARMV} \
  58. 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
  59. TARGET_ARCH=arm64
  60. CC="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/aarch64-linux-android-clang" \
  61. CXX="${ANDROID_NDK_TOOLCHAIN_ROOT}/${TARGET_ARCH}/bin/aarch64-linux-android-clang++" \
  62. 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
  63. }
  64. build_for_linux () {
  65. TARGET_OS=linux
  66. OUTPUT_DIR="${BUILD_DIR}/${TARGET_OS}"
  67. echo "...Getting project dependencies (via go get) for Linux."
  68. GOOS=linux go get -d -v -tags "${BUILD_TAGS}" ./...
  69. if [ $? != 0 ]; then
  70. echo "....'go get' failed, exiting"
  71. exit $?
  72. fi
  73. prepare_build linux
  74. TARGET_ARCH=386
  75. # TODO: is "CFLAGS=-m32" required?
  76. CFLAGS=-m32 \
  77. 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
  78. TARGET_ARCH=amd64
  79. 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
  80. }
  81. build_for_windows () {
  82. TARGET_OS=windows
  83. OUTPUT_DIR="${BUILD_DIR}/${TARGET_OS}"
  84. echo "...Getting project dependencies (via go get) for Windows."
  85. GOOS=windows go get -d -v -tags "${BUILD_TAGS}" ./...
  86. if [ $? != 0 ]; then
  87. echo "....'go get' failed, exiting"
  88. exit $?
  89. fi
  90. prepare_build windows
  91. TARGET_ARCH=386
  92. CGO_ENABLED=1 \
  93. CGO_LDFLAGS="-static-libgcc -L /usr/i686-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  94. CC=/usr/bin/i686-w64-mingw32-gcc \
  95. 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
  96. TARGET_ARCH=amd64
  97. CGO_ENABLED=1 \
  98. CGO_LDFLAGS="-static-libgcc -L /usr/x86_64-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  99. CC=/usr/bin/x86_64-w64-mingw32-gcc \
  100. 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
  101. }
  102. build_for_ios () {
  103. echo "To build for iOS please use build-darwin.sh"
  104. }
  105. build_for_macos () {
  106. echo "To build for macos please use build-darwin.sh"
  107. }
  108. TARGET=$1
  109. case $TARGET in
  110. windows)
  111. echo "..Building for Windows"
  112. build_for_windows
  113. exit $?
  114. ;;
  115. linux)
  116. echo "..Building for Linux"
  117. build_for_linux
  118. exit $?
  119. ;;
  120. macos)
  121. echo "..Building for MacOS"
  122. build_for_macos
  123. exit $?
  124. ;;
  125. android)
  126. echo "..Building for Android"
  127. build_for_android
  128. exit $?
  129. ;;
  130. ios)
  131. echo "..Building for iOS"
  132. build_for_ios
  133. exit $?
  134. ;;
  135. all)
  136. echo "..Building all"
  137. build_for_windows
  138. if [ $? != 0 ]; then
  139. exit $?
  140. fi
  141. build_for_linux
  142. if [ $? != 0 ]; then
  143. exit $?
  144. fi
  145. build_for_macos
  146. if [ $? != 0 ]; then
  147. exit $?
  148. fi
  149. build_for_android
  150. if [ $? != 0 ]; then
  151. exit $?
  152. fi
  153. build_for_ios
  154. if [ $? != 0 ]; then
  155. exit $?
  156. fi
  157. ;;
  158. *)
  159. echo "..invalid target"
  160. exit 1
  161. ;;
  162. esac
  163. echo "BUILD DONE"