if.rst 17 KB

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