make.bash 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. # The "OPENSSL" tag enables support of OpenSSL for use by IndistinguishableTLS.
  9. # This needs to be outside of prepare_build because it's used by go-get.
  10. PRIVATE_PLUGINS_TAG=""
  11. BUILD_TAGS="${PRIVATE_PLUGINS_TAG}"
  12. WINDOWS_BUILD_TAGS="OPENSSL ${BUILD_TAGS}"
  13. LINUX_BUILD_TAGS="${BUILD_TAGS}"
  14. OSX_BUILD_TAGS="${BUILD_TAGS}"
  15. prepare_build () {
  16. BUILDINFOFILE="${EXE_BASENAME}_buildinfo.txt"
  17. BUILDDATE=$(date --iso-8601=seconds)
  18. BUILDREPO=$(git config --get remote.origin.url)
  19. BUILDREV=$(git rev-parse --short HEAD)
  20. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  21. # - starts the string with a `{`
  22. # - 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
  23. # - pipes to `xargs` to run a command on each line output from the first command
  24. # - 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
  25. # - 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)
  26. # - `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>",`
  27. # - 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
  28. 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/,$/}/')
  29. LDFLAGS="\
  30. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=$BUILDDATE \
  31. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=$BUILDREPO \
  32. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=$BUILDREV \
  33. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=$GOVERSION \
  34. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=$DEPENDENCIES \
  35. "
  36. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > $BUILDINFOFILE
  37. echo "Variables for ldflags:"
  38. echo " Build date: ${BUILDDATE}"
  39. echo " Build repo: ${BUILDREPO}"
  40. echo " Build revision: ${BUILDREV}"
  41. echo " Go version: ${GOVERSION}"
  42. echo " Dependencies: ${DEPENDENCIES}"
  43. echo ""
  44. }
  45. if [ ! -d bin ]; then
  46. mkdir bin
  47. fi
  48. build_for_windows () {
  49. echo "...Getting project dependencies (via go get) for Windows. Parameter is: '$1'"
  50. GOOS=windows go get -d -v -tags "$WINDOWS_BUILD_TAGS" ./...
  51. prepare_build $WINDOWS_BUILD_TAGS
  52. if [ $? != 0 ]; then
  53. echo "....'go get' failed, exiting"
  54. exit $?
  55. fi
  56. if [ -z $1 ] || [ "$1" == "32" ]; then
  57. unset PKG_CONFIG_PATH
  58. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH_32
  59. echo "...Building windows-i686"
  60. echo "....PKG_CONFIG_PATH=$PKG_CONFIG_PATH"
  61. CGO_CFLAGS="-I $PKG_CONFIG_PATH/include/" \
  62. CGO_LDFLAGS="-L $PKG_CONFIG_PATH -L /usr/i686-w64-mingw32/lib/ -lssl -lcrypto -lwsock32 -lcrypt32 -lgdi32" \
  63. CC=/usr/bin/i686-w64-mingw32-gcc \
  64. GOOS=windows GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "$WINDOWS_BUILD_TAGS" -o bin/windows/${EXE_BASENAME}-i686
  65. RETVAL=$?
  66. echo ".....gox completed, exit code: $?"
  67. if [ $RETVAL != 0 ]; then
  68. echo ".....gox failed, exiting"
  69. exit $RETVAL
  70. fi
  71. unset RETVAL
  72. ## We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  73. echo "....No UPX for this build"
  74. fi
  75. if [ -z $1 ] || [ "$1" == "64" ]; then
  76. unset PKG_CONFIG_PATH
  77. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH_64
  78. echo "...Building windows-x86_64"
  79. echo "....PKG_CONFIG_PATH=$PKG_CONFIG_PATH"
  80. CGO_CFLAGS="-I $PKG_CONFIG_PATH/include/" \
  81. CGO_LDFLAGS="-L $PKG_CONFIG_PATH -L /usr/x86_64-w64-mingw32/lib/ -lssl -lcrypto -lwsock32 -lcrypt32 -lgdi32" \
  82. CC=/usr/bin/x86_64-w64-mingw32-gcc \
  83. GOOS=windows GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "$WINDOWS_BUILD_TAGS" -o bin/windows/${EXE_BASENAME}-x86_64
  84. RETVAL=$?
  85. if [ $RETVAL != 0 ]; then
  86. echo ".....gox failed, exiting"
  87. exit $RETVAL
  88. fi
  89. unset RETVAL
  90. # We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  91. echo "....No UPX for this build"
  92. fi
  93. }
  94. build_for_linux () {
  95. echo "Getting project dependencies (via go get) for Linux. Parameter is: '$1'"
  96. GOOS=linux go get -d -v -tags "$LINUX_BUILD_TAGS" ./...
  97. prepare_build $LINUX_BUILD_TAGS
  98. if [ $? != 0 ]; then
  99. echo "...'go get' failed, exiting"
  100. exit $?
  101. fi
  102. if [ -z $1 ] || [ "$1" == "32" ]; then
  103. echo "...Building linux-i686"
  104. # TODO: is "CFLAGS=-m32" required?
  105. CFLAGS=-m32 GOOS=linux GOARCH=386 go build -v -x -ldflags "$LDFLAGS" -tags "$LINUX_BUILD_TAGS" -o bin/linux/${EXE_BASENAME}-i686
  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}-i686
  114. RETVAL=$?
  115. if [ $RETVAL != 0 ]; then
  116. echo ".....goupx failed, exiting"
  117. exit $RETVAL
  118. fi
  119. unset RETVAL
  120. fi
  121. if [ -z $1 ] || [ "$1" == "64" ]; then
  122. echo "...Building linux-x86_64"
  123. GOOS=linux GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "$LINUX_BUILD_TAGS" -o bin/linux/${EXE_BASENAME}-x86_64
  124. RETVAL=$?
  125. if [ $RETVAL != 0 ]; then
  126. echo "....gox failed, exiting"
  127. exit $RETVAL
  128. fi
  129. unset RETVAL
  130. echo "....UPX packaging output"
  131. goupx --best bin/linux/${EXE_BASENAME}-x86_64
  132. RETVAL=$?
  133. if [ $RETVAL != 0 ]; then
  134. echo ".....goupx failed, exiting"
  135. exit $RETVAL
  136. fi
  137. unset RETVAL
  138. fi
  139. }
  140. build_for_osx () {
  141. echo "Getting project dependencies (via go get) for OSX"
  142. GOOS=darwin go get -d -v -tags "$OSX_BUILD_TAGS" ./...
  143. prepare_build $OSX_BUILD_TAGS
  144. if [ $? != 0 ]; then
  145. echo "..'go get' failed, exiting"
  146. exit $?
  147. fi
  148. echo "Building darwin-x86_64..."
  149. echo "..Disabling CGO for this build"
  150. # TODO: is "CGO_ENABLED=0" required?
  151. CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -v -x -ldflags "$LDFLAGS" -tags "$OSX_BUILD_TAGS" -o bin/darwin/${EXE_BASENAME}-x86_64
  152. # Darwin binaries don't seem to be UPXable when built this way
  153. echo "..No UPX for this build"
  154. }
  155. TARGET=$1
  156. case $TARGET in
  157. windows)
  158. echo "..Building for Windows"
  159. build_for_windows $2
  160. exit $?
  161. ;;
  162. linux)
  163. echo "..Building for Linux"
  164. build_for_linux $2
  165. exit $?
  166. ;;
  167. osx)
  168. echo "..Building for OSX"
  169. build_for_osx
  170. exit $?
  171. ;;
  172. all)
  173. echo "..Building all"
  174. build_for_windows $2
  175. if [ $? != 0 ]; then
  176. exit $?
  177. fi
  178. build_for_linux $2
  179. if [ $? != 0 ]; then
  180. exit $?
  181. fi
  182. build_for_osx
  183. if [ $? != 0 ]; then
  184. exit $?
  185. fi
  186. ;;
  187. *)
  188. echo "..No selection made, building all"
  189. build_for_windows $2
  190. if [ $? != 0 ]; then
  191. exit $?
  192. fi
  193. build_for_linux $2
  194. if [ $? != 0 ]; then
  195. exit $?
  196. fi
  197. build_for_osx
  198. if [ $? != 0 ]; then
  199. exit $?
  200. fi
  201. ;;
  202. esac
  203. echo "Done"