cmCommand.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmCommand_h
  11. #define cmCommand_h
  12. #include "cmObject.h"
  13. #include "cmListFileCache.h"
  14. #include "cmMakefile.h"
  15. #include "cmCommandArgumentsHelper.h"
  16. /** \class cmCommand
  17. * \brief Superclass for all commands in CMake.
  18. *
  19. * cmCommand is the base class for all commands in CMake. A command
  20. * manifests as an entry in CMakeLists.txt and produces one or
  21. * more makefile rules. Commands are associated with a particular
  22. * makefile. This base class cmCommand defines the API for commands
  23. * to support such features as enable/disable, inheritance,
  24. * documentation, and construction.
  25. */
  26. class cmCommand : public cmObject
  27. {
  28. public:
  29. cmTypeMacro(cmCommand, cmObject);
  30. /**
  31. * Construct the command. By default it is enabled with no makefile.
  32. */
  33. cmCommand()
  34. {this->Makefile = 0; this->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. {this->Makefile = m; }
  44. cmMakefile* GetMakefile() { return this->Makefile; }
  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. cmExecutionStatus &status)
  52. {
  53. std::vector<std::string> expandedArguments;
  54. if(!this->Makefile->ExpandArguments(args, expandedArguments))
  55. {
  56. // There was an error expanding arguments. It was already
  57. // reported, so we can skip this command without error.
  58. return true;
  59. }
  60. return this->InitialPass(expandedArguments,status);
  61. }
  62. /**
  63. * This is called when the command is first encountered in
  64. * the CMakeLists.txt file.
  65. */
  66. virtual bool InitialPass(std::vector<std::string> const& args,
  67. cmExecutionStatus &) = 0;
  68. /**
  69. * This is called at the end after all the information
  70. * specified by the command is accumulated. Most commands do
  71. * not implement this method. At this point, reading and
  72. * writing to the cache can be done.
  73. */
  74. virtual void FinalPass() {};
  75. /**
  76. * Does this command have a final pass? Query after InitialPass.
  77. */
  78. virtual bool HasFinalPass() const { return false; }
  79. /**
  80. * This is a virtual constructor for the command.
  81. */
  82. virtual cmCommand* Clone() = 0;
  83. /**
  84. * This determines if the command is invoked when in script mode.
  85. */
  86. virtual bool IsScriptable() const
  87. {
  88. return false;
  89. }
  90. /**
  91. * This determines if usage of the method is discouraged or not.
  92. * This is currently only used for generating the documentation.
  93. */
  94. virtual bool IsDiscouraged() const
  95. {
  96. return false;
  97. }
  98. /**
  99. * This is used to avoid including this command
  100. * in documentation. This is mainly used by
  101. * cmMacroHelperCommand and cmFunctionHelperCommand
  102. * which cannot provide appropriate documentation.
  103. */
  104. virtual bool ShouldAppearInDocumentation() const
  105. {
  106. return true;
  107. }
  108. /**
  109. * The name of the command as specified in CMakeList.txt.
  110. */
  111. virtual const char* GetName() const = 0;
  112. /**
  113. * Enable the command.
  114. */
  115. void EnabledOn()
  116. {this->Enabled = true;}
  117. /**
  118. * Disable the command.
  119. */
  120. void EnabledOff()
  121. {this->Enabled = false;}
  122. /**
  123. * Query whether the command is enabled.
  124. */
  125. bool GetEnabled() const
  126. {return this->Enabled;}
  127. /**
  128. * Disable or enable the command.
  129. */
  130. void SetEnabled(bool enabled)
  131. {this->Enabled = enabled;}
  132. /**
  133. * Return the last error string.
  134. */
  135. const char* GetError()
  136. {
  137. if(this->Error.length() == 0)
  138. {
  139. this->Error = this->GetName();
  140. this->Error += " unknown error.";
  141. }
  142. return this->Error.c_str();
  143. }
  144. /**
  145. * Set the error message
  146. */
  147. void SetError(const char* e)
  148. {
  149. this->Error = this->GetName();
  150. this->Error += " ";
  151. this->Error += e;
  152. }
  153. protected:
  154. cmMakefile* Makefile;
  155. cmCommandArgumentsHelper Helper;
  156. private:
  157. bool Enabled;
  158. std::string Error;
  159. };
  160. #endif