cmake.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #include "cmPropertyDefinitionMap.h"
  38. #include "cmPropertyMap.h"
  39. class cmGlobalGenerator;
  40. class cmLocalGenerator;
  41. class cmCacheManager;
  42. class cmMakefile;
  43. class cmCommand;
  44. class cmVariableWatch;
  45. class cmFileTimeComparison;
  46. class cmExternalMakefileProjectGenerator;
  47. class cmake
  48. {
  49. public:
  50. typedef std::map<cmStdString, cmCommand*> RegisteredCommandsMap;
  51. ///! construct an instance of cmake
  52. cmake();
  53. ///! destruct an instance of cmake
  54. ~cmake();
  55. ///! construct an instance of cmake
  56. static const char *GetCMakeFilesDirectory() {return "/CMakeFiles";};
  57. static const char *GetCMakeFilesDirectoryPostSlash() {
  58. return "CMakeFiles/";};
  59. //@{
  60. /**
  61. * Set/Get the home directory (or output directory) in the project. The
  62. * home directory is the top directory of the project. It is where
  63. * cmake was run. Remember that CMake processes
  64. * CMakeLists files by recursing up the tree starting at the StartDirectory
  65. * and going up until it reaches the HomeDirectory.
  66. */
  67. void SetHomeDirectory(const char* dir);
  68. const char* GetHomeDirectory() const
  69. {
  70. return this->cmHomeDirectory.c_str();
  71. }
  72. void SetHomeOutputDirectory(const char* lib);
  73. const char* GetHomeOutputDirectory() const
  74. {
  75. return this->HomeOutputDirectory.c_str();
  76. }
  77. //@}
  78. //@{
  79. /**
  80. * Set/Get the start directory (or output directory). The start directory
  81. * is the directory of the CMakeLists.txt file that started the current
  82. * round of processing. Remember that CMake processes CMakeLists files by
  83. * recursing up the tree starting at the StartDirectory and going up until
  84. * it reaches the HomeDirectory.
  85. */
  86. void SetStartDirectory(const char* dir)
  87. {
  88. this->cmStartDirectory = dir;
  89. cmSystemTools::ConvertToUnixSlashes(this->cmStartDirectory);
  90. }
  91. const char* GetStartDirectory() const
  92. {
  93. return this->cmStartDirectory.c_str();
  94. }
  95. void SetStartOutputDirectory(const char* lib)
  96. {
  97. this->StartOutputDirectory = lib;
  98. cmSystemTools::ConvertToUnixSlashes(this->StartOutputDirectory);
  99. }
  100. const char* GetStartOutputDirectory() const
  101. {
  102. return this->StartOutputDirectory.c_str();
  103. }
  104. //@}
  105. /**
  106. * Dump documentation to a file. If 0 is returned, the
  107. * operation failed.
  108. */
  109. int DumpDocumentationToFile(std::ostream&);
  110. /**
  111. * Handle a command line invocation of cmake.
  112. */
  113. int Run(const std::vector<std::string>&args)
  114. { return this->Run(args, false); }
  115. int Run(const std::vector<std::string>&args, bool noconfigure);
  116. /**
  117. * Run the global generator Generate step.
  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. void PreLoadCMakeFiles();
  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 this->GlobalGenerator; }
  139. ///! Return the global generator assigned to this instance of cmake, const
  140. const cmGlobalGenerator* GetGlobalGenerator() const
  141. { return this->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 this->CacheManager; }
  148. ///! set the cmake command this instance of cmake should use
  149. void SetCMakeCommand(const char* cmd) { this->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 ExecuteCMakeCommand(std::vector<std::string>&);
  163. /**
  164. * Get the system information and write it to the file specified
  165. */
  166. int GetSystemInformation(std::vector<std::string>&);
  167. /**
  168. * Add a command to this cmake instance
  169. */
  170. void AddCommand(cmCommand* );
  171. void RenameCommand(const char* oldName, const char* newName);
  172. void RemoveCommand(const char* name);
  173. /**
  174. * Get a command by its name
  175. */
  176. cmCommand *GetCommand(const char *name);
  177. /** Get list of all commands */
  178. RegisteredCommandsMap* GetCommands() { return &this->Commands; }
  179. /** Check if a command exists. */
  180. bool CommandExists(const char* name) const;
  181. ///! Parse command line arguments
  182. void SetArgs(const std::vector<std::string>&);
  183. ///! Is this cmake running as a result of a TRY_COMPILE command
  184. bool GetIsInTryCompile() { return this->InTryCompile; }
  185. ///! Is this cmake running as a result of a TRY_COMPILE command
  186. void SetIsInTryCompile(bool i) { this->InTryCompile = i; }
  187. ///! Parse command line arguments that might set cache values
  188. bool SetCacheArgs(const std::vector<std::string>&);
  189. typedef void (*ProgressCallbackType)
  190. (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(ProgressCallbackType 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 this->VariableWatch; }
  203. /** Get the documentation entries for the supported commands.
  204. * If withCurrentCommands is true, the documentation for the
  205. * recommended set of commands is included.
  206. * If withCompatCommands is true, the documentation for discouraged
  207. * (compatibility) commands is included.
  208. * You probably don't want to set both to false.
  209. */
  210. void GetCommandDocumentation(std::vector<cmDocumentationEntry>& entries,
  211. bool withCurrentCommands = true,
  212. bool withCompatCommands = true) const;
  213. void GetPropertiesDocumentation(std::vector<cmDocumentationEntry>&);
  214. void GetGeneratorDocumentation(std::vector<cmDocumentationEntry>&);
  215. ///! Set/Get a property of this target file
  216. void SetProperty(const char *prop, const char *value);
  217. const char *GetProperty(const char *prop);
  218. const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
  219. bool GetPropertyAsBool(const char *prop);
  220. // Get the properties
  221. cmPropertyMap &GetProperties() { return this->Properties; };
  222. ///! Do all the checks before running configure
  223. int DoPreConfigureChecks();
  224. /**
  225. * Set and get the script mode option. In script mode there is no
  226. * generator and no cache. Also, language are not enabled, so
  227. * add_executable and things do not do anything.
  228. */
  229. void SetScriptMode(bool mode) { this->ScriptMode = mode; }
  230. bool GetScriptMode() { return this->ScriptMode; }
  231. ///! Debug the try compile stuff by not delelting the files
  232. bool GetDebugTryCompile(){return this->DebugTryCompile;}
  233. void DebugTryCompileOn(){this->DebugTryCompile = true;}
  234. ///! Get the list of files written by CMake using FILE(WRITE / WRITE_FILE
  235. void AddWrittenFile(const char* file);
  236. bool HasWrittenFile(const char* file);
  237. void CleanupWrittenFiles();
  238. /**
  239. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  240. */
  241. int AddCMakePaths(const char *arg0);
  242. /**
  243. * Get the file comparison class
  244. */
  245. cmFileTimeComparison* GetFileComparison() { return this->FileComparison; }
  246. /**
  247. * Get the path to ctest
  248. */
  249. const char* GetCTestCommand();
  250. const char* GetCPackCommand();
  251. const char* GetCMakeCommand() const { return this->CMakeCommand.c_str(); }
  252. // Do we want debug output during the cmake run.
  253. bool GetDebugOutput() { return this->DebugOutput; }
  254. void DebugOutputOn() { this->DebugOutput = true;}
  255. // Define a property
  256. void DefineProperty(const char *name, cmProperty::ScopeType scope,
  257. const char *ShortDescription,
  258. const char *FullDescription,
  259. bool chain = false);
  260. // Is a property defined?
  261. bool IsPropertyDefined(const char *name, cmProperty::ScopeType scope);
  262. bool IsPropertyChained(const char *name, cmProperty::ScopeType scope);
  263. protected:
  264. cmPropertyMap Properties;
  265. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>
  266. PropertyDefinitions;
  267. typedef
  268. cmExternalMakefileProjectGenerator* (*CreateExtraGeneratorFunctionType)();
  269. typedef std::map<cmStdString,
  270. CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap;
  271. typedef cmGlobalGenerator* (*CreateGeneratorFunctionType)();
  272. typedef std::map<cmStdString,
  273. CreateGeneratorFunctionType> RegisteredGeneratorsMap;
  274. RegisteredCommandsMap Commands;
  275. RegisteredGeneratorsMap Generators;
  276. RegisteredExtraGeneratorsMap ExtraGenerators;
  277. void AddDefaultCommands();
  278. void AddDefaultGenerators();
  279. void AddDefaultExtraGenerators();
  280. void AddExtraGenerator(const char* name, CreateExtraGeneratorFunctionType newFunction);
  281. cmGlobalGenerator *GlobalGenerator;
  282. cmCacheManager *CacheManager;
  283. std::string cmHomeDirectory;
  284. std::string HomeOutputDirectory;
  285. std::string cmStartDirectory;
  286. std::string StartOutputDirectory;
  287. std::set<cmStdString> WrittenFiles;
  288. ///! return true if the same cmake was used to make the cache.
  289. bool CacheVersionMatches();
  290. ///! read in a cmake list file to initialize the cache
  291. void ReadListFile(const char *path);
  292. ///! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  293. /// If it is set, truncate it to 50kb
  294. void TruncateOutputLog(const char* fname);
  295. /**
  296. * Method called to check build system integrity at build time.
  297. * Returns 1 if CMake should rerun and 0 otherwise.
  298. */
  299. int CheckBuildSystem();
  300. void SetDirectoriesFromFile(const char* arg);
  301. //! Make sure all commands are what they say they are and there is no
  302. //macros.
  303. void CleanupCommandsAndMacros();
  304. void GenerateGraphViz(const char* fileName) const;
  305. static int ExecuteEchoColor(std::vector<std::string>& args);
  306. static int ExecuteLinkScript(std::vector<std::string>& args);
  307. cmVariableWatch* VariableWatch;
  308. ///! Find the full path to one of the cmake programs like ctest, cpack, etc.
  309. std::string FindCMakeProgram(const char* name) const;
  310. private:
  311. ProgressCallbackType ProgressCallback;
  312. void* ProgressCallbackClientData;
  313. bool Verbose;
  314. bool InTryCompile;
  315. bool ScriptMode;
  316. bool DebugOutput;
  317. std::string CMakeCommand;
  318. std::string CXXEnvironment;
  319. std::string CCEnvironment;
  320. std::string CheckBuildSystemArgument;
  321. std::string CTestCommand;
  322. std::string CPackCommand;
  323. bool ClearBuildSystem;
  324. bool DebugTryCompile;
  325. cmFileTimeComparison* FileComparison;
  326. std::string GraphVizFile;
  327. void UpdateConversionPathTable();
  328. };
  329. #define CMAKE_STANDARD_OPTIONS_TABLE \
  330. {"-C <initial-cache>", "Pre-load a script to populate the cache.", \
  331. "When cmake is first run in an empty build tree, it creates a " \
  332. "CMakeCache.txt file and populates it with customizable settings " \
  333. "for the project. This option may be used to specify a file from " \
  334. "which to load cache entries before the first pass through " \
  335. "the project's cmake listfiles. The loaded entries take priority " \
  336. "over the project's default values. The given file should be a CMake " \
  337. "script containing SET commands that use the CACHE option, " \
  338. "not a cache-format file."}, \
  339. {"-D <var>:<type>=<value>", "Create a cmake cache entry.", \
  340. "When cmake is first run in an empty build tree, it creates a " \
  341. "CMakeCache.txt file and populates it with customizable settings " \
  342. "for the project. This option may be used to specify a setting " \
  343. "that takes priority over the project's default value. The option " \
  344. "may be repeated for as many cache entries as desired."}, \
  345. {"-U <globbing_expr>", "Remove matching entries from CMake cache.", \
  346. "This option may be used to remove one or more variables from the " \
  347. "CMakeCache.txt file, globbing expressions using * and ? are supported. "\
  348. "The option may be repeated for as many cache entries as desired.\n" \
  349. "Use with care, you can make your CMakeCache.txt non-working."}, \
  350. {"-G <generator-name>", "Specify a makefile generator.", \
  351. "CMake may support multiple native build systems on certain platforms. " \
  352. "A makefile generator is responsible for generating a particular build " \
  353. "system. Possible generator names are specified in the Generators " \
  354. "section."}
  355. #define CMAKE_STANDARD_INTRODUCTION \
  356. {0, \
  357. "CMake is a cross-platform build system generator. Projects " \
  358. "specify their build process with platform-independent CMake listfiles " \
  359. "included in each directory of a source tree with the name " \
  360. "CMakeLists.txt. " \
  361. "Users build a project by using CMake to generate a build system " \
  362. "for a native tool on their platform.", 0}
  363. #endif