bazel-compile-commands
bazel-compile-commands copied to clipboard
Generate a `compile_commands.json` from a Bazel workspace
compile_commands.json generator for Bazel
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.
Usage of bazel-compile-commands
Inside a Bazel workspace run:
bazel-compile-commands
This will generate a compile_commands.json file in the current directory.
- Documentation or
man 1 bazel-compile-commands - Download latest release
VS Code integration
See the .vscode/tasks.json file as an example on how
to integratebazel-compile-commands into VS Code.
Build
Linux and macOS
bazel build --config=gnu //bcc:bazel-compile-commands
Windows
bazel build --config=cl //bcc:bazel-compile-commands
Alternative tools
- Bear - Can work when used with the
--spawn_strategy=localBazel flag. - hedronvision/bazel-compile-commands-extractor - Can be integrated into Bazel files.
- grailbio/bazel-compilation-database - Needs integration into your Bazel files
Links
Issues
- https://github.com/bazelbuild/bazel/issues/12852
License
This source code is under the MIT license with the exceptions mentioned in "Third party source code in this repository".