blob: c5852ebfb542bc84f7701e3d9a5c265ebf0ee833 [file] [log] [blame]
Sean Silva93ca0212012-12-13 01:10:46 +00001==============================================
2JSON Compilation Database Format Specification
3==============================================
4
5This document describes a format for specifying how to replay single
6compilations independently of the build system.
7
8Background
9==========
10
11Tools based on the C++ Abstract Syntax Tree need full information how to
12parse a translation unit. Usually this information is implicitly
13available in the build system, but running tools as part of the build
14system is not necessarily the best solution:
15
16- Build systems are inherently change driven, so running multiple tools
17 over the same code base without changing the code does not fit into
18 the architecture of many build systems.
19- Figuring out whether things have changed is often an IO bound
20 process; this makes it hard to build low latency end user tools based
21 on the build system.
22- Build systems are inherently sequential in the build graph, for
23 example due to generated source code. While tools that run
24 independently of the build still need the generated source code to
25 exist, running tools multiple times over unchanging source does not
26 require serialization of the runs according to the build dependency
27 graph.
28
29Supported Systems
30=================
31
32Currently `CMake <http://cmake.org>`_ (since 2.8.5) supports generation
33of compilation databases for Unix Makefile builds (Ninja builds in the
Dmitri Gribenko97555a12012-12-15 21:10:51 +000034works) with the option ``CMAKE_EXPORT_COMPILE_COMMANDS``.
Sean Silva93ca0212012-12-13 01:10:46 +000035
36Clang's tooling interface supports reading compilation databases; see
Sean Silva159cc9e2013-01-02 13:07:47 +000037the :doc:`LibTooling documentation <LibTooling>`. libclang and its
Sean Silva93ca0212012-12-13 01:10:46 +000038python bindings also support this (since clang 3.2); see
39`CXCompilationDatabase.h </doxygen/group__COMPILATIONDB.html>`_.
40
41Format
42======
43
44A compilation database is a JSON file, which consist of an array of
45"command objects", where each command object specifies one way a
46translation unit is compiled in the project.
47
48Each command object contains the translation unit's main file, the
49working directory of the compile run and the actual compile command.
50
51Example:
52
53::
54
55 [
56 { "directory": "/home/user/llvm/build",
57 "command": "/usr/bin/clang++ -Irelative -DSOMEDEF='\"With spaces and quotes.\"' -c -o file.o file.cc",
58 "file": "file.cc" },
59 ...
60 ]
61
62The contracts for each field in the command object are:
63
64- **directory:** The working directory of the compilation. All paths
65 specified in the **command** or **file** fields must be either
66 absolute or relative to this directory.
67- **file:** The main translation unit source processed by this
68 compilation step. This is used by tools as the key into the
69 compilation database. There can be multiple command objects for the
70 same file, for example if the same source file is compiled with
71 different configurations.
72- **command:** The compile command executed. After JSON unescaping,
73 this must be a valid command to rerun the exact compilation step for
74 the translation unit in the environment the build system uses.
Dmitri Gribenko97555a12012-12-15 21:10:51 +000075 Parameters use shell quoting and shell escaping of quotes, with '``"``'
76 and '``\``' being the only special characters. Shell expansion is not
Sean Silva93ca0212012-12-13 01:10:46 +000077 supported.
78
79Build System Integration
80========================
81
82The convention is to name the file compile\_commands.json and put it at
83the top of the build directory. Clang tools are pointed to the top of
84the build directory to detect the file and use the compilation database
85to parse C++ code in the source tree.