if.rst 6.3 KB

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