cmake.h 22 KB

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