All Projects → grailbio → Bazel Compilation Database

grailbio / Bazel Compilation Database

Licence: apache-2.0
Tool to generate compile_commands.json from the Bazel build system

Projects that are alternatives of or similar to Bazel Compilation Database

Bazel Toolchain
LLVM toolchain for bazel
Stars: ✭ 93 (-60.59%)
Mutual labels:  bazel, clang
bazel-compile-commands-extractor
Goal: Enable awesome tooling for Bazel users of the C language family.
Stars: ✭ 295 (+25%)
Mutual labels:  bazel, clang
Starlark Rust
Starlark (https://github.com/bazelbuild/starlark) in Rust
Stars: ✭ 190 (-19.49%)
Mutual labels:  bazel
Sol2
Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
Stars: ✭ 2,791 (+1082.63%)
Mutual labels:  clang
Rules protobuf
Bazel rules for building protocol buffers and gRPC services (java, c++, go, ...)
Stars: ✭ 206 (-12.71%)
Mutual labels:  bazel
Xchammer
XCHammer generates Xcode projects from a Bazel Workspace.
Stars: ✭ 195 (-17.37%)
Mutual labels:  bazel
Rules apple
Bazel rules to build apps for Apple platforms.
Stars: ✭ 217 (-8.05%)
Mutual labels:  bazel
Simdjson
Parsing gigabytes of JSON per second
Stars: ✭ 15,115 (+6304.66%)
Mutual labels:  clang
Rules python
Experimental Bazel Python Rules
Stars: ✭ 233 (-1.27%)
Mutual labels:  bazel
Cmake Scripts
A selection of useful scripts for use in CMake projects, include code coverage, sanitizers, and dependency graph generation.
Stars: ✭ 202 (-14.41%)
Mutual labels:  clang
Scan Build
Clang's scan-build re-implementation in python
Stars: ✭ 224 (-5.08%)
Mutual labels:  clang
Trunk
Make bazel an out of box solution for C++/Java developers
Stars: ✭ 203 (-13.98%)
Mutual labels:  bazel
Rules haskell
Haskell rules for Bazel.
Stars: ✭ 196 (-16.95%)
Mutual labels:  bazel
Punes
Nintendo Entertaiment System emulator and NSF/NSFe Music Player (Linux, FreeBSD, OpenBSD and Windows)
Stars: ✭ 217 (-8.05%)
Mutual labels:  clang
Podtobuild
An easy way to integrate CocoaPods into Bazel
Stars: ✭ 193 (-18.22%)
Mutual labels:  bazel
Clang Tutor
A collection of out-of-tree Clang plugins for teaching and learning
Stars: ✭ 223 (-5.51%)
Mutual labels:  clang
Dpp
Directly include C headers in D source code
Stars: ✭ 189 (-19.92%)
Mutual labels:  clang
Purec
C backend for PureScript
Stars: ✭ 202 (-14.41%)
Mutual labels:  clang
Directx Vs Templates
Direct3D Visual Studio Templates
Stars: ✭ 205 (-13.14%)
Mutual labels:  clang
Rules kotlin
Bazel rules for Kotlin
Stars: ✭ 235 (-0.42%)
Mutual labels:  bazel

Compilation database with Bazel Tests Migration

If you use Bazel and want to use libclang based editors and tools, you can now generate JSON compilation database easily without using build intercept hooks. The advantage is that you can generate the database even if your source code does not compile, and the generation process is much faster.

For more information on compilation database, Guillaume Papin has an excellent article.

How to Use

Entire repo

Running generate.sh script from this project with current directory somewhere in your bazel workspace will generate a compile_commands.json file in the top-level directory of your workspace. You can even symlink the script to somewhere in your PATH.

For example,

INSTALL_DIR="/usr/local/bin"
VERSION="0.4.5"

# Download and symlink.
(
  cd "${INSTALL_DIR}" \
  && curl -L "https://github.com/grailbio/bazel-compilation-database/archive/${VERSION}.tar.gz" | tar -xz \
  && ln -f -s "${INSTALL_DIR}/bazel-compilation-database-${VERSION}/generate.sh" bazel-compdb
)

bazel-compdb # This will generate compile_commands.json in your workspace root.

# You can tweak some behavior with flags:
# 1. To use the source dir instead of bazel-execroot for directory in which clang commands are run.
bazel-compdb -s

Selected targets

You can define a target of rule type compilation_database with the attribute targets as a list of top-level cc_.* labels which you want to include in your compilation database. You do not need to include targets that are dependencies of your top-level targets. So these will mostly be targets of type cc_binary and cc_test.

For example,

In your WORKSPACE file:

# Change master to the git tag you want.
http_archive(
    name = "com_grail_bazel_compdb",
    strip_prefix = "bazel-compilation-database-master",
    urls = ["https://github.com/grailbio/bazel-compilation-database/archive/master.tar.gz"],
)

In your BUILD file located in any package:

## Replace workspace_name and dir_path as per your setup.
load("@com_grail_bazel_compdb//:aspects.bzl", "compilation_database")

compilation_database(
    name = "example_compdb",
    targets = [
        "//a_cc_binary_label",
        "//a_cc_library_label",
    ],
    # [Optional]
    # If your exec root (value returned by `bazel info execution_root`)
    # is constant across your users, then you can supply the value here.
    # Otherwise, the default is `__EXEC_ROOT__` which you can replace in
    # the output file using `sed` or similar tool (see below).
    exec_root = "/path/to/bazel/exec_root",
)

Then, in your terminal (you can wrap this in a shell script and check it in your repo):

# Command to generate the compilation database file.
bazel build //path/to/pkg/dir:example_compdb

# Location of the compilation database file.
outfile="$(bazel info bazel-bin)/path/to/pkg/dir/compile_commands.json"

# Command to replace the marker for exec_root in the file.
execroot=$(bazel info execution_root)
sed -i.bak "[email protected][email protected]${execroot}@" "${outfile}"

# The compilation database is now ready to use at this location.
echo "Compilation Database: ${outfile}"

YouCompleteMe

If you want to use this project solely for semantic auto completion using ycmd (YouCompleteMe) based editor plugins, then the easiest approach is to install this project as a vim plugin with your favourite plugin manager. The plugin will set g:ycm_global_ycm_extra_conf and instrument bazel with the correct paths. e.g. Using Plugged add the following to your vimrc.

Plug 'grailbio/bazel-compilation-database'

An alternative approach is to follow the instructions as above for making the files available in this repo somewhere in the workspace, and then configure vim to use the .ycm_extra_conf.py script that you just extracted. One way is to make a symlink to the py script from the top of your workspace root. Another way is to set the ycm_global_ycm_extra_conf variable in vim.

With both of these approaches, you don't have to maintain a separate compile_commands.json file through a script and/or a compilation_database target. Compile commands are fetched from bazel as the files are opened in your editor.

Contributing

Contributions are most welcome. Please submit a pull request giving the owners of this github repo access to your branch for minor style related edits, etc.

Known Issues

Please check open issues at the github repo.

We have tested only for C and C++ code, and with tools like YouCompleteMe, rtags, and the woboq code browser.

Alternatives

  1. Kythe: uses Bazel action listeners
  2. Bear: uses build intercept hooks

These approaches could be more accurate than the approach of this tool in some rare cases, but need a more complicated setup and a full build every time you refresh the database.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].