make.bash 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. LDFLAGS="\
  17. -s \
  18. -w \
  19. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildDate=$BUILDDATE \
  20. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRepo=$BUILDREPO \
  21. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.buildRev=$BUILDREV \
  22. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo.goVersion=$GOVERSION \
  23. "
  24. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > $BUILDINFOFILE
  25. echo "Variables for ldflags:"
  26. echo " Build date: ${BUILDDATE}"
  27. echo " Build repo: ${BUILDREPO}"
  28. echo " Build revision: ${BUILDREV}"
  29. echo " Go version: ${GOVERSION}"
  30. echo ""
  31. }
  32. if [ ! -d bin ]; then
  33. mkdir bin
  34. fi
  35. build_for_windows () {
  36. prepare_build windows
  37. echo "...Building windows-i686"
  38. CGO_LDFLAGS="-L /usr/i686-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  39. CC=/usr/bin/i686-w64-mingw32-gcc \
  40. GOOS=windows GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/windows/${EXE_BASENAME}-i686.exe
  41. RETVAL=$?
  42. echo ".....gox completed, exit code: $?"
  43. if [ $RETVAL != 0 ]; then
  44. echo ".....gox failed, exiting"
  45. exit $RETVAL
  46. fi
  47. unset RETVAL
  48. ## We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  49. echo "....No UPX for this build"
  50. echo "...Building windows-x86_64"
  51. CGO_LDFLAGS="-L /usr/x86_64-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  52. CC=/usr/bin/x86_64-w64-mingw32-gcc \
  53. GOOS=windows GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/windows/${EXE_BASENAME}-x86_64.exe
  54. RETVAL=$?
  55. if [ $RETVAL != 0 ]; then
  56. echo ".....gox failed, exiting"
  57. exit $RETVAL
  58. fi
  59. unset RETVAL
  60. # We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  61. echo "....No UPX for this build"
  62. }
  63. build_for_linux () {
  64. prepare_build linux
  65. echo "...Building linux-i686"
  66. # TODO: is "CFLAGS=-m32" required?
  67. CFLAGS=-m32 GOOS=linux GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/linux/${EXE_BASENAME}-i686
  68. RETVAL=$?
  69. if [ $RETVAL != 0 ]; then
  70. echo ".....gox failed, exiting"
  71. exit $RETVAL
  72. fi
  73. unset RETVAL
  74. echo "....UPX packaging output"
  75. upx --best bin/linux/${EXE_BASENAME}-i686
  76. RETVAL=$?
  77. if [ $RETVAL != 0 ]; then
  78. echo ".....upx failed, exiting"
  79. exit $RETVAL
  80. fi
  81. unset RETVAL
  82. echo "...Building linux-x86_64"
  83. GOOS=linux GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/linux/${EXE_BASENAME}-x86_64
  84. RETVAL=$?
  85. if [ $RETVAL != 0 ]; then
  86. echo "....gox failed, exiting"
  87. exit $RETVAL
  88. fi
  89. unset RETVAL
  90. echo "....UPX packaging output"
  91. upx --best bin/linux/${EXE_BASENAME}-x86_64
  92. RETVAL=$?
  93. if [ $RETVAL != 0 ]; then
  94. echo ".....upx failed, exiting"
  95. exit $RETVAL
  96. fi
  97. unset RETVAL
  98. }
  99. build_for_osx () {
  100. prepare_build darwin
  101. echo "Building darwin-x86_64..."
  102. echo "..Disabling CGO for this build"
  103. # TODO: is "CGO_ENABLED=0" required?
  104. CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/darwin/${EXE_BASENAME}-x86_64
  105. # Darwin binaries don't seem to be UPXable when built this way
  106. echo "..No UPX for this build"
  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. osx)
  121. echo "..Building for OSX"
  122. build_for_osx
  123. exit $?
  124. ;;
  125. all)
  126. echo "..Building all"
  127. build_for_windows
  128. if [ $? != 0 ]; then
  129. exit $?
  130. fi
  131. build_for_linux
  132. if [ $? != 0 ]; then
  133. exit $?
  134. fi
  135. build_for_osx
  136. if [ $? != 0 ]; then
  137. exit $?
  138. fi
  139. ;;
  140. *)
  141. echo "..invalid target"
  142. exit 1
  143. ;;
  144. esac
  145. echo "Done"