cmIfCommand.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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;}
  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. };
  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. /**
  57. * This is called when the command is first encountered in
  58. * the CMakeLists.txt file.
  59. */
  60. virtual bool InitialPass(std::vector<std::string> const&) { return false; }
  61. /**
  62. * The name of the command as specified in CMakeList.txt.
  63. */
  64. virtual const char* GetName() { return "IF";}
  65. /**
  66. * Succinct documentation.
  67. */
  68. virtual const char* GetTerseDocumentation()
  69. {
  70. return "Conditionally execute a group of commands.";
  71. }
  72. /**
  73. * This determines if the command is invoked when in script mode.
  74. */
  75. virtual bool IsScriptable() { return true; }
  76. /**
  77. * More documentation.
  78. */
  79. virtual const char* GetFullDocumentation()
  80. {
  81. return
  82. " IF(expression)\n"
  83. " # THEN section.\n"
  84. " COMMAND1(ARGS ...)\n"
  85. " COMMAND2(ARGS ...)\n"
  86. " ...\n"
  87. " ELSE(expression)\n"
  88. " # ELSE section.\n"
  89. " COMMAND1(ARGS ...)\n"
  90. " COMMAND2(ARGS ...)\n"
  91. " ...\n"
  92. " ENDIF(expression)\n"
  93. "Evaluates the given expression. If the result is true, the commands "
  94. "in the THEN section are invoked. Otherwise, the commands in the "
  95. "ELSE section are invoked. The ELSE section is optional. Note that "
  96. "the same expression must be given to IF, ELSE, and ENDIF. Long "
  97. "expressions can be used and the order or precedence is that the "
  98. "EXISTS, COMMAND, and DEFINED operators will be evaluated first. "
  99. "Then any EQUAL, LESS, GREATER, STRLESS, STRGREATER, STREQUAL, MATCHES "
  100. "will be evaluated. Then NOT operators and finally AND, OR operators "
  101. "will be evaluated. Possible expressions are:\n"
  102. " IF(variable)\n"
  103. "True if the variable's value is not empty, 0, N, NO, OFF, FALSE, "
  104. "NOTFOUND, or <variable>-NOTFOUND.\n"
  105. " IF(NOT variable)\n"
  106. "True if the variable's value is empty, 0, N, NO, OFF, FALSE, "
  107. "NOTFOUND, or <variable>-NOTFOUND.\n"
  108. " IF(variable1 AND variable2)\n"
  109. "True if both variables would be considered true individually.\n"
  110. " IF(variable1 OR variable2)\n"
  111. "True if either variable would be considered true individually.\n"
  112. " IF(COMMAND command-name)\n"
  113. "True if the given name is a command that can be invoked.\n"
  114. " IF(EXISTS file-name)\n"
  115. " IF(EXISTS directory-name)\n"
  116. "True if the named file or directory exists. "
  117. "Behavior is well-defined only for full paths.\n"
  118. " IF(FILE_IS_NEWER file1 file2)\n"
  119. "True if file1 is newer than file2 or if one of the two files "
  120. "doesn't exist. "
  121. "Behavior is well-defined only for full paths.\n"
  122. " IF(IS_DIRECTORY directory-name)\n"
  123. "True if the given name is a directory. "
  124. "Behavior is well-defined only for full paths.\n"
  125. " IF(variable MATCHES regex)\n"
  126. " IF(string MATCHES regex)\n"
  127. "True if the given string or variable's value matches the given "
  128. "regular expression.\n"
  129. " IF(variable LESS number)\n"
  130. " IF(string LESS number)\n"
  131. " IF(variable GREATER number)\n"
  132. " IF(string GREATER number)\n"
  133. " IF(variable EQUAL number)\n"
  134. " IF(string EQUAL number)\n"
  135. "True if the given string or variable's value is a valid number and "
  136. "the inequality or equality is true.\n"
  137. " IF(variable STRLESS string)\n"
  138. " IF(string STRLESS string)\n"
  139. " IF(variable STRGREATER string)\n"
  140. " IF(string STRGREATER string)\n"
  141. " IF(variable STREQUAL string)\n"
  142. " IF(string STREQUAL string)\n"
  143. "True if the given string or variable's value is lexicographically "
  144. "less (or greater, or equal) than the string on the right.\n"
  145. " IF(DEFINED variable)\n"
  146. "True if the given variable is defined. It does not matter if the "
  147. "variable is true or false just if it has been set.";
  148. }
  149. // this is a shared function for both If and Else to determine if the
  150. // arguments were valid, and if so, was the response true. If there is
  151. // an error, the errorString will be set.
  152. static bool IsTrue(const std::vector<std::string> &args,
  153. char** errorString, const cmMakefile *mf);
  154. // Get a definition from the makefile. If it doesn't exist,
  155. // return the original string.
  156. static const char* GetVariableOrString(const char* str,
  157. const cmMakefile* mf);
  158. static const char* GetVariableOrNumber(const char* str,
  159. const cmMakefile* mf);
  160. cmTypeMacro(cmIfCommand, cmCommand);
  161. };
  162. #endif