cmake.h 22 KB

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