cmake.h 12 KB

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