if.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. * in CMake versions prior to 4.0, policy :policy:`CMP0054` is not set
  68. to ``NEW`` and the string's value happens to be a variable name that
  69. is affected by :policy:`CMP0054`'s 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:: 2.6
  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. Lexicographically compares two CMake paths component-by-component without
  273. accessing the filesystem. Only if every component of both paths match will
  274. the two paths compare equal. Multiple path separators are effectively
  275. collapsed into a single separator, but note that backslashes are not
  276. converted to forward slashes.
  277. No other :ref:`path normalization <Normalization>` is performed.
  278. Trailing slashes are preserved, thus ``/a/b`` and ``/a/b/`` are not equal.
  279. Component-wise comparison is superior to string-based comparison due to the
  280. handling of multiple path separators. In the following example, the
  281. expression evaluates to true using ``PATH_EQUAL``, but false with
  282. ``STREQUAL``:
  283. .. code-block:: cmake
  284. # comparison is TRUE
  285. if ("/a//b/c" PATH_EQUAL "/a/b/c")
  286. ...
  287. endif()
  288. # comparison is FALSE
  289. if ("/a//b/c" STREQUAL "/a/b/c")
  290. ...
  291. endif()
  292. See :ref:`cmake_path(COMPARE) <Path Comparison>` for more details.
  293. Variable Expansion
  294. ^^^^^^^^^^^^^^^^^^
  295. The if command was written very early in CMake's history, predating
  296. the ``${}`` variable evaluation syntax, and for convenience evaluates
  297. variables named by its arguments as shown in the above signatures.
  298. Note that normal variable evaluation with ``${}`` applies before the if
  299. command even receives the arguments. Therefore code like
  300. .. code-block:: cmake
  301. set(var1 OFF)
  302. set(var2 "var1")
  303. if(${var2})
  304. appears to the if command as
  305. .. code-block:: cmake
  306. if(var1)
  307. and is evaluated according to the ``if(<variable>)`` case documented
  308. above. The result is ``OFF`` which is false. However, if we remove the
  309. ``${}`` from the example then the command sees
  310. .. code-block:: cmake
  311. if(var2)
  312. which is true because ``var2`` is defined to ``var1`` which is not a false
  313. constant.
  314. Automatic evaluation applies in the other cases whenever the
  315. above-documented condition syntax accepts ``<variable|string>``:
  316. * The left hand argument to `MATCHES`_ is first checked to see if it is
  317. a defined variable. If so, the variable's value is used, otherwise the
  318. original value is used.
  319. * If the left hand argument to `MATCHES`_ is missing it returns false
  320. without error
  321. * Both left and right hand arguments to `LESS`_, `GREATER`_, `EQUAL`_,
  322. `LESS_EQUAL`_, and `GREATER_EQUAL`_, are independently tested to see if
  323. they are defined variables. If so, their defined values are used otherwise
  324. the original value is used.
  325. * Both left and right hand arguments to `STRLESS`_, `STRGREATER`_,
  326. `STREQUAL`_, `STRLESS_EQUAL`_, and `STRGREATER_EQUAL`_ are independently
  327. tested to see if they are defined variables. If so, their defined values are
  328. used otherwise the original value is used.
  329. * Both left and right hand arguments to `VERSION_LESS`_,
  330. `VERSION_GREATER`_, `VERSION_EQUAL`_, `VERSION_LESS_EQUAL`_, and
  331. `VERSION_GREATER_EQUAL`_ are independently tested to see if they are defined
  332. variables. If so, their defined values are used otherwise the original value
  333. is used.
  334. * The left hand argument to `IN_LIST`_ is tested to see if it is a defined
  335. variable. If so, the variable's value is used, otherwise the original
  336. value is used.
  337. * The right hand argument to `NOT`_ is tested to see if it is a boolean
  338. constant. If so, the value is used, otherwise it is assumed to be a
  339. variable and it is dereferenced.
  340. * The left and right hand arguments to `AND`_ and `OR`_ are independently
  341. tested to see if they are boolean constants. If so, they are used as
  342. such, otherwise they are assumed to be variables and are dereferenced.
  343. .. versionchanged:: 3.1
  344. To prevent ambiguity, potential variable or keyword names can be
  345. specified in a :ref:`Quoted Argument` or a :ref:`Bracket Argument`.
  346. A quoted or bracketed variable or keyword will be interpreted as a
  347. string and not dereferenced or interpreted.
  348. See policy :policy:`CMP0054`.
  349. There is no automatic evaluation for environment or cache
  350. :ref:`Variable References`. Their values must be referenced as
  351. ``$ENV{<name>}`` or ``$CACHE{<name>}`` wherever the above-documented
  352. condition syntax accepts ``<variable|string>``.
  353. See also
  354. ^^^^^^^^
  355. * :command:`else`
  356. * :command:`elseif`
  357. * :command:`endif`