bazel-compile-commands
v0.4.0
published 1 year ago
8 stars
1 forks
4 watchers
MIT License
public
0 assets
Maintained byDanny Wolf
v0.4.0
April 12, 2024
[expand for release notes]

bazel-compile-commands

Usage

See releases for install information.

If you're using the VS Code clangd plugin, you may want to experiment with this option in .vscode/settings.json:

  "clangd.arguments": [
    // Allow clangd to execute your compiler to find system headers. I've seen strange errors when this is enabled, so it might be worth trying without it.
    "--query-driver=/**"
  ],

To run, use bazel run @wolfd_bazel_compile_commands//:generate_compile_commands -- <optional query pattern>

Consider adding an alias to your root BUILD.bazel:

alias(
    name = "compile_commands",
    actual = "@wolfd_bazel_compile_commands//:generate_compile_commands",
)

And then all you need to do is run bazel run compile_commands

You may find in large repos that //... is too much, and you need to run it on a subset of your repo. If you don't provide a specific query we default to //... except attr(tags, '\bmanual\b', //...) which will avoid looking at targets that are tagged as manual. See the bazel aquery docs for more information on how to use aquery. The query is wrapped with deps(), but filtered down to CppCompile and Middleman actions.

This tool is not particularly configurable, feel free to fork it and modify it to your needs.

Details

This tool uses bazel aquery to get the actual command lines that will be used to compile each file. The major complication is that we want to give clangd context for how header files are used in the project. The way we solve this problem is by assocating every header with the command line of a source file (.cc/.cpp) that includes it. We trace through the whole dependency tree, for headers included in sources, we associate them directly, for headers included in other headers, we trace back up the tree until we find a source file.

We use pre-compiled protobuf outputs. To regenerate them, use ./regenerate_protos.sh (run from the root of the repo). This requires nix to be installed.

Known issues

  • Headers stored in tree artifacts are not expanded, so there won't be an entry in compile_commands.json for them.
  • cc_librarys using strip_include_prefix should work (if they've been built recently), but clicking through will open the generated _virtual_includes folder, not the original source (if it exists).

Related projects