Skip to content

Commit 3a600a1

Browse files
authored
Merge pull request #30213 from travisdowns/td-noblock-tools
tools: run clang-format/clang-tidy in a separate bazel output_base
2 parents a1dc3d3 + 41393c6 commit 3a600a1

5 files changed

Lines changed: 80 additions & 3 deletions

File tree

tools/bazel-tools/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.cache/

tools/bazel-tools/support/bazel-tool-trampoline.sh

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,61 @@ debug "running $BAZEL_TOOL_TRAMPOLINE_TARGET"
5252
debug "bazel invoke cwd: $(pwd)"
5353
debug "target cwd : $BAZEL_TOOL_TRAMPOLINE_CWD"
5454

55+
# If BAZEL_TRAMPOLINE_PARALLEL=1 use a separate output_base so this tool can run
56+
# in parallel with the main bazel server without contending for the same lock.
57+
# Best for tools that need to run frequently and don't need a ton of files in
58+
# their output base. disk_cache will in general obviate the need to actually
59+
# rebuild anything.
60+
#
61+
# Determining the default output_base is tricky: `bazel info output_base` itself
62+
# takes the main server's lock, so if the main server is busy the query would
63+
# block (defeating the point). Strategy (keyed on cache existence):
64+
# - No cache yet: no choice but to block — call `bazel info output_base` and
65+
# seed the cache with the result.
66+
# - Cache exists: try `bazel --noblock_for_lock info output_base` to
67+
# opportunistically refresh the cache; on lock-held (exit 9) just use the
68+
# cached value.
69+
if [[ ${BAZEL_TRAMPOLINE_PARALLEL:-0} -gt 0 ]]; then
70+
cache_dir="$support_script_dir/../.cache"
71+
cache_file="$cache_dir/output_base"
72+
mkdir -p "$cache_dir"
73+
74+
if [[ ! -f $cache_file ]]; then
75+
debug "output_base from: blocking bazel query (no cache yet)"
76+
default_output_base="$(bazel info output_base)"
77+
echo "$default_output_base" >"$cache_file"
78+
else
79+
# Bazel exits 9 (LOCK_HELD_NOBLOCK_FOR_LOCK) when --noblock_for_lock
80+
# cannot acquire the server lock. Any other non-zero exit is a real
81+
# error and should surface rather than silently falling back to cache.
82+
rc=0
83+
fresh=$(bazel --noblock_for_lock info output_base 2>/dev/null) || rc=$?
84+
if ((rc == 0)); then
85+
debug "output_base from: live bazel query (cache refreshed)"
86+
default_output_base=$fresh
87+
echo "$default_output_base" >"$cache_file"
88+
elif ((rc == 9)); then
89+
default_output_base="$(cat "$cache_file")"
90+
debug "output_base from: cache ($cache_file, lock held)"
91+
else
92+
echo "ERROR: bazel-tool-trampoline.sh: bazel info output_base failed (exit $rc)" >&2
93+
exit "$rc"
94+
fi
95+
fi
96+
97+
tool_output_base="${default_output_base}_rptool"
98+
output_base_flag=(--output_base="$tool_output_base")
99+
100+
debug "tool output_base : $tool_output_base"
101+
else
102+
output_base_flag=()
103+
debug "tool output_base : (default)"
104+
fi
105+
55106
# These flags are to hide a bunch of Bazel's built-in output.
56107

57-
exec bazel run "$BAZEL_TOOL_TRAMPOLINE_TARGET" \
108+
exec bazel "${output_base_flag[@]}" \
109+
run "$BAZEL_TOOL_TRAMPOLINE_TARGET" \
58110
--run_under=//tools/bazel-tools/support:run \
59111
--noshow_progress \
60112
--ui_event_filters=,+error,+fail \

tools/clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
clang-tool.sh
1+
clang-tool-parallel.sh

tools/clang-tool-parallel.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2026 Redpanda Data, Inc.
3+
#
4+
# Use of this software is governed by the Business Source License
5+
# included in the file licenses/BSL.md
6+
#
7+
# As of the Change Date specified in that file, in accordance with
8+
# the Business Source License, use of this software will be governed
9+
# by the Apache License, Version 2.0
10+
11+
# Variant of clang-tool.sh that runs the tool against a separate bazel
12+
# output_base so it can execute in parallel with the main bazel server.
13+
# Only suitable for tools that don't depend on bazel-built artifacts
14+
# (e.g. clang-tidy plugins) living in the default output_base.
15+
16+
export BAZEL_TRAMPOLINE_PARALLEL="${BAZEL_TRAMPOLINE_PARALLEL:-1}"
17+
18+
tools_dir="$(cd -- "$(dirname -- "$(realpath "${BASH_SOURCE[0]}")")" &>/dev/null && pwd)"
19+
20+
# `exec -a` doesn't propagate through a shebang into bash's $0, so forward
21+
# the invocation name via an env var that clang-tool.sh consults.
22+
export CLANG_TOOL_NAME="$(basename "$0")"
23+
exec "$tools_dir/clang-tool.sh" "$@"

tools/clang-tool.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
tools_dir="$(cd -- "$(dirname -- "$(realpath "${BASH_SOURCE[0]}")")" &>/dev/null && pwd)"
88

9-
export BAZEL_TOOL_TRAMPOLINE_TARGET="@current_llvm_toolchain_llvm//:bin/$(basename "$0")"
9+
tool_name="${CLANG_TOOL_NAME:-$(basename "$0")}"
10+
export BAZEL_TOOL_TRAMPOLINE_TARGET="@current_llvm_toolchain_llvm//:bin/$tool_name"
1011

1112
if [[ ${BAZEL_TRAMPOLINE_DEBUG:-0} -gt 0 ]]; then
1213
echo "DEBUG: clang-tool.sh: CWD : $(pwd)" >&2

0 commit comments

Comments
 (0)