make.bash 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/bin/env bash
  2. set -e
  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. EXE_BASENAME="psiphon-tunnel-core"
  8. BUILDINFOFILE="${EXE_BASENAME}_buildinfo.txt"
  9. BUILDDATE=$(date --iso-8601=seconds)
  10. BUILDREPO=$(git config --get remote.origin.url)
  11. BUILDREV=$(git rev-parse --short HEAD)
  12. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  13. LDFLAGS="\
  14. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildDate=$BUILDDATE \
  15. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildRepo=$BUILDREPO \
  16. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.buildRev=$BUILDREV \
  17. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon.goVersion=$GOVERSION \
  18. "
  19. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > $BUILDINFOFILE
  20. echo "Variables for ldflags:"
  21. echo " Build date: ${BUILDDATE}"
  22. echo " Build repo: ${BUILDREPO}"
  23. echo " Build revision: ${BUILDREV}"
  24. echo " Go version: ${GOVERSION}"
  25. echo ""
  26. if [ ! -d bin ]; then
  27. mkdir bin
  28. fi
  29. build_for_windows () {
  30. echo "...Getting project dependencies (via go get) for Windows. Parameter is: '$1'"
  31. GOOS=windows go get -d -v ./...
  32. if [ $? != 0 ]; then
  33. echo "....'go get' failed, exiting"
  34. exit $?
  35. fi
  36. if [ -z $1 ] || [ "$1" == "32" ]; then
  37. unset PKG_CONFIG_PATH
  38. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH_32
  39. echo "...Building windows-i686"
  40. echo "....PKG_CONFIG_PATH=$PKG_CONFIG_PATH"
  41. CGO_CFLAGS="-I $PKG_CONFIG_PATH/include/" \
  42. CGO_LDFLAGS="-L $PKG_CONFIG_PATH -L /usr/i686-w64-mingw32/lib/ -lssl -lcrypto -lwsock32 -lcrypt32 -lgdi32" \
  43. CC=/usr/bin/i686-w64-mingw32-gcc \
  44. gox -verbose -ldflags "$LDFLAGS" -osarch windows/386 -output bin/windows/${EXE_BASENAME}-i686
  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. fi
  55. if [ -z $1 ] || [ "$1" == "64" ]; then
  56. unset PKG_CONFIG_PATH
  57. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH_64
  58. echo "...Building windows-x86_64"
  59. echo "....PKG_CONFIG_PATH=$PKG_CONFIG_PATH"
  60. CGO_CFLAGS="-I $PKG_CONFIG_PATH/include/" \
  61. CGO_LDFLAGS="-L $PKG_CONFIG_PATH -L /usr/x86_64-w64-mingw32/lib/ -lssl -lcrypto -lwsock32 -lcrypt32 -lgdi32" \
  62. CC=/usr/bin/x86_64-w64-mingw32-gcc \
  63. gox -verbose -ldflags "$LDFLAGS" -osarch windows/amd64 -output bin/windows/${EXE_BASENAME}-x86_64
  64. RETVAL=$?
  65. if [ $RETVAL != 0 ]; then
  66. echo ".....gox failed, exiting"
  67. exit $RETVAL
  68. fi
  69. unset RETVAL
  70. # We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  71. echo "....No UPX for this build"
  72. fi
  73. }
  74. build_for_linux () {
  75. echo "Getting project dependencies (via go get) for Linux. Parameter is: '$1'"
  76. GOOS=linux go get -d -v ./...
  77. if [ $? != 0 ]; then
  78. echo "...'go get' failed, exiting"
  79. exit $?
  80. fi
  81. if [ -z $1 ] || [ "$1" == "32" ]; then
  82. echo "...Building linux-i686"
  83. CFLAGS=-m32 gox -verbose -ldflags "$LDFLAGS" -osarch linux/386 -output bin/linux/${EXE_BASENAME}-i686
  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. goupx --best bin/linux/${EXE_BASENAME}-i686
  92. RETVAL=$?
  93. if [ $RETVAL != 0 ]; then
  94. echo ".....goupx failed, exiting"
  95. exit $RETVAL
  96. fi
  97. unset RETVAL
  98. fi
  99. if [ -z $1 ] || [ "$1" == "64" ]; then
  100. echo "...Building linux-x86_64"
  101. gox -verbose -ldflags "$LDFLAGS" -osarch linux/amd64 -output bin/linux/${EXE_BASENAME}-x86_64
  102. RETVAL=$?
  103. if [ $RETVAL != 0 ]; then
  104. echo "....gox failed, exiting"
  105. exit $RETVAL
  106. fi
  107. unset RETVAL
  108. echo "....UPX packaging output"
  109. goupx --best bin/linux/${EXE_BASENAME}-x86_64
  110. RETVAL=$?
  111. if [ $RETVAL != 0 ]; then
  112. echo ".....goupx failed, exiting"
  113. exit $RETVAL
  114. fi
  115. unset RETVAL
  116. fi
  117. }
  118. build_for_osx () {
  119. echo "Getting project dependencies (via go get) for OSX"
  120. GOOS=darwin go get -d -v ./...
  121. if [ $? != 0 ]; then
  122. echo "..'go get' failed, exiting"
  123. exit $?
  124. fi
  125. echo "Building darwin-x86_64..."
  126. echo "..Disabling CGO for this build"
  127. CGO_ENABLED=0 gox -verbose -ldflags "$LDFLAGS" -osarch darwin/amd64 -output bin/darwin/${EXE_BASENAME}-x86_64
  128. # Darwin binaries don't seem to be UPXable when built this way
  129. echo "..No UPX for this build"
  130. }
  131. TARGET=$1
  132. case $TARGET in
  133. windows)
  134. echo "..Building for Windows"
  135. build_for_windows $2
  136. exit $?
  137. ;;
  138. linux)
  139. echo "..Building for Linux"
  140. build_for_linux $2
  141. exit $?
  142. ;;
  143. osx)
  144. echo "..Building for OSX"
  145. build_for_osx
  146. exit $?
  147. ;;
  148. all)
  149. echo "..Building all"
  150. build_for_windows $2
  151. if [ $? != 0 ]; then
  152. exit $?
  153. fi
  154. build_for_linux $2
  155. if [ $? != 0 ]; then
  156. exit $?
  157. fi
  158. build_for_osx
  159. if [ $? != 0 ]; then
  160. exit $?
  161. fi
  162. ;;
  163. *)
  164. echo "..No selection made, building all"
  165. build_for_windows $2
  166. if [ $? != 0 ]; then
  167. exit $?
  168. fi
  169. build_for_linux $2
  170. if [ $? != 0 ]; then
  171. exit $?
  172. fi
  173. build_for_osx
  174. if [ $? != 0 ]; then
  175. exit $?
  176. fi
  177. ;;
  178. esac
  179. echo "Done"