cmake.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmake_h
  11. #define cmake_h
  12. #include "cmListFileCache.h"
  13. #include "cmSystemTools.h"
  14. #include "cmPropertyDefinitionMap.h"
  15. #include "cmPropertyMap.h"
  16. #include "cmInstalledFile.h"
  17. #include "cmCacheManager.h"
  18. class cmGlobalGeneratorFactory;
  19. class cmGlobalGenerator;
  20. class cmLocalGenerator;
  21. class cmMakefile;
  22. class cmCommand;
  23. class cmVariableWatch;
  24. class cmFileTimeComparison;
  25. class cmExternalMakefileProjectGenerator;
  26. class cmDocumentationSection;
  27. class cmPolicies;
  28. class cmTarget;
  29. class cmGeneratedFileStream;
  30. /** \brief Represents a cmake invocation.
  31. *
  32. * This class represents a cmake invocation. It is the top level class when
  33. * running cmake. Most cmake based GUIs should primarily create an instance
  34. * of this class and communicate with it.
  35. *
  36. * The basic process for a GUI is as follows:
  37. *
  38. * -# Create a cmake instance
  39. * -# Set the Home & Start directories, generator, and cmake command. this
  40. * can be done using the Set methods or by using SetArgs and passing in
  41. * command line arguments.
  42. * -# Load the cache by calling LoadCache (duh)
  43. * -# if you are using command line arguments with -D or -C flags then
  44. * call SetCacheArgs (or if for some other reason you want to modify the
  45. * cache), do it now.
  46. * -# Finally call Configure
  47. * -# Let the user change values and go back to step 5
  48. * -# call Generate
  49. * If your GUI allows the user to change the start & home directories then
  50. * you must at a minimum redo steps 2 through 7.
  51. */
  52. class cmake
  53. {
  54. public:
  55. enum MessageType
  56. { AUTHOR_WARNING,
  57. FATAL_ERROR,
  58. INTERNAL_ERROR,
  59. MESSAGE,
  60. WARNING,
  61. LOG,
  62. DEPRECATION_ERROR,
  63. DEPRECATION_WARNING
  64. };
  65. /** \brief Describes the working modes of cmake */
  66. enum WorkingMode
  67. {
  68. NORMAL_MODE, ///< Cmake runs to create project files
  69. /** \brief Script mode (started by using -P).
  70. *
  71. * In script mode there is no generator and no cache. Also,
  72. * languages are not enabled, so add_executable and things do
  73. * nothing.
  74. */
  75. SCRIPT_MODE,
  76. /** \brief A pkg-config like mode
  77. *
  78. * In this mode cmake just searches for a package and prints the results to
  79. * stdout. This is similar to SCRIPT_MODE, but commands like add_library()
  80. * work too, since they may be used e.g. in exported target files. Started
  81. * via --find-package.
  82. */
  83. FIND_PACKAGE_MODE
  84. };
  85. typedef std::map<std::string, cmCommand*> RegisteredCommandsMap;
  86. typedef std::map<std::string, cmInstalledFile> InstalledFilesMap;
  87. /// Default constructor
  88. cmake();
  89. /// Destructor
  90. ~cmake();
  91. static const char *GetCMakeFilesDirectory() {return "/CMakeFiles";}
  92. static const char *GetCMakeFilesDirectoryPostSlash() {
  93. return "CMakeFiles/";}
  94. //@{
  95. /**
  96. * Set/Get the home directory (or output directory) in the project. The
  97. * home directory is the top directory of the project. It is the
  98. * path-to-source cmake was run with. Remember that CMake processes
  99. * CMakeLists files by recursing up the tree starting at the StartDirectory
  100. * and going up until it reaches the HomeDirectory.
  101. */
  102. void SetHomeDirectory(const std::string& dir);
  103. const char* GetHomeDirectory() const
  104. {
  105. return this->cmHomeDirectory.c_str();
  106. }
  107. void SetHomeOutputDirectory(const std::string& lib);
  108. const char* GetHomeOutputDirectory() const
  109. {
  110. return this->HomeOutputDirectory.c_str();
  111. }
  112. //@}
  113. //@{
  114. /**
  115. * Set/Get the start directory (or output directory). The start directory
  116. * is the directory of the CMakeLists.txt file that started the current
  117. * round of processing. Remember that CMake processes CMakeLists files by
  118. * recursing up the tree starting at the StartDirectory and going up until
  119. * it reaches the HomeDirectory.
  120. */
  121. void SetStartDirectory(const std::string& dir)
  122. {
  123. this->cmStartDirectory = dir;
  124. cmSystemTools::ConvertToUnixSlashes(this->cmStartDirectory);
  125. }
  126. const char* GetStartDirectory() const
  127. {
  128. return this->cmStartDirectory.c_str();
  129. }
  130. void SetStartOutputDirectory(const std::string& lib)
  131. {
  132. this->StartOutputDirectory = lib;
  133. cmSystemTools::ConvertToUnixSlashes(this->StartOutputDirectory);
  134. }
  135. const char* GetStartOutputDirectory() const
  136. {
  137. return this->StartOutputDirectory.c_str();
  138. }
  139. //@}
  140. /**
  141. * Handle a command line invocation of cmake.
  142. */
  143. int Run(const std::vector<std::string>&args)
  144. { return this->Run(args, false); }
  145. int Run(const std::vector<std::string>&args, bool noconfigure);
  146. /**
  147. * Run the global generator Generate step.
  148. */
  149. int Generate();
  150. /**
  151. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  152. * one has not already been set. It will then Call Configure on the
  153. * GlobalGenerator. This in turn will read in an process all the CMakeList
  154. * files for the tree. It will not produce any actual Makefiles, or
  155. * workspaces. Generate does that. */
  156. int Configure();
  157. int ActualConfigure();
  158. ///! Break up a line like VAR:type="value" into var, type and value
  159. static bool ParseCacheEntry(const std::string& entry,
  160. std::string& var,
  161. std::string& value,
  162. cmCacheManager::CacheEntryType& type);
  163. int LoadCache();
  164. bool LoadCache(const std::string& path);
  165. bool LoadCache(const std::string& path, bool internal,
  166. std::set<std::string>& excludes,
  167. std::set<std::string>& includes);
  168. bool SaveCache(const std::string& path);
  169. bool DeleteCache(const std::string& path);
  170. void PreLoadCMakeFiles();
  171. ///! Create a GlobalGenerator
  172. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name);
  173. ///! Return the global generator assigned to this instance of cmake
  174. cmGlobalGenerator* GetGlobalGenerator() { return this->GlobalGenerator; }
  175. ///! Return the global generator assigned to this instance of cmake, const
  176. const cmGlobalGenerator* GetGlobalGenerator() const
  177. { return this->GlobalGenerator; }
  178. ///! Return the global generator assigned to this instance of cmake
  179. void SetGlobalGenerator(cmGlobalGenerator *);
  180. ///! Get the names of the current registered generators
  181. void GetRegisteredGenerators(std::vector<std::string>& names);
  182. ///! Set the name of the selected generator-specific platform.
  183. void SetGeneratorPlatform(std::string const& ts)
  184. { this->GeneratorPlatform = ts; }
  185. ///! Get the name of the selected generator-specific platform.
  186. std::string const& GetGeneratorPlatform() const
  187. { return this->GeneratorPlatform; }
  188. ///! Set the name of the selected generator-specific toolset.
  189. void SetGeneratorToolset(std::string const& ts)
  190. { this->GeneratorToolset = ts; }
  191. ///! Get the name of the selected generator-specific toolset.
  192. std::string const& GetGeneratorToolset() const
  193. { return this->GeneratorToolset; }
  194. ///! get the cmCachemManager used by this invocation of cmake
  195. cmCacheManager *GetCacheManager() { return this->CacheManager; }
  196. /**
  197. * Given a variable name, return its value (as a string).
  198. */
  199. const char* GetCacheDefinition(const std::string&) const;
  200. ///! Add an entry into the cache
  201. void AddCacheEntry(const std::string& key, const char* value,
  202. const char* helpString,
  203. int type);
  204. /**
  205. * Get the system information and write it to the file specified
  206. */
  207. int GetSystemInformation(std::vector<std::string>&);
  208. /**
  209. * Add a command to this cmake instance
  210. */
  211. void AddCommand(cmCommand* );
  212. void RenameCommand(const std::string& oldName, const std::string& newName);
  213. void RemoveCommand(const std::string& name);
  214. void RemoveUnscriptableCommands();
  215. /**
  216. * Get a command by its name
  217. */
  218. cmCommand *GetCommand(const std::string& name);
  219. /** Check if a command exists. */
  220. bool CommandExists(const std::string& name) const;
  221. ///! Parse command line arguments
  222. void SetArgs(const std::vector<std::string>&,
  223. bool directoriesSetBefore = false);
  224. ///! Is this cmake running as a result of a TRY_COMPILE command
  225. bool GetIsInTryCompile() { return this->InTryCompile; }
  226. ///! Is this cmake running as a result of a TRY_COMPILE command
  227. void SetIsInTryCompile(bool i) { this->InTryCompile = i; }
  228. ///! Parse command line arguments that might set cache values
  229. bool SetCacheArgs(const std::vector<std::string>&);
  230. typedef void (*ProgressCallbackType)
  231. (const char*msg, float progress, void *);
  232. /**
  233. * Set the function used by GUIs to receive progress updates
  234. * Function gets passed: message as a const char*, a progress
  235. * amount ranging from 0 to 1.0 and client data. The progress
  236. * number provided may be negative in cases where a message is
  237. * to be displayed without any progress percentage.
  238. */
  239. void SetProgressCallback(ProgressCallbackType f, void* clientData=0);
  240. ///! this is called by generators to update the progress
  241. void UpdateProgress(const char *msg, float prog);
  242. ///! get the cmake policies instance
  243. cmPolicies *GetPolicies() {return this->Policies;}
  244. ///! Get the variable watch object
  245. cmVariableWatch* GetVariableWatch() { return this->VariableWatch; }
  246. void GetGeneratorDocumentation(std::vector<cmDocumentationEntry>&);
  247. ///! Set/Get a property of this target file
  248. void SetProperty(const std::string& prop, const char *value);
  249. void AppendProperty(const std::string& prop,
  250. const char *value,bool asString=false);
  251. const char *GetProperty(const std::string& prop);
  252. const char *GetProperty(const std::string& prop,
  253. cmProperty::ScopeType scope);
  254. bool GetPropertyAsBool(const std::string& prop);
  255. ///! Get or create an cmInstalledFile instance and return a pointer to it
  256. cmInstalledFile *GetOrCreateInstalledFile(
  257. cmMakefile* mf, const std::string& name);
  258. cmInstalledFile const* GetInstalledFile(const std::string& name) const;
  259. InstalledFilesMap const& GetInstalledFiles() const
  260. { return this->InstalledFiles; }
  261. ///! Do all the checks before running configure
  262. int DoPreConfigureChecks();
  263. void SetWorkingMode(WorkingMode mode) { this->CurrentWorkingMode = mode; }
  264. WorkingMode GetWorkingMode() { return this->CurrentWorkingMode; }
  265. ///! Debug the try compile stuff by not deleting the files
  266. bool GetDebugTryCompile(){return this->DebugTryCompile;}
  267. void DebugTryCompileOn(){this->DebugTryCompile = true;}
  268. /**
  269. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  270. */
  271. int AddCMakePaths();
  272. /**
  273. * Get the file comparison class
  274. */
  275. cmFileTimeComparison* GetFileComparison() { return this->FileComparison; }
  276. // Do we want debug output during the cmake run.
  277. bool GetDebugOutput() { return this->DebugOutput; }
  278. void SetDebugOutputOn(bool b) { this->DebugOutput = b;}
  279. // Do we want trace output during the cmake run.
  280. bool GetTrace() { return this->Trace;}
  281. void SetTrace(bool b) { this->Trace = b;}
  282. bool GetWarnUninitialized() { return this->WarnUninitialized;}
  283. void SetWarnUninitialized(bool b) { this->WarnUninitialized = b;}
  284. bool GetWarnUnused() { return this->WarnUnused;}
  285. void SetWarnUnused(bool b) { this->WarnUnused = b;}
  286. bool GetWarnUnusedCli() { return this->WarnUnusedCli;}
  287. void SetWarnUnusedCli(bool b) { this->WarnUnusedCli = b;}
  288. bool GetCheckSystemVars() { return this->CheckSystemVars;}
  289. void SetCheckSystemVars(bool b) { this->CheckSystemVars = b;}
  290. void MarkCliAsUsed(const std::string& variable);
  291. // Define a property
  292. void DefineProperty(const std::string& name, cmProperty::ScopeType scope,
  293. const char *ShortDescription,
  294. const char *FullDescription,
  295. bool chain = false);
  296. // get property definition
  297. cmPropertyDefinition *GetPropertyDefinition
  298. (const std::string& name, cmProperty::ScopeType scope);
  299. // Is a property defined?
  300. bool IsPropertyDefined(const std::string& name, cmProperty::ScopeType scope);
  301. bool IsPropertyChained(const std::string& name, cmProperty::ScopeType scope);
  302. /** Get the list of configurations (in upper case) considered to be
  303. debugging configurations.*/
  304. std::vector<std::string> GetDebugConfigs();
  305. void SetCMakeEditCommand(std::string const& s)
  306. { this->CMakeEditCommand = s; }
  307. std::string const& GetCMakeEditCommand() const
  308. { return this->CMakeEditCommand; }
  309. void SetSuppressDevWarnings(bool v)
  310. {
  311. this->SuppressDevWarnings = v;
  312. this->DoSuppressDevWarnings = true;
  313. }
  314. /** Display a message to the user. */
  315. void IssueMessage(cmake::MessageType t, std::string const& text,
  316. cmListFileBacktrace const& backtrace = cmListFileBacktrace(NULL));
  317. ///! run the --build option
  318. int Build(const std::string& dir,
  319. const std::string& target,
  320. const std::string& config,
  321. const std::vector<std::string>& nativeOptions,
  322. bool clean);
  323. void UnwatchUnusedCli(const std::string& var);
  324. void WatchUnusedCli(const std::string& var);
  325. protected:
  326. void RunCheckForUnusedVariables();
  327. void InitializeProperties();
  328. int HandleDeleteCacheVariables(const std::string& var);
  329. cmPropertyMap Properties;
  330. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>
  331. PropertyDefinitions;
  332. typedef
  333. cmExternalMakefileProjectGenerator* (*CreateExtraGeneratorFunctionType)();
  334. typedef std::map<std::string,
  335. CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap;
  336. typedef std::vector<cmGlobalGeneratorFactory*> RegisteredGeneratorsVector;
  337. RegisteredCommandsMap Commands;
  338. RegisteredGeneratorsVector Generators;
  339. RegisteredExtraGeneratorsMap ExtraGenerators;
  340. void AddDefaultCommands();
  341. void AddDefaultGenerators();
  342. void AddDefaultExtraGenerators();
  343. void AddExtraGenerator(const std::string& name,
  344. CreateExtraGeneratorFunctionType newFunction);
  345. cmPolicies *Policies;
  346. cmGlobalGenerator *GlobalGenerator;
  347. cmCacheManager *CacheManager;
  348. std::string cmHomeDirectory;
  349. std::string HomeOutputDirectory;
  350. std::string cmStartDirectory;
  351. std::string StartOutputDirectory;
  352. bool SuppressDevWarnings;
  353. bool DoSuppressDevWarnings;
  354. std::string GeneratorPlatform;
  355. std::string GeneratorToolset;
  356. ///! read in a cmake list file to initialize the cache
  357. void ReadListFile(const std::vector<std::string>& args, const char *path);
  358. bool FindPackage(const std::vector<std::string>& args);
  359. ///! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  360. /// If it is set, truncate it to 50kb
  361. void TruncateOutputLog(const char* fname);
  362. /**
  363. * Method called to check build system integrity at build time.
  364. * Returns 1 if CMake should rerun and 0 otherwise.
  365. */
  366. int CheckBuildSystem();
  367. void SetDirectoriesFromFile(const char* arg);
  368. //! Make sure all commands are what they say they are and there is no
  369. /// macros.
  370. void CleanupCommandsAndMacros();
  371. void GenerateGraphViz(const char* fileName) const;
  372. cmVariableWatch* VariableWatch;
  373. private:
  374. cmake(const cmake&); // Not implemented.
  375. void operator=(const cmake&); // Not implemented.
  376. ProgressCallbackType ProgressCallback;
  377. void* ProgressCallbackClientData;
  378. bool Verbose;
  379. bool InTryCompile;
  380. WorkingMode CurrentWorkingMode;
  381. bool DebugOutput;
  382. bool Trace;
  383. bool WarnUninitialized;
  384. bool WarnUnused;
  385. bool WarnUnusedCli;
  386. bool CheckSystemVars;
  387. std::map<std::string, bool> UsedCliVariables;
  388. std::string CMakeEditCommand;
  389. std::string CXXEnvironment;
  390. std::string CCEnvironment;
  391. std::string CheckBuildSystemArgument;
  392. std::string CheckStampFile;
  393. std::string CheckStampList;
  394. std::string VSSolutionFile;
  395. bool ClearBuildSystem;
  396. bool DebugTryCompile;
  397. cmFileTimeComparison* FileComparison;
  398. std::string GraphVizFile;
  399. InstalledFilesMap InstalledFiles;
  400. void UpdateConversionPathTable();
  401. };
  402. #define CMAKE_STANDARD_OPTIONS_TABLE \
  403. {"-C <initial-cache>", "Pre-load a script to populate the cache."}, \
  404. {"-D <var>:<type>=<value>", "Create a cmake cache entry."}, \
  405. {"-U <globbing_expr>", "Remove matching entries from CMake cache."}, \
  406. {"-G <generator-name>", "Specify a build system generator."},\
  407. {"-T <toolset-name>", "Specify toolset name if supported by generator."}, \
  408. {"-A <platform-name>", "Specify platform name if supported by generator."}, \
  409. {"-Wno-dev", "Suppress developer warnings."},\
  410. {"-Wdev", "Enable developer warnings."}
  411. #define FOR_EACH_C_FEATURE(F) \
  412. F(c_function_prototypes) \
  413. F(c_restrict) \
  414. F(c_static_assert) \
  415. F(c_variadic_macros)
  416. #define FOR_EACH_CXX_FEATURE(F) \
  417. F(cxx_aggregate_default_initializers) \
  418. F(cxx_alias_templates) \
  419. F(cxx_alignas) \
  420. F(cxx_alignof) \
  421. F(cxx_attributes) \
  422. F(cxx_attribute_deprecated) \
  423. F(cxx_auto_type) \
  424. F(cxx_binary_literals) \
  425. F(cxx_constexpr) \
  426. F(cxx_contextual_conversions) \
  427. F(cxx_decltype) \
  428. F(cxx_decltype_auto) \
  429. F(cxx_decltype_incomplete_return_types) \
  430. F(cxx_default_function_template_args) \
  431. F(cxx_defaulted_functions) \
  432. F(cxx_defaulted_move_initializers) \
  433. F(cxx_delegating_constructors) \
  434. F(cxx_deleted_functions) \
  435. F(cxx_digit_separators) \
  436. F(cxx_enum_forward_declarations) \
  437. F(cxx_explicit_conversions) \
  438. F(cxx_extended_friend_declarations) \
  439. F(cxx_extern_templates) \
  440. F(cxx_final) \
  441. F(cxx_func_identifier) \
  442. F(cxx_generalized_initializers) \
  443. F(cxx_generic_lambdas) \
  444. F(cxx_inheriting_constructors) \
  445. F(cxx_inline_namespaces) \
  446. F(cxx_lambdas) \
  447. F(cxx_lambda_init_captures) \
  448. F(cxx_local_type_template_args) \
  449. F(cxx_long_long_type) \
  450. F(cxx_noexcept) \
  451. F(cxx_nonstatic_member_init) \
  452. F(cxx_nullptr) \
  453. F(cxx_override) \
  454. F(cxx_range_for) \
  455. F(cxx_raw_string_literals) \
  456. F(cxx_reference_qualified_functions) \
  457. F(cxx_relaxed_constexpr) \
  458. F(cxx_return_type_deduction) \
  459. F(cxx_right_angle_brackets) \
  460. F(cxx_rvalue_references) \
  461. F(cxx_sizeof_member) \
  462. F(cxx_static_assert) \
  463. F(cxx_strong_enums) \
  464. F(cxx_template_template_parameters) \
  465. F(cxx_thread_local) \
  466. F(cxx_trailing_return_types) \
  467. F(cxx_unicode_literals) \
  468. F(cxx_uniform_initialization) \
  469. F(cxx_unrestricted_unions) \
  470. F(cxx_user_literals) \
  471. F(cxx_variable_templates) \
  472. F(cxx_variadic_macros) \
  473. F(cxx_variadic_templates)
  474. #endif