make.bash 5.9 KB

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