1
0

cmIfCommand.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmIfCommand_h
  11. #define cmIfCommand_h
  12. #include "cmCommand.h"
  13. #include "cmFunctionBlocker.h"
  14. class cmIfFunctionBlocker : public cmFunctionBlocker
  15. {
  16. public:
  17. cmIfFunctionBlocker() {
  18. this->HasRun = false; this->ScopeDepth = 0; }
  19. virtual ~cmIfFunctionBlocker() {}
  20. virtual bool IsFunctionBlocked(const cmListFileFunction& lff,
  21. cmMakefile &mf,
  22. cmExecutionStatus &);
  23. virtual bool ShouldRemove(const cmListFileFunction& lff,
  24. cmMakefile &mf);
  25. std::vector<cmListFileArgument> Args;
  26. std::vector<cmListFileFunction> Functions;
  27. bool IsBlocking;
  28. bool HasRun;
  29. unsigned int ScopeDepth;
  30. };
  31. /// Starts an if block
  32. class cmIfCommand : public cmCommand
  33. {
  34. public:
  35. /**
  36. * This is a virtual constructor for the command.
  37. */
  38. virtual cmCommand* Clone()
  39. {
  40. return new cmIfCommand;
  41. }
  42. /**
  43. * This overrides the default InvokeInitialPass implementation.
  44. * It records the arguments before expansion.
  45. */
  46. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  47. cmExecutionStatus &);
  48. /**
  49. * This is called when the command is first encountered in
  50. * the CMakeLists.txt file.
  51. */
  52. virtual bool InitialPass(std::vector<std::string> const&,
  53. cmExecutionStatus &) { return false;};
  54. /**
  55. * The name of the command as specified in CMakeList.txt.
  56. */
  57. virtual const char* GetName() const { return "if";}
  58. /**
  59. * Succinct documentation.
  60. */
  61. virtual const char* GetTerseDocumentation() const
  62. {
  63. return "Conditionally execute a group of commands.";
  64. }
  65. /**
  66. * This determines if the command is invoked when in script mode.
  67. */
  68. virtual bool IsScriptable() const { return true; }
  69. /**
  70. * More documentation.
  71. */
  72. virtual const char* GetFullDocumentation() const
  73. {
  74. return
  75. " if(expression)\n"
  76. " # then section.\n"
  77. " COMMAND1(ARGS ...)\n"
  78. " COMMAND2(ARGS ...)\n"
  79. " ...\n"
  80. " elseif(expression2)\n"
  81. " # elseif section.\n"
  82. " COMMAND1(ARGS ...)\n"
  83. " COMMAND2(ARGS ...)\n"
  84. " ...\n"
  85. " else(expression)\n"
  86. " # else section.\n"
  87. " COMMAND1(ARGS ...)\n"
  88. " COMMAND2(ARGS ...)\n"
  89. " ...\n"
  90. " endif(expression)\n"
  91. "Evaluates the given expression. If the result is true, the commands "
  92. "in the THEN section are invoked. Otherwise, the commands in the "
  93. "else section are invoked. The elseif and else sections are "
  94. "optional. You may have multiple elseif clauses. Note that "
  95. "the expression in the else and endif clause is optional. Long "
  96. "expressions can be used and there is a traditional order of "
  97. "precedence. "
  98. "Parenthetical expressions are evaluated first followed by unary "
  99. "operators such as EXISTS, COMMAND, and DEFINED. "
  100. "Then any EQUAL, LESS, GREATER, STRLESS, STRGREATER, STREQUAL, MATCHES "
  101. "will be evaluated. Then NOT operators and finally AND, OR operators "
  102. "will be evaluated. Possible expressions are:\n"
  103. " if(<constant>)\n"
  104. "True if the constant is 1, ON, YES, TRUE, Y, or a non-zero number. "
  105. "False if the constant is 0, OFF, NO, FALSE, N, IGNORE, \"\", "
  106. "or ends in the suffix '-NOTFOUND'. "
  107. "Named boolean constants are case-insensitive. "
  108. "If the argument is not one of these constants, "
  109. "it is treated as a variable:"
  110. "\n"
  111. " if(<variable>)\n"
  112. "True if the variable is defined to a value that is not a false "
  113. "constant. False otherwise. "
  114. "(Note macro arguments are not variables.)"
  115. "\n"
  116. " if(NOT <expression>)\n"
  117. "True if the expression is not true."
  118. "\n"
  119. " if(<expr1> AND <expr2>)\n"
  120. "True if both expressions would be considered true individually."
  121. "\n"
  122. " if(<expr1> OR <expr2>)\n"
  123. "True if either expression would be considered true individually."
  124. "\n"
  125. " if(COMMAND command-name)\n"
  126. "True if the given name is a command, macro or function that can be "
  127. "invoked.\n"
  128. " if(POLICY policy-id)\n"
  129. "True if the given name is an existing policy "
  130. "(of the form CMP<NNNN>).\n"
  131. " if(TARGET target-name)\n"
  132. "True if the given name is an existing target, built or imported.\n"
  133. " if(EXISTS file-name)\n"
  134. " if(EXISTS directory-name)\n"
  135. "True if the named file or directory exists. "
  136. "Behavior is well-defined only for full paths.\n"
  137. " if(file1 IS_NEWER_THAN file2)\n"
  138. "True if file1 is newer than file2 or if one of the two files "
  139. "doesn't exist. "
  140. "Behavior is well-defined only for full paths.\n"
  141. " if(IS_DIRECTORY directory-name)\n"
  142. "True if the given name is a directory. "
  143. "Behavior is well-defined only for full paths.\n"
  144. " if(IS_SYMLINK file-name)\n"
  145. "True if the given name is a symbolic link. "
  146. "Behavior is well-defined only for full paths.\n"
  147. " if(IS_ABSOLUTE path)\n"
  148. "True if the given path is an absolute path.\n"
  149. " if(<variable|string> MATCHES regex)\n"
  150. "True if the given string or variable's value matches the given "
  151. "regular expression.\n"
  152. " if(<variable|string> LESS <variable|string>)\n"
  153. " if(<variable|string> GREATER <variable|string>)\n"
  154. " if(<variable|string> EQUAL <variable|string>)\n"
  155. "True if the given string or variable's value is a valid number and "
  156. "the inequality or equality is true.\n"
  157. " if(<variable|string> STRLESS <variable|string>)\n"
  158. " if(<variable|string> STRGREATER <variable|string>)\n"
  159. " if(<variable|string> STREQUAL <variable|string>)\n"
  160. "True if the given string or variable's value is lexicographically "
  161. "less (or greater, or equal) than the string or variable on the right.\n"
  162. " if(<variable|string> VERSION_LESS <variable|string>)\n"
  163. " if(<variable|string> VERSION_EQUAL <variable|string>)\n"
  164. " if(<variable|string> VERSION_GREATER <variable|string>)\n"
  165. "Component-wise integer version number comparison (version format is "
  166. "major[.minor[.patch[.tweak]]]).\n"
  167. " if(DEFINED <variable>)\n"
  168. "True if the given variable is defined. It does not matter if the "
  169. "variable is true or false just if it has been set.\n"
  170. " if((expression) AND (expression OR (expression)))\n"
  171. "The expressions inside the parenthesis are evaluated first and "
  172. "then the remaining expression is evaluated as in the previous "
  173. "examples. Where there are nested parenthesis the innermost are "
  174. "evaluated as part of evaluating the expression "
  175. "that contains them."
  176. "\n"
  177. "The if command was written very early in CMake's history, predating "
  178. "the ${} variable evaluation syntax, and for convenience evaluates "
  179. "variables named by its arguments as shown in the above signatures. "
  180. "Note that normal variable evaluation with ${} applies before the "
  181. "if command even receives the arguments. "
  182. "Therefore code like\n"
  183. " set(var1 OFF)\n"
  184. " set(var2 \"var1\")\n"
  185. " if(${var2})\n"
  186. "appears to the if command as\n"
  187. " if(var1)\n"
  188. "and is evaluated according to the if(<variable>) case "
  189. "documented above. "
  190. "The result is OFF which is false. "
  191. "However, if we remove the ${} from the example then the command sees\n"
  192. " if(var2)\n"
  193. "which is true because var2 is defined to \"var1\" which is not "
  194. "a false constant."
  195. "\n"
  196. "Automatic evaluation applies in the other cases whenever the "
  197. "above-documented signature accepts <variable|string>:\n"
  198. "1) The left hand argument to MATCHES is first checked to see "
  199. "if it is a defined variable, if so the variable's value is "
  200. "used, otherwise the original value is used. \n"
  201. "2) If the left hand argument to MATCHES is missing it returns "
  202. "false without error \n"
  203. "3) Both left and right hand arguments to LESS GREATER EQUAL "
  204. "are independently tested to see if they are defined variables, "
  205. "if so their defined values are used otherwise the original "
  206. "value is used. \n"
  207. "4) Both left and right hand arguments to STRLESS STREQUAL "
  208. "STRGREATER are independently tested to see if they are defined "
  209. "variables, if so their defined values are used otherwise the "
  210. "original value is used. \n"
  211. "5) Both left and right hand argumemnts to VERSION_LESS "
  212. "VERSION_EQUAL VERSION_GREATER are independently tested to see "
  213. "if they are defined variables, if so their defined values are "
  214. "used otherwise the original value is used. \n"
  215. "6) The right hand argument to NOT is tested to see if it is a "
  216. "boolean constant, if so the value is used, otherwise it is "
  217. "assumed to be a variable and it is dereferenced. \n"
  218. "7) The left and right hand arguments to AND OR are "
  219. "independently tested to see if they are boolean constants, if "
  220. "so they are used as such, otherwise they are assumed to be "
  221. "variables and are dereferenced. \n"
  222. ;
  223. }
  224. // this is a shared function for both If and Else to determine if the
  225. // arguments were valid, and if so, was the response true. If there is
  226. // an error, the errorString will be set.
  227. static bool IsTrue(const std::vector<std::string> &args,
  228. std::string &errorString, cmMakefile *mf,
  229. cmake::MessageType &status);
  230. // Get a definition from the makefile. If it doesn't exist,
  231. // return the original string.
  232. static const char* GetVariableOrString(const char* str,
  233. const cmMakefile* mf);
  234. cmTypeMacro(cmIfCommand, cmCommand);
  235. };
  236. #endif