This repository contains two tools to generate a compile_commands.json file
from a Bazel workspace without the need to modify any of the Bazel configuration
files. This is the equivalent of CMAKE_EXPORT_COMPILE_COMMANDS from CMake for
Bazel.
The
Microsoft C/C++ Extension for Visual Studio Code
struggles sometimes with Bazel workspaces since not all include paths are at the
default locations. The compile_commands.json file can be used with clangd to
have a significant better LSP experience. For
Visual Studio Code (VS Code), the
clangd extension
can be used to utilize the project compile information via the
compile_commands.json file.
Invoke bazel-compile-commands //... to generate a compile_commands.json file
for the
Bazel label
//.... For more information, see
man 1 bazel-compile-commands.
The bazel-compile-commands command is ideal when you need a
compile_commands.json file instantly and don't require automatic updates of
the compile_commands.json file. For example in CI use cases when you when
static analysis tools use a compile_commands.json file as input.
Inside a Bazel workspace run:
bazel-compile-commandsThis will generate a compile_commands.json file in the current directory.
- Documentation or
man 1 bazel-compile-commands - Download latest release
See the .vscode/tasks.json file as an example on how
to integratebazel-compile-commands into VS Code.
- Bartek Kryza mentions
bazel-compile-commandsin the blog post "compile_commands.json gallery", Dec. 2, 2004. - Blog post in System/5: BazelCon 2024 recap, section IDE support, bullet point "Compilation database", Oct. 22, 2024.
- BazelCon 2024: The State of Compilation Database in Bazel (Video)
bazel build --config=gnu //bcc:bazel-compile-commandsbazel build --config=cl //bcc:bazel-compile-commands- Bear - Can work when used with the
--spawn_strategy=localBazel flag. - hedronvision/bazel-compile-commands-extractor - Can be integrated into Bazel files.
This source code is under the MIT license with the exceptions mentioned in "Third party source code in this repository".