cmake-presets.7.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. .. cmake-manual-description: CMakePresets.json
  2. cmake-presets(7)
  3. ****************
  4. .. only:: html
  5. .. contents::
  6. Introduction
  7. ============
  8. One problem that CMake users often face is sharing settings with other people
  9. for common ways to configure a project. This may be done to support CI builds,
  10. or for users who frequently use the same build. CMake supports two files,
  11. ``CMakePresets.json`` and ``CMakeUserPresets.json``, that allow users to
  12. specify common configure options and share them with others.
  13. ``CMakePresets.json`` and ``CMakeUserPresets.json`` live in the project's root
  14. directory. They both have exactly the same format, and both are optional
  15. (though at least one must be present if ``--preset`` is specified.)
  16. ``CMakePresets.json`` is meant to save project-wide builds, while
  17. ``CMakeUserPresets.json`` is meant for developers to save their own local
  18. builds. ``CMakePresets.json`` may be checked into a version control system, and
  19. ``CMakeUserPresets.json`` should NOT be checked in. For example, if a project
  20. is using Git, ``CMakePresets.json`` may be tracked, and
  21. ``CMakeUserPresets.json`` should be added to the ``.gitignore``.
  22. Format
  23. ======
  24. The files are a JSON document with an object as the root:
  25. .. literalinclude:: presets/example.json
  26. :language: json
  27. The root object recognizes the following fields:
  28. ``version``
  29. A required integer representing the version of the JSON schema. Currently,
  30. the only supported version is 1.
  31. ``cmakeMinimumRequired``
  32. An optional object representing the minimum version of CMake needed to
  33. build this project. This object consists of the following fields:
  34. ``major``
  35. An optional integer representing the major version.
  36. ``minor``
  37. An optional integer representing the minor version.
  38. ``patch``
  39. An optional integer representing the patch version.
  40. ``vendor``
  41. An optional map containing vendor-specific information. CMake does not
  42. interpret the contents of this field except to verify that it is a map if
  43. it does exist. However, the keys should be a vendor-specific domain name
  44. followed by a ``/``-separated path. For example, the Example IDE 1.0 could
  45. use ``example.com/ExampleIDE/1.0``. The value of each field can be anything
  46. desired by the vendor, though will typically be a map. For example:
  47. .. code-block:: json
  48. {
  49. "version": 1,
  50. "vendor": {
  51. "example.com/ExampleIDE/1.0": {
  52. "autoFormat": true
  53. }
  54. },
  55. "configurePresets": []
  56. }
  57. ``configurePresets``
  58. An optional array of configure preset objects. Each preset may contain the
  59. following fields:
  60. ``name``
  61. A required string representing the machine-friendly name of the preset.
  62. This identifier is used in the ``--preset`` argument. There must not be
  63. two presets in the union of ``CMakePresets.json`` and
  64. ``CMakeUserPresets.json`` in the same directory with the same name.
  65. ``hidden``
  66. An optional boolean specifying whether or not a preset should be hidden.
  67. If a preset is hidden, it cannot be used in the ``--preset=`` argument,
  68. will not show up in the :manual:`CMake GUI <cmake-gui(1)>`, and does not
  69. have to have a valid ``generator`` or ``binaryDir``, even from
  70. inheritance. ``hidden`` presets are intended to be used as a base for
  71. other presets to inherit via the ``inherits`` field.
  72. ``inherits``
  73. An optional array of strings representing the names of presets to inherit
  74. from. The preset will inherit all of the fields from the ``inherits``
  75. presets by default (except ``name``, ``hidden``, ``inherits``,
  76. ``description``, and ``longDescription``), but can override them as
  77. desired. If multiple ``inherits`` presets provide conflicting values for
  78. the same field, the earlier preset in the ``inherits`` list will be
  79. preferred. Presets in ``CMakePresets.json`` may not inherit from presets
  80. in ``CMakeUserPresets.json``.
  81. This field can also be a string, which is equivalent to an array
  82. containing one string.
  83. ``vendor``
  84. An optional map containing vendor-specific information. CMake does not
  85. interpret the contents of this field except to verify that it is a map
  86. if it does exist. However, it should follow the same conventions as the
  87. root-level ``vendor`` field. If vendors use their own per-preset
  88. ``vendor`` field, they should implement inheritance in a sensible manner
  89. when appropriate.
  90. ``displayName``
  91. An optional string with a human-friendly name of the preset.
  92. ``description``
  93. An optional string with a human-friendly description of the preset.
  94. ``generator``
  95. An optional string representing the generator to use for the preset. If
  96. ``generator`` is not specified, it must be inherited from the
  97. ``inherits`` preset (unless this preset is ``hidden``).
  98. Note that for Visual Studio generators, unlike in the command line ``-G``
  99. argument, you cannot include the platform name in the generator name. Use
  100. the ``architecture`` field instead.
  101. ``architecture``
  102. An optional string representing the platform name to use for Visual
  103. Studio generators.
  104. ``toolset``
  105. An optional string representing the toolset name to use for Visual Studio
  106. generators.
  107. ``cmakeGeneratorConfig``
  108. An optional string telling CMake how to handle the ``architecture`` and
  109. ``toolset`` fields. Valid values are:
  110. ``"default"``
  111. Set the platform and toolset using the ``architecture`` and ``toolset``
  112. fields respectively. On non-Visual Studio generators, this will result
  113. in an error if ``architecture`` or ``toolset`` are set.
  114. ``"ignore"``
  115. Do not set the platform or toolset at all, even on Visual Studio
  116. generators. This is useful if, for example, a preset uses the Ninja
  117. generator, and an IDE knows how to set up the Visual C++ environment
  118. from the ``architecture`` and ``toolset`` fields. In that case, CMake
  119. will ignore ``architecture`` and ``toolset``, but the IDE can use them
  120. to set up the environment before invoking CMake.
  121. ``binaryDir``
  122. An optional string representing the path to the output binary directory.
  123. This field supports macro expansion. If a relative path is specified, it
  124. is calculated relative to the source directory. If ``binaryDir`` is not
  125. specified, it must be inherited from the ``inherits`` preset (unless this
  126. preset is ``hidden``).
  127. ``cmakeExecutable``
  128. An optional string representing the path to the CMake executable to use
  129. for this preset. This is reserved for use by IDEs, and is not used by
  130. CMake itself. IDEs that use this field should expand any macros in it.
  131. ``cacheVariables``
  132. An optional map of cache variables. The key is the variable name (which
  133. may not be an empty string), and the value is either ``null``, a string
  134. representing the value of the variable (which supports macro expansion),
  135. or an object with the following fields:
  136. ``type``
  137. An optional string representing the type of the variable.
  138. ``value``
  139. A required string representing the value of the variable. This field
  140. supports macro expansion.
  141. Cache variables are inherited through the ``inherits`` field, and the
  142. preset's variables will be the union of its own ``cacheVariables`` and
  143. the ``cacheVariables`` from all its parents. If multiple presets in this
  144. union define the same variable, the standard rules of ``inherits`` are
  145. applied. Setting a variable to ``null`` causes it to not be set, even if
  146. a value was inherited from another preset.
  147. ``environment``
  148. An optional map of environment variables. The key is the variable name
  149. (which may not be an empty string), and the value is either ``null`` or
  150. a string representing the value of the variable. Each variable is set
  151. regardless of whether or not a value was given to it by the process's
  152. environment. This field supports macro expansion, and environment
  153. variables in this map may reference each other, and may be listed in any
  154. order, as long as such references do not cause a cycle (for example,
  155. if ``ENV_1`` is ``$env{ENV_2}``, ``ENV_2`` may not be ``$env{ENV_1}``.)
  156. Environment variables are inherited through the ``inherits`` field, and
  157. the preset's environment will be the union of its own ``environment`` and
  158. the ``environment`` from all its parents. If multiple presets in this
  159. union define the same variable, the standard rules of ``inherits`` are
  160. applied. Setting a variable to ``null`` causes it to not be set, even if
  161. a value was inherited from another preset.
  162. ``warnings``
  163. An optional object specifying warnings. The object may contain the
  164. following fields:
  165. ``dev``
  166. An optional boolean. Equivalent to passing ``-Wdev`` or ``-Wno-dev``
  167. on the command line. This may not be set to ``false`` if ``errors.dev``
  168. is set to ``true``.
  169. ``deprecated``
  170. An optional boolean. Equivalent to passing ``-Wdeprecated`` or
  171. ``-Wno-deprecated`` on the command line. This may not be set to
  172. ``false`` if ``errors.deprecated`` is set to ``true``.
  173. ``uninitialized``
  174. An optional boolean. Setting this to ``true`` is equivalent to passing
  175. ``--warn-uninitialized`` on the command line.
  176. ``unusedCli``
  177. An optional boolean. Setting this to ``false`` is equivalent to passing
  178. ``--no-warn-unused-cli`` on the command line.
  179. ``systemVars``
  180. An optional boolean. Setting this to ``true`` is equivalent to passing
  181. ``--check-system-vars`` on the command line.
  182. ``errors``
  183. An optional object specifying errors. The object may contain the
  184. following fields:
  185. ``dev``
  186. An optional boolean. Equivalent to passing ``-Werror=dev`` or
  187. ``-Wno-error=dev`` on the command line. This may not be set to ``true``
  188. if ``warnings.dev`` is set to ``false``.
  189. ``deprecated``
  190. An optional boolean. Equivalent to passing ``-Werror=deprecated`` or
  191. ``-Wno-error=deprecated`` on the command line. This may not be set to
  192. ``true`` if ``warnings.deprecated`` is set to ``false``.
  193. As mentioned above, some fields support macro expansion. Macros are
  194. recognized in the form ``$<macro-namespace>{<macro-name>}``. All macros are
  195. evaluated in the context of the preset being used, even if the macro is in a
  196. field that was inherited from another preset. For example, if the ``Base``
  197. preset sets variable ``PRESET_NAME`` to ``${presetName}``, and the
  198. ``Derived`` preset inherits from ``Base``, ``PRESET_NAME`` will be set to
  199. ``Derived``.
  200. It is an error to not put a closing brace at the end of a macro name. For
  201. example, ``${sourceDir`` is invalid. A dollar sign (``$``) followed by
  202. anything other than a left curly brace (``{``) with a possible namespace is
  203. interpreted as a literal dollar sign.
  204. Recognized macros include:
  205. ``${sourceDir}``
  206. Path to the project source directory.
  207. ``${sourceParentDir}``
  208. Path to the project source directory's parent directory.
  209. ``${presetName}``
  210. Name specified in the preset's ``name`` field.
  211. ``${generator}``
  212. Generator specified in the preset's ``generator`` field.
  213. ``${dollar}``
  214. A literal dollar sign (``$``).
  215. ``$env{<variable-name>}``
  216. Environment variable with name ``<variable-name>``. The variable name may
  217. not be an empty string. If the variable is defined in the ``environment``
  218. field, that value is used instead of the value from the parent environment.
  219. If the environment variable is not defined, this evaluates as an empty
  220. string.
  221. Note that while Windows environment variable names are case-insensitive,
  222. variable names within a preset are still case-sensitive. This may lead to
  223. unexpected results when using inconsistent casing. For best results, keep
  224. the casing of environment variable names consistent.
  225. ``$penv{<variable-name>}``
  226. Similar to ``$env{<variable-name>}``, except that the value only comes from
  227. the parent environment, and never from the ``environment`` field. This
  228. allows you to prepend or append values to existing environment variables.
  229. For example, setting ``PATH`` to ``/path/to/ninja/bin:$penv{PATH}`` will
  230. prepend ``/path/to/ninja/bin`` to the ``PATH`` environment variable. This
  231. is needed because ``$env{<variable-name>}`` does not allow circular
  232. references.
  233. ``$vendor{<macro-name>}``
  234. An extension point for vendors to insert their own macros. CMake will not
  235. be able to use presets which have a ``$vendor{<macro-name>}`` macro, and
  236. effectively ignores such presets. However, it will still be able to use
  237. other presets from the same file.
  238. CMake does not make any attempt to interpret ``$vendor{<macro-name>}``
  239. macros. However, to avoid name collisions, IDE vendors should prefix
  240. ``<macro-name>`` with a very short (preferably <= 4 characters) vendor
  241. identifier prefix, followed by a ``.``, followed by the macro name. For
  242. example, the Example IDE could have ``$vendor{xide.ideInstallDir}``.