4 Conditionally execute a group of commands.
13 elseif(<condition>) # optional block, can be repeated
15 else() # optional block
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``
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
32 .. _`Condition Syntax`:
37 The following syntax applies to the ``condition`` argument of
38 the ``if``, ``elseif`` and :command:`while` clauses.
40 Compound conditions are evaluated in the following order of precedence:
44 2. Unary tests such as `EXISTS`_, `COMMAND`_, and `DEFINED`_.
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`_.
52 4. Unary logical operator `NOT`_.
54 5. Binary logical operators `AND`_ and `OR`_, from left to right,
55 without any short-circuit.
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.
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
79 A quoted string always evaluates to false unless:
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
91 ``if(NOT <condition>)``
92 True if the condition is not true.
96 ``if(<cond1> AND <cond2>)``
97 True if both conditions would be considered true individually.
101 ``if(<cond1> OR <cond2>)``
102 True if either condition would be considered true individually.
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.
115 ``if(COMMAND command-name)``
116 True if the given name is a command, macro or function that can be
119 ``if(POLICY policy-id)``
120 True if the given name is an existing policy (of the form ``CMP<NNNN>``).
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
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.
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:
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})``.
149 .. versionadded:: 3.14
150 Added support for ``CACHE{<name>}`` variables.
152 ``if(<variable|string> IN_LIST <variable>)``
153 .. versionadded:: 3.3
154 True if the given element is contained in the named list variable.
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.
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.
176 ``if(IS_DIRECTORY path-to-directory)``
177 True if the given name is a directory. Behavior is well-defined only
180 ``if(IS_SYMLINK file-name)``
181 True if the given name is a symbolic link. Behavior is well-defined
184 ``if(IS_ABSOLUTE path)``
185 True if the given path is an absolute path. Note the following special
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 (``~``)
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.
205 .. versionadded:: 3.9
206 ``()`` groups are captured in :variable:`CMAKE_MATCH_<n>` variables.
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.
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.
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.
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.
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.
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.
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.
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.
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.
265 .. _STRGREATER_EQUAL:
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.
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.
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.
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.
299 .. _VERSION_LESS_EQUAL:
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.
308 .. _VERSION_GREATER_EQUAL:
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.
322 ``if(<variable|string> PATH_EQUAL <variable|string>)``
323 .. versionadded:: 3.24
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.
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
336 .. code-block:: cmake
339 if ("/a//b/c" PATH_EQUAL "/a/b/c")
343 # comparison is FALSE
344 if ("/a//b/c" STREQUAL "/a/b/c")
348 See :ref:`cmake_path(COMPARE) <Path COMPARE>` for more details.
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
359 .. code-block:: cmake
365 appears to the if command as
367 .. code-block:: cmake
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
375 .. code-block:: cmake
379 which is true because ``var2`` is defined to ``var1`` which is not a false
382 Automatic evaluation applies in the other cases whenever the
383 above-documented condition syntax accepts ``<variable|string>``:
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.
389 * If the left hand argument to ``MATCHES`` is missing it returns false
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.
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.
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
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.
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.
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`.
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>``.