make.bash 5.1 KB

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