cmake.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 cmVariableWatch;
  42. class cmake
  43. {
  44. public:
  45. ///! construct an instance of cmake
  46. cmake();
  47. ///! destruct an instance of cmake
  48. ~cmake();
  49. /**
  50. * Return major and minor version numbers for cmake.
  51. */
  52. static unsigned int GetMajorVersion();
  53. static unsigned int GetMinorVersion();
  54. static const char *GetReleaseVersion();
  55. //@{
  56. /**
  57. * Set/Get the home directory (or output directory) in the project. The
  58. * home directory is the top directory of the project. It is where
  59. * cmake was run. Remember that CMake processes
  60. * CMakeLists files by recursing up the tree starting at the StartDirectory
  61. * and going up until it reaches the HomeDirectory.
  62. */
  63. void SetHomeDirectory(const char* dir);
  64. const char* GetHomeDirectory() const
  65. {
  66. return m_cmHomeDirectory.c_str();
  67. }
  68. void SetHomeOutputDirectory(const char* lib);
  69. const char* GetHomeOutputDirectory() const
  70. {
  71. return m_HomeOutputDirectory.c_str();
  72. }
  73. //@}
  74. //@{
  75. /**
  76. * Set/Get the start directory (or output directory). The start directory
  77. * is the directory of the CMakeLists.txt file that started the current
  78. * round of processing. Remember that CMake processes CMakeLists files by
  79. * recursing up the tree starting at the StartDirectory and going up until
  80. * it reaches the HomeDirectory.
  81. */
  82. void SetStartDirectory(const char* dir)
  83. {
  84. m_cmStartDirectory = dir;
  85. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  86. }
  87. const char* GetStartDirectory() const
  88. {
  89. return m_cmStartDirectory.c_str();
  90. }
  91. void SetStartOutputDirectory(const char* lib)
  92. {
  93. m_StartOutputDirectory = lib;
  94. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  95. }
  96. const char* GetStartOutputDirectory() const
  97. {
  98. return m_StartOutputDirectory.c_str();
  99. }
  100. //@}
  101. /**
  102. * Dump documentation to a file. If 0 is returned, the
  103. * operation failed.
  104. */
  105. int DumpDocumentationToFile(std::ostream&);
  106. /**
  107. * Handle a command line invocation of cmake.
  108. */
  109. int Run(const std::vector<std::string>&args);
  110. /**
  111. * Generate the SourceFilesList from the SourceLists. This should only be
  112. * done once to be safe. The argument is a list of command line
  113. * arguments. The first argument should be the name or full path
  114. * to the command line version of cmake. For building a GUI,
  115. * you would pass in the following arguments:
  116. * /path/to/cmake -H/path/to/source -B/path/to/build
  117. * If you only want to parse the CMakeLists.txt files,
  118. * but not actually generate the makefiles, use buildMakefiles = false.
  119. */
  120. int Generate();
  121. /**
  122. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  123. * one has not already been set. It will then Call Configure on the
  124. * GlobalGenerator. This in turn will read in an process all the CMakeList
  125. * files for the tree. It will not produce any actual Makefiles, or
  126. * workspaces. Generate does that. */
  127. int Configure();
  128. /**
  129. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  130. * one has not already been set. It will then Call Configure on the
  131. * GlobalGenerator. This in turn will read in an process all the CMakeList
  132. * files for the tree. It will not produce any actual Makefiles, or
  133. * workspaces. Generate does that. */
  134. int LoadCache();
  135. ///! Create a GlobalGenerator
  136. cmGlobalGenerator* CreateGlobalGenerator(const char* name);
  137. ///! Return the global generator assigned to this instance of cmake
  138. cmGlobalGenerator* GetGlobalGenerator() { return m_GlobalGenerator; };
  139. ///! Return the global generator assigned to this instance of cmake
  140. void SetGlobalGenerator(cmGlobalGenerator *);
  141. ///! Get the names of the current registered generators
  142. void GetRegisteredGenerators(std::vector<std::string>& names);
  143. ///! get the cmCachemManager used by this invocation of cmake
  144. cmCacheManager *GetCacheManager() { return m_CacheManager; }
  145. ///! set the cmake command this instance of cmake should use
  146. void SetCMakeCommand(const char* cmd) { m_CMakeCommand = cmd; }
  147. /**
  148. * Given a variable name, return its value (as a string).
  149. */
  150. const char* GetCacheDefinition(const char*) const;
  151. ///! Add an entry into the cache
  152. void AddCacheEntry(const char* key, const char* value,
  153. const char* helpString,
  154. int type);
  155. /**
  156. * Execute commands during the build process. Supports options such
  157. * as echo, remove file etc.
  158. */
  159. static int CMakeCommand(std::vector<std::string>&);
  160. /**
  161. * Add a command to this cmake instance
  162. */
  163. void AddCommand(cmCommand* );
  164. /**
  165. * Get a command by its name
  166. */
  167. cmCommand *GetCommand(const char *name);
  168. /** Check if a command exists. */
  169. bool CommandExists(const char* name) const;
  170. /**
  171. * Is cmake in the process of a local cmake invocation. If so, we know the
  172. * cache is already configured and ready to go.
  173. */
  174. bool GetLocal() { return m_Local; }
  175. ///! Display command line useage
  176. void Usage(const char *program);
  177. ///! Parse command line arguments
  178. void SetArgs(const std::vector<std::string>&);
  179. ///! Is this cmake running as a result of a TRY_COMPILE command
  180. bool GetIsInTryCompile() { return m_InTryCompile; }
  181. ///! Is this cmake running as a result of a TRY_COMPILE command
  182. void SetIsInTryCompile(bool i) { m_InTryCompile = i; }
  183. ///! Parse command line arguments that might set cache values
  184. void SetCacheArgs(const std::vector<std::string>&);
  185. typedef void (*ProgressCallback)(const char*msg, float progress, void *);
  186. /**
  187. * Set the function used by GUI's to receive progress updates
  188. * Function gets passed: message as a const char*, a progress
  189. * amount ranging from 0 to 1.0 and client data. The progress
  190. * number provided may be negative in cases where a message is
  191. * to be displayed without any progress percentage.
  192. */
  193. void SetProgressCallback(ProgressCallback f, void* clientData=0);
  194. ///! this is called by generators to update the progress
  195. void UpdateProgress(const char *msg, float prog);
  196. ///! Get the variable watch object
  197. cmVariableWatch* GetVariableWatch() { return m_VariableWatch; }
  198. void GetCommandDocumentation(std::vector<cmDocumentationEntry>&) const;
  199. protected:
  200. typedef std::map<cmStdString, cmCommand*> RegisteredCommandsMap;
  201. RegisteredCommandsMap m_Commands;
  202. void AddDefaultCommands();
  203. cmGlobalGenerator *m_GlobalGenerator;
  204. cmCacheManager *m_CacheManager;
  205. std::string m_cmHomeDirectory;
  206. std::string m_HomeOutputDirectory;
  207. std::string m_cmStartDirectory;
  208. std::string m_StartOutputDirectory;
  209. ///! return true if the same cmake was used to make the cache.
  210. bool CacheVersionMatches();
  211. ///! read in a cmake list file to initialize the cache
  212. void ReadListFile(const char *path);
  213. ///! used by Run
  214. int LocalGenerate();
  215. /**
  216. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  217. */
  218. int AddCMakePaths(const char *arg0);
  219. cmVariableWatch* m_VariableWatch;
  220. private:
  221. ProgressCallback m_ProgressCallback;
  222. void* m_ProgressCallbackClientData;
  223. bool m_Verbose;
  224. bool m_Local;
  225. bool m_InTryCompile;
  226. std::string m_CMakeCommand;
  227. const char* m_CXXEnvironment;
  228. const char* m_CCEnvironment;
  229. };
  230. #define CMAKE_STANDARD_OPTIONS_TABLE \
  231. {"-C<initial-cache>", "Pre-load cmake cache from given file.", \
  232. "When cmake is first run in an empty build tree, it creates a " \
  233. "CMakeCache.txt file and populates it with customizable settings " \
  234. "for the project. This option may be used to specify a file from " \
  235. "which to load cache entries before the first pass through " \
  236. "the project's cmake listfiles. The loaded entries take priority " \
  237. "over the project's default values."}, \
  238. {"-D<var>:<type>=<value>", "Create a cmake cache entry.", \
  239. "When cmake is first run in an empty build tree, it creates a " \
  240. "CMakeCache.txt file and populates it with customizable settings " \
  241. "for the project. This option may be used to specify a setting " \
  242. "that takes priority over the project's default value. The option " \
  243. "may be repeated for as many cache entries as desired."}, \
  244. {"-G<generator-name>", "Specify a makefile generator.", \
  245. "CMake may support multiple native build systems on certain platforms. " \
  246. "A makefile generator is responsible for generating a particular build " \
  247. "system. Possible generator names are\n" \
  248. " \"Unix Makefiles\" - Standard UNIX Makefiles"}