cmIfCommand.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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
  15. * \brief subclass of function blocker
  16. *
  17. *
  18. */
  19. class cmIfFunctionBlocker : public cmFunctionBlocker
  20. {
  21. public:
  22. cmIfFunctionBlocker() {
  23. this->HasRun = false; this->ScopeDepth = 0; }
  24. virtual ~cmIfFunctionBlocker() {}
  25. virtual bool IsFunctionBlocked(const cmListFileFunction& lff,
  26. cmMakefile &mf,
  27. cmExecutionStatus &);
  28. virtual bool ShouldRemove(const cmListFileFunction& lff,
  29. cmMakefile &mf);
  30. std::vector<cmListFileArgument> Args;
  31. std::vector<cmListFileFunction> Functions;
  32. bool IsBlocking;
  33. bool HasRun;
  34. unsigned int ScopeDepth;
  35. };
  36. /** \class cmIfCommand
  37. * \brief starts an if block
  38. *
  39. * cmIfCommand starts an if block
  40. */
  41. class cmIfCommand : public cmCommand
  42. {
  43. public:
  44. /**
  45. * This is a virtual constructor for the command.
  46. */
  47. virtual cmCommand* Clone()
  48. {
  49. return new cmIfCommand;
  50. }
  51. /**
  52. * This overrides the default InvokeInitialPass implementation.
  53. * It records the arguments before expansion.
  54. */
  55. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  56. cmExecutionStatus &);
  57. /**
  58. * This is called when the command is first encountered in
  59. * the CMakeLists.txt file.
  60. */
  61. virtual bool InitialPass(std::vector<std::string> const&,
  62. cmExecutionStatus &) { return false;};
  63. /**
  64. * The name of the command as specified in CMakeList.txt.
  65. */
  66. virtual const char* GetName() { return "if";}
  67. /**
  68. * Succinct documentation.
  69. */
  70. virtual const char* GetTerseDocumentation()
  71. {
  72. return "Conditionally execute a group of commands.";
  73. }
  74. /**
  75. * This determines if the command is invoked when in script mode.
  76. */
  77. virtual bool IsScriptable() { return true; }
  78. /**
  79. * More documentation.
  80. */
  81. virtual const char* GetFullDocumentation()
  82. {
  83. return
  84. " if(expression)\n"
  85. " # then section.\n"
  86. " COMMAND1(ARGS ...)\n"
  87. " COMMAND2(ARGS ...)\n"
  88. " ...\n"
  89. " elseif(expression2)\n"
  90. " # elseif section.\n"
  91. " COMMAND1(ARGS ...)\n"
  92. " COMMAND2(ARGS ...)\n"
  93. " ...\n"
  94. " else(expression)\n"
  95. " # else section.\n"
  96. " COMMAND1(ARGS ...)\n"
  97. " COMMAND2(ARGS ...)\n"
  98. " ...\n"
  99. " endif(expression)\n"
  100. "Evaluates the given expression. If the result is true, the commands "
  101. "in the THEN section are invoked. Otherwise, the commands in the "
  102. "else section are invoked. The elseif and else sections are "
  103. "optional. You may have multiple elseif clauses. Note that "
  104. "the expression in the else and endif clause is optional. Long "
  105. "expressions can be used and there is a traditional order of "
  106. "precedence. "
  107. "Parenthetical expressions are evaluated first followed by unary "
  108. "operators such as EXISTS, COMMAND, and DEFINED. "
  109. "Then any EQUAL, LESS, GREATER, STRLESS, STRGREATER, STREQUAL, MATCHES "
  110. "will be evaluated. Then NOT operators and finally AND, OR operators "
  111. "will be evaluated. Possible expressions are:\n"
  112. " if(variable)\n"
  113. "True if the variable's value is not empty, 0, N, NO, OFF, FALSE, "
  114. "NOTFOUND, or <variable>-NOTFOUND.\n"
  115. " if(NOT variable)\n"
  116. "True if the variable's value is empty, 0, N, NO, OFF, FALSE, "
  117. "NOTFOUND, or <variable>-NOTFOUND.\n"
  118. " if(variable1 AND variable2)\n"
  119. "True if both variables would be considered true individually.\n"
  120. " if(variable1 OR variable2)\n"
  121. "True if either variable would be considered true individually.\n"
  122. " if(COMMAND command-name)\n"
  123. "True if the given name is a command, macro or function that can be "
  124. "invoked.\n"
  125. " if(POLICY policy-id)\n"
  126. "True if the given name is an existing policy "
  127. "(of the form CMP<NNNN>).\n"
  128. " if(TARGET target-name)\n"
  129. "True if the given name is an existing target, built or imported.\n"
  130. " if(EXISTS file-name)\n"
  131. " if(EXISTS directory-name)\n"
  132. "True if the named file or directory exists. "
  133. "Behavior is well-defined only for full paths.\n"
  134. " if(file1 IS_NEWER_THAN file2)\n"
  135. "True if file1 is newer than file2 or if one of the two files "
  136. "doesn't exist. "
  137. "Behavior is well-defined only for full paths.\n"
  138. " if(IS_DIRECTORY directory-name)\n"
  139. "True if the given name is a directory. "
  140. "Behavior is well-defined only for full paths.\n"
  141. " if(IS_ABSOLUTE path)\n"
  142. "True if the given path is an absolute path.\n"
  143. " if(variable MATCHES regex)\n"
  144. " if(string MATCHES regex)\n"
  145. "True if the given string or variable's value matches the given "
  146. "regular expression.\n"
  147. " if(variable LESS number)\n"
  148. " if(string LESS number)\n"
  149. " if(variable GREATER number)\n"
  150. " if(string GREATER number)\n"
  151. " if(variable EQUAL number)\n"
  152. " if(string EQUAL number)\n"
  153. "True if the given string or variable's value is a valid number and "
  154. "the inequality or equality is true.\n"
  155. " if(variable STRLESS string)\n"
  156. " if(string STRLESS string)\n"
  157. " if(variable STRGREATER string)\n"
  158. " if(string STRGREATER string)\n"
  159. " if(variable STREQUAL string)\n"
  160. " if(string STREQUAL string)\n"
  161. "True if the given string or variable's value is lexicographically "
  162. "less (or greater, or equal) than the string or variable on the right.\n"
  163. " if(version1 VERSION_LESS version2)\n"
  164. " if(version1 VERSION_EQUAL version2)\n"
  165. " if(version1 VERSION_GREATER version2)\n"
  166. "Component-wise integer version number comparison (version format is "
  167. "major[.minor[.patch[.tweak]]]).\n"
  168. " if(DEFINED variable)\n"
  169. "True if the given variable is defined. It does not matter if the "
  170. "variable is true or false just if it has been set.\n"
  171. " if((expression) AND (expression OR (expression)))\n"
  172. "The expressions inside the parenthesis are evaluated first and "
  173. "then the remaining expression is evaluated as in the previous "
  174. "examples. Where there are nested parenthesis the innermost are "
  175. "evaluated as part of evaluating the expression "
  176. "that contains them."
  177. "\n"
  178. "The if statement was written fairly early in CMake's history "
  179. "and it has some convenience features that are worth covering. "
  180. "The if statement reduces operations until there is "
  181. "a single remaining value, at that point if the case "
  182. "insensitive value is: ON, 1, YES, TRUE, Y it returns true, if "
  183. "it is OFF, 0, NO, FALSE, N, NOTFOUND, *-NOTFOUND, IGNORE it "
  184. "will return false. \n"
  185. "This is fairly reasonable. The convenience feature that sometimes "
  186. "throws new authors is how CMake handles values that do not "
  187. "match the true or false list. Those values are treated as "
  188. "variables and are dereferenced even though they do not have "
  189. "the required ${} syntax. This means that if you write\n"
  190. " if (boobah)\n"
  191. "CMake will treat it as if you wrote \n"
  192. " if (${boobah})\n"
  193. "likewise if you write \n"
  194. " if (fubar AND sol)\n"
  195. "CMake will conveniently treat it as \n"
  196. " if (\"${fubar}\" AND \"${sol}\")\n"
  197. "The later is really the correct way to write it, but the "
  198. "former will work as well. Only some operations in the if "
  199. "statement have this special handling of arguments. The "
  200. "specific details follow: \n"
  201. "1) The left hand argument to MATCHES is first checked to see "
  202. "if it is a defined variable, if so the variable's value is "
  203. "used, otherwise the original value is used. \n"
  204. "2) If the left hand argument to MATCHES is missing it returns "
  205. "false without error \n"
  206. "3) Both left and right hand arguments to LESS GREATER EQUAL "
  207. "are independently tested to see if they are defined variables, "
  208. "if so their defined values are used otherwise the original "
  209. "value is used. \n"
  210. "4) Both left and right hand arguments to STRLESS STREQUAL "
  211. "STRGREATER are independently tested to see if they are defined "
  212. "variables, if so their defined values are used otherwise the "
  213. "original value is used. \n"
  214. "5) Both left and right hand argumemnts to VERSION_LESS "
  215. "VERSION_EQUAL VERSION_GREATER are independently tested to see "
  216. "if they are defined variables, if so their defined values are "
  217. "used otherwise the original value is used. \n"
  218. "6) The right hand argument to NOT is tested to see if it is a "
  219. "boolean constant, if so the value is used, otherwise it is "
  220. "assumed to be a variable and it is dereferenced. \n"
  221. "7) The left and right hand arguments to AND OR are "
  222. "independently tested to see if they are boolean constants, if "
  223. "so they are used as such, otherwise they are assumed to be "
  224. "variables and are dereferenced. \n"
  225. ;
  226. }
  227. // this is a shared function for both If and Else to determine if the
  228. // arguments were valid, and if so, was the response true. If there is
  229. // an error, the errorString will be set.
  230. static bool IsTrue(const std::vector<std::string> &args,
  231. std::string &errorString, cmMakefile *mf,
  232. cmake::MessageType &status);
  233. // Get a definition from the makefile. If it doesn't exist,
  234. // return the original string.
  235. static const char* GetVariableOrString(const char* str,
  236. const cmMakefile* mf);
  237. cmTypeMacro(cmIfCommand, cmCommand);
  238. };
  239. #endif