index.rst 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. IDE Integration Guide
  2. *********************
  3. .. only:: html
  4. .. contents::
  5. Introduction
  6. ============
  7. Integrated development environments (IDEs) may want to integrate with CMake to
  8. improve the development experience for CMake users. This document lays out the
  9. recommended best practices for such integration.
  10. Bundling
  11. ========
  12. Many IDE vendors will want to bundle a copy of CMake with their IDE. IDEs that
  13. bundle CMake should present the user with the option of using an external CMake
  14. installation instead of the bundled one, in case the bundled copy becomes
  15. outdated and the user wants to use a newer version.
  16. While IDE vendors may be tempted to bundle different versions of CMake with
  17. their application, such practice is not recommended. CMake has strong
  18. guarantees of backwards compatibility, and there is no reason not to use a
  19. newer version of CMake than what a project requires, or indeed, the very latest
  20. version. Therefore, it is recommended that IDE vendors that bundle CMake with
  21. their application always include the very latest patch version of CMake
  22. available at the time of release.
  23. As a suggestion, IDEs may also ship a copy of the Ninja buildsystem alongside
  24. CMake. Ninja is highly performant and well-supported on all platforms that
  25. support CMake. IDEs that bundle Ninja should use Ninja 1.10 or later, which
  26. contains features needed to support Fortran builds.
  27. Presets
  28. =======
  29. CMake supports a file format called ``CMakePresets.json``, and its
  30. user-specific counterpart, ``CMakeUserPresets.json``. This file contains
  31. information on the various configure presets that a user may want. Each preset
  32. may have a different compiler, build flags, etc. The details of this format are
  33. explained in the :manual:`cmake(1)` manual.
  34. IDE vendors are encouraged to read and evaluate this file the same way CMake
  35. does, and present the user with the presets listed in the file. Users should be
  36. able to see (and possibly edit) the CMake cache variables, environment
  37. variables, and command line options that are defined for a given preset. The
  38. IDE should then construct the list of appropriate :manual:`cmake(1)` command
  39. line arguments based on these settings, rather than using the
  40. :option:`--preset= <cmake --preset>` option directly. The
  41. :option:`--preset= <cmake --preset>` option is intended only as a convenient
  42. frontend for command line users, and should not be used by the IDE.
  43. For example, if a preset named ``ninja`` specifies ``Ninja`` as the generator
  44. and ``${sourceDir}/build`` as the build directory, instead of running:
  45. .. code-block:: console
  46. cmake -S /path/to/source --preset=ninja
  47. the IDE should instead calculate the settings of the ``ninja`` preset, and then
  48. run:
  49. .. code-block:: console
  50. cmake -S /path/to/source -B /path/to/source/build -G Ninja
  51. In cases where a preset contains lots of cache variables, and passing all of
  52. them as :option:`-D <cmake -D>` flags would cause the command line length limit
  53. of the platform to be exceeded, the IDE should instead construct a temporary
  54. cache script and pass it with the :option:`-C <cmake -C>` flag.
  55. While reading, parsing, and evaluating the contents of ``CMakePresets.json`` is
  56. straightforward, it is not trivial. In addition to the documentation, IDE
  57. vendors may also wish to refer to the CMake source code and test cases for a
  58. better understanding of how to implement the format.
  59. :download:`This file <../../manual/presets/schema.json>` provides a
  60. machine-readable JSON schema for the ``CMakePresets.json`` format that IDE
  61. vendors may find useful for validation and providing editing assistance.
  62. Configuring
  63. ===========
  64. IDEs that invoke :manual:`cmake(1)` to run the configure step may wish to
  65. receive information about the artifacts that the build will produce, as well
  66. as the include directories, compile definitions, etc. used to build the
  67. artifacts. Such information can be obtained by using the
  68. :manual:`File API <cmake-file-api(7)>`. The manual page for the File API
  69. contains more information about the API and how to invoke it.
  70. :manual:`Server mode <cmake-server(7)>` was removed as of CMake 3.20 and
  71. should not be used on CMake 3.14 or later.
  72. IDEs should avoid creating more build trees than necessary, and only create
  73. multiple build trees if the user wishes to switch to a different compiler,
  74. use different compile flags, etc. In particular, IDEs should NOT create
  75. multiple single-config build trees which all have the same properties except
  76. for a differing :variable:`CMAKE_BUILD_TYPE`, effectively creating a
  77. multi-config environment. Instead, the :generator:`Ninja Multi-Config`
  78. generator, in conjunction with the :manual:`File API <cmake-file-api(7)>` to
  79. get the list of build configurations, should be used for this purpose.
  80. IDEs should not use the "extra generators" with Makefile or Ninja generators,
  81. which generate IDE project files in addition to the Makefile or Ninja files.
  82. Instead the :manual:`File API <cmake-file-api(7)>` should be used to get the
  83. list of build artifacts.
  84. Building
  85. ========
  86. If a Makefile or Ninja generator is used to generate the build tree, it is not
  87. recommended to invoke ``make`` or ``ninja`` directly. Instead, it is
  88. recommended that the IDE invoke :manual:`cmake(1)` with the
  89. :option:`--build <cmake --build>` argument, which will in turn invoke the
  90. appropriate build tool.
  91. If an IDE project generator is used, such as :generator:`Xcode` or one of the
  92. :ref:`Visual Studio Generators`, and the IDE understands the project format
  93. used, the IDE should read the project file and build it the same way it would
  94. otherwise.
  95. The :manual:`File API <cmake-file-api(7)>` can be used to obtain a list of
  96. build configurations from the build tree, and the IDE should present this list
  97. to the user to select a build configuration.
  98. Testing
  99. =======
  100. :manual:`ctest(1)` supports outputting a JSON format with information about the
  101. available tests and test configurations. IDEs which want to run CTest should
  102. obtain this information and use it to present the user with a list of tests.
  103. IDEs should not invoke the ``test`` target of the generated buildsystem.
  104. Instead, they should invoke :manual:`ctest(1)` directly.
  105. IDEs with CMake integration
  106. ===========================
  107. The following IDEs support CMake natively:
  108. * `CLion`_
  109. * `KDevelop`_
  110. * `QtCreator`_
  111. * `Vim`_ (via a plugin)
  112. * `Visual Studio`_
  113. * `VSCode`_ (via a plugin)
  114. .. _CLion: https://www.jetbrains.com/clion/
  115. .. _KDevelop: https://kdevelop.org/
  116. .. _QtCreator: https://www.qt.io/product/development-tools
  117. .. _Vim: https://www.vim.org/
  118. .. _Visual Studio: https://visualstudio.microsoft.com/
  119. .. _VSCode: https://code.visualstudio.com/
  120. Additionally, CMake has builtin support for some IDEs:
  121. * :ref:`IDE Build Tool Generators`:
  122. Generate IDE native build systems such as
  123. :ref:`Visual Studio <Visual Studio Generators>` or :generator:`Xcode`.
  124. * :ref:`Extra Generators`:
  125. Extend :ref:`Command-Line Build Tool Generators` to generate IDE
  126. project files that hook into the command-line build system.
  127. Superseded by the :manual:`File API <cmake-file-api(7)>`.