JSONCompilationDatabase.rst 3.7 KB

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