-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
build.sh
executable file
·163 lines (144 loc) · 4.73 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
set -euo pipefail
source ./scripts/test_lib.sh
GIT_SHA=$(git rev-parse --short HEAD || echo "GitNotFound")
if [[ -n "${FAILPOINTS:-}" ]]; then
GIT_SHA="$GIT_SHA"-FAILPOINTS
fi
VERSION_SYMBOL="${ROOT_MODULE}/api/v3/version.GitSHA"
# use go env if noset
GOOS=${GOOS:-$(go env GOOS)}
GOARCH=${GOARCH:-$(go env GOARCH)}
CGO_ENABLED="${CGO_ENABLED:-0}"
# Set GO_LDFLAGS="-s" for building without symbols for debugging.
# shellcheck disable=SC2206
GO_LDFLAGS=(${GO_LDFLAGS:-} "-X=${VERSION_SYMBOL}=${GIT_SHA}")
GO_BUILD_ENV=("CGO_ENABLED=${CGO_ENABLED}" "GO_BUILD_FLAGS=${GO_BUILD_FLAGS:-}" "GOOS=${GOOS}" "GOARCH=${GOARCH}")
GOFAIL_VERSION=$(cd tools/mod && go list -m -f '{{.Version}}' go.etcd.io/gofail)
# enable/disable failpoints
toggle_failpoints() {
mode="$1"
if command -v gofail >/dev/null 2>&1; then
run gofail "$mode" server/etcdserver/ server/lease/leasehttp server/mvcc/ server/wal/ server/mvcc/backend/
if [[ "$mode" == "enable" ]]; then
go get go.etcd.io/gofail@"${GOFAIL_VERSION}"
cd ./server && go get go.etcd.io/gofail@"${GOFAIL_VERSION}"
cd ../etcdutl && go get go.etcd.io/gofail@"${GOFAIL_VERSION}"
cd ../etcdctl && go get go.etcd.io/gofail@"${GOFAIL_VERSION}"
cd ../tests && go get go.etcd.io/gofail@"${GOFAIL_VERSION}"
cd ../
else
go mod tidy
cd ./server && go mod tidy
cd ../etcdutl && go mod tidy
cd ../etcdctl && go mod tidy
cd ../tests && go mod tidy
cd ../
fi
elif [[ "$mode" != "disable" ]]; then
log_error "FAILPOINTS set but gofail not found"
exit 1
fi
}
toggle_failpoints_default() {
mode="disable"
if [[ -n "${FAILPOINTS:-}" ]]; then mode="enable"; fi
toggle_failpoints "$mode"
}
etcd_build() {
out="bin"
if [[ -n "${BINDIR:-}" ]]; then out="${BINDIR}"; fi
toggle_failpoints_default
run rm -f "${out}/etcd"
(
cd ./server
# Static compilation is useful when etcd is run in a container. $GO_BUILD_FLAGS is OK
# shellcheck disable=SC2086
run env "${GO_BUILD_ENV[@]}" go build ${GO_BUILD_FLAGS:-} \
-trimpath \
-installsuffix=cgo \
"-ldflags=${GO_LDFLAGS[*]}" \
-o="../${out}/etcd" . || return 2
) || return 2
run rm -f "${out}/etcdutl"
# shellcheck disable=SC2086
(
cd ./etcdutl
run env GO_BUILD_FLAGS="${GO_BUILD_FLAGS:-}" "${GO_BUILD_ENV[@]}" go build ${GO_BUILD_FLAGS:-} \
-trimpath \
-installsuffix=cgo \
"-ldflags=${GO_LDFLAGS[*]}" \
-o="../${out}/etcdutl" . || return 2
) || return 2
run rm -f "${out}/etcdctl"
# shellcheck disable=SC2086
(
cd ./etcdctl
run env GO_BUILD_FLAGS="${GO_BUILD_FLAGS:-}" "${GO_BUILD_ENV[@]}" go build ${GO_BUILD_FLAGS:-} \
-trimpath \
-installsuffix=cgo \
"-ldflags=${GO_LDFLAGS[*]}" \
-o="../${out}/etcdctl" . || return 2
) || return 2
# Verify whether symbol we overriden exists
# For cross-compiling we cannot run: ${out}/etcd --version | grep -q "Git SHA: ${GIT_SHA}"
# We need symbols to do this check:
if [[ "${GO_LDFLAGS[*]}" != *"-s"* ]]; then
go tool nm "${out}/etcd" | grep "${VERSION_SYMBOL}" > /dev/null
if [[ "${PIPESTATUS[*]}" != "0 0" ]]; then
log_error "FAIL: Symbol ${VERSION_SYMBOL} not found in binary: ${out}/etcd"
return 2
fi
fi
}
tools_build() {
out="bin"
if [[ -n "${BINDIR:-}" ]]; then out="${BINDIR}"; fi
tools_path="tools/benchmark
tools/etcd-dump-db
tools/etcd-dump-logs
tools/local-tester/bridge"
for tool in ${tools_path}
do
echo "Building" "'${tool}'"...
run rm -f "${out}/${tool}"
# shellcheck disable=SC2086
run env GO_BUILD_FLAGS="${GO_BUILD_FLAGS:-}" CGO_ENABLED=${CGO_ENABLED} go build ${GO_BUILD_FLAGS:-} \
-trimpath \
-installsuffix=cgo \
"-ldflags=${GO_LDFLAGS[*]}" \
-o="${out}/${tool}" "./${tool}" || return 2
done
tests_build "${@}"
}
tests_build() {
out="bin"
if [[ -n "${BINDIR:-}" ]]; then out="${BINDIR}"; fi
tools_path="
functional/cmd/etcd-agent
functional/cmd/etcd-proxy
functional/cmd/etcd-runner
functional/cmd/etcd-tester"
(
cd tests || exit 2
for tool in ${tools_path}; do
echo "Building" "'${tool}'"...
run rm -f "../${out}/${tool}"
# shellcheck disable=SC2086
run env CGO_ENABLED=${CGO_ENABLED} GO_BUILD_FLAGS="${GO_BUILD_FLAGS:-}" go build ${GO_BUILD_FLAGS:-} \
-installsuffix=cgo \
"-ldflags=${GO_LDFLAGS[*]}" \
-o="../${out}/${tool}" "./${tool}" || return 2
done
) || return 2
}
toggle_failpoints_default
# only build when called directly, not sourced
if echo "$0" | grep -E "build(.sh)?$" >/dev/null; then
if etcd_build; then
log_success "SUCCESS: etcd_build (GOARCH=${GOARCH})"
else
log_error "FAIL: etcd_build (GOARCH=${GOARCH})"
exit 2
fi
fi