make.bash 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. prepare_build () {
  10. BUILDINFOFILE="${EXE_BASENAME}_buildinfo.txt"
  11. BUILDDATE=$(date --iso-8601=seconds)
  12. BUILDREPO=$(git config --get remote.origin.url)
  13. BUILDREV=$(git rev-parse --short HEAD)
  14. GOVERSION=$(go version | perl -ne '/go version (.*?) / && print $1')
  15. # - starts the string with a `{`
  16. # - 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
  17. # - pipes to `xargs` to run a command on each line output from the first command
  18. # - 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
  19. # - 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)
  20. # - `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>",`
  21. # - 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
  22. DEPENDENCIES=$(echo -n "{" && go list -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/,$/}/')
  23. LDFLAGS="\
  24. -linkmode external -extldflags \"-static\" \
  25. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildDate=$BUILDDATE \
  26. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRepo=$BUILDREPO \
  27. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.buildRev=$BUILDREV \
  28. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.goVersion=$GOVERSION \
  29. -X github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common.dependencies=$DEPENDENCIES \
  30. "
  31. echo -e "${BUILDDATE}\n${BUILDREPO}\n${BUILDREV}\n" > $BUILDINFOFILE
  32. echo "Variables for ldflags:"
  33. echo " Build date: ${BUILDDATE}"
  34. echo " Build repo: ${BUILDREPO}"
  35. echo " Build revision: ${BUILDREV}"
  36. echo " Go version: ${GOVERSION}"
  37. echo " Dependencies: ${DEPENDENCIES}"
  38. echo ""
  39. }
  40. build_for_linux () {
  41. echo "Getting project dependencies (via go get) for Linux. Parameter is: '$1'"
  42. GOOS=linux GOARCH=amd64 go get -d -v ./...
  43. prepare_build
  44. if [ $? != 0 ]; then
  45. echo "...'go get' failed, exiting"
  46. exit $?
  47. fi
  48. GOOS=linux GOARCH=amd64 go build -ldflags "$LDFLAGS" -o psiphond main.go
  49. if [ $? != 0 ]; then
  50. echo "...'go build' failed, exiting"
  51. exit $?
  52. fi
  53. chmod 555 psiphond
  54. if [ "$1" == "generate" ]; then
  55. ./psiphond --ipaddress 0.0.0.0 --web 3000 --protocol SSH:3001 --protocol OSSH:3002 --logFilename /var/log/psiphond/psiphond.log generate
  56. chmod 666 psiphond.config
  57. chmod 666 psiphond-traffic-rules.config
  58. chmod 666 server-entry.dat
  59. fi
  60. }
  61. build_for_linux generate
  62. echo "Done"