This repository provides a plugin for Bazel to create a Clang tooling
compatible JSON compilation database
(compile_commands.json
).
YouCompleteMe can use the same file for semantic completion.
Note that in order to use the compile_commands.json
generated by this plugin with Clang tooling,
you need to set up Bazel to use Clang for compiling C and C++ in the first place
(export CC=clang++
seems to work for me).
I am using this for my own C++ projects and thought it might be useful to others. Use at your own risk. Only tested on Linux with Bazel 0.5.1.
Import the repository into your WORKSPACE
:
http_archive(
name = "bazel_compilation_database",
url = "https://github.com/curiousleo/bazel_compilation_database/archive/9ccdbe1252050a7ddbd146dd425f4fb48251f378.zip",
sha256 = "f1e0d4ed4590af2ff84f258acdcddae7aa1047e5531ec704fc271b0dc2b45898",
strip_prefix = "bazel_compilation_database-9ccdbe1252050a7ddbd146dd425f4fb48251f378",
)
Enable the extra_action
that captures the compilation commands in your project's .bazelrc
:
build --experimental_action_listener=@bazel_compilation_database//:capture
# Optional: restrict usage of the listener by regex
# build --experimental_extra_action_filter=^//src(/|:)
This handy script will then collect the compilation commands into compile_commands.json
:
#!/usr/bin/env bash
ACTION_DIRECTORY="$(bazel info output_path)/local-fastbuild/extra_actions/external/bazel_compilation_database/capture_action"
EXEC_DIRECTORY="$(bazel info execution_root)"
COMMAND_DB_FILE="$(bazel info workspace)/compile_commands.json"
bazel run @bazel_compilation_database//:collect -- "$ACTION_DIRECTORY" "$EXEC_DIRECTORY" "$COMMAND_DB_FILE"
If you saved this snippet in update_compilation_database.sh
and made it executable, then running
it after bazel build
will update the compile_commands.json
file in the root of your project,
like so:
bazel build <your target> && ./update_compilation_database.sh
You may need to bazel clean
your project before this works.