make.bash 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/ConsoleClient"
  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. EXE_BASENAME="psiphon-tunnel-core"
  10. prepare_build () {
  11. BUILDINFOFILE="${EXE_BASENAME}_buildinfo.txt"
  12. BUILDDATE=$(date --iso-8601=seconds)
  13. BUILDREPO=$(git config --get remote.origin.url)
  14. BUILDREV=$(git rev-parse --short HEAD)
  15. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  16. # see DEPENDENCIES comment in MobileLibrary/Android/make.bash
  17. 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 "}")
  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. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.dependencies=$DEPENDENCIES \
  26. "
  27. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > $BUILDINFOFILE
  28. echo "Variables for ldflags:"
  29. echo " Build date: ${BUILDDATE}"
  30. echo " Build repo: ${BUILDREPO}"
  31. echo " Build revision: ${BUILDREV}"
  32. echo " Go version: ${GOVERSION}"
  33. echo " Dependencies: ${DEPENDENCIES}"
  34. echo ""
  35. }
  36. if [ ! -d bin ]; then
  37. mkdir bin
  38. fi
  39. build_for_windows () {
  40. prepare_build windows
  41. echo "...Building windows-i686"
  42. CGO_LDFLAGS="-L /usr/i686-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  43. CC=/usr/bin/i686-w64-mingw32-gcc \
  44. GOOS=windows GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/windows/${EXE_BASENAME}-i686.exe
  45. RETVAL=$?
  46. echo ".....gox completed, exit code: $?"
  47. if [ $RETVAL != 0 ]; then
  48. echo ".....gox failed, exiting"
  49. exit $RETVAL
  50. fi
  51. unset RETVAL
  52. ## We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  53. echo "....No UPX for this build"
  54. echo "...Building windows-x86_64"
  55. CGO_LDFLAGS="-L /usr/x86_64-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  56. CC=/usr/bin/x86_64-w64-mingw32-gcc \
  57. GOOS=windows GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/windows/${EXE_BASENAME}-x86_64.exe
  58. RETVAL=$?
  59. if [ $RETVAL != 0 ]; then
  60. echo ".....gox failed, exiting"
  61. exit $RETVAL
  62. fi
  63. unset RETVAL
  64. # We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  65. echo "....No UPX for this build"
  66. }
  67. build_for_linux () {
  68. prepare_build linux
  69. echo "...Building linux-i686"
  70. # TODO: is "CFLAGS=-m32" required?
  71. CFLAGS=-m32 GOOS=linux GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/linux/${EXE_BASENAME}-i686
  72. RETVAL=$?
  73. if [ $RETVAL != 0 ]; then
  74. echo ".....gox failed, exiting"
  75. exit $RETVAL
  76. fi
  77. unset RETVAL
  78. echo "....UPX packaging output"
  79. upx --best bin/linux/${EXE_BASENAME}-i686
  80. RETVAL=$?
  81. if [ $RETVAL != 0 ]; then
  82. echo ".....upx failed, exiting"
  83. exit $RETVAL
  84. fi
  85. unset RETVAL
  86. echo "...Building linux-x86_64"
  87. GOOS=linux GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/linux/${EXE_BASENAME}-x86_64
  88. RETVAL=$?
  89. if [ $RETVAL != 0 ]; then
  90. echo "....gox failed, exiting"
  91. exit $RETVAL
  92. fi
  93. unset RETVAL
  94. echo "....UPX packaging output"
  95. upx --best bin/linux/${EXE_BASENAME}-x86_64
  96. RETVAL=$?
  97. if [ $RETVAL != 0 ]; then
  98. echo ".....upx failed, exiting"
  99. exit $RETVAL
  100. fi
  101. unset RETVAL
  102. }
  103. build_for_osx () {
  104. prepare_build darwin
  105. echo "Building darwin-x86_64..."
  106. echo "..Disabling CGO for this build"
  107. # TODO: is "CGO_ENABLED=0" required?
  108. CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/darwin/${EXE_BASENAME}-x86_64
  109. # Darwin binaries don't seem to be UPXable when built this way
  110. echo "..No UPX for this build"
  111. }
  112. TARGET=$1
  113. case $TARGET in
  114. windows)
  115. echo "..Building for Windows"
  116. build_for_windows
  117. exit $?
  118. ;;
  119. linux)
  120. echo "..Building for Linux"
  121. build_for_linux
  122. exit $?
  123. ;;
  124. osx)
  125. echo "..Building for OSX"
  126. build_for_osx
  127. exit $?
  128. ;;
  129. all)
  130. echo "..Building all"
  131. build_for_windows
  132. if [ $? != 0 ]; then
  133. exit $?
  134. fi
  135. build_for_linux
  136. if [ $? != 0 ]; then
  137. exit $?
  138. fi
  139. build_for_osx
  140. if [ $? != 0 ]; then
  141. exit $?
  142. fi
  143. ;;
  144. *)
  145. echo "..invalid target"
  146. exit 1
  147. ;;
  148. esac
  149. echo "Done"