make.bash 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. GOOS=windows GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "$WINDOWS_BUILD_TAGS" -o bin/windows/${EXE_BASENAME}-i686.exe
  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. echo "...Building windows-x86_64"
  70. GOOS=windows GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "$WINDOWS_BUILD_TAGS" -o bin/windows/${EXE_BASENAME}-x86_64.exe
  71. RETVAL=$?
  72. if [ $RETVAL != 0 ]; then
  73. echo ".....gox failed, exiting"
  74. exit $RETVAL
  75. fi
  76. unset RETVAL
  77. # We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  78. echo "....No UPX for this build"
  79. fi
  80. }
  81. build_for_linux () {
  82. echo "Getting project dependencies (via go get) for Linux. Parameter is: '$1'"
  83. GOOS=linux go get -d -v -tags "$LINUX_BUILD_TAGS" ./...
  84. prepare_build "$LINUX_BUILD_TAGS"
  85. if [ $? != 0 ]; then
  86. echo "...'go get' failed, exiting"
  87. exit $?
  88. fi
  89. if [ -z $1 ] || [ "$1" == "32" ]; then
  90. echo "...Building linux-i686"
  91. # TODO: is "CFLAGS=-m32" required?
  92. CFLAGS=-m32 GOOS=linux GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "$LINUX_BUILD_TAGS" -o bin/linux/${EXE_BASENAME}-i686
  93. RETVAL=$?
  94. if [ $RETVAL != 0 ]; then
  95. echo ".....gox failed, exiting"
  96. exit $RETVAL
  97. fi
  98. unset RETVAL
  99. echo "....UPX packaging output"
  100. goupx --best bin/linux/${EXE_BASENAME}-i686
  101. RETVAL=$?
  102. if [ $RETVAL != 0 ]; then
  103. echo ".....goupx failed, exiting"
  104. exit $RETVAL
  105. fi
  106. unset RETVAL
  107. fi
  108. if [ -z $1 ] || [ "$1" == "64" ]; then
  109. echo "...Building linux-x86_64"
  110. GOOS=linux GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "$LINUX_BUILD_TAGS" -o bin/linux/${EXE_BASENAME}-x86_64
  111. RETVAL=$?
  112. if [ $RETVAL != 0 ]; then
  113. echo "....gox failed, exiting"
  114. exit $RETVAL
  115. fi
  116. unset RETVAL
  117. echo "....UPX packaging output"
  118. goupx --best bin/linux/${EXE_BASENAME}-x86_64
  119. RETVAL=$?
  120. if [ $RETVAL != 0 ]; then
  121. echo ".....goupx failed, exiting"
  122. exit $RETVAL
  123. fi
  124. unset RETVAL
  125. fi
  126. }
  127. build_for_osx () {
  128. echo "Getting project dependencies (via go get) for OSX"
  129. GOOS=darwin go get -d -v -tags "$OSX_BUILD_TAGS" ./...
  130. prepare_build "$OSX_BUILD_TAGS"
  131. if [ $? != 0 ]; then
  132. echo "..'go get' failed, exiting"
  133. exit $?
  134. fi
  135. echo "Building darwin-x86_64..."
  136. echo "..Disabling CGO for this build"
  137. # TODO: is "CGO_ENABLED=0" required?
  138. CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "$OSX_BUILD_TAGS" -o bin/darwin/${EXE_BASENAME}-x86_64
  139. # Darwin binaries don't seem to be UPXable when built this way
  140. echo "..No UPX for this build"
  141. }
  142. TARGET=$1
  143. case $TARGET in
  144. windows)
  145. echo "..Building for Windows"
  146. build_for_windows $2
  147. exit $?
  148. ;;
  149. linux)
  150. echo "..Building for Linux"
  151. build_for_linux $2
  152. exit $?
  153. ;;
  154. osx)
  155. echo "..Building for OSX"
  156. build_for_osx
  157. exit $?
  158. ;;
  159. all)
  160. echo "..Building all"
  161. build_for_windows $2
  162. if [ $? != 0 ]; then
  163. exit $?
  164. fi
  165. build_for_linux $2
  166. if [ $? != 0 ]; then
  167. exit $?
  168. fi
  169. build_for_osx
  170. if [ $? != 0 ]; then
  171. exit $?
  172. fi
  173. ;;
  174. *)
  175. echo "..No selection made, building all"
  176. build_for_windows $2
  177. if [ $? != 0 ]; then
  178. exit $?
  179. fi
  180. build_for_linux $2
  181. if [ $? != 0 ]; then
  182. exit $?
  183. fi
  184. build_for_osx
  185. if [ $? != 0 ]; then
  186. exit $?
  187. fi
  188. ;;
  189. esac
  190. echo "Done"