cmake.h 11 KB

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