Skip to content

Commit 5eb9f4a

Browse files
committed
Provide a way to create a compilation DB in the bazel project
This uses http://bant.build/ to create a compilation DB. The change provides two useful scripts: * `etc/bazel-make-compilation-db.sh` creates a compilation db. To avoid a huge compile_commands.json, this generates the compile_flags.txt which is a much shorter file applied to all files. This keeps memory usage low for tools such as clang-tidy * `etc/run-clang-tidy.sh` a convenience script that creates the compilation DB and then calls `run-clang-tidy-cached.cc`. It uses `clang-tidy` from the llvm-toolchain configured in the MODULE.bazel Issues #8586 Signed-off-by: Henner Zeller <[email protected]>
1 parent cf96e6a commit 5eb9f4a

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

MODULE.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ bazel_dep(name = "tcmalloc", version = "0.0.0-20250331-43fcf6e")
7575
bazel_dep(name = "zlib", version = "1.3.1.bcr.5")
7676
bazel_dep(name = "yaml-cpp", version = "0.8.0")
7777

78+
# Make compilation DB (and possibly build cleaner)
79+
bazel_dep(name = "bant", version = "0.2.4", dev_dependency = True)
80+
7881
# Use HEAD of master upstream to fix bugs such as missing stdint include
7982
git_override(
8083
module_name = "yaml-cpp",

MODULE.bazel.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

etc/bazel-make-compilation-db.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
5+
# Which bazel and bant to use. If unset, defaults are used.
6+
BAZEL=${BAZEL:-bazel}
7+
BANT="$($(dirname "$0")/get-bant-path.sh)"
8+
9+
# Important to run with --remote_download_outputs=all to make sure generated
10+
# files are actually visible locally in case a remote cache (that includes
11+
# --disk_cache) is used ( https://github.com/hzeller/bazel-gen-file-issue )
12+
BAZEL_OPTS="-c opt --remote_download_outputs=all"
13+
14+
# Tickle some build targets to fetch all dependencies and generate files,
15+
# so that they can be seen by the users of the compilation db.
16+
# Right now, comile everything (which should not be too taxing with the
17+
# bazel cache), but it could be made more specific to only trigger specific
18+
# targets that are sufficient to get and regenerate everything.
19+
"${BAZEL}" build -k ${BAZEL_OPTS} src/...
20+
21+
# Create compilation DB. Command 'compilation-db' would create a huge *.json file,
22+
# but compile_flags.txt is perfectly sufficient and easier for tools to use as
23+
# these tools will require much less memory.
24+
25+
"${BANT}" compile-flags -o compile_flags.txt
26+
27+
# The QT headers are not properly picked up; add them manually.
28+
for f in bazel-out/../../../external/qt-bazel*/qt_source/qtbase*/build/include/Q* ; do echo "-I$f" ; done >> compile_flags.txt
29+
30+
# If there are two styles of comp-dbs, tools might have issues. Warn user.
31+
if [ -r compile_commands.json ]; then
32+
printf "\n\033[1;31mSuggest to remove old compile_commands.json to not interfere with compile_flags.txt\033[0m\n\n"
33+
fi

etc/get-bant-path.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
5+
# Print path to a bant binary. Can be provided by an environment variable
6+
# or built from our dependency.
7+
8+
BANT=${BANT:-needs-to-be-compiled-locally}
9+
10+
# Bant not given, compile from bzlmod dep.
11+
if [ "${BANT}" = "needs-to-be-compiled-locally" ]; then
12+
BAZEL=${BAZEL:-bazelisk}
13+
if ! command -v "${BAZEL}">/dev/null ; then
14+
BAZEL=bazel
15+
fi
16+
# run_under will print the final path.
17+
BANT="$("${BAZEL}" run -c opt --run_under='echo' @bant//bant:bant 2>/dev/null)"
18+
fi
19+
20+
echo "$BANT"

etc/run-clang-tidy.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# Invocation without parameters simply uses the .clang-tidy config to run on
4+
# all *.{cc,h} files. Additional parameters passed to this script are passed
5+
# to clang-tidy as-is, e.g. if you only want to run a particular check and
6+
# the auto-fix
7+
# run-clang-tidy.sh --checks="-*,modernize-loop-convert" --fix
8+
9+
set -eu
10+
11+
BAZEL=${BAZEL:-bazelisk}
12+
if ! command -v "${BAZEL}">/dev/null ; then
13+
BAZEL=bazel
14+
fi
15+
16+
# Use either CLANG_TIDY provided by the user as environment variable or use
17+
# our own from the toolchain we configured in the MODULE.bazel
18+
export CLANG_TIDY="${CLANG_TIDY:-$("${BAZEL}" run -c opt --run_under='echo' @llvm_toolchain//:clang-tidy 2>/dev/null)}"
19+
20+
# The user should keep the compilation DB fresh, but refresh here
21+
# if substantial things changed or it is not there in the first place.
22+
if [ MODULE.bazel -nt compile_flags.txt -o BUILD.bazel -nt compile_flags.txt ] ; then
23+
"$(dirname "$0")/bazel-make-compilation-db.sh"
24+
fi
25+
26+
"$(dirname "$0")/run-clang-tidy-cached.cc" "$@"

0 commit comments

Comments
 (0)