Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 1 | ============================================== |
| 2 | JSON Compilation Database Format Specification |
| 3 | ============================================== |
| 4 | |
| 5 | This document describes a format for specifying how to replay single |
| 6 | compilations independently of the build system. |
| 7 | |
| 8 | Background |
| 9 | ========== |
| 10 | |
| 11 | Tools based on the C++ Abstract Syntax Tree need full information how to |
| 12 | parse a translation unit. Usually this information is implicitly |
| 13 | available in the build system, but running tools as part of the build |
| 14 | system 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 | |
| 29 | Supported Systems |
| 30 | ================= |
| 31 | |
Eugene Zelenko | adcb3f5 | 2019-01-23 20:39:07 +0000 | [diff] [blame] | 32 | Currently `CMake <https://cmake.org>`_ (since 2.8.5) supports generation |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 33 | of compilation databases for Unix Makefile builds (Ninja builds in the |
Dmitri Gribenko | 7ac0cc3 | 2012-12-15 21:10:51 +0000 | [diff] [blame] | 34 | works) with the option ``CMAKE_EXPORT_COMPILE_COMMANDS``. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 35 | |
Dmitri Gribenko | b46dc57 | 2013-01-30 17:58:14 +0000 | [diff] [blame] | 36 | For projects on Linux, there is an alternative to intercept compiler |
| 37 | calls with a tool called `Bear <https://github.com/rizsotto/Bear>`_. |
| 38 | |
Nathan Ridge | 5233ad1 | 2021-11-28 19:29:06 -0500 | [diff] [blame] | 39 | `Bazel <https://bazel.build>`_ can export a compilation database via |
| 40 | `this extractor extension |
| 41 | <https://github.com/hedronvision/bazel-compile-commands-extractor>`_. |
| 42 | Bazel is otherwise resistant to Bear and other compiler-intercept |
| 43 | techniques. |
| 44 | |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 45 | Clang's tooling interface supports reading compilation databases; see |
Sean Silva | 173d2526 | 2013-01-02 13:07:47 +0000 | [diff] [blame] | 46 | the :doc:`LibTooling documentation <LibTooling>`. libclang and its |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 47 | python bindings also support this (since clang 3.2); see |
| 48 | `CXCompilationDatabase.h </doxygen/group__COMPILATIONDB.html>`_. |
| 49 | |
| 50 | Format |
| 51 | ====== |
| 52 | |
| 53 | A compilation database is a JSON file, which consist of an array of |
| 54 | "command objects", where each command object specifies one way a |
| 55 | translation unit is compiled in the project. |
| 56 | |
| 57 | Each command object contains the translation unit's main file, the |
| 58 | working directory of the compile run and the actual compile command. |
| 59 | |
| 60 | Example: |
| 61 | |
| 62 | :: |
| 63 | |
| 64 | [ |
| 65 | { "directory": "/home/user/llvm/build", |
Dmitri Gribenko | 2625cf0 | 2013-01-30 17:58:39 +0000 | [diff] [blame] | 66 | "command": "/usr/bin/clang++ -Irelative -DSOMEDEF=\"With spaces, quotes and \\-es.\" -c -o file.o file.cc", |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 67 | "file": "file.cc" }, |
| 68 | ... |
| 69 | ] |
| 70 | |
| 71 | The contracts for each field in the command object are: |
| 72 | |
| 73 | - **directory:** The working directory of the compilation. All paths |
| 74 | specified in the **command** or **file** fields must be either |
| 75 | absolute or relative to this directory. |
| 76 | - **file:** The main translation unit source processed by this |
| 77 | compilation step. This is used by tools as the key into the |
| 78 | compilation database. There can be multiple command objects for the |
| 79 | same file, for example if the same source file is compiled with |
| 80 | different configurations. |
| 81 | - **command:** The compile command executed. After JSON unescaping, |
| 82 | this must be a valid command to rerun the exact compilation step for |
| 83 | the translation unit in the environment the build system uses. |
Dmitri Gribenko | 7ac0cc3 | 2012-12-15 21:10:51 +0000 | [diff] [blame] | 84 | Parameters use shell quoting and shell escaping of quotes, with '``"``' |
| 85 | and '``\``' being the only special characters. Shell expansion is not |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 86 | supported. |
Joerg Sonnenberger | c937970 | 2016-11-25 14:14:43 +0000 | [diff] [blame] | 87 | - **arguments:** The compile command executed as list of strings. |
| 88 | Either **arguments** or **command** is required. |
Joerg Sonnenberger | 399aea3 | 2016-12-01 23:37:45 +0000 | [diff] [blame] | 89 | - **output:** The name of the output created by this compilation step. |
| 90 | This field is optional. It can be used to distinguish different processing |
| 91 | modes of the same input file. |
Sean Silva | bf9b4cd | 2012-12-13 01:10:46 +0000 | [diff] [blame] | 92 | |
| 93 | Build System Integration |
| 94 | ======================== |
| 95 | |
| 96 | The convention is to name the file compile\_commands.json and put it at |
| 97 | the top of the build directory. Clang tools are pointed to the top of |
| 98 | the build directory to detect the file and use the compilation database |
| 99 | to parse C++ code in the source tree. |
Sam McCall | 60d74e4 | 2017-11-09 10:37:39 +0000 | [diff] [blame] | 100 | |
| 101 | Alternatives |
| 102 | ============ |
Sam McCall | 095f086 | 2021-01-31 11:16:52 +0100 | [diff] [blame] | 103 | For simple projects, Clang tools also recognize a ``compile_flags.txt`` file. |
| 104 | This should contain one argument per line. The same flags will be used to |
| 105 | compile any file. |
| 106 | |
| 107 | Example: |
| 108 | |
| 109 | :: |
| 110 | |
| 111 | -xc++ |
| 112 | -I |
| 113 | libwidget/include/ |
| 114 | |
| 115 | Here ``-I libwidget/include`` is two arguments, and so becomes two lines. |
| 116 | Paths are relative to the directory containing ``compile_flags.txt``. |
| 117 | |