cmake.h 17 KB

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