make.bash 3.3 KB

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