A simple compilation database (compile_commands.json) generator for Bazel.
- Implemented with aspects.
- Works with generated sources.
- The generated
compile_commands.jsoncontains info for headers. - Supports C++ (not C) builds with rules_cc rules. Might work with custom C++ rules providing CcInfo, but see Limitations.
.bazelrc
common --registry=https://raw.githubusercontent.com/snailbaron/registry/main
common --registry=https://bcr.bazel.build
MODULE.bazel
bazel_dep(name = "snailbaron-compilation-database", version = "0.0.2")BUILD.bazel
load("@snailbaron-compilation-database//:compdb.bzl", "compilation_database")
compilation_database(
name = "compdb",
srcs = [
"//:target_a",
"//:target_b",
# ...
],
)In the terminal:
bazel build //:compdb
ln -s bazel-bin/compile_commands.json .
ln -s $(bazel info output_base)/external .- C is not supported.
-xc++is currently forcefully inserted for every file. - The aspect requires the CcInfo provider, propagates along the
depsattribute, and looks intosrcsandhdrsattributes to get the source files. This should work for the built-in/rules_cc rules, and for custom C++ rules if they are similar enough (also provide CcInfo and havedeps/srcs/hdrsattributes with similar semantics). - You must explicitly list all the targets you wish to generate the compilation database for. There is no way to generate it for
//.... - You cannot customize the name or location of the genrated file: it's always named
compile_commands.jsonand located in the directory corresponding to wherecompilation_databaseis defined. Also, if you work with multiple configurations, you might want to symlinkbazel-out/<your-configuration>instead ofbazel-bin. - Only tested on Linux right now, and not very rigorously.