if.rst 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 constants, it
  40. is treated as a variable.
  41. ``if(<variable>)``
  42. True if the variable is defined to a value that is not a false
  43. constant. False otherwise. (Note macro arguments are not variables.)
  44. ``if(NOT <expression>)``
  45. True if the expression is not true.
  46. ``if(<expr1> AND <expr2>)``
  47. True if both expressions would be considered true individually.
  48. ``if(<expr1> OR <expr2>)``
  49. True if either expression would be considered true individually.
  50. ``if(COMMAND command-name)``
  51. True if the given name is a command, macro or function that can be
  52. invoked.
  53. ``if(POLICY policy-id)``
  54. True if the given name is an existing policy (of the form ``CMP<NNNN>``).
  55. ``if(TARGET target-name)``
  56. True if the given name is an existing logical target name such as those
  57. created by the :command:`add_executable`, :command:`add_library`, or
  58. :command:`add_custom_target` commands.
  59. ``if(EXISTS path-to-file-or-directory)``
  60. True if the named file or directory exists. Behavior is well-defined
  61. only for full paths.
  62. ``if(file1 IS_NEWER_THAN file2)``
  63. True if file1 is newer than file2 or if one of the two files doesn't
  64. exist. Behavior is well-defined only for full paths. If the file
  65. time stamps are exactly the same, an ``IS_NEWER_THAN`` comparison returns
  66. true, so that any dependent build operations will occur in the event
  67. of a tie. This includes the case of passing the same file name for
  68. both file1 and file2.
  69. ``if(IS_DIRECTORY path-to-directory)``
  70. True if the given name is a directory. Behavior is well-defined only
  71. for full paths.
  72. ``if(IS_SYMLINK file-name)``
  73. True if the given name is a symbolic link. Behavior is well-defined
  74. only for full paths.
  75. ``if(IS_ABSOLUTE path)``
  76. True if the given path is an absolute path.
  77. ``if(<variable|string> MATCHES regex)``
  78. True if the given string or variable's value matches the given regular
  79. expression.
  80. ``if(<variable|string> LESS <variable|string>)``
  81. True if the given string or variable's value is a valid number and less
  82. than that on the right.
  83. ``if(<variable|string> GREATER <variable|string>)``
  84. True if the given string or variable's value is a valid number and greater
  85. than that on the right.
  86. ``if(<variable|string> EQUAL <variable|string>)``
  87. True if the given string or variable's value is a valid number and equal
  88. to that on the right.
  89. ``if(<variable|string> STRLESS <variable|string>)``
  90. True if the given string or variable's value is lexicographically less
  91. than the string or variable on the right.
  92. ``if(<variable|string> STRGREATER <variable|string>)``
  93. True if the given string or variable's value is lexicographically greater
  94. than the string or variable on the right.
  95. ``if(<variable|string> STREQUAL <variable|string>)``
  96. True if the given string or variable's value is lexicographically equal
  97. to the string or variable on the right.
  98. ``if(<variable|string> VERSION_LESS <variable|string>)``
  99. Component-wise integer version number comparison (version format is
  100. ``major[.minor[.patch[.tweak]]]``).
  101. ``if(<variable|string> VERSION_EQUAL <variable|string>)``
  102. Component-wise integer version number comparison (version format is
  103. ``major[.minor[.patch[.tweak]]]``).
  104. ``if(<variable|string> VERSION_GREATER <variable|string>)``
  105. Component-wise integer version number comparison (version format is
  106. ``major[.minor[.patch[.tweak]]]``).
  107. ``if(DEFINED <variable>)``
  108. True if the given variable is defined. It does not matter if the
  109. variable is true or false just if it has been set. (Note macro
  110. arguments are not variables.)
  111. ``if((expression) AND (expression OR (expression)))``
  112. The expressions inside the parenthesis are evaluated first and then
  113. the remaining expression is evaluated as in the previous examples.
  114. Where there are nested parenthesis the innermost are evaluated as part
  115. of evaluating the expression that contains them.
  116. The if command was written very early in CMake's history, predating
  117. the ``${}`` variable evaluation syntax, and for convenience evaluates
  118. variables named by its arguments as shown in the above signatures.
  119. Note that normal variable evaluation with ``${}`` applies before the if
  120. command even receives the arguments. Therefore code like::
  121. set(var1 OFF)
  122. set(var2 "var1")
  123. if(${var2})
  124. appears to the if command as::
  125. if(var1)
  126. and is evaluated according to the ``if(<variable>)`` case documented
  127. above. The result is ``OFF`` which is false. However, if we remove the
  128. ``${}`` from the example then the command sees::
  129. if(var2)
  130. which is true because ``var2`` is defined to "var1" which is not a false
  131. constant.
  132. Automatic evaluation applies in the other cases whenever the
  133. above-documented signature accepts ``<variable|string>``:
  134. * The left hand argument to ``MATCHES`` is first checked to see if it is
  135. a defined variable, if so the variable's value is used, otherwise the
  136. original value is used.
  137. * If the left hand argument to ``MATCHES`` is missing it returns false
  138. without error
  139. * Both left and right hand arguments to ``LESS``, ``GREATER``, and
  140. ``EQUAL`` are independently tested to see if they are defined
  141. variables, if so their defined values are used otherwise the original
  142. value is used.
  143. * Both left and right hand arguments to ``STRLESS``, ``STREQUAL``, and
  144. ``STRGREATER`` are independently tested to see if they are defined
  145. variables, if so their defined values are used otherwise the original
  146. value is used.
  147. * Both left and right hand arguments to ``VERSION_LESS``,
  148. ``VERSION_EQUAL``, and ``VERSION_GREATER`` are independently tested
  149. to see if they are defined variables, if so their defined values are
  150. used otherwise the original value is used.
  151. * The right hand argument to ``NOT`` is tested to see if it is a boolean
  152. constant, if so the value is used, otherwise it is assumed to be a
  153. variable and it is dereferenced.
  154. * The left and right hand arguments to ``AND`` and ``OR`` are independently
  155. tested to see if they are boolean constants, if so they are used as
  156. such, otherwise they are assumed to be variables and are dereferenced.