cmIfCommand.h 5.9 KB

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