build 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env bash
  2. # set some environment variables
  3. ORG_PATH="github.com/coreos"
  4. REPO_PATH="${ORG_PATH}/etcd"
  5. GIT_SHA=$(git rev-parse --short HEAD || echo "GitNotFound")
  6. if [[ -n "$FAILPOINTS" ]]; then
  7. GIT_SHA="$GIT_SHA"-FAILPOINTS
  8. fi
  9. # Set GO_LDFLAGS="-s" for building without symbols for debugging.
  10. GO_LDFLAGS="$GO_LDFLAGS -X ${REPO_PATH}/version.GitSHA=${GIT_SHA}"
  11. # enable/disable failpoints
  12. toggle_failpoints() {
  13. mode="$1"
  14. if command -v gofail >/dev/null 2>&1; then
  15. gofail "$mode" etcdserver/ mvcc/backend/
  16. elif [[ "$mode" != "disable" ]]; then
  17. echo "FAILPOINTS set but gofail not found"
  18. exit 1
  19. fi
  20. }
  21. etcd_setup_gopath() {
  22. echo "Setting GOPATH from vendor directory at 'gopath'"
  23. d=$(dirname "$0")
  24. CDIR=$(cd "$d" || return && pwd)
  25. cd "$CDIR" || return
  26. etcdGOPATH="${CDIR}/gopath"
  27. # preserve old gopath to support building with unvendored tooling deps (e.g., gofail)
  28. if [[ -n "$GOPATH" ]]; then
  29. GOPATH=":$GOPATH"
  30. fi
  31. rm -rf "${etcdGOPATH:?}/"
  32. mkdir -p "${etcdGOPATH}/vendor" "${etcdGOPATH}/etcd_src/src/github.com/coreos"
  33. export GOPATH=${etcdGOPATH}/vendor:${etcdGOPATH}/etcd_src${GOPATH}
  34. ln -s "${CDIR}/vendor" "${etcdGOPATH}/vendor/src"
  35. ln -s "${CDIR}" "${etcdGOPATH}/etcd_src/src/github.com/coreos/etcd"
  36. }
  37. toggle_failpoints_default() {
  38. mode="disable"
  39. if [[ -n "$FAILPOINTS" ]]; then mode="enable"; fi
  40. toggle_failpoints "$mode"
  41. }
  42. etcd_build() {
  43. out="bin"
  44. if [[ -n "${BINDIR}" ]]; then out="${BINDIR}"; fi
  45. toggle_failpoints_default
  46. # Static compilation is useful when etcd is run in a container. $GO_BUILD_FLAGS is OK
  47. # shellcheck disable=SC2086
  48. CGO_ENABLED=0 go build $GO_BUILD_FLAGS \
  49. -installsuffix cgo \
  50. -ldflags "$GO_LDFLAGS" \
  51. -o "${out}/etcd" ${REPO_PATH} || return
  52. # shellcheck disable=SC2086
  53. CGO_ENABLED=0 go build $GO_BUILD_FLAGS \
  54. -installsuffix cgo \
  55. -ldflags "$GO_LDFLAGS" \
  56. -o "${out}/etcdctl" ${REPO_PATH}/etcdctl || return
  57. }
  58. tools_build() {
  59. out="bin"
  60. if [[ -n "${BINDIR}" ]]; then out="${BINDIR}"; fi
  61. tools_path="functional/cmd/etcd-agent
  62. functional/cmd/etcd-proxy
  63. functional/cmd/etcd-runner
  64. functional/cmd/etcd-tester"
  65. for tool in ${tools_path}
  66. do
  67. echo "Building" "'${tool}'"...
  68. # shellcheck disable=SC2086
  69. CGO_ENABLED=0 go build ${GO_BUILD_FLAGS} \
  70. -installsuffix cgo \
  71. -ldflags "${GO_LDFLAGS}" \
  72. -o "${out}/${tool}" "${REPO_PATH}/${tool}" || return
  73. done
  74. }
  75. toggle_failpoints_default
  76. if [[ "${ETCD_SETUP_GOPATH}" == "1" ]]; then
  77. etcd_setup_gopath
  78. fi
  79. # only build when called directly, not sourced
  80. if echo "$0" | grep "build$" >/dev/null; then
  81. etcd_build
  82. fi