cmake.h 13 KB

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