make.bash 5.2 KB

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