301cdce030baee8c054ea1c65e9755e8cd0adec6
[platform/upstream/cmake.git] / Help / command / if.rst
1 if
2 --
3
4 Conditionally execute a group of commands.
5
6 Synopsis
7 ^^^^^^^^
8
9 .. code-block:: cmake
10
11   if(<condition>)
12     <commands>
13   elseif(<condition>) # optional block, can be repeated
14     <commands>
15   else()              # optional block
16     <commands>
17   endif()
18
19 Evaluates the ``condition`` argument of the ``if`` clause according to the
20 `Condition syntax`_ described below. If the result is true, then the
21 ``commands`` in the ``if`` block are executed.
22 Otherwise, optional ``elseif`` blocks are processed in the same way.
23 Finally, if no ``condition`` is true, ``commands`` in the optional ``else``
24 block are executed.
25
26 Per legacy, the :command:`else` and :command:`endif` commands admit
27 an optional ``<condition>`` argument.
28 If used, it must be a verbatim
29 repeat of the argument of the opening
30 ``if`` command.
31
32 .. _`Condition Syntax`:
33
34 Condition Syntax
35 ^^^^^^^^^^^^^^^^
36
37 The following syntax applies to the ``condition`` argument of
38 the ``if``, ``elseif`` and :command:`while` clauses.
39
40 Compound conditions are evaluated in the following order of precedence:
41
42 1. Parentheses.
43
44 2. Unary tests such as `EXISTS`_, `COMMAND`_, and `DEFINED`_.
45
46 3. Binary tests such as `EQUAL`_, `LESS`_, `LESS_EQUAL`_, `GREATER`_,
47    `GREATER_EQUAL`_, `STREQUAL`_, `STRLESS`_, `STRLESS_EQUAL`_,
48    `STRGREATER`_, `STRGREATER_EQUAL`_, `VERSION_EQUAL`_, `VERSION_LESS`_,
49    `VERSION_LESS_EQUAL`_, `VERSION_GREATER`_, `VERSION_GREATER_EQUAL`_,
50    `PATH_EQUAL`_, and `MATCHES`_.
51
52 4. Unary logical operator `NOT`_.
53
54 5. Binary logical operators `AND`_ and `OR`_, from left to right,
55    without any short-circuit.
56
57 Basic Expressions
58 """""""""""""""""
59
60 ``if(<constant>)``
61  True if the constant is ``1``, ``ON``, ``YES``, ``TRUE``, ``Y``,
62  or a non-zero number (including floating point numbers).
63  False if the constant is ``0``, ``OFF``,
64  ``NO``, ``FALSE``, ``N``, ``IGNORE``, ``NOTFOUND``, the empty string,
65  or ends in the suffix ``-NOTFOUND``.  Named boolean constants are
66  case-insensitive.  If the argument is not one of these specific
67  constants, it is treated as a variable or string (see `Variable Expansion`_
68  further below) and one of the following two forms applies.
69
70 ``if(<variable>)``
71  True if given a variable that is defined to a value that is not a false
72  constant.  False otherwise, including if the variable is undefined.
73  Note that macro arguments are not variables.
74  :ref:`Environment Variables <CMake Language Environment Variables>` also
75  cannot be tested this way, e.g. ``if(ENV{some_var})`` will always evaluate
76  to false.
77
78 ``if(<string>)``
79  A quoted string always evaluates to false unless:
80
81  * The string's value is one of the true constants, or
82  * Policy :policy:`CMP0054` is not set to ``NEW`` and the string's value
83    happens to be a variable name that is affected by :policy:`CMP0054`'s
84    behavior.
85
86 Logic Operators
87 """""""""""""""
88
89 .. _NOT:
90
91 ``if(NOT <condition>)``
92  True if the condition is not true.
93
94 .. _AND:
95
96 ``if(<cond1> AND <cond2>)``
97  True if both conditions would be considered true individually.
98
99 .. _OR:
100
101 ``if(<cond1> OR <cond2>)``
102  True if either condition would be considered true individually.
103
104 ``if((condition) AND (condition OR (condition)))``
105  The conditions inside the parenthesis are evaluated first and then
106  the remaining condition is evaluated as in the other examples.
107  Where there are nested parenthesis the innermost are evaluated as part
108  of evaluating the condition that contains them.
109
110 Existence Checks
111 """"""""""""""""
112
113 .. _COMMAND:
114
115 ``if(COMMAND command-name)``
116  True if the given name is a command, macro or function that can be
117  invoked.
118
119 ``if(POLICY policy-id)``
120  True if the given name is an existing policy (of the form ``CMP<NNNN>``).
121
122 ``if(TARGET target-name)``
123  True if the given name is an existing logical target name created
124  by a call to the :command:`add_executable`, :command:`add_library`,
125  or :command:`add_custom_target` command that has already been invoked
126  (in any directory).
127
128 ``if(TEST test-name)``
129  .. versionadded:: 3.3
130   True if the given name is an existing test name created by the
131   :command:`add_test` command.
132
133 .. _DEFINED:
134
135 ``if(DEFINED <name>|CACHE{<name>}|ENV{<name>})``
136  True if a variable, cache variable or environment variable
137  with given ``<name>`` is defined. The value of the variable
138  does not matter. Note the following caveats:
139
140  * Macro arguments are not variables.
141  * It is not possible to test directly whether a `<name>` is a non-cache
142    variable.  The expression ``if(DEFINED someName)`` will evaluate to true
143    if either a cache or non-cache variable ``someName`` exists.  In
144    comparison, the expression ``if(DEFINED CACHE{someName})`` will only
145    evaluate to true if a cache variable ``someName`` exists.  Both expressions
146    need to be tested if you need to know whether a non-cache variable exists:
147    ``if(DEFINED someName AND NOT DEFINED CACHE{someName})``.
148
149  .. versionadded:: 3.14
150   Added support for ``CACHE{<name>}`` variables.
151
152 ``if(<variable|string> IN_LIST <variable>)``
153  .. versionadded:: 3.3
154   True if the given element is contained in the named list variable.
155
156 File Operations
157 """""""""""""""
158
159 .. _EXISTS:
160
161 ``if(EXISTS path-to-file-or-directory)``
162  True if the named file or directory exists.  Behavior is well-defined
163  only for explicit full paths (a leading ``~/`` is not expanded as
164  a home directory and is considered a relative path).
165  Resolves symbolic links, i.e. if the named file or directory is a
166  symbolic link, returns true if the target of the symbolic link exists.
167
168 ``if(file1 IS_NEWER_THAN file2)``
169  True if ``file1`` is newer than ``file2`` or if one of the two files doesn't
170  exist.  Behavior is well-defined only for full paths.  If the file
171  time stamps are exactly the same, an ``IS_NEWER_THAN`` comparison returns
172  true, so that any dependent build operations will occur in the event
173  of a tie.  This includes the case of passing the same file name for
174  both file1 and file2.
175
176 ``if(IS_DIRECTORY path-to-directory)``
177  True if the given name is a directory.  Behavior is well-defined only
178  for full paths.
179
180 ``if(IS_SYMLINK file-name)``
181  True if the given name is a symbolic link.  Behavior is well-defined
182  only for full paths.
183
184 ``if(IS_ABSOLUTE path)``
185  True if the given path is an absolute path.  Note the following special
186  cases:
187
188  * An empty ``path`` evaluates to false.
189  * On Windows hosts, any ``path`` that begins with a drive letter and colon
190    (e.g. ``C:``), a forward slash or a backslash will evaluate to true.
191    This means a path like ``C:no\base\dir`` will evaluate to true, even
192    though the non-drive part of the path is relative.
193  * On non-Windows hosts, any ``path`` that begins with a tilde (``~``)
194    evaluates to true.
195
196 Comparisons
197 """""""""""
198
199 .. _MATCHES:
200
201 ``if(<variable|string> MATCHES regex)``
202  True if the given string or variable's value matches the given regular
203  expression.  See :ref:`Regex Specification` for regex format.
204
205  .. versionadded:: 3.9
206   ``()`` groups are captured in :variable:`CMAKE_MATCH_<n>` variables.
207
208 .. _LESS:
209
210 ``if(<variable|string> LESS <variable|string>)``
211  True if the given string or variable's value is a valid number and less
212  than that on the right.
213
214 .. _GREATER:
215
216 ``if(<variable|string> GREATER <variable|string>)``
217  True if the given string or variable's value is a valid number and greater
218  than that on the right.
219
220 .. _EQUAL:
221
222 ``if(<variable|string> EQUAL <variable|string>)``
223  True if the given string or variable's value is a valid number and equal
224  to that on the right.
225
226 .. _LESS_EQUAL:
227
228 ``if(<variable|string> LESS_EQUAL <variable|string>)``
229  .. versionadded:: 3.7
230   True if the given string or variable's value is a valid number and less
231   than or equal to that on the right.
232
233 .. _GREATER_EQUAL:
234
235 ``if(<variable|string> GREATER_EQUAL <variable|string>)``
236  .. versionadded:: 3.7
237   True if the given string or variable's value is a valid number and greater
238   than or equal to that on the right.
239
240 .. _STRLESS:
241
242 ``if(<variable|string> STRLESS <variable|string>)``
243  True if the given string or variable's value is lexicographically less
244  than the string or variable on the right.
245
246 .. _STRGREATER:
247
248 ``if(<variable|string> STRGREATER <variable|string>)``
249  True if the given string or variable's value is lexicographically greater
250  than the string or variable on the right.
251
252 .. _STREQUAL:
253
254 ``if(<variable|string> STREQUAL <variable|string>)``
255  True if the given string or variable's value is lexicographically equal
256  to the string or variable on the right.
257
258 .. _STRLESS_EQUAL:
259
260 ``if(<variable|string> STRLESS_EQUAL <variable|string>)``
261  .. versionadded:: 3.7
262   True if the given string or variable's value is lexicographically less
263   than or equal to the string or variable on the right.
264
265 .. _STRGREATER_EQUAL:
266
267 ``if(<variable|string> STRGREATER_EQUAL <variable|string>)``
268  .. versionadded:: 3.7
269   True if the given string or variable's value is lexicographically greater
270   than or equal to the string or variable on the right.
271
272 Version Comparisons
273 """""""""""""""""""
274
275 .. _VERSION_LESS:
276
277 ``if(<variable|string> VERSION_LESS <variable|string>)``
278  Component-wise integer version number comparison (version format is
279  ``major[.minor[.patch[.tweak]]]``, omitted components are treated as zero).
280  Any non-integer version component or non-integer trailing part of a version
281  component effectively truncates the string at that point.
282
283 .. _VERSION_GREATER:
284
285 ``if(<variable|string> VERSION_GREATER <variable|string>)``
286  Component-wise integer version number comparison (version format is
287  ``major[.minor[.patch[.tweak]]]``, omitted components are treated as zero).
288  Any non-integer version component or non-integer trailing part of a version
289  component effectively truncates the string at that point.
290
291 .. _VERSION_EQUAL:
292
293 ``if(<variable|string> VERSION_EQUAL <variable|string>)``
294  Component-wise integer version number comparison (version format is
295  ``major[.minor[.patch[.tweak]]]``, omitted components are treated as zero).
296  Any non-integer version component or non-integer trailing part of a version
297  component effectively truncates the string at that point.
298
299 .. _VERSION_LESS_EQUAL:
300
301 ``if(<variable|string> VERSION_LESS_EQUAL <variable|string>)``
302  .. versionadded:: 3.7
303   Component-wise integer version number comparison (version format is
304   ``major[.minor[.patch[.tweak]]]``, omitted components are treated as zero).
305   Any non-integer version component or non-integer trailing part of a version
306   component effectively truncates the string at that point.
307
308 .. _VERSION_GREATER_EQUAL:
309
310 ``if(<variable|string> VERSION_GREATER_EQUAL <variable|string>)``
311  .. versionadded:: 3.7
312   Component-wise integer version number comparison (version format is
313   ``major[.minor[.patch[.tweak]]]``, omitted components are treated as zero).
314   Any non-integer version component or non-integer trailing part of a version
315   component effectively truncates the string at that point.
316
317 Path Comparisons
318 """"""""""""""""
319
320 .. _PATH_EQUAL:
321
322 ``if(<variable|string> PATH_EQUAL <variable|string>)``
323   .. versionadded:: 3.24
324
325   Compares the two paths component-by-component.  Only if every component of
326   both paths match will the two paths compare equal.  Multiple path separators
327   are effectively collapsed into a single separator, but note that backslashes
328   are not converted to forward slashes.  No other
329   :ref:`path normalization <Normalization>` is performed.
330
331   Component-wise comparison is superior to string-based comparison due to the
332   handling of multiple path separators.  In the following example, the
333   expression evaluates to true using ``PATH_EQUAL``, but false with
334   ``STREQUAL``:
335
336   .. code-block:: cmake
337
338     # comparison is TRUE
339     if ("/a//b/c" PATH_EQUAL "/a/b/c")
340        ...
341     endif()
342
343     # comparison is FALSE
344     if ("/a//b/c" STREQUAL "/a/b/c")
345        ...
346     endif()
347
348   See :ref:`cmake_path(COMPARE) <Path COMPARE>` for more details.
349
350 Variable Expansion
351 ^^^^^^^^^^^^^^^^^^
352
353 The if command was written very early in CMake's history, predating
354 the ``${}`` variable evaluation syntax, and for convenience evaluates
355 variables named by its arguments as shown in the above signatures.
356 Note that normal variable evaluation with ``${}`` applies before the if
357 command even receives the arguments.  Therefore code like
358
359 .. code-block:: cmake
360
361  set(var1 OFF)
362  set(var2 "var1")
363  if(${var2})
364
365 appears to the if command as
366
367 .. code-block:: cmake
368
369   if(var1)
370
371 and is evaluated according to the ``if(<variable>)`` case documented
372 above.  The result is ``OFF`` which is false.  However, if we remove the
373 ``${}`` from the example then the command sees
374
375 .. code-block:: cmake
376
377   if(var2)
378
379 which is true because ``var2`` is defined to ``var1`` which is not a false
380 constant.
381
382 Automatic evaluation applies in the other cases whenever the
383 above-documented condition syntax accepts ``<variable|string>``:
384
385 * The left hand argument to ``MATCHES`` is first checked to see if it is
386   a defined variable, if so the variable's value is used, otherwise the
387   original value is used.
388
389 * If the left hand argument to ``MATCHES`` is missing it returns false
390   without error
391
392 * Both left and right hand arguments to ``LESS``, ``GREATER``, ``EQUAL``,
393   ``LESS_EQUAL``, and ``GREATER_EQUAL``, are independently tested to see if
394   they are defined variables, if so their defined values are used otherwise
395   the original value is used.
396
397 * Both left and right hand arguments to ``STRLESS``, ``STRGREATER``,
398   ``STREQUAL``, ``STRLESS_EQUAL``, and ``STRGREATER_EQUAL`` are independently
399   tested to see if they are defined variables, if so their defined values are
400   used otherwise the original value is used.
401
402 * Both left and right hand arguments to ``VERSION_LESS``,
403   ``VERSION_GREATER``, ``VERSION_EQUAL``, ``VERSION_LESS_EQUAL``, and
404   ``VERSION_GREATER_EQUAL`` are independently tested to see if they are defined
405   variables, if so their defined values are used otherwise the original value
406   is used.
407
408 * The right hand argument to ``NOT`` is tested to see if it is a boolean
409   constant, if so the value is used, otherwise it is assumed to be a
410   variable and it is dereferenced.
411
412 * The left and right hand arguments to ``AND`` and ``OR`` are independently
413   tested to see if they are boolean constants, if so they are used as
414   such, otherwise they are assumed to be variables and are dereferenced.
415
416 .. versionchanged:: 3.1
417   To prevent ambiguity, potential variable or keyword names can be
418   specified in a :ref:`Quoted Argument` or a :ref:`Bracket Argument`.
419   A quoted or bracketed variable or keyword will be interpreted as a
420   string and not dereferenced or interpreted.
421   See policy :policy:`CMP0054`.
422
423 There is no automatic evaluation for environment or cache
424 :ref:`Variable References`.  Their values must be referenced as
425 ``$ENV{<name>}`` or ``$CACHE{<name>}`` wherever the above-documented
426 condition syntax accepts ``<variable|string>``.