cmake.h 17 KB

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