make.bash 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. BUILD_TAGS="PRIVATE_PLUGINS"
  11. WINDOWS_BUILD_TAGS="OPENSSL ${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 "${BUILD_TAGS}" -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
  51. if [ $? != 0 ]; then
  52. echo "....'go get' failed, exiting"
  53. exit $?
  54. fi
  55. if [ -z $1 ] || [ "$1" == "32" ]; then
  56. unset PKG_CONFIG_PATH
  57. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH_32
  58. echo "...Building windows-i686"
  59. echo "....PKG_CONFIG_PATH=$PKG_CONFIG_PATH"
  60. CGO_CFLAGS="-I $PKG_CONFIG_PATH/include/" \
  61. CGO_LDFLAGS="-L $PKG_CONFIG_PATH -L /usr/i686-w64-mingw32/lib/ -lssl -lcrypto -lwsock32 -lcrypt32 -lgdi32" \
  62. CC=/usr/bin/i686-w64-mingw32-gcc \
  63. gox -verbose -ldflags "$LDFLAGS" -osarch windows/386 -tags "$WINDOWS_BUILD_TAGS" -output bin/windows/${EXE_BASENAME}-i686
  64. RETVAL=$?
  65. echo ".....gox completed, exit code: $?"
  66. if [ $RETVAL != 0 ]; then
  67. echo ".....gox failed, exiting"
  68. exit $RETVAL
  69. fi
  70. unset RETVAL
  71. ## We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  72. echo "....No UPX for this build"
  73. fi
  74. if [ -z $1 ] || [ "$1" == "64" ]; then
  75. unset PKG_CONFIG_PATH
  76. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH_64
  77. echo "...Building windows-x86_64"
  78. echo "....PKG_CONFIG_PATH=$PKG_CONFIG_PATH"
  79. CGO_CFLAGS="-I $PKG_CONFIG_PATH/include/" \
  80. CGO_LDFLAGS="-L $PKG_CONFIG_PATH -L /usr/x86_64-w64-mingw32/lib/ -lssl -lcrypto -lwsock32 -lcrypt32 -lgdi32" \
  81. CC=/usr/bin/x86_64-w64-mingw32-gcc \
  82. gox -verbose -ldflags "$LDFLAGS" -osarch windows/amd64 -tags "$WINDOWS_BUILD_TAGS" -output bin/windows/${EXE_BASENAME}-x86_64
  83. RETVAL=$?
  84. if [ $RETVAL != 0 ]; then
  85. echo ".....gox failed, exiting"
  86. exit $RETVAL
  87. fi
  88. unset RETVAL
  89. # We are finding that UPXing the full Windows Psiphon client produces better results if psiphon-tunnel-core.exe is not already UPX'd.
  90. echo "....No UPX for this build"
  91. fi
  92. }
  93. build_for_linux () {
  94. echo "Getting project dependencies (via go get) for Linux. Parameter is: '$1'"
  95. GOOS=linux go get -d -v -tags "$LINUX_BUILD_TAGS" ./...
  96. prepare_build
  97. if [ $? != 0 ]; then
  98. echo "...'go get' failed, exiting"
  99. exit $?
  100. fi
  101. if [ -z $1 ] || [ "$1" == "32" ]; then
  102. echo "...Building linux-i686"
  103. CFLAGS=-m32 gox -verbose -ldflags "$LDFLAGS" -osarch linux/386 -tags "$LINUX_BUILD_TAGS" -output bin/linux/${EXE_BASENAME}-i686
  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}-i686
  112. RETVAL=$?
  113. if [ $RETVAL != 0 ]; then
  114. echo ".....goupx failed, exiting"
  115. exit $RETVAL
  116. fi
  117. unset RETVAL
  118. fi
  119. if [ -z $1 ] || [ "$1" == "64" ]; then
  120. echo "...Building linux-x86_64"
  121. gox -verbose -ldflags "$LDFLAGS" -osarch linux/amd64 -tags "$LINUX_BUILD_TAGS" -output bin/linux/${EXE_BASENAME}-x86_64
  122. RETVAL=$?
  123. if [ $RETVAL != 0 ]; then
  124. echo "....gox failed, exiting"
  125. exit $RETVAL
  126. fi
  127. unset RETVAL
  128. echo "....UPX packaging output"
  129. goupx --best bin/linux/${EXE_BASENAME}-x86_64
  130. RETVAL=$?
  131. if [ $RETVAL != 0 ]; then
  132. echo ".....goupx failed, exiting"
  133. exit $RETVAL
  134. fi
  135. unset RETVAL
  136. fi
  137. }
  138. build_for_osx () {
  139. echo "Getting project dependencies (via go get) for OSX"
  140. GOOS=darwin go get -d -v -tags "$OSX_BUILD_TAGS" ./...
  141. prepare_build
  142. if [ $? != 0 ]; then
  143. echo "..'go get' failed, exiting"
  144. exit $?
  145. fi
  146. echo "Building darwin-x86_64..."
  147. echo "..Disabling CGO for this build"
  148. CGO_ENABLED=0 gox -verbose -ldflags "$LDFLAGS" -osarch darwin/amd64 -tags "$OSX_BUILD_TAGS" -output bin/darwin/${EXE_BASENAME}-x86_64
  149. # Darwin binaries don't seem to be UPXable when built this way
  150. echo "..No UPX for this build"
  151. }
  152. TARGET=$1
  153. case $TARGET in
  154. windows)
  155. echo "..Building for Windows"
  156. build_for_windows $2
  157. exit $?
  158. ;;
  159. linux)
  160. echo "..Building for Linux"
  161. build_for_linux $2
  162. exit $?
  163. ;;
  164. osx)
  165. echo "..Building for OSX"
  166. build_for_osx
  167. exit $?
  168. ;;
  169. all)
  170. echo "..Building all"
  171. build_for_windows $2
  172. if [ $? != 0 ]; then
  173. exit $?
  174. fi
  175. build_for_linux $2
  176. if [ $? != 0 ]; then
  177. exit $?
  178. fi
  179. build_for_osx
  180. if [ $? != 0 ]; then
  181. exit $?
  182. fi
  183. ;;
  184. *)
  185. echo "..No selection made, building all"
  186. build_for_windows $2
  187. if [ $? != 0 ]; then
  188. exit $?
  189. fi
  190. build_for_linux $2
  191. if [ $? != 0 ]; then
  192. exit $?
  193. fi
  194. build_for_osx
  195. if [ $? != 0 ]; then
  196. exit $?
  197. fi
  198. ;;
  199. esac
  200. echo "Done"