if.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. if
  2. --
  3. Conditionally execute a group of commands.
  4. .. code-block:: cmake
  5. if(expression)
  6. # then section.
  7. COMMAND1(ARGS ...)
  8. COMMAND2(ARGS ...)
  9. ...
  10. elseif(expression2)
  11. # elseif section.
  12. COMMAND1(ARGS ...)
  13. COMMAND2(ARGS ...)
  14. ...
  15. else(expression)
  16. # else section.
  17. COMMAND1(ARGS ...)
  18. COMMAND2(ARGS ...)
  19. ...
  20. endif(expression)
  21. Evaluates the given expression. If the result is true, the commands
  22. in the THEN section are invoked. Otherwise, the commands in the else
  23. section are invoked. The elseif and else sections are optional. You
  24. may have multiple elseif clauses. Note that the expression in the
  25. else and endif clause is optional. Long expressions can be used and
  26. there is a traditional order of precedence. Parenthetical expressions
  27. are evaluated first followed by unary tests such as ``EXISTS``,
  28. ``COMMAND``, and ``DEFINED``. Then any binary tests such as
  29. ``EQUAL``, ``LESS``, ``GREATER``, ``STRLESS``, ``STRGREATER``,
  30. ``STREQUAL``, and ``MATCHES`` will be evaluated. Then boolean ``NOT``
  31. operators and finally boolean ``AND`` and then ``OR`` operators will
  32. be evaluated.
  33. Possible expressions are:
  34. ``if(<constant>)``
  35. True if the constant is ``1``, ``ON``, ``YES``, ``TRUE``, ``Y``,
  36. or a non-zero number. False if the constant is ``0``, ``OFF``,
  37. ``NO``, ``FALSE``, ``N``, ``IGNORE``, ``NOTFOUND``, the empty string,
  38. or ends in the suffix ``-NOTFOUND``. Named boolean constants are
  39. case-insensitive. If the argument is not one of these specific
  40. constants, it is treated as a variable or string and the following
  41. signature is used.
  42. ``if(<variable|string>)``
  43. True if given a variable that is defined to a value that is not a false
  44. constant. False otherwise. (Note macro arguments are not variables.)
  45. ``if(NOT <expression>)``
  46. True if the expression is not true.
  47. ``if(<expr1> AND <expr2>)``
  48. True if both expressions would be considered true individually.
  49. ``if(<expr1> OR <expr2>)``
  50. True if either expression would be considered true individually.
  51. ``if(COMMAND command-name)``
  52. True if the given name is a command, macro or function that can be
  53. invoked.
  54. ``if(POLICY policy-id)``
  55. True if the given name is an existing policy (of the form ``CMP<NNNN>``).
  56. ``if(TARGET target-name)``
  57. True if the given name is an existing logical target name such as those
  58. created by the :command:`add_executable`, :command:`add_library`, or
  59. :command:`add_custom_target` commands.
  60. ``if(TEST test-name)``
  61. True if the given name is an existing test name created by the
  62. :command:`add_test` command.
  63. ``if(EXISTS path-to-file-or-directory)``
  64. True if the named file or directory exists. Behavior is well-defined
  65. only for full paths.
  66. ``if(file1 IS_NEWER_THAN file2)``
  67. True if file1 is newer than file2 or if one of the two files doesn't
  68. exist. Behavior is well-defined only for full paths. If the file
  69. time stamps are exactly the same, an ``IS_NEWER_THAN`` comparison returns
  70. true, so that any dependent build operations will occur in the event
  71. of a tie. This includes the case of passing the same file name for
  72. both file1 and file2.
  73. ``if(IS_DIRECTORY path-to-directory)``
  74. True if the given name is a directory. Behavior is well-defined only
  75. for full paths.
  76. ``if(IS_SYMLINK file-name)``
  77. True if the given name is a symbolic link. Behavior is well-defined
  78. only for full paths.
  79. ``if(IS_ABSOLUTE path)``
  80. True if the given path is an absolute path.
  81. ``if(<variable|string> MATCHES regex)``
  82. True if the given string or variable's value matches the given regular
  83. expression.
  84. ``if(<variable|string> LESS <variable|string>)``
  85. True if the given string or variable's value is a valid number and less
  86. than that on the right.
  87. ``if(<variable|string> GREATER <variable|string>)``
  88. True if the given string or variable's value is a valid number and greater
  89. than that on the right.
  90. ``if(<variable|string> EQUAL <variable|string>)``
  91. True if the given string or variable's value is a valid number and equal
  92. to that on the right.
  93. ``if(<variable|string> STRLESS <variable|string>)``
  94. True if the given string or variable's value is lexicographically less
  95. than the string or variable on the right.
  96. ``if(<variable|string> STRGREATER <variable|string>)``
  97. True if the given string or variable's value is lexicographically greater
  98. than the string or variable on the right.
  99. ``if(<variable|string> STREQUAL <variable|string>)``
  100. True if the given string or variable's value is lexicographically equal
  101. to the string or variable on the right.
  102. ``if(<variable|string> VERSION_LESS <variable|string>)``
  103. Component-wise integer version number comparison (version format is
  104. ``major[.minor[.patch[.tweak]]]``).
  105. ``if(<variable|string> VERSION_EQUAL <variable|string>)``
  106. Component-wise integer version number comparison (version format is
  107. ``major[.minor[.patch[.tweak]]]``).
  108. ``if(<variable|string> VERSION_GREATER <variable|string>)``
  109. Component-wise integer version number comparison (version format is
  110. ``major[.minor[.patch[.tweak]]]``).
  111. ``if(<variable|string> IN_LIST <variable>)``
  112. True if the given element is contained in the named list variable.
  113. ``if(DEFINED <variable>)``
  114. True if the given variable is defined. It does not matter if the
  115. variable is true or false just if it has been set. (Note macro
  116. arguments are not variables.)
  117. ``if((expression) AND (expression OR (expression)))``
  118. The expressions inside the parenthesis are evaluated first and then
  119. the remaining expression is evaluated as in the previous examples.
  120. Where there are nested parenthesis the innermost are evaluated as part
  121. of evaluating the expression that contains them.
  122. The if command was written very early in CMake's history, predating
  123. the ``${}`` variable evaluation syntax, and for convenience evaluates
  124. variables named by its arguments as shown in the above signatures.
  125. Note that normal variable evaluation with ``${}`` applies before the if
  126. command even receives the arguments. Therefore code like::
  127. set(var1 OFF)
  128. set(var2 "var1")
  129. if(${var2})
  130. appears to the if command as::
  131. if(var1)
  132. and is evaluated according to the ``if(<variable>)`` case documented
  133. above. The result is ``OFF`` which is false. However, if we remove the
  134. ``${}`` from the example then the command sees::
  135. if(var2)
  136. which is true because ``var2`` is defined to "var1" which is not a false
  137. constant.
  138. Automatic evaluation applies in the other cases whenever the
  139. above-documented signature accepts ``<variable|string>``:
  140. * The left hand argument to ``MATCHES`` is first checked to see if it is
  141. a defined variable, if so the variable's value is used, otherwise the
  142. original value is used.
  143. * If the left hand argument to ``MATCHES`` is missing it returns false
  144. without error
  145. * Both left and right hand arguments to ``LESS``, ``GREATER``, and
  146. ``EQUAL`` are independently tested to see if they are defined
  147. variables, if so their defined values are used otherwise the original
  148. value is used.
  149. * Both left and right hand arguments to ``STRLESS``, ``STREQUAL``, and
  150. ``STRGREATER`` are independently tested to see if they are defined
  151. variables, if so their defined values are used otherwise the original
  152. value is used.
  153. * Both left and right hand arguments to ``VERSION_LESS``,
  154. ``VERSION_EQUAL``, and ``VERSION_GREATER`` are independently tested
  155. to see if they are defined variables, if so their defined values are
  156. used otherwise the original value is used.
  157. * The right hand argument to ``NOT`` is tested to see if it is a boolean
  158. constant, if so the value is used, otherwise it is assumed to be a
  159. variable and it is dereferenced.
  160. * The left and right hand arguments to ``AND`` and ``OR`` are independently
  161. tested to see if they are boolean constants, if so they are used as
  162. such, otherwise they are assumed to be variables and are dereferenced.
  163. To prevent ambiguity, potential variable or keyword names can be
  164. specified in a :ref:`Quoted Argument` or a :ref:`Bracket Argument`.
  165. A quoted or bracketed variable or keyword will be interpreted as a
  166. string and not dereferenced or interpreted.
  167. See policy :policy:`CMP0054`.