17330f729Sjoerg============================================== 27330f729SjoergJSON Compilation Database Format Specification 37330f729Sjoerg============================================== 47330f729Sjoerg 57330f729SjoergThis document describes a format for specifying how to replay single 67330f729Sjoergcompilations independently of the build system. 77330f729Sjoerg 87330f729SjoergBackground 97330f729Sjoerg========== 107330f729Sjoerg 117330f729SjoergTools based on the C++ Abstract Syntax Tree need full information how to 127330f729Sjoergparse a translation unit. Usually this information is implicitly 137330f729Sjoergavailable in the build system, but running tools as part of the build 147330f729Sjoergsystem is not necessarily the best solution: 157330f729Sjoerg 167330f729Sjoerg- Build systems are inherently change driven, so running multiple tools 177330f729Sjoerg over the same code base without changing the code does not fit into 187330f729Sjoerg the architecture of many build systems. 197330f729Sjoerg- Figuring out whether things have changed is often an IO bound 207330f729Sjoerg process; this makes it hard to build low latency end user tools based 217330f729Sjoerg on the build system. 227330f729Sjoerg- Build systems are inherently sequential in the build graph, for 237330f729Sjoerg example due to generated source code. While tools that run 247330f729Sjoerg independently of the build still need the generated source code to 257330f729Sjoerg exist, running tools multiple times over unchanging source does not 267330f729Sjoerg require serialization of the runs according to the build dependency 277330f729Sjoerg graph. 287330f729Sjoerg 297330f729SjoergSupported Systems 307330f729Sjoerg================= 317330f729Sjoerg 327330f729SjoergCurrently `CMake <https://cmake.org>`_ (since 2.8.5) supports generation 337330f729Sjoergof compilation databases for Unix Makefile builds (Ninja builds in the 347330f729Sjoergworks) with the option ``CMAKE_EXPORT_COMPILE_COMMANDS``. 357330f729Sjoerg 367330f729SjoergFor projects on Linux, there is an alternative to intercept compiler 377330f729Sjoergcalls with a tool called `Bear <https://github.com/rizsotto/Bear>`_. 387330f729Sjoerg 397330f729SjoergClang's tooling interface supports reading compilation databases; see 407330f729Sjoergthe :doc:`LibTooling documentation <LibTooling>`. libclang and its 417330f729Sjoergpython bindings also support this (since clang 3.2); see 427330f729Sjoerg`CXCompilationDatabase.h </doxygen/group__COMPILATIONDB.html>`_. 437330f729Sjoerg 447330f729SjoergFormat 457330f729Sjoerg====== 467330f729Sjoerg 477330f729SjoergA compilation database is a JSON file, which consist of an array of 487330f729Sjoerg"command objects", where each command object specifies one way a 497330f729Sjoergtranslation unit is compiled in the project. 507330f729Sjoerg 517330f729SjoergEach command object contains the translation unit's main file, the 527330f729Sjoergworking directory of the compile run and the actual compile command. 537330f729Sjoerg 547330f729SjoergExample: 557330f729Sjoerg 567330f729Sjoerg:: 577330f729Sjoerg 587330f729Sjoerg [ 597330f729Sjoerg { "directory": "/home/user/llvm/build", 607330f729Sjoerg "command": "/usr/bin/clang++ -Irelative -DSOMEDEF=\"With spaces, quotes and \\-es.\" -c -o file.o file.cc", 617330f729Sjoerg "file": "file.cc" }, 627330f729Sjoerg ... 637330f729Sjoerg ] 647330f729Sjoerg 657330f729SjoergThe contracts for each field in the command object are: 667330f729Sjoerg 677330f729Sjoerg- **directory:** The working directory of the compilation. All paths 687330f729Sjoerg specified in the **command** or **file** fields must be either 697330f729Sjoerg absolute or relative to this directory. 707330f729Sjoerg- **file:** The main translation unit source processed by this 717330f729Sjoerg compilation step. This is used by tools as the key into the 727330f729Sjoerg compilation database. There can be multiple command objects for the 737330f729Sjoerg same file, for example if the same source file is compiled with 747330f729Sjoerg different configurations. 757330f729Sjoerg- **command:** The compile command executed. After JSON unescaping, 767330f729Sjoerg this must be a valid command to rerun the exact compilation step for 777330f729Sjoerg the translation unit in the environment the build system uses. 787330f729Sjoerg Parameters use shell quoting and shell escaping of quotes, with '``"``' 797330f729Sjoerg and '``\``' being the only special characters. Shell expansion is not 807330f729Sjoerg supported. 817330f729Sjoerg- **arguments:** The compile command executed as list of strings. 827330f729Sjoerg Either **arguments** or **command** is required. 837330f729Sjoerg- **output:** The name of the output created by this compilation step. 847330f729Sjoerg This field is optional. It can be used to distinguish different processing 857330f729Sjoerg modes of the same input file. 867330f729Sjoerg 877330f729SjoergBuild System Integration 887330f729Sjoerg======================== 897330f729Sjoerg 907330f729SjoergThe convention is to name the file compile\_commands.json and put it at 917330f729Sjoergthe top of the build directory. Clang tools are pointed to the top of 927330f729Sjoergthe build directory to detect the file and use the compilation database 937330f729Sjoergto parse C++ code in the source tree. 947330f729Sjoerg 957330f729SjoergAlternatives 967330f729Sjoerg============ 97*e038c9c4SjoergFor simple projects, Clang tools also recognize a ``compile_flags.txt`` file. 98*e038c9c4SjoergThis should contain one argument per line. The same flags will be used to 99*e038c9c4Sjoergcompile any file. 100*e038c9c4Sjoerg 101*e038c9c4SjoergExample: 102*e038c9c4Sjoerg 103*e038c9c4Sjoerg:: 104*e038c9c4Sjoerg 105*e038c9c4Sjoerg -xc++ 106*e038c9c4Sjoerg -I 107*e038c9c4Sjoerg libwidget/include/ 108*e038c9c4Sjoerg 109*e038c9c4SjoergHere ``-I libwidget/include`` is two arguments, and so becomes two lines. 110*e038c9c4SjoergPaths are relative to the directory containing ``compile_flags.txt``. 111*e038c9c4Sjoerg 112