cmIfCommand.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmIfCommand_h
  14. #define cmIfCommand_h
  15. #include "cmCommand.h"
  16. #include "cmFunctionBlocker.h"
  17. /** \class cmIfFunctionBlocker
  18. * \brief subclass of function blocker
  19. *
  20. *
  21. */
  22. class cmIfFunctionBlocker : public cmFunctionBlocker
  23. {
  24. public:
  25. cmIfFunctionBlocker() {this->HasRun = false; this->ScopeDepth = 0;}
  26. virtual ~cmIfFunctionBlocker() {}
  27. virtual bool IsFunctionBlocked(const cmListFileFunction& lff,
  28. cmMakefile &mf);
  29. virtual bool ShouldRemove(const cmListFileFunction& lff,
  30. cmMakefile &mf);
  31. virtual void ScopeEnded(cmMakefile &mf);
  32. std::vector<cmListFileArgument> Args;
  33. bool IsBlocking;
  34. bool HasRun;
  35. unsigned int ScopeDepth;
  36. };
  37. /** \class cmIfCommand
  38. * \brief starts an if block
  39. *
  40. * cmIfCommand starts an if block
  41. */
  42. class cmIfCommand : public cmCommand
  43. {
  44. public:
  45. /**
  46. * This is a virtual constructor for the command.
  47. */
  48. virtual cmCommand* Clone()
  49. {
  50. return new cmIfCommand;
  51. }
  52. /**
  53. * This overrides the default InvokeInitialPass implementation.
  54. * It records the arguments before expansion.
  55. */
  56. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args);
  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&) { return false; }
  62. /**
  63. * The name of the command as specified in CMakeList.txt.
  64. */
  65. virtual const char* GetName() { return "IF";}
  66. /**
  67. * Succinct documentation.
  68. */
  69. virtual const char* GetTerseDocumentation()
  70. {
  71. return "Conditionally execute a group of commands.";
  72. }
  73. /**
  74. * This determines if the command is invoked when in script mode.
  75. */
  76. virtual bool IsScriptable() { return true; }
  77. /**
  78. * More documentation.
  79. */
  80. virtual const char* GetFullDocumentation()
  81. {
  82. return
  83. " IF(expression)\n"
  84. " # THEN section.\n"
  85. " COMMAND1(ARGS ...)\n"
  86. " COMMAND2(ARGS ...)\n"
  87. " ...\n"
  88. " ELSEIF(expression2)\n"
  89. " # ELSEIF section.\n"
  90. " COMMAND1(ARGS ...)\n"
  91. " COMMAND2(ARGS ...)\n"
  92. " ...\n"
  93. " ELSE(expression)\n"
  94. " # ELSE section.\n"
  95. " COMMAND1(ARGS ...)\n"
  96. " COMMAND2(ARGS ...)\n"
  97. " ...\n"
  98. " ENDIF(expression)\n"
  99. "Evaluates the given expression. If the result is true, the commands "
  100. "in the THEN section are invoked. Otherwise, the commands in the "
  101. "ELSE section are invoked. The ELSEIF and ELSE sections are "
  102. "optional. You may have multiple ELSEIF clauses. Note that "
  103. "the same expression must be given to IF, and ENDIF. Long "
  104. "expressions can be used and the order or precedence is that the "
  105. "EXISTS, COMMAND, and DEFINED operators will be evaluated first. "
  106. "Then any EQUAL, LESS, GREATER, STRLESS, STRGREATER, STREQUAL, MATCHES "
  107. "will be evaluated. Then NOT operators and finally AND, OR operators "
  108. "will be evaluated. Possible expressions are:\n"
  109. " IF(variable)\n"
  110. "True if the variable's value is not empty, 0, N, NO, OFF, FALSE, "
  111. "NOTFOUND, or <variable>-NOTFOUND.\n"
  112. " IF(NOT variable)\n"
  113. "True if the variable's value is empty, 0, N, NO, OFF, FALSE, "
  114. "NOTFOUND, or <variable>-NOTFOUND.\n"
  115. " IF(variable1 AND variable2)\n"
  116. "True if both variables would be considered true individually.\n"
  117. " IF(variable1 OR variable2)\n"
  118. "True if either variable would be considered true individually.\n"
  119. " IF(COMMAND command-name)\n"
  120. "True if the given name is a command that can be invoked.\n"
  121. " IF(EXISTS file-name)\n"
  122. " IF(EXISTS directory-name)\n"
  123. "True if the named file or directory exists. "
  124. "Behavior is well-defined only for full paths.\n"
  125. " IF(file1 IS_NEWER_THAN file2)\n"
  126. "True if file1 is newer than file2 or if one of the two files "
  127. "doesn't exist. "
  128. "Behavior is well-defined only for full paths.\n"
  129. " IF(IS_DIRECTORY directory-name)\n"
  130. "True if the given name is a directory. "
  131. "Behavior is well-defined only for full paths.\n"
  132. " IF(IS_ABSOLUTE path)\n"
  133. "True if the given path is an absolute path.\n "
  134. " IF(variable MATCHES regex)\n"
  135. " IF(string MATCHES regex)\n"
  136. "True if the given string or variable's value matches the given "
  137. "regular expression.\n"
  138. " IF(variable LESS number)\n"
  139. " IF(string LESS number)\n"
  140. " IF(variable GREATER number)\n"
  141. " IF(string GREATER number)\n"
  142. " IF(variable EQUAL number)\n"
  143. " IF(string EQUAL number)\n"
  144. "True if the given string or variable's value is a valid number and "
  145. "the inequality or equality is true.\n"
  146. " IF(variable STRLESS string)\n"
  147. " IF(string STRLESS string)\n"
  148. " IF(variable STRGREATER string)\n"
  149. " IF(string STRGREATER string)\n"
  150. " IF(variable STREQUAL string)\n"
  151. " IF(string STREQUAL string)\n"
  152. "True if the given string or variable's value is lexicographically "
  153. "less (or greater, or equal) than the string on the right.\n"
  154. " IF(DEFINED variable)\n"
  155. "True if the given variable is defined. It does not matter if the "
  156. "variable is true or false just if it has been set.";
  157. }
  158. // this is a shared function for both If and Else to determine if the
  159. // arguments were valid, and if so, was the response true. If there is
  160. // an error, the errorString will be set.
  161. static bool IsTrue(const std::vector<std::string> &args,
  162. char** errorString, cmMakefile *mf);
  163. // Get a definition from the makefile. If it doesn't exist,
  164. // return the original string.
  165. static const char* GetVariableOrString(const char* str,
  166. const cmMakefile* mf);
  167. static const char* GetVariableOrNumber(const char* str,
  168. const cmMakefile* mf);
  169. cmTypeMacro(cmIfCommand, cmCommand);
  170. };
  171. #endif