make.bash 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=$BUILDDATE \
  26. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=$BUILDREPO \
  27. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=$BUILDREV \
  28. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=$GOVERSION \
  29. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=$DEPENDENCIES \
  30. "
  31. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > $BUILDINFOFILE
  32. echo "Variables for ldflags:"
  33. echo " Build date: ${BUILDDATE}"
  34. echo " Build repo: ${BUILDREPO}"
  35. echo " Build revision: ${BUILDREV}"
  36. echo " Go version: ${GOVERSION}"
  37. echo " Dependencies: ${DEPENDENCIES}"
  38. echo ""
  39. }
  40. if [ ! -d bin ]; then
  41. mkdir bin
  42. fi
  43. build_for_windows () {
  44. echo "...Getting project dependencies (via go get) for Windows"
  45. GOOS=windows go get -d -v -tags "${BUILD_TAGS}" ./...
  46. if [ $? != 0 ]; then
  47. echo "....'go get' failed, exiting"
  48. exit $?
  49. fi
  50. prepare_build windows
  51. echo "...Building windows-i686"
  52. CGO_LDFLAGS="-L /usr/i686-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  53. CC=/usr/bin/i686-w64-mingw32-gcc \
  54. GOOS=windows GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/windows/${EXE_BASENAME}-i686.exe
  55. RETVAL=$?
  56. echo ".....gox completed, exit code: $?"
  57. if [ $RETVAL != 0 ]; then
  58. echo ".....gox failed, exiting"
  59. exit $RETVAL
  60. fi
  61. unset RETVAL
  62. ## We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  63. echo "....No UPX for this build"
  64. echo "...Building windows-x86_64"
  65. CGO_LDFLAGS="-L /usr/x86_64-w64-mingw32/lib/ -lwsock32 -lcrypt32 -lgdi32" \
  66. CC=/usr/bin/x86_64-w64-mingw32-gcc \
  67. GOOS=windows GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/windows/${EXE_BASENAME}-x86_64.exe
  68. RETVAL=$?
  69. if [ $RETVAL != 0 ]; then
  70. echo ".....gox failed, exiting"
  71. exit $RETVAL
  72. fi
  73. unset RETVAL
  74. # We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  75. echo "....No UPX for this build"
  76. }
  77. build_for_linux () {
  78. echo "Getting project dependencies (via go get) for Linux"
  79. GOOS=linux go get -d -v -tags "${BUILD_TAGS}" ./...
  80. if [ $? != 0 ]; then
  81. echo "...'go get' failed, exiting"
  82. exit $?
  83. fi
  84. prepare_build linux
  85. echo "...Building linux-i686"
  86. # TODO: is "CFLAGS=-m32" required?
  87. CFLAGS=-m32 GOOS=linux GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/linux/${EXE_BASENAME}-i686
  88. RETVAL=$?
  89. if [ $RETVAL != 0 ]; then
  90. echo ".....gox failed, exiting"
  91. exit $RETVAL
  92. fi
  93. unset RETVAL
  94. echo "....UPX packaging output"
  95. goupx --best bin/linux/${EXE_BASENAME}-i686
  96. RETVAL=$?
  97. if [ $RETVAL != 0 ]; then
  98. echo ".....goupx failed, exiting"
  99. exit $RETVAL
  100. fi
  101. unset RETVAL
  102. echo "...Building linux-x86_64"
  103. GOOS=linux GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/linux/${EXE_BASENAME}-x86_64
  104. RETVAL=$?
  105. if [ $RETVAL != 0 ]; then
  106. echo "....gox failed, exiting"
  107. exit $RETVAL
  108. fi
  109. unset RETVAL
  110. echo "....UPX packaging output"
  111. goupx --best bin/linux/${EXE_BASENAME}-x86_64
  112. RETVAL=$?
  113. if [ $RETVAL != 0 ]; then
  114. echo ".....goupx failed, exiting"
  115. exit $RETVAL
  116. fi
  117. unset RETVAL
  118. }
  119. build_for_osx () {
  120. echo "Getting project dependencies (via go get) for OSX"
  121. GOOS=darwin go get -d -v -tags "${BUILD_TAGS}" ./...
  122. if [ $? != 0 ]; then
  123. echo "..'go get' failed, exiting"
  124. exit $?
  125. fi
  126. prepare_build darwin
  127. echo "Building darwin-x86_64..."
  128. echo "..Disabling CGO for this build"
  129. # TODO: is "CGO_ENABLED=0" required?
  130. CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "${BUILD_TAGS}" -o bin/darwin/${EXE_BASENAME}-x86_64
  131. # Darwin binaries don't seem to be UPXable when built this way
  132. echo "..No UPX for this build"
  133. }
  134. TARGET=$1
  135. case $TARGET in
  136. windows)
  137. echo "..Building for Windows"
  138. build_for_windows
  139. exit $?
  140. ;;
  141. linux)
  142. echo "..Building for Linux"
  143. build_for_linux
  144. exit $?
  145. ;;
  146. osx)
  147. echo "..Building for OSX"
  148. build_for_osx
  149. exit $?
  150. ;;
  151. all)
  152. echo "..Building all"
  153. build_for_windows
  154. if [ $? != 0 ]; then
  155. exit $?
  156. fi
  157. build_for_linux
  158. if [ $? != 0 ]; then
  159. exit $?
  160. fi
  161. build_for_osx
  162. if [ $? != 0 ]; then
  163. exit $?
  164. fi
  165. ;;
  166. *)
  167. echo "..invalid target"
  168. exit 1
  169. ;;
  170. esac
  171. echo "Done"