cmCommand.h 4.8 KB

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