make.bash 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env bash
  2. set -e
  3. BASE_DIR=$( cd "$(dirname "$0")" ; pwd -P )
  4. cd $BASE_DIR
  5. if [ ! -f make.bash ]; then
  6. echo "make.bash must be run from $GOPATH/src/github.com/Psiphon-Labs/psiphon-tunnel-core/Server"
  7. exit 1
  8. fi
  9. PRIVATE_PLUGINS_TAG=""
  10. BUILD_TAGS="${PRIVATE_PLUGINS_TAG}"
  11. prepare_build () {
  12. BUILDINFOFILE="${EXE_BASENAME}_buildinfo.txt"
  13. BUILDDATE=$(date -Iseconds)
  14. BUILDREPO=$(git config --get remote.origin.url)
  15. BUILDREV=$(git rev-parse --short HEAD)
  16. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  17. # - starts the string with a `{`
  18. # - 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
  19. # - pipes to `xargs` to run a command on each line output from the first command
  20. # - 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
  21. # - 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)
  22. # - `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>",`
  23. # - 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
  24. 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/,$/}/')
  25. LDFLAGS="\
  26. -linkmode external -extldflags \"-static\" \
  27. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=$BUILDDATE \
  28. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=$BUILDREPO \
  29. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=$BUILDREV \
  30. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=$GOVERSION \
  31. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.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. build_for_linux () {
  43. echo "Getting project dependencies (via go get) for Linux. Parameter is: '$1'"
  44. GOOS=linux GOARCH=amd64 go get -d -v -tags "${BUILD_TAGS}" ./...
  45. prepare_build
  46. if [ $? != 0 ]; then
  47. echo "...'go get' failed, exiting"
  48. exit $?
  49. fi
  50. GOOS=linux GOARCH=amd64 go build -v -x -tags "${BUILD_TAGS}" -ldflags "$LDFLAGS" -o psiphond
  51. if [ $? != 0 ]; then
  52. echo "...'go build' failed, exiting"
  53. exit $?
  54. fi
  55. chmod 555 psiphond
  56. if [ "$1" == "generate" ]; then
  57. ./psiphond --ipaddress 0.0.0.0 --web 3000 --protocol SSH:3001 --protocol OSSH:3002 --logFilename /var/log/psiphond/psiphond.log generate
  58. chmod 666 psiphond.config
  59. chmod 666 psiphond-traffic-rules.config
  60. chmod 666 psiphond-osl.config
  61. chmod 666 psiphond-tactics.config
  62. chmod 666 server-entry.dat
  63. fi
  64. }
  65. build_for_linux generate
  66. echo "Done"