1bf9b4cd5SSean Silva==============================================
2bf9b4cd5SSean SilvaJSON Compilation Database Format Specification
3bf9b4cd5SSean Silva==============================================
4bf9b4cd5SSean Silva
5bf9b4cd5SSean SilvaThis document describes a format for specifying how to replay single
6bf9b4cd5SSean Silvacompilations independently of the build system.
7bf9b4cd5SSean Silva
8bf9b4cd5SSean SilvaBackground
9bf9b4cd5SSean Silva==========
10bf9b4cd5SSean Silva
11bf9b4cd5SSean SilvaTools based on the C++ Abstract Syntax Tree need full information how to
12bf9b4cd5SSean Silvaparse a translation unit. Usually this information is implicitly
13bf9b4cd5SSean Silvaavailable in the build system, but running tools as part of the build
14bf9b4cd5SSean Silvasystem is not necessarily the best solution:
15bf9b4cd5SSean Silva
16bf9b4cd5SSean Silva-  Build systems are inherently change driven, so running multiple tools
17bf9b4cd5SSean Silva   over the same code base without changing the code does not fit into
18bf9b4cd5SSean Silva   the architecture of many build systems.
19bf9b4cd5SSean Silva-  Figuring out whether things have changed is often an IO bound
20bf9b4cd5SSean Silva   process; this makes it hard to build low latency end user tools based
21bf9b4cd5SSean Silva   on the build system.
22bf9b4cd5SSean Silva-  Build systems are inherently sequential in the build graph, for
23bf9b4cd5SSean Silva   example due to generated source code. While tools that run
24bf9b4cd5SSean Silva   independently of the build still need the generated source code to
25bf9b4cd5SSean Silva   exist, running tools multiple times over unchanging source does not
26bf9b4cd5SSean Silva   require serialization of the runs according to the build dependency
27bf9b4cd5SSean Silva   graph.
28bf9b4cd5SSean Silva
29bf9b4cd5SSean SilvaSupported Systems
30bf9b4cd5SSean Silva=================
31bf9b4cd5SSean Silva
32*b8467952SDaveClang has the ablity to generate compilation database fragments via
33*b8467952SDavethe :option:`-MJ argument <clang -MJ\<arg>>`. You can concatenate those
34*b8467952SDavefragments together between ``[`` and ``]`` to create a compilation database.
35*b8467952SDave
36adcb3f52SEugene ZelenkoCurrently `CMake <https://cmake.org>`_ (since 2.8.5) supports generation
37bf9b4cd5SSean Silvaof compilation databases for Unix Makefile builds (Ninja builds in the
387ac0cc36SDmitri Gribenkoworks) with the option ``CMAKE_EXPORT_COMPILE_COMMANDS``.
39bf9b4cd5SSean Silva
40b46dc57fSDmitri GribenkoFor projects on Linux, there is an alternative to intercept compiler
41b46dc57fSDmitri Gribenkocalls with a tool called `Bear <https://github.com/rizsotto/Bear>`_.
42b46dc57fSDmitri Gribenko
435233ad17SNathan Ridge`Bazel <https://bazel.build>`_ can export a compilation database via
445233ad17SNathan Ridge`this extractor extension
455233ad17SNathan Ridge<https://github.com/hedronvision/bazel-compile-commands-extractor>`_.
465233ad17SNathan RidgeBazel is otherwise resistant to Bear and other compiler-intercept
475233ad17SNathan Ridgetechniques.
485233ad17SNathan Ridge
49bf9b4cd5SSean SilvaClang's tooling interface supports reading compilation databases; see
50173d2526SSean Silvathe :doc:`LibTooling documentation <LibTooling>`. libclang and its
51bf9b4cd5SSean Silvapython bindings also support this (since clang 3.2); see
52bf9b4cd5SSean Silva`CXCompilationDatabase.h </doxygen/group__COMPILATIONDB.html>`_.
53bf9b4cd5SSean Silva
54bf9b4cd5SSean SilvaFormat
55bf9b4cd5SSean Silva======
56bf9b4cd5SSean Silva
57bf9b4cd5SSean SilvaA compilation database is a JSON file, which consist of an array of
58bf9b4cd5SSean Silva"command objects", where each command object specifies one way a
59bf9b4cd5SSean Silvatranslation unit is compiled in the project.
60bf9b4cd5SSean Silva
61bf9b4cd5SSean SilvaEach command object contains the translation unit's main file, the
62bf9b4cd5SSean Silvaworking directory of the compile run and the actual compile command.
63bf9b4cd5SSean Silva
64bf9b4cd5SSean SilvaExample:
65bf9b4cd5SSean Silva
66bf9b4cd5SSean Silva::
67bf9b4cd5SSean Silva
68bf9b4cd5SSean Silva    [
69bf9b4cd5SSean Silva      { "directory": "/home/user/llvm/build",
7016949762SSam McCall        "arguments": ["/usr/bin/clang++", "-Irelative", "-DSOMEDEF=With spaces, quotes and \\-es.", "-c", "-o", "file.o", "file.cc"],
71bf9b4cd5SSean Silva        "file": "file.cc" },
7216949762SSam McCall
7316949762SSam McCall      { "directory": "/home/user/llvm/build",
7416949762SSam McCall        "command": "/usr/bin/clang++ -Irelative -DSOMEDEF=\"With spaces, quotes and \\-es.\" -c -o file.o file.cc",
7516949762SSam McCall        "file": "file2.cc" },
7616949762SSam McCall
77bf9b4cd5SSean Silva      ...
78bf9b4cd5SSean Silva    ]
79bf9b4cd5SSean Silva
80bf9b4cd5SSean SilvaThe contracts for each field in the command object are:
81bf9b4cd5SSean Silva
82bf9b4cd5SSean Silva-  **directory:** The working directory of the compilation. All paths
83bf9b4cd5SSean Silva   specified in the **command** or **file** fields must be either
84bf9b4cd5SSean Silva   absolute or relative to this directory.
85bf9b4cd5SSean Silva-  **file:** The main translation unit source processed by this
86bf9b4cd5SSean Silva   compilation step. This is used by tools as the key into the
87bf9b4cd5SSean Silva   compilation database. There can be multiple command objects for the
88bf9b4cd5SSean Silva   same file, for example if the same source file is compiled with
89bf9b4cd5SSean Silva   different configurations.
9016949762SSam McCall-  **arguments:** The compile command argv as list of strings.
9116949762SSam McCall   This should run the compilation step for the translation unit ``file``.
9216949762SSam McCall   ``arguments[0]`` should be the executable name, such as ``clang++``.
9316949762SSam McCall   Arguments should not be escaped, but ready to pass to ``execvp()``.
9416949762SSam McCall-  **command:** The compile command as a single shell-escaped string.
9516949762SSam McCall   Arguments may be shell quoted and escaped following platform conventions,
9616949762SSam McCall   with '``"``' and '``\``' being the only special characters. Shell expansion
9716949762SSam McCall   is not supported.
9816949762SSam McCall
9916949762SSam McCall   Either **arguments** or **command** is required. **arguments** is preferred,
10016949762SSam McCall   as shell (un)escaping is a possible source of errors.
101399aea30SJoerg Sonnenberger-  **output:** The name of the output created by this compilation step.
102399aea30SJoerg Sonnenberger   This field is optional. It can be used to distinguish different processing
103399aea30SJoerg Sonnenberger   modes of the same input file.
104bf9b4cd5SSean Silva
105bf9b4cd5SSean SilvaBuild System Integration
106bf9b4cd5SSean Silva========================
107bf9b4cd5SSean Silva
108bf9b4cd5SSean SilvaThe convention is to name the file compile\_commands.json and put it at
109bf9b4cd5SSean Silvathe top of the build directory. Clang tools are pointed to the top of
110bf9b4cd5SSean Silvathe build directory to detect the file and use the compilation database
111bf9b4cd5SSean Silvato parse C++ code in the source tree.
11260d74e45SSam McCall
11360d74e45SSam McCallAlternatives
11460d74e45SSam McCall============
115095f0865SSam McCallFor simple projects, Clang tools also recognize a ``compile_flags.txt`` file.
116095f0865SSam McCallThis should contain one argument per line. The same flags will be used to
117095f0865SSam McCallcompile any file.
118095f0865SSam McCall
119095f0865SSam McCallExample:
120095f0865SSam McCall
121095f0865SSam McCall::
122095f0865SSam McCall
123095f0865SSam McCall    -xc++
124095f0865SSam McCall    -I
125095f0865SSam McCall    libwidget/include/
126095f0865SSam McCall
127095f0865SSam McCallHere ``-I libwidget/include`` is two arguments, and so becomes two lines.
128095f0865SSam McCallPaths are relative to the directory containing ``compile_flags.txt``.
129095f0865SSam McCall
130