if.rst 17 KB

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