make.bash 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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="PRIVATE_PLUGINS"
  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 "${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/,$/}/')
  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
  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. gox -verbose -ldflags "$LDFLAGS" -osarch windows/386 -tags "$WINDOWS_BUILD_TAGS" -output 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. gox -verbose -ldflags "$LDFLAGS" -osarch windows/amd64 -tags "$WINDOWS_BUILD_TAGS" -output 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
  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. CFLAGS=-m32 gox -verbose -ldflags "$LDFLAGS" -osarch linux/386 -tags "$LINUX_BUILD_TAGS" -output bin/linux/${EXE_BASENAME}-i686
  105. RETVAL=$?
  106. if [ $RETVAL != 0 ]; then
  107. echo ".....gox failed, exiting"
  108. exit $RETVAL
  109. fi
  110. unset RETVAL
  111. echo "....UPX packaging output"
  112. goupx --best bin/linux/${EXE_BASENAME}-i686
  113. RETVAL=$?
  114. if [ $RETVAL != 0 ]; then
  115. echo ".....goupx failed, exiting"
  116. exit $RETVAL
  117. fi
  118. unset RETVAL
  119. fi
  120. if [ -z $1 ] || [ "$1" == "64" ]; then
  121. echo "...Building linux-x86_64"
  122. gox -verbose -ldflags "$LDFLAGS" -osarch linux/amd64 -tags "$LINUX_BUILD_TAGS" -output bin/linux/${EXE_BASENAME}-x86_64
  123. RETVAL=$?
  124. if [ $RETVAL != 0 ]; then
  125. echo "....gox failed, exiting"
  126. exit $RETVAL
  127. fi
  128. unset RETVAL
  129. echo "....UPX packaging output"
  130. goupx --best bin/linux/${EXE_BASENAME}-x86_64
  131. RETVAL=$?
  132. if [ $RETVAL != 0 ]; then
  133. echo ".....goupx failed, exiting"
  134. exit $RETVAL
  135. fi
  136. unset RETVAL
  137. fi
  138. }
  139. build_for_osx () {
  140. echo "Getting project dependencies (via go get) for OSX"
  141. GOOS=darwin go get -d -v -tags "$OSX_BUILD_TAGS" ./...
  142. prepare_build
  143. if [ $? != 0 ]; then
  144. echo "..'go get' failed, exiting"
  145. exit $?
  146. fi
  147. echo "Building darwin-x86_64..."
  148. echo "..Disabling CGO for this build"
  149. CGO_ENABLED=0 gox -verbose -ldflags "$LDFLAGS" -osarch darwin/amd64 -tags "$OSX_BUILD_TAGS" -output bin/darwin/${EXE_BASENAME}-x86_64
  150. # Darwin binaries don't seem to be UPXable when built this way
  151. echo "..No UPX for this build"
  152. }
  153. TARGET=$1
  154. case $TARGET in
  155. windows)
  156. echo "..Building for Windows"
  157. build_for_windows $2
  158. exit $?
  159. ;;
  160. linux)
  161. echo "..Building for Linux"
  162. build_for_linux $2
  163. exit $?
  164. ;;
  165. osx)
  166. echo "..Building for OSX"
  167. build_for_osx
  168. exit $?
  169. ;;
  170. all)
  171. echo "..Building all"
  172. build_for_windows $2
  173. if [ $? != 0 ]; then
  174. exit $?
  175. fi
  176. build_for_linux $2
  177. if [ $? != 0 ]; then
  178. exit $?
  179. fi
  180. build_for_osx
  181. if [ $? != 0 ]; then
  182. exit $?
  183. fi
  184. ;;
  185. *)
  186. echo "..No selection made, building all"
  187. build_for_windows $2
  188. if [ $? != 0 ]; then
  189. exit $?
  190. fi
  191. build_for_linux $2
  192. if [ $? != 0 ]; then
  193. exit $?
  194. fi
  195. build_for_osx
  196. if [ $? != 0 ]; then
  197. exit $?
  198. fi
  199. ;;
  200. esac
  201. echo "Done"