make.bash 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/usr/bin/env bash
  2. set -e -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. EXE_BASENAME="psiphon-tunnel-core"
  8. # BUILD_TAGS needs to be outside of prepare_build because it determines what's fetched by go-get.
  9. PRIVATE_PLUGINS_TAG=""
  10. BUILD_TAGS="${PRIVATE_PLUGINS_TAG}"
  11. WINDOWS_BUILD_TAGS="${BUILD_TAGS}"
  12. LINUX_BUILD_TAGS="${BUILD_TAGS}"
  13. OSX_BUILD_TAGS="${BUILD_TAGS}"
  14. prepare_build () {
  15. BUILDINFOFILE="${EXE_BASENAME}_buildinfo.txt"
  16. BUILDDATE=$(date --iso-8601=seconds)
  17. BUILDREPO=$(git config --get remote.origin.url)
  18. BUILDREV=$(git rev-parse --short HEAD)
  19. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  20. # - starts the string with a `{`
  21. # - 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
  22. # - pipes to `xargs` to run a command on each line output from the first command
  23. # - 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
  24. # - 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)
  25. # - `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>",`
  26. # - 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
  27. DEPENDENCIES=$(echo -n "{" && go list -tags "$1" -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/,$/}/')
  28. LDFLAGS="\
  29. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=$BUILDDATE \
  30. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=$BUILDREPO \
  31. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=$BUILDREV \
  32. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=$GOVERSION \
  33. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=$DEPENDENCIES \
  34. "
  35. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > $BUILDINFOFILE
  36. echo "Variables for ldflags:"
  37. echo " Build date: ${BUILDDATE}"
  38. echo " Build repo: ${BUILDREPO}"
  39. echo " Build revision: ${BUILDREV}"
  40. echo " Go version: ${GOVERSION}"
  41. echo " Dependencies: ${DEPENDENCIES}"
  42. echo ""
  43. }
  44. if [ ! -d bin ]; then
  45. mkdir bin
  46. fi
  47. build_for_windows () {
  48. echo "...Getting project dependencies (via go get) for Windows. Parameter is: '$1'"
  49. GOOS=windows go get -d -v -tags "$WINDOWS_BUILD_TAGS" ./...
  50. prepare_build "$WINDOWS_BUILD_TAGS"
  51. if [ $? != 0 ]; then
  52. echo "....'go get' failed, exiting"
  53. exit $?
  54. fi
  55. if [ -z $1 ] || [ "$1" == "32" ]; then
  56. echo "...Building windows-i686"
  57. CGO_LDFLAGS="-L /usr/i686-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  58. CC=/usr/bin/i686-w64-mingw32-gcc \
  59. GOOS=windows GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "$WINDOWS_BUILD_TAGS" -o bin/windows/${EXE_BASENAME}-i686.exe
  60. RETVAL=$?
  61. echo ".....gox completed, exit code: $?"
  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. if [ -z $1 ] || [ "$1" == "64" ]; then
  71. echo "...Building windows-x86_64"
  72. CGO_LDFLAGS="-L /usr/x86_64-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  73. CC=/usr/bin/x86_64-w64-mingw32-gcc \
  74. GOOS=windows GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "$WINDOWS_BUILD_TAGS" -o bin/windows/${EXE_BASENAME}-x86_64.exe
  75. RETVAL=$?
  76. if [ $RETVAL != 0 ]; then
  77. echo ".....gox failed, exiting"
  78. exit $RETVAL
  79. fi
  80. unset RETVAL
  81. # We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  82. echo "....No UPX for this build"
  83. fi
  84. }
  85. build_for_linux () {
  86. echo "Getting project dependencies (via go get) for Linux. Parameter is: '$1'"
  87. GOOS=linux go get -d -v -tags "$LINUX_BUILD_TAGS" ./...
  88. prepare_build "$LINUX_BUILD_TAGS"
  89. if [ $? != 0 ]; then
  90. echo "...'go get' failed, exiting"
  91. exit $?
  92. fi
  93. if [ -z $1 ] || [ "$1" == "32" ]; then
  94. echo "...Building linux-i686"
  95. # TODO: is "CFLAGS=-m32" required?
  96. CFLAGS=-m32 GOOS=linux GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "$LINUX_BUILD_TAGS" -o bin/linux/${EXE_BASENAME}-i686
  97. RETVAL=$?
  98. if [ $RETVAL != 0 ]; then
  99. echo ".....gox failed, exiting"
  100. exit $RETVAL
  101. fi
  102. unset RETVAL
  103. echo "....UPX packaging output"
  104. goupx --best bin/linux/${EXE_BASENAME}-i686
  105. RETVAL=$?
  106. if [ $RETVAL != 0 ]; then
  107. echo ".....goupx failed, exiting"
  108. exit $RETVAL
  109. fi
  110. unset RETVAL
  111. fi
  112. if [ -z $1 ] || [ "$1" == "64" ]; then
  113. echo "...Building linux-x86_64"
  114. GOOS=linux GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "$LINUX_BUILD_TAGS" -o bin/linux/${EXE_BASENAME}-x86_64
  115. RETVAL=$?
  116. if [ $RETVAL != 0 ]; then
  117. echo "....gox failed, exiting"
  118. exit $RETVAL
  119. fi
  120. unset RETVAL
  121. echo "....UPX packaging output"
  122. goupx --best bin/linux/${EXE_BASENAME}-x86_64
  123. RETVAL=$?
  124. if [ $RETVAL != 0 ]; then
  125. echo ".....goupx failed, exiting"
  126. exit $RETVAL
  127. fi
  128. unset RETVAL
  129. fi
  130. }
  131. build_for_osx () {
  132. echo "Getting project dependencies (via go get) for OSX"
  133. GOOS=darwin go get -d -v -tags "$OSX_BUILD_TAGS" ./...
  134. prepare_build "$OSX_BUILD_TAGS"
  135. if [ $? != 0 ]; then
  136. echo "..'go get' failed, exiting"
  137. exit $?
  138. fi
  139. echo "Building darwin-x86_64..."
  140. echo "..Disabling CGO for this build"
  141. # TODO: is "CGO_ENABLED=0" required?
  142. CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "$OSX_BUILD_TAGS" -o 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"