index.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 ``--preset=``
  40. option directly. The ``--preset=`` option is intended only as a convenient
  41. frontend for command line users, and should not be used by the IDE.
  42. For example, if a preset named ``ninja`` specifies ``Ninja`` as the generator
  43. and ``${sourceDir}/build`` as the build directory, instead of running:
  44. .. code-block:: console
  45. cmake -S /path/to/source --preset=ninja
  46. the IDE should instead calculate the settings of the ``ninja`` preset, and then
  47. run:
  48. .. code-block:: console
  49. cmake -S /path/to/source -B /path/to/source/build -G Ninja
  50. While reading, parsing, and evaluating the contents of ``CMakePresets.json`` is
  51. straightforward, it is not trivial. In addition to the documentation, IDE
  52. vendors may also wish to refer to the CMake source code and test cases for a
  53. better understanding of how to implement the format.
  54. :download:`This file <../../manual/presets/schema.json>` provides a
  55. machine-readable JSON schema for the ``CMakePresets.json`` format that IDE
  56. vendors may find useful for validation and providing editing assistance.
  57. Configuring
  58. ===========
  59. IDEs that invoke :manual:`cmake(1)` to run the configure step may wish to
  60. receive information about the artifacts that the build will produce, as well
  61. as the include directories, compile definitions, etc. used to build the
  62. artifacts. Such information can be obtained by using the
  63. :manual:`File API <cmake-file-api(7)>`. The manual page for the File API
  64. contains more information about the API and how to invoke it.
  65. :manual:`Server mode <cmake-server(7)>` was removed as of CMake 3.20 and
  66. should not be used on CMake 3.14 or later.
  67. IDEs should avoid creating more build trees than necessary, and only create
  68. multiple build trees if the user wishes to switch to a different compiler,
  69. use different compile flags, etc. In particular, IDEs should NOT create
  70. multiple single-config build trees which all have the same properties except
  71. for a differing :variable:`CMAKE_BUILD_TYPE`, effectively creating a
  72. multi-config environment. Instead, the :generator:`Ninja Multi-Config`
  73. generator, in conjunction with the :manual:`File API <cmake-file-api(7)>` to
  74. get the list of build configurations, should be used for this purpose.
  75. IDEs should not use the "extra generators" with Makefile or Ninja generators,
  76. which generate IDE project files in addition to the Makefile or Ninja files.
  77. Instead the :manual:`File API <cmake-file-api(7)>` should be used to get the
  78. list of build artifacts.
  79. Building
  80. ========
  81. If a Makefile or Ninja generator is used to generate the build tree, it is not
  82. recommended to invoke ``make`` or ``ninja`` directly. Instead, it is
  83. recommended that the IDE invoke :manual:`cmake(1)` with the ``--build``
  84. argument, which will in turn invoke the appropriate build tool.
  85. If an IDE project generator is used, such as :generator:`Xcode` or one of the
  86. Visual Studio generators, and the IDE understands the project format used, the
  87. IDE should read the project file and build it the same way it would otherwise.
  88. The :manual:`File API <cmake-file-api(7)>` can be used to obtain a list of
  89. build configurations from the build tree, and the IDE should present this list
  90. to the user to select a build configuration.
  91. Testing
  92. =======
  93. :manual:`ctest(1)` supports outputting a JSON format with information about the
  94. available tests and test configurations. IDEs which want to run CTest should
  95. obtain this information and use it to present the user with a list of tests.
  96. IDEs should not invoke the ``test`` target of the generated buildsystem.
  97. Instead, they should invoke :manual:`ctest(1)` directly.
  98. IDEs with CMake integration
  99. ===========================
  100. The following IDEs support CMake natively:
  101. * `CLion`_
  102. * `KDevelop`_
  103. * `QtCreator`_
  104. * `Vim`_ (via a plugin)
  105. * `Visual Studio`_
  106. * `VSCode`_ (via a plugin)
  107. .. _CLion: https://www.jetbrains.com/clion/
  108. .. _KDevelop: https://www.kdevelop.org/
  109. .. _QtCreator: https://www.qt.io/product/development-tools
  110. .. _Vim: https://www.vim.org/
  111. .. _Visual Studio: https://visualstudio.microsoft.com/
  112. .. _VSCode: https://code.visualstudio.com/
  113. Additionally, CMake has builtin support for some IDEs:
  114. * :ref:`IDE Build Tool Generators`:
  115. Generate IDE native build systems such as Visual Studio or Xcode.
  116. * :ref:`Extra Generators`:
  117. Extend :ref:`Command-Line Build Tool Generators` to generate IDE
  118. project files that hook into the command-line build system.
  119. Superseded by the :manual:`File API <cmake-file-api(7)>`.