cmCommand.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 command is invoked when in script mode.
  81. */
  82. virtual bool IsScriptable()
  83. {
  84. return false;
  85. }
  86. /**
  87. * This determines if the method is deprecated or not.
  88. */
  89. virtual bool IsDeprecated(int /*major*/, int /*minor*/)
  90. {
  91. return false;
  92. }
  93. /**
  94. * The name of the command as specified in CMakeList.txt.
  95. */
  96. virtual const char* GetName() = 0;
  97. /**
  98. * Succinct documentation.
  99. */
  100. virtual const char* GetTerseDocumentation() = 0;
  101. /**
  102. * More documentation.
  103. */
  104. virtual const char* GetFullDocumentation() = 0;
  105. /**
  106. * Enable the command.
  107. */
  108. void EnabledOn()
  109. {m_Enabled = true;}
  110. /**
  111. * Disable the command.
  112. */
  113. void EnabledOff()
  114. {m_Enabled = false;}
  115. /**
  116. * Query whether the command is enabled.
  117. */
  118. bool GetEnabled()
  119. {return m_Enabled;}
  120. /**
  121. * Disable or enable the command.
  122. */
  123. void SetEnabled(bool enabled)
  124. {m_Enabled = enabled;}
  125. /**
  126. * Return the last error string.
  127. */
  128. const char* GetError()
  129. {
  130. if(m_Error.length() == 0)
  131. {
  132. m_Error = this->GetName();
  133. m_Error += " unknown error.";
  134. }
  135. return m_Error.c_str();
  136. }
  137. /**
  138. * Returns true if this class is the given class, or a subclass of it.
  139. */
  140. static bool IsTypeOf(const char *type)
  141. { return !strcmp("cmCommand", type); }
  142. /**
  143. * Returns true if this object is an instance of the given class or
  144. * a subclass of it.
  145. */
  146. virtual bool IsA(const char *type)
  147. { return cmCommand::IsTypeOf(type); }
  148. protected:
  149. void SetError(const char* e)
  150. {
  151. m_Error = this->GetName();
  152. m_Error += " ";
  153. m_Error += e;
  154. }
  155. cmMakefile* m_Makefile;
  156. private:
  157. bool m_Enabled;
  158. std::string m_Error;
  159. };
  160. // All subclasses of cmCommand should invoke this macro.
  161. #define cmTypeMacro(thisClass,superclass) \
  162. typedef superclass Superclass; \
  163. static bool IsTypeOf(const char *type) \
  164. { \
  165. if ( !strcmp(#thisClass,type) ) \
  166. { \
  167. return true; \
  168. } \
  169. return Superclass::IsTypeOf(type); \
  170. } \
  171. virtual bool IsA(const char *type) \
  172. { \
  173. return thisClass::IsTypeOf(type); \
  174. } \
  175. static thisClass* SafeDownCast(cmCommand *c) \
  176. { \
  177. if ( c && c->IsA(#thisClass) ) \
  178. { \
  179. return (thisClass *)c; \
  180. } \
  181. return 0;\
  182. }
  183. #endif