xref: /openbsd-src/gnu/llvm/clang/docs/JSONCompilationDatabase.rst (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick==============================================
2e5dd7070SpatrickJSON Compilation Database Format Specification
3e5dd7070Spatrick==============================================
4e5dd7070Spatrick
5e5dd7070SpatrickThis document describes a format for specifying how to replay single
6e5dd7070Spatrickcompilations independently of the build system.
7e5dd7070Spatrick
8e5dd7070SpatrickBackground
9e5dd7070Spatrick==========
10e5dd7070Spatrick
11e5dd7070SpatrickTools based on the C++ Abstract Syntax Tree need full information how to
12e5dd7070Spatrickparse a translation unit. Usually this information is implicitly
13e5dd7070Spatrickavailable in the build system, but running tools as part of the build
14e5dd7070Spatricksystem is not necessarily the best solution:
15e5dd7070Spatrick
16e5dd7070Spatrick-  Build systems are inherently change driven, so running multiple tools
17e5dd7070Spatrick   over the same code base without changing the code does not fit into
18e5dd7070Spatrick   the architecture of many build systems.
19e5dd7070Spatrick-  Figuring out whether things have changed is often an IO bound
20e5dd7070Spatrick   process; this makes it hard to build low latency end user tools based
21e5dd7070Spatrick   on the build system.
22e5dd7070Spatrick-  Build systems are inherently sequential in the build graph, for
23e5dd7070Spatrick   example due to generated source code. While tools that run
24e5dd7070Spatrick   independently of the build still need the generated source code to
25e5dd7070Spatrick   exist, running tools multiple times over unchanging source does not
26e5dd7070Spatrick   require serialization of the runs according to the build dependency
27e5dd7070Spatrick   graph.
28e5dd7070Spatrick
29e5dd7070SpatrickSupported Systems
30e5dd7070Spatrick=================
31e5dd7070Spatrick
32*12c85518SrobertClang has the ability to generate compilation database fragments via
33*12c85518Srobert``-MJ argument <clang -MJ\<arg>>``. You can concatenate those
34*12c85518Srobertfragments together between ``[`` and ``]`` to create a compilation database.
35*12c85518Srobert
36e5dd7070SpatrickCurrently `CMake <https://cmake.org>`_ (since 2.8.5) supports generation
37e5dd7070Spatrickof compilation databases for Unix Makefile builds (Ninja builds in the
38e5dd7070Spatrickworks) with the option ``CMAKE_EXPORT_COMPILE_COMMANDS``.
39e5dd7070Spatrick
40e5dd7070SpatrickFor projects on Linux, there is an alternative to intercept compiler
41e5dd7070Spatrickcalls with a tool called `Bear <https://github.com/rizsotto/Bear>`_.
42e5dd7070Spatrick
43*12c85518Srobert`Bazel <https://bazel.build>`_ can export a compilation database via
44*12c85518Srobert`this extractor extension
45*12c85518Srobert<https://github.com/hedronvision/bazel-compile-commands-extractor>`_.
46*12c85518SrobertBazel is otherwise resistant to Bear and other compiler-intercept
47*12c85518Sroberttechniques.
48*12c85518Srobert
49e5dd7070SpatrickClang's tooling interface supports reading compilation databases; see
50e5dd7070Spatrickthe :doc:`LibTooling documentation <LibTooling>`. libclang and its
51e5dd7070Spatrickpython bindings also support this (since clang 3.2); see
52e5dd7070Spatrick`CXCompilationDatabase.h </doxygen/group__COMPILATIONDB.html>`_.
53e5dd7070Spatrick
54e5dd7070SpatrickFormat
55e5dd7070Spatrick======
56e5dd7070Spatrick
57e5dd7070SpatrickA compilation database is a JSON file, which consist of an array of
58e5dd7070Spatrick"command objects", where each command object specifies one way a
59e5dd7070Spatricktranslation unit is compiled in the project.
60e5dd7070Spatrick
61e5dd7070SpatrickEach command object contains the translation unit's main file, the
62e5dd7070Spatrickworking directory of the compile run and the actual compile command.
63e5dd7070Spatrick
64e5dd7070SpatrickExample:
65e5dd7070Spatrick
66e5dd7070Spatrick::
67e5dd7070Spatrick
68e5dd7070Spatrick    [
69e5dd7070Spatrick      { "directory": "/home/user/llvm/build",
70*12c85518Srobert        "arguments": ["/usr/bin/clang++", "-Irelative", "-DSOMEDEF=With spaces, quotes and \\-es.", "-c", "-o", "file.o", "file.cc"],
71e5dd7070Spatrick        "file": "file.cc" },
72*12c85518Srobert
73*12c85518Srobert      { "directory": "/home/user/llvm/build",
74*12c85518Srobert        "command": "/usr/bin/clang++ -Irelative -DSOMEDEF=\"With spaces, quotes and \\-es.\" -c -o file.o file.cc",
75*12c85518Srobert        "file": "file2.cc" },
76*12c85518Srobert
77e5dd7070Spatrick      ...
78e5dd7070Spatrick    ]
79e5dd7070Spatrick
80e5dd7070SpatrickThe contracts for each field in the command object are:
81e5dd7070Spatrick
82e5dd7070Spatrick-  **directory:** The working directory of the compilation. All paths
83e5dd7070Spatrick   specified in the **command** or **file** fields must be either
84e5dd7070Spatrick   absolute or relative to this directory.
85e5dd7070Spatrick-  **file:** The main translation unit source processed by this
86e5dd7070Spatrick   compilation step. This is used by tools as the key into the
87e5dd7070Spatrick   compilation database. There can be multiple command objects for the
88e5dd7070Spatrick   same file, for example if the same source file is compiled with
89e5dd7070Spatrick   different configurations.
90*12c85518Srobert-  **arguments:** The compile command argv as list of strings.
91*12c85518Srobert   This should run the compilation step for the translation unit ``file``.
92*12c85518Srobert   ``arguments[0]`` should be the executable name, such as ``clang++``.
93*12c85518Srobert   Arguments should not be escaped, but ready to pass to ``execvp()``.
94*12c85518Srobert-  **command:** The compile command as a single shell-escaped string.
95*12c85518Srobert   Arguments may be shell quoted and escaped following platform conventions,
96*12c85518Srobert   with '``"``' and '``\``' being the only special characters. Shell expansion
97*12c85518Srobert   is not supported.
98*12c85518Srobert
99*12c85518Srobert   Either **arguments** or **command** is required. **arguments** is preferred,
100*12c85518Srobert   as shell (un)escaping is a possible source of errors.
101e5dd7070Spatrick-  **output:** The name of the output created by this compilation step.
102e5dd7070Spatrick   This field is optional. It can be used to distinguish different processing
103e5dd7070Spatrick   modes of the same input file.
104e5dd7070Spatrick
105e5dd7070SpatrickBuild System Integration
106e5dd7070Spatrick========================
107e5dd7070Spatrick
108e5dd7070SpatrickThe convention is to name the file compile\_commands.json and put it at
109e5dd7070Spatrickthe top of the build directory. Clang tools are pointed to the top of
110e5dd7070Spatrickthe build directory to detect the file and use the compilation database
111e5dd7070Spatrickto parse C++ code in the source tree.
112e5dd7070Spatrick
113e5dd7070SpatrickAlternatives
114e5dd7070Spatrick============
115a9ac8606SpatrickFor simple projects, Clang tools also recognize a ``compile_flags.txt`` file.
116a9ac8606SpatrickThis should contain one argument per line. The same flags will be used to
117a9ac8606Spatrickcompile any file.
118a9ac8606Spatrick
119a9ac8606SpatrickExample:
120a9ac8606Spatrick
121a9ac8606Spatrick::
122a9ac8606Spatrick
123a9ac8606Spatrick    -xc++
124a9ac8606Spatrick    -I
125a9ac8606Spatrick    libwidget/include/
126a9ac8606Spatrick
127a9ac8606SpatrickHere ``-I libwidget/include`` is two arguments, and so becomes two lines.
128a9ac8606SpatrickPaths are relative to the directory containing ``compile_flags.txt``.
129a9ac8606Spatrick
130