make.bash 6.4 KB

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