cmake.h 16 KB

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