1
0

if.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. if
  2. --
  3. Conditionally execute a group of commands.
  4. Synopsis
  5. ^^^^^^^^
  6. .. code-block:: cmake
  7. if(<condition>)
  8. <commands>
  9. elseif(<condition>) # optional block, can be repeated
  10. <commands>
  11. else() # optional block
  12. <commands>
  13. endif()
  14. Evaluates the ``condition`` argument of the ``if`` clause according to the
  15. `Condition syntax`_ described below. If the result is true, then the
  16. ``commands`` in the ``if`` block are executed.
  17. Otherwise, optional ``elseif`` blocks are processed in the same way.
  18. Finally, if no ``condition`` is true, ``commands`` in the optional ``else``
  19. block are executed.
  20. Per legacy, the :command:`else` and :command:`endif` commands admit
  21. an optional ``<condition>`` argument.
  22. If used, it must be a verbatim
  23. repeat of the argument of the opening
  24. ``if`` command.
  25. .. _`Condition Syntax`:
  26. Condition Syntax
  27. ^^^^^^^^^^^^^^^^
  28. The following syntax applies to the ``condition`` argument of
  29. the ``if``, ``elseif`` and :command:`while` clauses.
  30. Compound conditions are evaluated in the following order of precedence:
  31. Innermost parentheses are evaluated first. Next come unary tests such
  32. as `EXISTS`_, `COMMAND`_, and `DEFINED`_. Then binary tests such as
  33. `EQUAL`_, `LESS`_, `LESS_EQUAL`_, `GREATER`_, `GREATER_EQUAL`_,
  34. `STREQUAL`_, `STRLESS`_, `STRLESS_EQUAL`_, `STRGREATER`_,
  35. `STRGREATER_EQUAL`_, `VERSION_EQUAL`_, `VERSION_LESS`_,
  36. `VERSION_LESS_EQUAL`_, `VERSION_GREATER`_, `VERSION_GREATER_EQUAL`_,
  37. and `MATCHES`_. Then the boolean operators in the order `NOT`_, `AND`_,
  38. and finally `OR`_.
  39. Basic Expressions
  40. """""""""""""""""
  41. ``if(<constant>)``
  42. True if the constant is ``1``, ``ON``, ``YES``, ``TRUE``, ``Y``,
  43. or a non-zero number (including floating point numbers).
  44. False if the constant is ``0``, ``OFF``,
  45. ``NO``, ``FALSE``, ``N``, ``IGNORE``, ``NOTFOUND``, the empty string,
  46. or ends in the suffix ``-NOTFOUND``. Named boolean constants are
  47. case-insensitive. If the argument is not one of these specific
  48. constants, it is treated as a variable or string (see `Variable Expansion`_
  49. further below) and one of the following two forms applies.
  50. ``if(<variable>)``
  51. True if given a variable that is defined to a value that is not a false
  52. constant. False otherwise, including if the variable is undefined.
  53. Note that macro arguments are not variables.
  54. Environment variables also cannot be tested this way, e.g.
  55. ``if(ENV{some_var})`` will always evaluate to false.
  56. ``if(<string>)``
  57. A quoted string always evaluates to false unless:
  58. * The string's value is one of the true constants, or
  59. * Policy :policy:`CMP0054` is not set to ``NEW`` and the string's value
  60. happens to be a variable name that is affected by :policy:`CMP0054`'s
  61. behavior.
  62. Logic Operators
  63. """""""""""""""
  64. .. _NOT:
  65. ``if(NOT <condition>)``
  66. True if the condition is not true.
  67. .. _AND:
  68. ``if(<cond1> AND <cond2>)``
  69. True if both conditions would be considered true individually.
  70. .. _OR:
  71. ``if(<cond1> OR <cond2>)``
  72. True if either condition would be considered true individually.
  73. ``if((condition) AND (condition OR (condition)))``
  74. The conditions inside the parenthesis are evaluated first and then
  75. the remaining condition is evaluated as in the other examples.
  76. Where there are nested parenthesis the innermost are evaluated as part
  77. of evaluating the condition that contains them.
  78. Existence Checks
  79. """"""""""""""""
  80. .. _COMMAND:
  81. ``if(COMMAND command-name)``
  82. True if the given name is a command, macro or function that can be
  83. invoked.
  84. ``if(POLICY policy-id)``
  85. True if the given name is an existing policy (of the form ``CMP<NNNN>``).
  86. ``if(TARGET target-name)``
  87. True if the given name is an existing logical target name created
  88. by a call to the :command:`add_executable`, :command:`add_library`,
  89. or :command:`add_custom_target` command that has already been invoked
  90. (in any directory).
  91. ``if(TEST test-name)``
  92. .. versionadded:: 3.3
  93. True if the given name is an existing test name created by the
  94. :command:`add_test` command.
  95. .. _DEFINED:
  96. ``if(DEFINED <name>|CACHE{<name>}|ENV{<name>})``
  97. True if a variable, cache variable or environment variable
  98. with given ``<name>`` is defined. The value of the variable
  99. does not matter. Note the following caveats:
  100. * Macro arguments are not variables.
  101. * It is not possible to test directly whether a `<name>` is a non-cache
  102. variable. The expression ``if(DEFINED someName)`` will evaluate to true
  103. if either a cache or non-cache variable ``someName`` exists. In
  104. comparison, the expression ``if(DEFINED CACHE{someName})`` will only
  105. evaluate to true if a cache variable ``someName`` exists. Both expressions
  106. need to be tested if you need to know whether a non-cache variable exists:
  107. ``if(DEFINED someName AND NOT DEFINED CACHE{someName})``.
  108. .. versionadded:: 3.14
  109. Added support for ``CACHE{<name>}`` variables.
  110. ``if(<variable|string> IN_LIST <variable>)``
  111. .. versionadded:: 3.3
  112. True if the given element is contained in the named list variable.
  113. File Operations
  114. """""""""""""""
  115. .. _EXISTS:
  116. ``if(EXISTS path-to-file-or-directory)``
  117. True if the named file or directory exists. Behavior is well-defined
  118. only for explicit full paths (a leading ``~/`` is not expanded as
  119. a home directory and is considered a relative path).
  120. Resolves symbolic links, i.e. if the named file or directory is a
  121. symbolic link, returns true if the target of the symbolic link exists.
  122. ``if(file1 IS_NEWER_THAN file2)``
  123. True if ``file1`` is newer than ``file2`` or if one of the two files doesn't
  124. exist. Behavior is well-defined only for full paths. If the file
  125. time stamps are exactly the same, an ``IS_NEWER_THAN`` comparison returns
  126. true, so that any dependent build operations will occur in the event
  127. of a tie. This includes the case of passing the same file name for
  128. both file1 and file2.
  129. ``if(IS_DIRECTORY path-to-directory)``
  130. True if the given name is a directory. Behavior is well-defined only
  131. for full paths.
  132. ``if(IS_SYMLINK file-name)``
  133. True if the given name is a symbolic link. Behavior is well-defined
  134. only for full paths.
  135. ``if(IS_ABSOLUTE path)``
  136. True if the given path is an absolute path. Note the following special
  137. cases:
  138. * An empty ``path`` evaluates to false.
  139. * On Windows hosts, any ``path`` that begins with a drive letter and colon
  140. (e.g. ``C:``), a forward slash or a backslash will evaluate to true.
  141. This means a path like ``C:no\base\dir`` will evaluate to true, even
  142. though the non-drive part of the path is relative.
  143. * On non-Windows hosts, any ``path`` that begins with a tilde (``~``)
  144. evaluates to true.
  145. Comparisons
  146. """""""""""
  147. .. _MATCHES:
  148. ``if(<variable|string> MATCHES regex)``
  149. True if the given string or variable's value matches the given regular
  150. expression. See :ref:`Regex Specification` for regex format.
  151. .. versionadded:: 3.9
  152. ``()`` groups are captured in :variable:`CMAKE_MATCH_<n>` variables.
  153. .. _LESS:
  154. ``if(<variable|string> LESS <variable|string>)``
  155. True if the given string or variable's value is a valid number and less
  156. than that on the right.
  157. .. _GREATER:
  158. ``if(<variable|string> GREATER <variable|string>)``
  159. True if the given string or variable's value is a valid number and greater
  160. than that on the right.
  161. .. _EQUAL:
  162. ``if(<variable|string> EQUAL <variable|string>)``
  163. True if the given string or variable's value is a valid number and equal
  164. to that on the right.
  165. .. _LESS_EQUAL:
  166. ``if(<variable|string> LESS_EQUAL <variable|string>)``
  167. .. versionadded:: 3.7
  168. True if the given string or variable's value is a valid number and less
  169. than or equal to that on the right.
  170. .. _GREATER_EQUAL:
  171. ``if(<variable|string> GREATER_EQUAL <variable|string>)``
  172. .. versionadded:: 3.7
  173. True if the given string or variable's value is a valid number and greater
  174. than or equal to that on the right.
  175. .. _STRLESS:
  176. ``if(<variable|string> STRLESS <variable|string>)``
  177. True if the given string or variable's value is lexicographically less
  178. than the string or variable on the right.
  179. .. _STRGREATER:
  180. ``if(<variable|string> STRGREATER <variable|string>)``
  181. True if the given string or variable's value is lexicographically greater
  182. than the string or variable on the right.
  183. .. _STREQUAL:
  184. ``if(<variable|string> STREQUAL <variable|string>)``
  185. True if the given string or variable's value is lexicographically equal
  186. to the string or variable on the right.
  187. .. _STRLESS_EQUAL:
  188. ``if(<variable|string> STRLESS_EQUAL <variable|string>)``
  189. .. versionadded:: 3.7
  190. True if the given string or variable's value is lexicographically less
  191. than or equal to the string or variable on the right.
  192. .. _STRGREATER_EQUAL:
  193. ``if(<variable|string> STRGREATER_EQUAL <variable|string>)``
  194. .. versionadded:: 3.7
  195. True if the given string or variable's value is lexicographically greater
  196. than or equal to the string or variable on the right.
  197. Version Comparisons
  198. """""""""""""""""""
  199. .. _VERSION_LESS:
  200. ``if(<variable|string> VERSION_LESS <variable|string>)``
  201. Component-wise integer version number comparison (version format is
  202. ``major[.minor[.patch[.tweak]]]``, omitted components are treated as zero).
  203. Any non-integer version component or non-integer trailing part of a version
  204. component effectively truncates the string at that point.
  205. .. _VERSION_GREATER:
  206. ``if(<variable|string> VERSION_GREATER <variable|string>)``
  207. Component-wise integer version number comparison (version format is
  208. ``major[.minor[.patch[.tweak]]]``, omitted components are treated as zero).
  209. Any non-integer version component or non-integer trailing part of a version
  210. component effectively truncates the string at that point.
  211. .. _VERSION_EQUAL:
  212. ``if(<variable|string> VERSION_EQUAL <variable|string>)``
  213. Component-wise integer version number comparison (version format is
  214. ``major[.minor[.patch[.tweak]]]``, omitted components are treated as zero).
  215. Any non-integer version component or non-integer trailing part of a version
  216. component effectively truncates the string at that point.
  217. .. _VERSION_LESS_EQUAL:
  218. ``if(<variable|string> VERSION_LESS_EQUAL <variable|string>)``
  219. .. versionadded:: 3.7
  220. Component-wise integer version number comparison (version format is
  221. ``major[.minor[.patch[.tweak]]]``, omitted components are treated as zero).
  222. Any non-integer version component or non-integer trailing part of a version
  223. component effectively truncates the string at that point.
  224. .. _VERSION_GREATER_EQUAL:
  225. ``if(<variable|string> VERSION_GREATER_EQUAL <variable|string>)``
  226. .. versionadded:: 3.7
  227. Component-wise integer version number comparison (version format is
  228. ``major[.minor[.patch[.tweak]]]``, omitted components are treated as zero).
  229. Any non-integer version component or non-integer trailing part of a version
  230. component effectively truncates the string at that point.
  231. Variable Expansion
  232. ^^^^^^^^^^^^^^^^^^
  233. The if command was written very early in CMake's history, predating
  234. the ``${}`` variable evaluation syntax, and for convenience evaluates
  235. variables named by its arguments as shown in the above signatures.
  236. Note that normal variable evaluation with ``${}`` applies before the if
  237. command even receives the arguments. Therefore code like
  238. .. code-block:: cmake
  239. set(var1 OFF)
  240. set(var2 "var1")
  241. if(${var2})
  242. appears to the if command as
  243. .. code-block:: cmake
  244. if(var1)
  245. and is evaluated according to the ``if(<variable>)`` case documented
  246. above. The result is ``OFF`` which is false. However, if we remove the
  247. ``${}`` from the example then the command sees
  248. .. code-block:: cmake
  249. if(var2)
  250. which is true because ``var2`` is defined to ``var1`` which is not a false
  251. constant.
  252. Automatic evaluation applies in the other cases whenever the
  253. above-documented condition syntax accepts ``<variable|string>``:
  254. * The left hand argument to ``MATCHES`` is first checked to see if it is
  255. a defined variable, if so the variable's value is used, otherwise the
  256. original value is used.
  257. * If the left hand argument to ``MATCHES`` is missing it returns false
  258. without error
  259. * Both left and right hand arguments to ``LESS``, ``GREATER``, ``EQUAL``,
  260. ``LESS_EQUAL``, and ``GREATER_EQUAL``, are independently tested to see if
  261. they are defined variables, if so their defined values are used otherwise
  262. the original value is used.
  263. * Both left and right hand arguments to ``STRLESS``, ``STRGREATER``,
  264. ``STREQUAL``, ``STRLESS_EQUAL``, and ``STRGREATER_EQUAL`` are independently
  265. tested to see if they are defined variables, if so their defined values are
  266. used otherwise the original value is used.
  267. * Both left and right hand arguments to ``VERSION_LESS``,
  268. ``VERSION_GREATER``, ``VERSION_EQUAL``, ``VERSION_LESS_EQUAL``, and
  269. ``VERSION_GREATER_EQUAL`` are independently tested to see if they are defined
  270. variables, if so their defined values are used otherwise the original value
  271. is used.
  272. * The right hand argument to ``NOT`` is tested to see if it is a boolean
  273. constant, if so the value is used, otherwise it is assumed to be a
  274. variable and it is dereferenced.
  275. * The left and right hand arguments to ``AND`` and ``OR`` are independently
  276. tested to see if they are boolean constants, if so they are used as
  277. such, otherwise they are assumed to be variables and are dereferenced.
  278. .. versionchanged:: 3.1
  279. To prevent ambiguity, potential variable or keyword names can be
  280. specified in a :ref:`Quoted Argument` or a :ref:`Bracket Argument`.
  281. A quoted or bracketed variable or keyword will be interpreted as a
  282. string and not dereferenced or interpreted.
  283. See policy :policy:`CMP0054`.
  284. There is no automatic evaluation for environment or cache
  285. :ref:`Variable References`. Their values must be referenced as
  286. ``$ENV{<name>}`` or ``$CACHE{<name>}`` wherever the above-documented
  287. condition syntax accepts ``<variable|string>``.