1*f4a2713aSLionel Sambuc============================================== 2*f4a2713aSLionel SambucJSON Compilation Database Format Specification 3*f4a2713aSLionel Sambuc============================================== 4*f4a2713aSLionel Sambuc 5*f4a2713aSLionel SambucThis document describes a format for specifying how to replay single 6*f4a2713aSLionel Sambuccompilations independently of the build system. 7*f4a2713aSLionel Sambuc 8*f4a2713aSLionel SambucBackground 9*f4a2713aSLionel Sambuc========== 10*f4a2713aSLionel Sambuc 11*f4a2713aSLionel SambucTools based on the C++ Abstract Syntax Tree need full information how to 12*f4a2713aSLionel Sambucparse a translation unit. Usually this information is implicitly 13*f4a2713aSLionel Sambucavailable in the build system, but running tools as part of the build 14*f4a2713aSLionel Sambucsystem is not necessarily the best solution: 15*f4a2713aSLionel Sambuc 16*f4a2713aSLionel Sambuc- Build systems are inherently change driven, so running multiple tools 17*f4a2713aSLionel Sambuc over the same code base without changing the code does not fit into 18*f4a2713aSLionel Sambuc the architecture of many build systems. 19*f4a2713aSLionel Sambuc- Figuring out whether things have changed is often an IO bound 20*f4a2713aSLionel Sambuc process; this makes it hard to build low latency end user tools based 21*f4a2713aSLionel Sambuc on the build system. 22*f4a2713aSLionel Sambuc- Build systems are inherently sequential in the build graph, for 23*f4a2713aSLionel Sambuc example due to generated source code. While tools that run 24*f4a2713aSLionel Sambuc independently of the build still need the generated source code to 25*f4a2713aSLionel Sambuc exist, running tools multiple times over unchanging source does not 26*f4a2713aSLionel Sambuc require serialization of the runs according to the build dependency 27*f4a2713aSLionel Sambuc graph. 28*f4a2713aSLionel Sambuc 29*f4a2713aSLionel SambucSupported Systems 30*f4a2713aSLionel Sambuc================= 31*f4a2713aSLionel Sambuc 32*f4a2713aSLionel SambucCurrently `CMake <http://cmake.org>`_ (since 2.8.5) supports generation 33*f4a2713aSLionel Sambucof compilation databases for Unix Makefile builds (Ninja builds in the 34*f4a2713aSLionel Sambucworks) with the option ``CMAKE_EXPORT_COMPILE_COMMANDS``. 35*f4a2713aSLionel Sambuc 36*f4a2713aSLionel SambucFor projects on Linux, there is an alternative to intercept compiler 37*f4a2713aSLionel Sambuccalls with a tool called `Bear <https://github.com/rizsotto/Bear>`_. 38*f4a2713aSLionel Sambuc 39*f4a2713aSLionel SambucClang's tooling interface supports reading compilation databases; see 40*f4a2713aSLionel Sambucthe :doc:`LibTooling documentation <LibTooling>`. libclang and its 41*f4a2713aSLionel Sambucpython bindings also support this (since clang 3.2); see 42*f4a2713aSLionel Sambuc`CXCompilationDatabase.h </doxygen/group__COMPILATIONDB.html>`_. 43*f4a2713aSLionel Sambuc 44*f4a2713aSLionel SambucFormat 45*f4a2713aSLionel Sambuc====== 46*f4a2713aSLionel Sambuc 47*f4a2713aSLionel SambucA compilation database is a JSON file, which consist of an array of 48*f4a2713aSLionel Sambuc"command objects", where each command object specifies one way a 49*f4a2713aSLionel Sambuctranslation unit is compiled in the project. 50*f4a2713aSLionel Sambuc 51*f4a2713aSLionel SambucEach command object contains the translation unit's main file, the 52*f4a2713aSLionel Sambucworking directory of the compile run and the actual compile command. 53*f4a2713aSLionel Sambuc 54*f4a2713aSLionel SambucExample: 55*f4a2713aSLionel Sambuc 56*f4a2713aSLionel Sambuc:: 57*f4a2713aSLionel Sambuc 58*f4a2713aSLionel Sambuc [ 59*f4a2713aSLionel Sambuc { "directory": "/home/user/llvm/build", 60*f4a2713aSLionel Sambuc "command": "/usr/bin/clang++ -Irelative -DSOMEDEF=\"With spaces, quotes and \\-es.\" -c -o file.o file.cc", 61*f4a2713aSLionel Sambuc "file": "file.cc" }, 62*f4a2713aSLionel Sambuc ... 63*f4a2713aSLionel Sambuc ] 64*f4a2713aSLionel Sambuc 65*f4a2713aSLionel SambucThe contracts for each field in the command object are: 66*f4a2713aSLionel Sambuc 67*f4a2713aSLionel Sambuc- **directory:** The working directory of the compilation. All paths 68*f4a2713aSLionel Sambuc specified in the **command** or **file** fields must be either 69*f4a2713aSLionel Sambuc absolute or relative to this directory. 70*f4a2713aSLionel Sambuc- **file:** The main translation unit source processed by this 71*f4a2713aSLionel Sambuc compilation step. This is used by tools as the key into the 72*f4a2713aSLionel Sambuc compilation database. There can be multiple command objects for the 73*f4a2713aSLionel Sambuc same file, for example if the same source file is compiled with 74*f4a2713aSLionel Sambuc different configurations. 75*f4a2713aSLionel Sambuc- **command:** The compile command executed. After JSON unescaping, 76*f4a2713aSLionel Sambuc this must be a valid command to rerun the exact compilation step for 77*f4a2713aSLionel Sambuc the translation unit in the environment the build system uses. 78*f4a2713aSLionel Sambuc Parameters use shell quoting and shell escaping of quotes, with '``"``' 79*f4a2713aSLionel Sambuc and '``\``' being the only special characters. Shell expansion is not 80*f4a2713aSLionel Sambuc supported. 81*f4a2713aSLionel Sambuc 82*f4a2713aSLionel SambucBuild System Integration 83*f4a2713aSLionel Sambuc======================== 84*f4a2713aSLionel Sambuc 85*f4a2713aSLionel SambucThe convention is to name the file compile\_commands.json and put it at 86*f4a2713aSLionel Sambucthe top of the build directory. Clang tools are pointed to the top of 87*f4a2713aSLionel Sambucthe build directory to detect the file and use the compilation database 88*f4a2713aSLionel Sambucto parse C++ code in the source tree. 89