make.bash 5.9 KB

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