build 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env bash
  2. # set some environment variables
  3. ORG_PATH="go.etcd.io"
  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/go.etcd.io"
  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/go.etcd.io/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="tools/benchmark
  62. tools/etcd-dump-db
  63. tools/etcd-dump-logs
  64. tools/local-tester/bridge
  65. functional/cmd/etcd-agent
  66. functional/cmd/etcd-proxy
  67. functional/cmd/etcd-runner
  68. functional/cmd/etcd-tester"
  69. for tool in ${tools_path}
  70. do
  71. echo "Building" "'${tool}'"...
  72. # shellcheck disable=SC2086
  73. CGO_ENABLED=0 go build ${GO_BUILD_FLAGS} \
  74. -installsuffix cgo \
  75. -ldflags "${GO_LDFLAGS}" \
  76. -o "${out}/${tool}" "${REPO_PATH}/${tool}" || return
  77. done
  78. }
  79. toggle_failpoints_default
  80. if [[ "${ETCD_SETUP_GOPATH}" == "1" ]]; then
  81. etcd_setup_gopath
  82. fi
  83. # only build when called directly, not sourced
  84. if echo "$0" | grep "build$" >/dev/null; then
  85. etcd_build
  86. fi