blob: 29071947290234504671c3ce1aec9b5f1c47c00e [file] [log] [blame]
Manuel Klimek8c246752012-06-12 17:51:04 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5<title>JSON Compilation Database Format Specification</title>
6<link type="text/css" rel="stylesheet" href="../menu.css">
7<link type="text/css" rel="stylesheet" href="../content.css">
8</head>
9<body>
10
11<!--#include virtual="../menu.html.incl"-->
12
13<div id="content">
14
15<h1>JSON Compilation Database Format Specification</h1>
Manuel Klimek1156afd2012-06-13 19:40:46 +000016<p>This document describes a format for specifying how to replay
Manuel Klimek8c246752012-06-12 17:51:04 +000017single compilations independently of the build system.</p>
18
19<h2>Background</h2>
Manuel Klimek1156afd2012-06-13 19:40:46 +000020<p>Tools based on the C++ Abstract Syntax Tree need full information how to
Manuel Klimek8c246752012-06-12 17:51:04 +000021parse a translation unit. Usually this information is implicitly
22available in the build system, but running tools as part of
23the build system is not necessarily the best solution:
24<ul>
25<li>Build systems are inherently change driven, so running multiple
26tools over the same code base without changing the code does not fit
27into the architecture of many build systems.</li>
28<li>Figuring out whether things have changed is often an IO bound
29process; this makes it hard to build low latency end user tools based
30on the build system.</li>
31<li>Build systems are inherently sequential in the build graph, for example
32due to generated source code. While tools that run independently of the
33build still need the generated source code to exist, running tools multiple
34times over unchanging source does not require serialization of the runs
35according to the build dependency graph.</li>
36</ul>
37</p>
38
39<h2>Supported Systems</h2>
Manuel Klimek1156afd2012-06-13 19:40:46 +000040<p>Currently <a href="http://cmake.org">CMake</a> (since 2.8.5) supports generation of compilation
Manuel Klimek8c246752012-06-12 17:51:04 +000041databases for Unix Makefile builds (Ninja builds in the works) with the option
42CMAKE_EXPORT_COMPILE_COMMANDS.</p>
43<p>Clang's tooling interface supports reading compilation databases; see
Manuel Klimekef0ebee2012-07-09 05:17:45 +000044the <a href="LibTooling.html">LibTooling documentation</a>. libclang and its
45python bindings also support this (since clang 3.2); see
46<a href="/doxygen/group__COMPILATIONDB.html">CXCompilationDatabase.h</a>.</p>
Manuel Klimek8c246752012-06-12 17:51:04 +000047
48<h2>Format</h2>
49<p>A compilation database is a JSON file, which consist of an array of
Manuel Klimek1156afd2012-06-13 19:40:46 +000050"command objects", where each command object specifies one way a translation unit
Manuel Klimek8c246752012-06-12 17:51:04 +000051is compiled in the project.</p>
Manuel Klimek1156afd2012-06-13 19:40:46 +000052<p>Each command object contains the translation unit's main file, the working
Manuel Klimek8c246752012-06-12 17:51:04 +000053directory of the compile run and the actual compile command.</p>
54<p>Example:
55<pre>
56[
57 { "directory": "/home/user/llvm/build",
58 "command": "/usr/bin/clang++ -Irelative -DSOMEDEF='\"With spaces and quotes.\"' -c -o file.o file.cc",
59 "file": "file.cc" },
60 ...
61]
62</pre>
63The contracts for each field in the command object are:
64<ul>
65<li><b>directory:</b> The working directory of the compilation. All paths specified
66in the <b>command</b> or <b>file</b> fields must be either absolute or relative to
67this directory.</li>
68<li><b>file:</b> The main translation unit source processed by this compilation step.
69This is used by tools as the key into the compilation database. There can be multiple
Manuel Klimek1156afd2012-06-13 19:40:46 +000070command objects for the same file, for example if the same source file is
Manuel Klimek8c246752012-06-12 17:51:04 +000071compiled with different configurations.</li>
72<li><b>command:</b> The compile command executed. After JSON unescaping, this must
73be a valid command to rerun the exact compilation step for the translation unit in
Manuel Klimek3f6d5132012-07-10 08:05:54 +000074the environment the build system uses. Parameters use shell quoting and shell escaping
75of quotes, with '"' and '\' being the only special characters. Shell expansion is
76not supported.</li>
Manuel Klimek8c246752012-06-12 17:51:04 +000077</ul>
78</p>
79
80<h2>Build System Integration</h2>
81<p>The convention is to name the file compile_commands.json and put it at the top
82of the build directory. Clang tools are pointed to the top of the build directory
83to detect the file and use the compilation database to parse C++ code in the source
84tree.</p>
85
86</div>
87</body>
88</html>
89