JSONCompilationDatabase.rst 3.6 KB

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