Skip to content

compile_commands.json

Fangrui Song edited this page Apr 1, 2018 · 13 revisions

Guillaume Papin(@Scarcasm) has a thorough article about compilation databases.

% mkdir build
% (cd build; cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=YES ..)
% ln -s build/compile_commands.json

Bear is a tool that generates a compilation database for clang tooling. It can be used for any project based on Makefile.

bear make
# generates compile_commands.json

scan-build is a python package that can generate a compilation database for clang tooling (uses Bear as a backend). This too can be used for any project based on a Makefile.

intercept-build make all # generates compile_commands.json from the `make all` ruleset
# Format: ninja -t compdb rule_names... > compile_commands.json
ninja -C out/Release -t compdb cxx cc > compile_commands.json

Load the clang_compilation_database tool in your wscript:

def configure(conf):
    conf.load('clang_compilation_database')
./waf configure build
ln -s build/compile_commands.json

stdout of an external command

You may use the initialization option "compilationDatabaseCommand" to provide the JSON compilation database. cquery will read its stdout rather than read compile_commands.json. This may be useful when cquery cannot parse the compile_commands.json correctly (e.g. MSVC cl.exe, Intel C++ Compiler options)

# extra command line option
'--init={"compilationDatabaseCommand":"mycompdb"}'
(setq cquery-extra-init-params '(:compilationDatabaseCommand "mycompdb"))

Suppose the project is at /tmp/c, mycompdb /tmp/c will be executed with stdin=initializationOptions and the stdout should be a JSON compilation database.

You may use this shell script as a starting point:

#!/bin/zsh
# mycompdb /tmp/c
cat >> /tmp/initialization-options
cat <<e
[ { "arguments": [ "c++", "a.o", "a.cc" ],
    "directory": "/tmp/c", "file": "a.cc" } ]
e

An example to scrub Intel C++ Compiler options:

#!/usr/bin/env python3
import json
import os
import sys
with open(os.path.join(sys.argv[1], 'compile_commands.json')) as f:
    db = json.load(f)
    for entry in db:
        args = entry['arguments']
        try:
            # Intel C++ Compiler option that is unknown to clang
            args.remove('-xHost')
        except ValueError:
            pass
    json.dump(db, sys.stdout)

Examples

Linux kernel

wget 'https://git.archlinux.org/svntogit/packages.git/plain/trunk/config?h=packages/linux' -O .config
yes '' | make config
bear make -j bzImage modules

Misc

compile_commands.json should reside in the project root because Emacs/VSCode plugins use it to indicate the project root, which will be used to initialize InitializeParams.rootUri.

Clone this wiki locally