cmCommand.h 4.6 KB

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