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