From 5a93b087a66fdeb61c8ba38a1c45fe5567315bbc Mon Sep 17 00:00:00 2001 From: Eduardo Patrocinio Date: Fri, 10 Jun 2022 11:19:05 -0400 Subject: [PATCH] Separate the build library functions and add a top level tools script The current Makefile doesn't allow the compilation of the tools directory. This commit creates a build library file, updates the Makefile and a top level script fod building tools. To build the tools, you can run make build_tools. As before, you can run make build to build etcd binaries. --- Makefile | 2 +- scripts/build.sh | 129 ++--------------------------------------- scripts/build_lib.sh | 120 ++++++++++++++++++++++++++++++++++++++ scripts/build_tools.sh | 6 ++ scripts/test.sh | 2 +- 5 files changed, 133 insertions(+), 126 deletions(-) create mode 100755 scripts/build_lib.sh create mode 100755 scripts/build_tools.sh diff --git a/Makefile b/Makefile index f2ba07712..d1f7865ed 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ build: ./bin/etcdutl version build_tools: - GO_BUILD_FLAGS="-v" ./scripts/build.sh tools_build + GO_BUILD_FLAGS="-v" ./scripts/build_tools.sh clean: rm -f ./codecov diff --git a/scripts/build.sh b/scripts/build.sh index e97c084f0..4a588e4de 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,131 +1,12 @@ #!/usr/bin/env bash +# This scripts build the etcd binaries +# To build the tools, run `build_tools.sh` + 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" - -# 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=0" "GO_BUILD_FLAGS=${GO_BUILD_FLAGS}" "GOOS=${GOOS}" "GOARCH=${GOARCH}") - -# enable/disable failpoints -toggle_failpoints() { - mode="$1" - if command -v gofail >/dev/null 2>&1; then - run gofail "$mode" server/etcdserver/ server/storage/backend/ - 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 \ - -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 \ - -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 \ - -installsuffix=cgo \ - "-ldflags=${GO_LDFLAGS[*]}" \ - -o="../${out}/etcdctl" . || return 2 - ) || return 2 - # Verify whether symbol we overwrote 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=0 go build ${GO_BUILD_FLAGS} \ - -installsuffix=cgo \ - "-ldflags='${GO_LDFLAGS[*]}'" \ - -o="${out}/${tool}" "./${tool}" || return 2 - done - tests_build "${@}" -} - -tests_build() { - out=${BINDIR:-./bin} - out=$(readlink -f "$out") - out="${out}/functional/cmd" - mkdir -p "${out}" - BINDIR="${out}" run ./tests/functional/build.sh || return 2 -} - -run_build() { - echo Running "$1" - if $1; then - log_success "SUCCESS: $1 (GOARCH=${GOARCH})" - else - log_error "FAIL: $1 (GOARCH=${GOARCH})" - exit 2 - fi - -} - -toggle_failpoints_default +source ./scripts/build_lib.sh # only build when called directly, not sourced if echo "$0" | grep -E "build(.sh)?$" >/dev/null; then - # if an argument is passed, run it; otherwise, run etcd_build - if [ -z "$1" ]; then - run_build "etcd_build" - else - run_build "$1" - fi + run_build etcd_build fi diff --git a/scripts/build_lib.sh b/scripts/build_lib.sh new file mode 100755 index 000000000..638269548 --- /dev/null +++ b/scripts/build_lib.sh @@ -0,0 +1,120 @@ +#!/usr/bin/env bash + +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" + +# 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=0" "GO_BUILD_FLAGS=${GO_BUILD_FLAGS}" "GOOS=${GOOS}" "GOARCH=${GOARCH}") + +# enable/disable failpoints +toggle_failpoints() { + mode="$1" + if command -v gofail >/dev/null 2>&1; then + run gofail "$mode" server/etcdserver/ server/storage/backend/ + 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 \ + -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 \ + -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 \ + -installsuffix=cgo \ + "-ldflags=${GO_LDFLAGS[*]}" \ + -o="../${out}/etcdctl" . || return 2 + ) || return 2 + # Verify whether symbol we overwrote 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=0 go build ${GO_BUILD_FLAGS} \ + -installsuffix=cgo \ + "-ldflags='${GO_LDFLAGS[*]}'" \ + -o="${out}/${tool}" "./${tool}" || return 2 + done + tests_build "${@}" +} + +tests_build() { + out=${BINDIR:-./bin} + out=$(readlink -f "$out") + out="${out}/functional/cmd" + mkdir -p "${out}" + BINDIR="${out}" run ./tests/functional/build.sh || return 2 +} + +run_build() { + echo Running "$1" + if $1; then + log_success "SUCCESS: $1 (GOARCH=${GOARCH})" + else + log_error "FAIL: $1 (GOARCH=${GOARCH})" + exit 2 + fi +} + +toggle_failpoints_default diff --git a/scripts/build_tools.sh b/scripts/build_tools.sh new file mode 100755 index 000000000..48fa9d6fa --- /dev/null +++ b/scripts/build_tools.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +source ./scripts/test_lib.sh +source ./scripts/build_lib.sh + +run_build tools_build diff --git a/scripts/test.sh b/scripts/test.sh index bb70192f9..4523e32df 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -44,7 +44,7 @@ export GOFLAGS=-mod=readonly export ETCD_VERIFY=all source ./scripts/test_lib.sh -source ./scripts/build.sh +source ./scripts/build_lib.sh if [ -n "${OUTPUT_FILE}" ]; then log_callout "Dumping output to: ${OUTPUT_FILE}"