cmake.h 18 KB

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