cmake.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // This class represents a cmake invocation. It is the top level class when
  14. // running cmake. Most cmake based GUIS should primarily create an instance
  15. // of this class and communicate with it.
  16. //
  17. // The basic process for a GUI is as follows:
  18. //
  19. // 1) Create a cmake instance
  20. // 2) Set the Home & Start directories, generator, and cmake command. this
  21. // can be done using the Set methods or by using SetArgs and passing in
  22. // command line arguments.
  23. // 3) Load the cache by calling LoadCache (duh)
  24. // 4) if you are using command line arguments with -D or -C flags then
  25. // call SetCacheArgs (or if for some other reason you want to modify the
  26. // cache, do it now.
  27. // 5) Finally call Configure
  28. // 6) Let the user change values and go back to step 5
  29. // 7) call Generate
  30. //
  31. // If your GUI allows the user to change the start & home directories then
  32. // you must at a minimum redo steps 2 through 7.
  33. //
  34. #include "cmStandardIncludes.h"
  35. #include "cmSystemTools.h"
  36. class cmGlobalGenerator;
  37. class cmLocalGenerator;
  38. class cmCacheManager;
  39. class cmMakefile;
  40. class cmCommand;
  41. class cmake
  42. {
  43. public:
  44. ///! construct an instance of cmake
  45. cmake();
  46. ///! destruct an instance of cmake
  47. ~cmake();
  48. /**
  49. * Return major and minor version numbers for cmake.
  50. */
  51. static unsigned int GetMajorVersion();
  52. static unsigned int GetMinorVersion();
  53. static const char *GetReleaseVersion();
  54. //@{
  55. /**
  56. * Set/Get the home directory (or output directory) in the project. The
  57. * home directory is the top directory of the project. It is where
  58. * cmake was run. Remember that CMake processes
  59. * CMakeLists files by recursing up the tree starting at the StartDirectory
  60. * and going up until it reaches the HomeDirectory.
  61. */
  62. void SetHomeDirectory(const char* dir);
  63. const char* GetHomeDirectory() const
  64. {
  65. return m_cmHomeDirectory.c_str();
  66. }
  67. void SetHomeOutputDirectory(const char* lib);
  68. const char* GetHomeOutputDirectory() const
  69. {
  70. return m_HomeOutputDirectory.c_str();
  71. }
  72. //@}
  73. //@{
  74. /**
  75. * Set/Get the start directory (or output directory). The start directory
  76. * is the directory of the CMakeLists.txt file that started the current
  77. * round of processing. Remember that CMake processes CMakeLists files by
  78. * recursing up the tree starting at the StartDirectory and going up until
  79. * it reaches the HomeDirectory.
  80. */
  81. void SetStartDirectory(const char* dir)
  82. {
  83. m_cmStartDirectory = dir;
  84. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  85. }
  86. const char* GetStartDirectory() const
  87. {
  88. return m_cmStartDirectory.c_str();
  89. }
  90. void SetStartOutputDirectory(const char* lib)
  91. {
  92. m_StartOutputDirectory = lib;
  93. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  94. }
  95. const char* GetStartOutputDirectory() const
  96. {
  97. return m_StartOutputDirectory.c_str();
  98. }
  99. //@}
  100. /**
  101. * Dump documentation to a file. If 0 is returned, the
  102. * operation failed.
  103. */
  104. int DumpDocumentationToFile(std::ostream&);
  105. /**
  106. * Handle a command line invocation of cmake.
  107. */
  108. int Run(const std::vector<std::string>&args);
  109. /**
  110. * Generate the SourceFilesList from the SourceLists. This should only be
  111. * done once to be safe. The argument is a list of command line
  112. * arguments. The first argument should be the name or full path
  113. * to the command line version of cmake. For building a GUI,
  114. * you would pass in the following arguments:
  115. * /path/to/cmake -H/path/to/source -B/path/to/build
  116. * If you only want to parse the CMakeLists.txt files,
  117. * but not actually generate the makefiles, use buildMakefiles = false.
  118. */
  119. int Generate();
  120. /**
  121. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  122. * one has not already been set. It will then Call Configure on the
  123. * GlobalGenerator. This in turn will read in an process all the CMakeList
  124. * files for the tree. It will not produce any actual Makefiles, or
  125. * workspaces. Generate does that. */
  126. int Configure();
  127. /**
  128. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  129. * one has not already been set. It will then Call Configure on the
  130. * GlobalGenerator. This in turn will read in an process all the CMakeList
  131. * files for the tree. It will not produce any actual Makefiles, or
  132. * workspaces. Generate does that. */
  133. int LoadCache();
  134. ///! Create a GlobalGenerator
  135. cmGlobalGenerator* CreateGlobalGenerator(const char* name);
  136. ///! Return the global generator assigned to this instance of cmake
  137. cmGlobalGenerator* GetGlobalGenerator() { return m_GlobalGenerator; };
  138. ///! Return the global generator assigned to this instance of cmake
  139. void SetGlobalGenerator(cmGlobalGenerator *);
  140. ///! Get the names of the current registered generators
  141. void GetRegisteredGenerators(std::vector<std::string>& names);
  142. ///! get the cmCachemManager used by this invocation of cmake
  143. cmCacheManager *GetCacheManager() { return m_CacheManager; }
  144. ///! set the cmake command this instance of cmake should use
  145. void SetCMakeCommand(const char* cmd) { m_CMakeCommand = cmd; }
  146. /**
  147. * Given a variable name, return its value (as a string).
  148. */
  149. const char* GetCacheDefinition(const char*) const;
  150. ///! Add an entry into the cache
  151. void AddCacheEntry(const char* key, const char* value,
  152. const char* helpString,
  153. int type);
  154. /**
  155. * Execute commands during the build process. Supports options such
  156. * as echo, remove file etc.
  157. */
  158. static int CMakeCommand(std::vector<std::string>&);
  159. /**
  160. * Add a command to this cmake instance
  161. */
  162. void AddCommand(cmCommand* );
  163. /**
  164. * Get a command by its name
  165. */
  166. cmCommand *GetCommand(const char *name);
  167. /** Check if a command exists. */
  168. bool CommandExists(const char* name) const;
  169. /**
  170. * Is cmake in the process of a local cmake invocation. If so, we know the
  171. * cache is already configured and ready to go.
  172. */
  173. bool GetLocal() { return m_Local; }
  174. ///! Display command line useage
  175. void Usage(const char *program);
  176. ///! Parse command line arguments
  177. void SetArgs(const std::vector<std::string>&);
  178. ///! Is this cmake running as a result of a TRY_COMPILE command
  179. bool GetIsInTryCompile() { return m_InTryCompile; }
  180. ///! Is this cmake running as a result of a TRY_COMPILE command
  181. void SetIsInTryCompile(bool i) { m_InTryCompile = i; }
  182. ///! Parse command line arguments that might set cache values
  183. void SetCacheArgs(const std::vector<std::string>&);
  184. typedef void (*ProgressCallback)(const char*msg, float progress, void *);
  185. /**
  186. * Set the function used by GUI's to receive progress updates
  187. * Function gets passed: message as a const char*, a progress
  188. * amount ranging from 0 to 1.0 and client data. The progress
  189. * number provided may be negative in cases where a message is
  190. * to be displayed without any progress percentage.
  191. */
  192. void SetProgressCallback(ProgressCallback f, void* clientData=0);
  193. ///! this is called by generators to update the progress
  194. void UpdateProgress(const char *msg, float prog);
  195. protected:
  196. typedef std::map<cmStdString, cmCommand*> RegisteredCommandsMap;
  197. RegisteredCommandsMap m_Commands;
  198. void AddDefaultCommands();
  199. cmGlobalGenerator *m_GlobalGenerator;
  200. cmCacheManager *m_CacheManager;
  201. std::string m_cmHomeDirectory;
  202. std::string m_HomeOutputDirectory;
  203. std::string m_cmStartDirectory;
  204. std::string m_StartOutputDirectory;
  205. ///! return true if the same cmake was used to make the cache.
  206. bool CacheVersionMatches();
  207. ///! read in a cmake list file to initialize the cache
  208. void ReadListFile(const char *path);
  209. ///! used by Run
  210. int LocalGenerate();
  211. /**
  212. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  213. */
  214. int AddCMakePaths(const char *arg0);
  215. private:
  216. ProgressCallback m_ProgressCallback;
  217. void* m_ProgressCallbackClientData;
  218. bool m_Verbose;
  219. bool m_Local;
  220. bool m_InTryCompile;
  221. std::string m_CMakeCommand;
  222. };