cmCommand.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 cmCommand_h
  14. #define cmCommand_h
  15. #include "cmListFileCache.h"
  16. #include "cmMakefile.h"
  17. /** \class cmCommand
  18. * \brief Superclass for all commands in CMake.
  19. *
  20. * cmCommand is the base class for all commands in CMake. A command
  21. * manifests as an entry in CMakeLists.txt and produces one or
  22. * more makefile rules. Commands are associated with a particular
  23. * makefile. This base class cmCommand defines the API for commands
  24. * to support such features as enable/disable, inheritance,
  25. * documentation, and construction.
  26. */
  27. class cmCommand
  28. {
  29. public:
  30. /**
  31. * Construct the command. By default it is enabled with no makefile.
  32. */
  33. cmCommand()
  34. {m_Makefile = 0; m_Enabled = true;}
  35. /**
  36. * Need virtual destructor to destroy real command type.
  37. */
  38. virtual ~cmCommand() {}
  39. /**
  40. * Specify the makefile.
  41. */
  42. void SetMakefile(cmMakefile*m)
  43. {m_Makefile = m; }
  44. /**
  45. * This is called by the cmMakefile when the command is first
  46. * encountered in the CMakeLists.txt file. It expands the command's
  47. * arguments and then invokes the InitialPass.
  48. */
  49. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args)
  50. {
  51. std::vector<std::string> expandedArguments;
  52. m_Makefile->ExpandArguments(args, expandedArguments);
  53. return this->InitialPass(expandedArguments);
  54. }
  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& args) = 0;
  60. /**
  61. * This is called at the end after all the information
  62. * specified by the command is accumulated. Most commands do
  63. * not implement this method. At this point, reading and
  64. * writing to the cache can be done.
  65. */
  66. virtual void FinalPass() {};
  67. /**
  68. * This is a virtual constructor for the command.
  69. */
  70. virtual cmCommand* Clone() = 0;
  71. /**
  72. * This determines if the command gets propagated down
  73. * to makefiles located in subdirectories.
  74. */
  75. virtual bool IsInherited()
  76. {
  77. return false;
  78. }
  79. /**
  80. * This determines if the method is deprecated or not.
  81. */
  82. virtual bool IsDeprecated(int /*major*/, int /*minor*/)
  83. {
  84. return false;
  85. }
  86. /**
  87. * The name of the command as specified in CMakeList.txt.
  88. */
  89. virtual const char* GetName() = 0;
  90. /**
  91. * Succinct documentation.
  92. */
  93. virtual const char* GetTerseDocumentation() = 0;
  94. /**
  95. * More documentation.
  96. */
  97. virtual const char* GetFullDocumentation() = 0;
  98. /**
  99. * Enable the command.
  100. */
  101. void EnabledOn()
  102. {m_Enabled = true;}
  103. /**
  104. * Disable the command.
  105. */
  106. void EnabledOff()
  107. {m_Enabled = false;}
  108. /**
  109. * Query whether the command is enabled.
  110. */
  111. bool GetEnabled()
  112. {return m_Enabled;}
  113. /**
  114. * Disable or enable the command.
  115. */
  116. void SetEnabled(bool enabled)
  117. {m_Enabled = enabled;}
  118. /**
  119. * Return the last error string.
  120. */
  121. const char* GetError()
  122. {
  123. if(m_Error.length() == 0)
  124. {
  125. m_Error = this->GetName();
  126. m_Error += " unknown error.";
  127. }
  128. return m_Error.c_str();
  129. }
  130. /**
  131. * Returns true if this class is the given class, or a subclass of it.
  132. */
  133. static bool IsTypeOf(const char *type)
  134. { return !strcmp("cmCommand", type); }
  135. /**
  136. * Returns true if this object is an instance of the given class or
  137. * a subclass of it.
  138. */
  139. virtual bool IsA(const char *type)
  140. { return cmCommand::IsTypeOf(type); }
  141. protected:
  142. void SetError(const char* e)
  143. {
  144. m_Error = this->GetName();
  145. m_Error += " ";
  146. m_Error += e;
  147. }
  148. cmMakefile* m_Makefile;
  149. private:
  150. bool m_Enabled;
  151. std::string m_Error;
  152. };
  153. // All subclasses of cmCommand should invoke this macro.
  154. #define cmTypeMacro(thisClass,superclass) \
  155. typedef superclass Superclass; \
  156. static bool IsTypeOf(const char *type) \
  157. { \
  158. if ( !strcmp(#thisClass,type) ) \
  159. { \
  160. return true; \
  161. } \
  162. return Superclass::IsTypeOf(type); \
  163. } \
  164. virtual bool IsA(const char *type) \
  165. { \
  166. return thisClass::IsTypeOf(type); \
  167. } \
  168. static thisClass* SafeDownCast(cmCommand *c) \
  169. { \
  170. if ( c && c->IsA(#thisClass) ) \
  171. { \
  172. return (thisClass *)c; \
  173. } \
  174. return 0;\
  175. }
  176. #endif