cmake.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. void RemoveUnscriptableCommands();
  174. /**
  175. * Get a command by its name
  176. */
  177. cmCommand *GetCommand(const char *name);
  178. /** Get list of all commands */
  179. RegisteredCommandsMap* GetCommands() { return &this->Commands; }
  180. /** Check if a command exists. */
  181. bool CommandExists(const char* name) const;
  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 this->InTryCompile; }
  186. ///! Is this cmake running as a result of a TRY_COMPILE command
  187. void SetIsInTryCompile(bool i) { this->InTryCompile = i; }
  188. ///! Parse command line arguments that might set cache values
  189. bool SetCacheArgs(const std::vector<std::string>&);
  190. typedef void (*ProgressCallbackType)
  191. (const char*msg, float progress, void *);
  192. /**
  193. * Set the function used by GUI's to receive progress updates
  194. * Function gets passed: message as a const char*, a progress
  195. * amount ranging from 0 to 1.0 and client data. The progress
  196. * number provided may be negative in cases where a message is
  197. * to be displayed without any progress percentage.
  198. */
  199. void SetProgressCallback(ProgressCallbackType f, void* clientData=0);
  200. ///! this is called by generators to update the progress
  201. void UpdateProgress(const char *msg, float prog);
  202. ///! Get the variable watch object
  203. cmVariableWatch* GetVariableWatch() { return this->VariableWatch; }
  204. /** Get the documentation entries for the supported commands.
  205. * If withCurrentCommands is true, the documentation for the
  206. * recommended set of commands is included.
  207. * If withCompatCommands is true, the documentation for discouraged
  208. * (compatibility) commands is included.
  209. * You probably don't want to set both to false.
  210. */
  211. void GetCommandDocumentation(std::vector<cmDocumentationEntry>& entries,
  212. bool withCurrentCommands = true,
  213. bool withCompatCommands = true) const;
  214. void GetPropertiesDocumentation(std::vector<cmDocumentationEntry>&,
  215. cmProperty::ScopeType type);
  216. void GetGeneratorDocumentation(std::vector<cmDocumentationEntry>&);
  217. ///! Set/Get a property of this target file
  218. void SetProperty(const char *prop, const char *value);
  219. const char *GetProperty(const char *prop);
  220. const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
  221. bool GetPropertyAsBool(const char *prop);
  222. // Get the properties
  223. cmPropertyMap &GetProperties() { return this->Properties; };
  224. ///! Do all the checks before running configure
  225. int DoPreConfigureChecks();
  226. /**
  227. * Set and get the script mode option. In script mode there is no
  228. * generator and no cache. Also, language are not enabled, so
  229. * add_executable and things do not do anything.
  230. */
  231. void SetScriptMode(bool mode) { this->ScriptMode = mode; }
  232. bool GetScriptMode() { return this->ScriptMode; }
  233. ///! Debug the try compile stuff by not delelting the files
  234. bool GetDebugTryCompile(){return this->DebugTryCompile;}
  235. void DebugTryCompileOn(){this->DebugTryCompile = true;}
  236. ///! Get the list of files written by CMake using FILE(WRITE / WRITE_FILE
  237. void AddWrittenFile(const char* file);
  238. bool HasWrittenFile(const char* file);
  239. void CleanupWrittenFiles();
  240. /**
  241. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  242. */
  243. int AddCMakePaths(const char *arg0);
  244. /**
  245. * Get the file comparison class
  246. */
  247. cmFileTimeComparison* GetFileComparison() { return this->FileComparison; }
  248. /**
  249. * Get the path to ctest
  250. */
  251. const char* GetCTestCommand();
  252. const char* GetCPackCommand();
  253. const char* GetCMakeCommand() const { return this->CMakeCommand.c_str(); }
  254. // Do we want debug output during the cmake run.
  255. bool GetDebugOutput() { return this->DebugOutput; }
  256. void DebugOutputOn() { this->DebugOutput = true;}
  257. // Define a property
  258. void DefineProperty(const char *name, cmProperty::ScopeType scope,
  259. const char *ShortDescription,
  260. const char *FullDescription,
  261. bool chain = false);
  262. // Is a property defined?
  263. bool IsPropertyDefined(const char *name, cmProperty::ScopeType scope);
  264. bool IsPropertyChained(const char *name, cmProperty::ScopeType scope);
  265. // Define the properties
  266. static void DefineProperties(cmake *cm);
  267. protected:
  268. cmPropertyMap Properties;
  269. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>
  270. PropertyDefinitions;
  271. typedef
  272. cmExternalMakefileProjectGenerator* (*CreateExtraGeneratorFunctionType)();
  273. typedef std::map<cmStdString,
  274. CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap;
  275. typedef cmGlobalGenerator* (*CreateGeneratorFunctionType)();
  276. typedef std::map<cmStdString,
  277. CreateGeneratorFunctionType> RegisteredGeneratorsMap;
  278. RegisteredCommandsMap Commands;
  279. RegisteredGeneratorsMap Generators;
  280. RegisteredExtraGeneratorsMap ExtraGenerators;
  281. void AddDefaultCommands();
  282. void AddDefaultGenerators();
  283. void AddDefaultExtraGenerators();
  284. void AddExtraGenerator(const char* name,
  285. CreateExtraGeneratorFunctionType newFunction);
  286. cmGlobalGenerator *GlobalGenerator;
  287. cmCacheManager *CacheManager;
  288. std::string cmHomeDirectory;
  289. std::string HomeOutputDirectory;
  290. std::string cmStartDirectory;
  291. std::string StartOutputDirectory;
  292. std::set<cmStdString> WrittenFiles;
  293. ///! return true if the same cmake was used to make the cache.
  294. bool CacheVersionMatches();
  295. ///! read in a cmake list file to initialize the cache
  296. void ReadListFile(const char *path);
  297. ///! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  298. /// If it is set, truncate it to 50kb
  299. void TruncateOutputLog(const char* fname);
  300. /**
  301. * Method called to check build system integrity at build time.
  302. * Returns 1 if CMake should rerun and 0 otherwise.
  303. */
  304. int CheckBuildSystem();
  305. void SetDirectoriesFromFile(const char* arg);
  306. //! Make sure all commands are what they say they are and there is no
  307. //macros.
  308. void CleanupCommandsAndMacros();
  309. void GenerateGraphViz(const char* fileName) const;
  310. static int ExecuteEchoColor(std::vector<std::string>& args);
  311. static int ExecuteLinkScript(std::vector<std::string>& args);
  312. cmVariableWatch* VariableWatch;
  313. ///! Find the full path to one of the cmake programs like ctest, cpack, etc.
  314. std::string FindCMakeProgram(const char* name) const;
  315. private:
  316. ProgressCallbackType ProgressCallback;
  317. void* ProgressCallbackClientData;
  318. bool Verbose;
  319. bool InTryCompile;
  320. bool ScriptMode;
  321. bool DebugOutput;
  322. std::string CMakeCommand;
  323. std::string CXXEnvironment;
  324. std::string CCEnvironment;
  325. std::string CheckBuildSystemArgument;
  326. std::string CTestCommand;
  327. std::string CPackCommand;
  328. bool ClearBuildSystem;
  329. bool DebugTryCompile;
  330. cmFileTimeComparison* FileComparison;
  331. std::string GraphVizFile;
  332. void UpdateConversionPathTable();
  333. };
  334. #define CMAKE_STANDARD_OPTIONS_TABLE \
  335. {"-C <initial-cache>", "Pre-load a script to populate the cache.", \
  336. "When cmake is first run in an empty build tree, it creates a " \
  337. "CMakeCache.txt file and populates it with customizable settings " \
  338. "for the project. This option may be used to specify a file from " \
  339. "which to load cache entries before the first pass through " \
  340. "the project's cmake listfiles. The loaded entries take priority " \
  341. "over the project's default values. The given file should be a CMake " \
  342. "script containing SET commands that use the CACHE option, " \
  343. "not a cache-format file."}, \
  344. {"-D <var>:<type>=<value>", "Create a cmake cache entry.", \
  345. "When cmake is first run in an empty build tree, it creates a " \
  346. "CMakeCache.txt file and populates it with customizable settings " \
  347. "for the project. This option may be used to specify a setting " \
  348. "that takes priority over the project's default value. The option " \
  349. "may be repeated for as many cache entries as desired."}, \
  350. {"-U <globbing_expr>", "Remove matching entries from CMake cache.", \
  351. "This option may be used to remove one or more variables from the " \
  352. "CMakeCache.txt file, globbing expressions using * and ? are supported. "\
  353. "The option may be repeated for as many cache entries as desired.\n" \
  354. "Use with care, you can make your CMakeCache.txt non-working."}, \
  355. {"-G <generator-name>", "Specify a makefile generator.", \
  356. "CMake may support multiple native build systems on certain platforms. " \
  357. "A makefile generator is responsible for generating a particular build " \
  358. "system. Possible generator names are specified in the Generators " \
  359. "section."}
  360. #define CMAKE_STANDARD_INTRODUCTION \
  361. {0, \
  362. "CMake is a cross-platform build system generator. Projects " \
  363. "specify their build process with platform-independent CMake listfiles " \
  364. "included in each directory of a source tree with the name " \
  365. "CMakeLists.txt. " \
  366. "Users build a project by using CMake to generate a build system " \
  367. "for a native tool on their platform.", 0}
  368. #endif