cmake-presets.7.rst 14 KB

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