cmIfCommand.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 gets propagated down
  73. * to makefiles located in subdirectories.
  74. */
  75. virtual bool IsInherited() {return true;}
  76. /**
  77. * This determines if the command is invoked when in script mode.
  78. */
  79. virtual bool IsScriptable() { return true; }
  80. /**
  81. * More documentation.
  82. */
  83. virtual const char* GetFullDocumentation()
  84. {
  85. return
  86. " IF(expression)\n"
  87. " # THEN section.\n"
  88. " COMMAND1(ARGS ...)\n"
  89. " COMMAND2(ARGS ...)\n"
  90. " ...\n"
  91. " ELSE(expression)\n"
  92. " # ELSE section.\n"
  93. " COMMAND1(ARGS ...)\n"
  94. " COMMAND2(ARGS ...)\n"
  95. " ...\n"
  96. " ENDIF(expression)\n"
  97. "Evaluates the given expression. If the result is true, the commands "
  98. "in the THEN section are invoked. Otherwise, the commands in the "
  99. "ELSE section are invoked. The ELSE section is optional. Note that "
  100. "the same expression must be given to IF, ELSE, and ENDIF. Long "
  101. "exressions can be used and the order or precidence is that the "
  102. "EXISTS, COMMAND, and DEFINED operators will be evaluated first. "
  103. "Then any EQUAL, LESS, GREATER, STRLESS, STRGREATER, STREQUAL, MATCHES "
  104. "will be evaluated. Then NOT operators and finally AND, OR operators "
  105. "will be evaluated. Possible expressions are:\n"
  106. " IF(variable)\n"
  107. "True if the variable's value is not empty, 0, FALSE, OFF, or NOTFOUND.\n"
  108. " IF(NOT variable)\n"
  109. "True if the variable's value is empty, 0, FALSE, OFF, or NOTFOUND.\n"
  110. " IF(variable1 AND variable2)\n"
  111. "True if both variables would be considered true individually. Only "
  112. "one AND is allowed to keep expressions short.\n"
  113. " IF(variable1 OR variable2)\n"
  114. "True if either variable would be considered true individually. Only "
  115. "one OR is allowed to keep expressions short.\n"
  116. " IF(COMMAND command-name)\n"
  117. "True if the given name is a command that can be invoked.\n"
  118. " IF(EXISTS file-name)\n"
  119. " IF(EXISTS directory-name)\n"
  120. "True if the named file or directory exists.\n"
  121. " IF(variable MATCHES regex)\n"
  122. " IF(string MATCHES regex)\n"
  123. "True if the given string or variable's value matches the given "
  124. "regular expression.\n"
  125. " IF(variable LESS number)\n"
  126. " IF(string LESS number)\n"
  127. " IF(variable GREATER number)\n"
  128. " IF(string GREATER number)\n"
  129. " IF(variable EQUAL number)\n"
  130. " IF(string EQUAL number)\n"
  131. "True if the given string or variable's value is a valid number and "
  132. "the inequality or equality is true.\n"
  133. " IF(variable STRLESS string)\n"
  134. " IF(string STRLESS string)\n"
  135. " IF(variable STRGREATER string)\n"
  136. " IF(string STRGREATER string)\n"
  137. " IF(variable STREQUAL string)\n"
  138. " IF(string STREQUAL string)\n"
  139. "True if the given string or variable's value is lexicographically "
  140. "less (or greater, or equal) than the string on the right.\n"
  141. " IF(DEFINED variable)\n"
  142. "True if the given variable is defined. It does not matter if the "
  143. "variable is true or false just if it has been set.";
  144. }
  145. // this is a shared function for both If and Else to determine if
  146. // the arguments were valid, and if so, was the response true
  147. static bool IsTrue(const std::vector<std::string> &args,
  148. bool &isValid, const cmMakefile *mf);
  149. // Get a definition from the makefile. If it doesn't exist,
  150. // return the original string.
  151. static const char* GetVariableOrString(const char* str,
  152. const cmMakefile* mf);
  153. static const char* GetVariableOrNumber(const char* str,
  154. const cmMakefile* mf);
  155. cmTypeMacro(cmIfCommand, cmCommand);
  156. };
  157. #endif