blob: b233d32720d56e61eb9cdd7632b5687b8de5bc9c [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001#!/bin/sh -e
2
3# set some environment variables
4ORG_PATH="github.com/coreos"
5REPO_PATH="${ORG_PATH}/etcd"
6
7GIT_SHA=$(git rev-parse --short HEAD || echo "GitNotFound")
8if [ ! -z "$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}/cmd/vendor/${REPO_PATH}/version.GitSHA=${GIT_SHA}"
14
15# enable/disable failpoints
16toggle_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
26toggle_failpoints_default() {
27 mode="disable"
28 if [ ! -z "$FAILPOINTS" ]; then mode="enable"; fi
29 toggle_failpoints "$mode"
30}
31
32etcd_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
44etcd_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
59toggle_failpoints_default
60
61# only build when called directly, not sourced
62if echo "$0" | grep "build$" >/dev/null; then
63 # force new gopath so builds outside of gopath work
64 etcd_setup_gopath
65 etcd_build
66fi