make.bash 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. # - starts the string with a `{`
  15. # - uses the `go list` command and passes it a template string (using the Go template syntax) saying I want all the dependencies of the package in the current directory, printing 1/line via printf
  16. # - pipes to `xargs` to run a command on each line output from the first command
  17. # - uses `go list` with a template string to print the "Import Path" (from just below `$GOPATH/src`) if the package is not part of the standard library
  18. # - pipes to `xargs` again, specifiying `pkg` as the placeholder name for each item being operated on (which is the list of non standard library import paths from the previous step)
  19. # - `xargs` runs a bash script (via `-c`) which changes to each import path in sequence, then echoes out `"<import path>":"<subshell output of getting the short git revision>",`
  20. # - this leaves a trailing `,` at the end, and no close to the JSON object, so simply `sed` replace the comma before the end of the line with `}` and you now have valid JSON
  21. 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/,$/}/')
  22. LDFLAGS="\
  23. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=$BUILDDATE \
  24. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=$BUILDREPO \
  25. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=$BUILDREV \
  26. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=$GOVERSION \
  27. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.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. echo "...Getting project dependencies (via go get) for Windows. Parameter is: '$1'"
  43. GOOS=windows go get -d -v ./...
  44. prepare_build
  45. if [ $? != 0 ]; then
  46. echo "....'go get' failed, exiting"
  47. exit $?
  48. fi
  49. if [ -z $1 ] || [ "$1" == "32" ]; then
  50. unset PKG_CONFIG_PATH
  51. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH_32
  52. echo "...Building windows-i686"
  53. echo "....PKG_CONFIG_PATH=$PKG_CONFIG_PATH"
  54. CGO_CFLAGS="-I $PKG_CONFIG_PATH/include/" \
  55. CGO_LDFLAGS="-L $PKG_CONFIG_PATH -L /usr/i686-w64-mingw32/lib/ -lssl -lcrypto -lwsock32 -lcrypt32 -lgdi32" \
  56. CC=/usr/bin/i686-w64-mingw32-gcc \
  57. gox -verbose -ldflags "$LDFLAGS" -osarch windows/386 -output bin/windows/${EXE_BASENAME}-i686
  58. RETVAL=$?
  59. echo ".....gox completed, exit code: $?"
  60. if [ $RETVAL != 0 ]; then
  61. echo ".....gox failed, exiting"
  62. exit $RETVAL
  63. fi
  64. unset RETVAL
  65. ## We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  66. echo "....No UPX for this build"
  67. fi
  68. if [ -z $1 ] || [ "$1" == "64" ]; then
  69. unset PKG_CONFIG_PATH
  70. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH_64
  71. echo "...Building windows-x86_64"
  72. echo "....PKG_CONFIG_PATH=$PKG_CONFIG_PATH"
  73. CGO_CFLAGS="-I $PKG_CONFIG_PATH/include/" \
  74. CGO_LDFLAGS="-L $PKG_CONFIG_PATH -L /usr/x86_64-w64-mingw32/lib/ -lssl -lcrypto -lwsock32 -lcrypt32 -lgdi32" \
  75. CC=/usr/bin/x86_64-w64-mingw32-gcc \
  76. gox -verbose -ldflags "$LDFLAGS" -osarch windows/amd64 -output bin/windows/${EXE_BASENAME}-x86_64
  77. RETVAL=$?
  78. if [ $RETVAL != 0 ]; then
  79. echo ".....gox failed, exiting"
  80. exit $RETVAL
  81. fi
  82. unset RETVAL
  83. # We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  84. echo "....No UPX for this build"
  85. fi
  86. }
  87. build_for_linux () {
  88. echo "Getting project dependencies (via go get) for Linux. Parameter is: '$1'"
  89. GOOS=linux go get -d -v ./...
  90. prepare_build
  91. if [ $? != 0 ]; then
  92. echo "...'go get' failed, exiting"
  93. exit $?
  94. fi
  95. if [ -z $1 ] || [ "$1" == "32" ]; then
  96. echo "...Building linux-i686"
  97. CFLAGS=-m32 gox -verbose -ldflags "$LDFLAGS" -osarch linux/386 -output bin/linux/${EXE_BASENAME}-i686
  98. RETVAL=$?
  99. if [ $RETVAL != 0 ]; then
  100. echo ".....gox failed, exiting"
  101. exit $RETVAL
  102. fi
  103. unset RETVAL
  104. echo "....UPX packaging output"
  105. goupx --best bin/linux/${EXE_BASENAME}-i686
  106. RETVAL=$?
  107. if [ $RETVAL != 0 ]; then
  108. echo ".....goupx failed, exiting"
  109. exit $RETVAL
  110. fi
  111. unset RETVAL
  112. fi
  113. if [ -z $1 ] || [ "$1" == "64" ]; then
  114. echo "...Building linux-x86_64"
  115. gox -verbose -ldflags "$LDFLAGS" -osarch linux/amd64 -output bin/linux/${EXE_BASENAME}-x86_64
  116. RETVAL=$?
  117. if [ $RETVAL != 0 ]; then
  118. echo "....gox failed, exiting"
  119. exit $RETVAL
  120. fi
  121. unset RETVAL
  122. echo "....UPX packaging output"
  123. goupx --best bin/linux/${EXE_BASENAME}-x86_64
  124. RETVAL=$?
  125. if [ $RETVAL != 0 ]; then
  126. echo ".....goupx failed, exiting"
  127. exit $RETVAL
  128. fi
  129. unset RETVAL
  130. fi
  131. }
  132. build_for_osx () {
  133. echo "Getting project dependencies (via go get) for OSX"
  134. GOOS=darwin go get -d -v ./...
  135. prepare_build
  136. if [ $? != 0 ]; then
  137. echo "..'go get' failed, exiting"
  138. exit $?
  139. fi
  140. echo "Building darwin-x86_64..."
  141. echo "..Disabling CGO for this build"
  142. CGO_ENABLED=0 gox -verbose -ldflags "$LDFLAGS" -osarch darwin/amd64 -output bin/darwin/${EXE_BASENAME}-x86_64
  143. # Darwin binaries don't seem to be UPXable when built this way
  144. echo "..No UPX for this build"
  145. }
  146. TARGET=$1
  147. case $TARGET in
  148. windows)
  149. echo "..Building for Windows"
  150. build_for_windows $2
  151. exit $?
  152. ;;
  153. linux)
  154. echo "..Building for Linux"
  155. build_for_linux $2
  156. exit $?
  157. ;;
  158. osx)
  159. echo "..Building for OSX"
  160. build_for_osx
  161. exit $?
  162. ;;
  163. all)
  164. echo "..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. *)
  179. echo "..No selection made, building all"
  180. build_for_windows $2
  181. if [ $? != 0 ]; then
  182. exit $?
  183. fi
  184. build_for_linux $2
  185. if [ $? != 0 ]; then
  186. exit $?
  187. fi
  188. build_for_osx
  189. if [ $? != 0 ]; then
  190. exit $?
  191. fi
  192. ;;
  193. esac
  194. echo "Done"