cmake.h 22 KB

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