cmake-presets.7.rst 13.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
.. cmake-manual-description: CMakePresets.json

cmake-presets(7)
****************

.. only:: html

   .. contents::

Introduction
============

One problem that CMake users often face is sharing settings with other people
for common ways to configure a project. This may be done to support CI builds,
or for users who frequently use the same build. CMake supports two files,
``CMakePresets.json`` and ``CMakeUserPresets.json``, that allow users to
specify common configure options and share them with others.

``CMakePresets.json`` and ``CMakeUserPresets.json`` live in the project's root
directory. They both have exactly the same format, and both are optional
(though at least one must be present if ``--preset`` is specified.)
``CMakePresets.json`` is meant to save project-wide builds, while
``CMakeUserPresets.json`` is meant for developers to save their own local
builds. ``CMakePresets.json`` may be checked into a version control system, and
``CMakeUserPresets.json`` should NOT be checked in. For example, if a project
is using Git, ``CMakePresets.json`` may be tracked, and
``CMakeUserPresets.json`` should be added to the ``.gitignore``.

Format
======

32
The files are a JSON document with an object as the root:
33

34
35
.. literalinclude:: presets/example.json
  :language: json
36

37
The root object recognizes the following fields:
38

39
``version``
40

41
42
  A required integer representing the version of the JSON schema. Currently,
  the only supported version is 1.
43

44
``cmakeMinimumRequired``
45

46
47
  An optional object representing the minimum version of CMake needed to
  build this project. This object consists of the following fields:
48

49
  ``major``
50

51
    An optional integer representing the major version.
52

53
  ``minor``
54

55
    An optional integer representing the minor version.
56

57
  ``patch``
58

59
    An optional integer representing the patch version.
60

61
``vendor``
62

63
64
65
66
67
68
  An optional map containing vendor-specific information. CMake does not
  interpret the contents of this field except to verify that it is a map if
  it does exist. However, the keys should be a vendor-specific domain name
  followed by a ``/``-separated path. For example, the Example IDE 1.0 could
  use ``example.com/ExampleIDE/1.0``. The value of each field can be anything
  desired by the vendor, though will typically be a map.
69

70
``configurePresets``
71

72
73
74
75
76
77
78
  An optional array of `Configure Preset`_ objects.

Configure Preset
^^^^^^^^^^^^^^^^

Each entry of the ``configurePresets`` array is a JSON object
that may contain the following fields:
79

80
``name``
81

82
83
84
85
  A required string representing the machine-friendly name of the preset.
  This identifier is used in the ``--preset`` argument. There must not be
  two presets in the union of ``CMakePresets.json`` and
  ``CMakeUserPresets.json`` in the same directory with the same name.
86

87
``hidden``
88

89
90
91
92
93
94
  An optional boolean specifying whether or not a preset should be hidden.
  If a preset is hidden, it cannot be used in the ``--preset=`` argument,
  will not show up in the :manual:`CMake GUI <cmake-gui(1)>`, and does not
  have to have a valid ``generator`` or ``binaryDir``, even from
  inheritance. ``hidden`` presets are intended to be used as a base for
  other presets to inherit via the ``inherits`` field.
95

96
``inherits``
97

98
99
100
101
102
103
104
105
  An optional array of strings representing the names of presets to inherit
  from. The preset will inherit all of the fields from the ``inherits``
  presets by default (except ``name``, ``hidden``, ``inherits``,
  ``description``, and ``displayName``), but can override them as
  desired. If multiple ``inherits`` presets provide conflicting values for
  the same field, the earlier preset in the ``inherits`` list will be
  preferred. Presets in ``CMakePresets.json`` may not inherit from presets
  in ``CMakeUserPresets.json``.
106

107
108
  This field can also be a string, which is equivalent to an array
  containing one string.
109

110
``vendor``
111

112
113
114
115
116
117
  An optional map containing vendor-specific information. CMake does not
  interpret the contents of this field except to verify that it is a map
  if it does exist. However, it should follow the same conventions as the
  root-level ``vendor`` field. If vendors use their own per-preset
  ``vendor`` field, they should implement inheritance in a sensible manner
  when appropriate.
118

119
``displayName``
120

121
  An optional string with a human-friendly name of the preset.
122

123
``description``
124

125
  An optional string with a human-friendly description of the preset.
126

127
``generator``
128

129
130
131
  An optional string representing the generator to use for the preset. If
  ``generator`` is not specified, it must be inherited from the
  ``inherits`` preset (unless this preset is ``hidden``).
132

133
134
135
  Note that for Visual Studio generators, unlike in the command line ``-G``
  argument, you cannot include the platform name in the generator name. Use
  the ``architecture`` field instead.
136

137
``architecture``, ``toolset``
138

139
140
141
  Optional fields representing the platform and toolset, respectively, for
  generators that support them. Each may be either a string or an object
  with the following fields:
142

143
  ``value``
144

145
    An optional string representing the value.
146

147
  ``strategy``
148

149
150
    An optional string telling CMake how to handle the ``architecture`` or
    ``toolset`` field. Valid values are:
151

152
    ``"set"``
153

154
155
      Set the respective value. This will result in an error for generators
      that do not support the respective field.
156

157
    ``"external"``
158

159
160
161
162
163
164
      Do not set the value, even if the generator supports it. This is
      useful if, for example, a preset uses the Ninja generator, and an IDE
      knows how to set up the Visual C++ environment from the
      ``architecture`` and ``toolset`` fields. In that case, CMake will
      ignore the field, but the IDE can use them to set up the environment
      before invoking CMake.
165

166
``binaryDir``
167

168
169
170
171
172
  An optional string representing the path to the output binary directory.
  This field supports `macro expansion`_. If a relative path is specified,
  it is calculated relative to the source directory. If ``binaryDir`` is not
  specified, it must be inherited from the ``inherits`` preset (unless this
  preset is ``hidden``).
173

174
``cmakeExecutable``
175

176
177
178
  An optional string representing the path to the CMake executable to use
  for this preset. This is reserved for use by IDEs, and is not used by
  CMake itself. IDEs that use this field should expand any macros in it.
179

180
``cacheVariables``
181

182
183
184
185
186
  An optional map of cache variables. The key is the variable name (which
  may not be an empty string), and the value is either ``null``, a boolean
  (which is equivalent to a value of ``"TRUE"`` or ``"FALSE"`` and a type
  of ``BOOL``), a string representing the value of the variable (which
  supports `macro expansion`_), or an object with the following fields:
187

188
  ``type``
189

190
    An optional string representing the type of the variable.
191

192
  ``value``
193

194
195
196
    A required string or boolean representing the value of the variable.
    A boolean is equivalent to ``"TRUE"`` or ``"FALSE"``. This field
    supports `macro expansion`_.
197

198
199
200
201
202
203
  Cache variables are inherited through the ``inherits`` field, and the
  preset's variables will be the union of its own ``cacheVariables`` and
  the ``cacheVariables`` from all its parents. If multiple presets in this
  union define the same variable, the standard rules of ``inherits`` are
  applied. Setting a variable to ``null`` causes it to not be set, even if
  a value was inherited from another preset.
204

205
``environment``
206

207
208
209
210
211
212
213
214
  An optional map of environment variables. The key is the variable name
  (which may not be an empty string), and the value is either ``null`` or
  a string representing the value of the variable. Each variable is set
  regardless of whether or not a value was given to it by the process's
  environment. This field supports `macro expansion`_, and environment
  variables in this map may reference each other, and may be listed in any
  order, as long as such references do not cause a cycle (for example,
  if ``ENV_1`` is ``$env{ENV_2}``, ``ENV_2`` may not be ``$env{ENV_1}``.)
215

216
217
218
219
220
221
  Environment variables are inherited through the ``inherits`` field, and
  the preset's environment will be the union of its own ``environment`` and
  the ``environment`` from all its parents. If multiple presets in this
  union define the same variable, the standard rules of ``inherits`` are
  applied. Setting a variable to ``null`` causes it to not be set, even if
  a value was inherited from another preset.
222

223
``warnings``
224

225
226
  An optional object specifying the warnings to enable. The object may
  contain the following fields:
227

228
  ``dev``
229

230
231
232
    An optional boolean. Equivalent to passing ``-Wdev`` or ``-Wno-dev``
    on the command line. This may not be set to ``false`` if ``errors.dev``
    is set to ``true``.
233

234
  ``deprecated``
235

236
237
238
    An optional boolean. Equivalent to passing ``-Wdeprecated`` or
    ``-Wno-deprecated`` on the command line. This may not be set to
    ``false`` if ``errors.deprecated`` is set to ``true``.
239

240
  ``uninitialized``
241

242
243
    An optional boolean. Setting this to ``true`` is equivalent to passing
    ``--warn-uninitialized`` on the command line.
244

245
  ``unusedCli``
246

247
248
    An optional boolean. Setting this to ``false`` is equivalent to passing
    ``--no-warn-unused-cli`` on the command line.
249

250
  ``systemVars``
251

252
253
    An optional boolean. Setting this to ``true`` is equivalent to passing
    ``--check-system-vars`` on the command line.
254

255
``errors``
256

257
258
  An optional object specifying the errors to enable. The object may
  contain the following fields:
259

260
  ``dev``
261

262
263
264
    An optional boolean. Equivalent to passing ``-Werror=dev`` or
    ``-Wno-error=dev`` on the command line. This may not be set to ``true``
    if ``warnings.dev`` is set to ``false``.
265

266
  ``deprecated``
267

268
269
270
    An optional boolean. Equivalent to passing ``-Werror=deprecated`` or
    ``-Wno-error=deprecated`` on the command line. This may not be set to
    ``true`` if ``warnings.deprecated`` is set to ``false``.
271

272
``debug``
273

274
275
  An optional object specifying debug options. The object may contain the
  following fields:
276

277
  ``output``
278

279
280
    An optional boolean. Setting this to ``true`` is equivalent to passing
    ``--debug-output`` on the command line.
281

282
  ``tryCompile``
283

284
285
    An optional boolean. Setting this to ``true`` is equivalent to passing
    ``--debug-trycompile`` on the command line.
286

287
  ``find``
288

289
290
    An optional boolean. Setting this to ``true`` is equivalent to passing
    ``--debug-find`` on the command line.
291

292
293
294
Macro Expansion
^^^^^^^^^^^^^^^

295
296
297
298
299
300
301
As mentioned above, some fields support macro expansion. Macros are
recognized in the form ``$<macro-namespace>{<macro-name>}``. All macros are
evaluated in the context of the preset being used, even if the macro is in a
field that was inherited from another preset. For example, if the ``Base``
preset sets variable ``PRESET_NAME`` to ``${presetName}``, and the
``Derived`` preset inherits from ``Base``, ``PRESET_NAME`` will be set to
``Derived``.
302

303
304
305
306
It is an error to not put a closing brace at the end of a macro name. For
example, ``${sourceDir`` is invalid. A dollar sign (``$``) followed by
anything other than a left curly brace (``{``) with a possible namespace is
interpreted as a literal dollar sign.
307

308
Recognized macros include:
309

310
``${sourceDir}``
311

312
  Path to the project source directory.
313

314
``${sourceParentDir}``
315

316
  Path to the project source directory's parent directory.
317

318
``${sourceDirName}``
319

320
321
  The last filename component of ``${sourceDir}``. For example, if
  ``${sourceDir}`` is ``/path/to/source``, this would be ``source``.
322

323
``${presetName}``
324

325
  Name specified in the preset's ``name`` field.
326

327
``${generator}``
328

329
  Generator specified in the preset's ``generator`` field.
330

331
``${dollar}``
332

333
  A literal dollar sign (``$``).
334

335
``$env{<variable-name>}``
336

337
338
339
340
341
  Environment variable with name ``<variable-name>``. The variable name may
  not be an empty string. If the variable is defined in the ``environment``
  field, that value is used instead of the value from the parent environment.
  If the environment variable is not defined, this evaluates as an empty
  string.
342

343
344
345
346
  Note that while Windows environment variable names are case-insensitive,
  variable names within a preset are still case-sensitive. This may lead to
  unexpected results when using inconsistent casing. For best results, keep
  the casing of environment variable names consistent.
347

348
``$penv{<variable-name>}``
349

350
351
352
353
354
355
356
  Similar to ``$env{<variable-name>}``, except that the value only comes from
  the parent environment, and never from the ``environment`` field. This
  allows you to prepend or append values to existing environment variables.
  For example, setting ``PATH`` to ``/path/to/ninja/bin:$penv{PATH}`` will
  prepend ``/path/to/ninja/bin`` to the ``PATH`` environment variable. This
  is needed because ``$env{<variable-name>}`` does not allow circular
  references.
357

358
``$vendor{<macro-name>}``
359

360
361
362
363
  An extension point for vendors to insert their own macros. CMake will not
  be able to use presets which have a ``$vendor{<macro-name>}`` macro, and
  effectively ignores such presets. However, it will still be able to use
  other presets from the same file.
364

365
366
367
368
369
  CMake does not make any attempt to interpret ``$vendor{<macro-name>}``
  macros. However, to avoid name collisions, IDE vendors should prefix
  ``<macro-name>`` with a very short (preferably <= 4 characters) vendor
  identifier prefix, followed by a ``.``, followed by the macro name. For
  example, the Example IDE could have ``$vendor{xide.ideInstallDir}``.
370
371
372
373
374
375

Schema
======

:download:`This file </manual/presets/schema.json>` provides a machine-readable
JSON schema for the ``CMakePresets.json`` format.