khenaidoo | ffe076b | 2019-01-15 16:08:08 -0500 | [diff] [blame^] | 1 | #!/bin/sh -e |
| 2 | |
| 3 | # set some environment variables |
| 4 | ORG_PATH="github.com/coreos" |
| 5 | REPO_PATH="${ORG_PATH}/etcd" |
| 6 | |
| 7 | GIT_SHA=$(git rev-parse --short HEAD || echo "GitNotFound") |
| 8 | if [ ! -z "$FAILPOINTS" ]; then |
| 9 | GIT_SHA="$GIT_SHA"-FAILPOINTS |
| 10 | fi |
| 11 | |
| 12 | # Set GO_LDFLAGS="-s" for building without symbols for debugging. |
| 13 | GO_LDFLAGS="$GO_LDFLAGS -X ${REPO_PATH}/cmd/vendor/${REPO_PATH}/version.GitSHA=${GIT_SHA}" |
| 14 | |
| 15 | # enable/disable failpoints |
| 16 | toggle_failpoints() { |
| 17 | mode="$1" |
| 18 | if which 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 | |
| 26 | toggle_failpoints_default() { |
| 27 | mode="disable" |
| 28 | if [ ! -z "$FAILPOINTS" ]; then mode="enable"; fi |
| 29 | toggle_failpoints "$mode" |
| 30 | } |
| 31 | |
| 32 | etcd_build() { |
| 33 | out="bin" |
| 34 | if [ -n "${BINDIR}" ]; then out="${BINDIR}"; fi |
| 35 | toggle_failpoints_default |
| 36 | # Static compilation is useful when etcd is run in a container. $GO_BUILD_FLAGS is OK |
| 37 | |
| 38 | # shellcheck disable=SC2086 |
| 39 | CGO_ENABLED=0 go build $GO_BUILD_FLAGS -installsuffix cgo -ldflags "$GO_LDFLAGS" -o "${out}/etcd" ${REPO_PATH}/cmd/etcd || return |
| 40 | # shellcheck disable=SC2086 |
| 41 | CGO_ENABLED=0 go build $GO_BUILD_FLAGS -installsuffix cgo -ldflags "$GO_LDFLAGS" -o "${out}/etcdctl" ${REPO_PATH}/cmd/etcdctl || return |
| 42 | } |
| 43 | |
| 44 | etcd_setup_gopath() { |
| 45 | d=$(dirname "$0") |
| 46 | CDIR=$(cd "$d" && pwd) |
| 47 | cd "$CDIR" |
| 48 | etcdGOPATH="${CDIR}/gopath" |
| 49 | # preserve old gopath to support building with unvendored tooling deps (e.g., gofail) |
| 50 | if [ -n "$GOPATH" ]; then |
| 51 | GOPATH=":$GOPATH" |
| 52 | fi |
| 53 | export GOPATH=${etcdGOPATH}$GOPATH |
| 54 | rm -rf "${etcdGOPATH}/src" |
| 55 | mkdir -p "${etcdGOPATH}" |
| 56 | ln -s "${CDIR}/cmd/vendor" "${etcdGOPATH}/src" |
| 57 | } |
| 58 | |
| 59 | toggle_failpoints_default |
| 60 | |
| 61 | # only build when called directly, not sourced |
| 62 | if echo "$0" | grep "build$" >/dev/null; then |
| 63 | # force new gopath so builds outside of gopath work |
| 64 | etcd_setup_gopath |
| 65 | etcd_build |
| 66 | fi |