make.bash 4.4 KB

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