make.bash 6.4 KB

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