cmake.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <cstddef>
  6. #include <functional>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <stack>
  11. #include <string>
  12. #include <unordered_set>
  13. #include <utility>
  14. #include <vector>
  15. #include <cm/string_view>
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmInstalledFile.h"
  18. #include "cmListFileCache.h"
  19. #include "cmMessageType.h"
  20. #include "cmState.h"
  21. #include "cmStateSnapshot.h"
  22. #include "cmStateTypes.h"
  23. #include "cmValue.h"
  24. #if !defined(CMAKE_BOOTSTRAP)
  25. # include <cm/optional>
  26. # include <cm3p/json/value.h>
  27. # include "cmCMakePresetsGraph.h"
  28. #endif
  29. class cmExternalMakefileProjectGeneratorFactory;
  30. class cmFileAPI;
  31. class cmFileTimeCache;
  32. class cmGlobalGenerator;
  33. class cmGlobalGeneratorFactory;
  34. class cmMakefile;
  35. #if !defined(CMAKE_BOOTSTRAP)
  36. class cmMakefileProfilingData;
  37. #endif
  38. class cmMessenger;
  39. class cmVariableWatch;
  40. struct cmBuildOptions;
  41. struct cmDocumentationEntry;
  42. /** \brief Represents a cmake invocation.
  43. *
  44. * This class represents a cmake invocation. It is the top level class when
  45. * running cmake. Most cmake based GUIs should primarily create an instance
  46. * of this class and communicate with it.
  47. *
  48. * The basic process for a GUI is as follows:
  49. *
  50. * -# Create a cmake instance
  51. * -# Set the Home directories, generator, and cmake command. this
  52. * can be done using the Set methods or by using SetArgs and passing in
  53. * command line arguments.
  54. * -# Load the cache by calling LoadCache (duh)
  55. * -# if you are using command line arguments with -D or -C flags then
  56. * call SetCacheArgs (or if for some other reason you want to modify the
  57. * cache), do it now.
  58. * -# Finally call Configure
  59. * -# Let the user change values and go back to step 5
  60. * -# call Generate
  61. * If your GUI allows the user to change the home directories then
  62. * you must at a minimum redo steps 2 through 7.
  63. */
  64. class cmake
  65. {
  66. public:
  67. enum Role
  68. {
  69. RoleInternal, // no commands
  70. RoleScript, // script commands
  71. RoleProject // all commands
  72. };
  73. enum DiagLevel
  74. {
  75. DIAG_IGNORE,
  76. DIAG_WARN,
  77. DIAG_ERROR
  78. };
  79. /** \brief Describes the working modes of cmake */
  80. enum WorkingMode
  81. {
  82. NORMAL_MODE, ///< Cmake runs to create project files
  83. /** \brief Script mode (started by using -P).
  84. *
  85. * In script mode there is no generator and no cache. Also,
  86. * languages are not enabled, so add_executable and things do
  87. * nothing.
  88. */
  89. SCRIPT_MODE,
  90. /** \brief Help mode
  91. *
  92. * Used to print help for things that can only be determined after finding
  93. * the source directory, for example, the list of presets.
  94. */
  95. HELP_MODE,
  96. /** \brief A pkg-config like mode
  97. *
  98. * In this mode cmake just searches for a package and prints the results to
  99. * stdout. This is similar to SCRIPT_MODE, but commands like add_library()
  100. * work too, since they may be used e.g. in exported target files. Started
  101. * via --find-package.
  102. */
  103. FIND_PACKAGE_MODE
  104. };
  105. /** \brief Define log level constants. */
  106. enum LogLevel
  107. {
  108. LOG_UNDEFINED,
  109. LOG_ERROR,
  110. LOG_WARNING,
  111. LOG_NOTICE,
  112. LOG_STATUS,
  113. LOG_VERBOSE,
  114. LOG_DEBUG,
  115. LOG_TRACE
  116. };
  117. /** \brief Define supported trace formats **/
  118. enum TraceFormat
  119. {
  120. TRACE_UNDEFINED,
  121. TRACE_HUMAN,
  122. TRACE_JSON_V1,
  123. };
  124. struct GeneratorInfo
  125. {
  126. std::string name;
  127. std::string baseName;
  128. std::string extraName;
  129. bool supportsToolset;
  130. bool supportsPlatform;
  131. std::vector<std::string> supportedPlatforms;
  132. std::string defaultPlatform;
  133. bool isAlias;
  134. };
  135. struct FileExtensions
  136. {
  137. bool Test(cm::string_view ext) const
  138. {
  139. return (this->unordered.find(ext) != this->unordered.end());
  140. }
  141. std::vector<std::string> ordered;
  142. std::unordered_set<cm::string_view> unordered;
  143. };
  144. using InstalledFilesMap = std::map<std::string, cmInstalledFile>;
  145. static const int NO_BUILD_PARALLEL_LEVEL = -1;
  146. static const int DEFAULT_BUILD_PARALLEL_LEVEL = 0;
  147. /// Default constructor
  148. cmake(Role role, cmState::Mode mode,
  149. cmState::ProjectKind projectKind = cmState::ProjectKind::Normal);
  150. /// Destructor
  151. ~cmake();
  152. cmake(cmake const&) = delete;
  153. cmake& operator=(cmake const&) = delete;
  154. #if !defined(CMAKE_BOOTSTRAP)
  155. Json::Value ReportVersionJson() const;
  156. Json::Value ReportCapabilitiesJson() const;
  157. #endif
  158. std::string ReportCapabilities() const;
  159. /**
  160. * Set the home directory from `-S` or from a known location
  161. * that contains a CMakeLists.txt. Will generate warnings
  162. * when overriding an existing source directory.
  163. *
  164. * | args | src dir| warning |
  165. * | ----------------- | ------ | -------------- |
  166. * | `dirA dirA` | dirA | N/A |
  167. * | `-S dirA -S dirA` | dirA | N/A |
  168. * | `-S dirA -S dirB` | dirB | Ignoring dirA |
  169. * | `-S dirA dirB` | dirB | Ignoring dirA |
  170. * | `dirA -S dirB` | dirB | Ignoring dirA |
  171. * | `dirA dirB` | dirB | Ignoring dirA |
  172. */
  173. void SetHomeDirectoryViaCommandLine(std::string const& path);
  174. //@{
  175. /**
  176. * Set/Get the home directory (or output directory) in the project. The
  177. * home directory is the top directory of the project. It is the
  178. * path-to-source cmake was run with.
  179. */
  180. void SetHomeDirectory(const std::string& dir);
  181. std::string const& GetHomeDirectory() const;
  182. void SetHomeOutputDirectory(const std::string& dir);
  183. std::string const& GetHomeOutputDirectory() const;
  184. //@}
  185. /**
  186. * Working directory at CMake launch
  187. */
  188. std::string const& GetCMakeWorkingDirectory() const
  189. {
  190. return this->CMakeWorkingDirectory;
  191. }
  192. /**
  193. * Handle a command line invocation of cmake.
  194. */
  195. int Run(const std::vector<std::string>& args)
  196. {
  197. return this->Run(args, false);
  198. }
  199. int Run(const std::vector<std::string>& args, bool noconfigure);
  200. /**
  201. * Run the global generator Generate step.
  202. */
  203. int Generate();
  204. /**
  205. * Configure the cmMakefiles. This routine will create a GlobalGenerator if
  206. * one has not already been set. It will then Call Configure on the
  207. * GlobalGenerator. This in turn will read in an process all the CMakeList
  208. * files for the tree. It will not produce any actual Makefiles, or
  209. * workspaces. Generate does that. */
  210. int Configure();
  211. int ActualConfigure();
  212. //! Break up a line like VAR:type="value" into var, type and value
  213. static bool ParseCacheEntry(const std::string& entry, std::string& var,
  214. std::string& value,
  215. cmStateEnums::CacheEntryType& type);
  216. int LoadCache();
  217. bool LoadCache(const std::string& path);
  218. bool LoadCache(const std::string& path, bool internal,
  219. std::set<std::string>& excludes,
  220. std::set<std::string>& includes);
  221. bool SaveCache(const std::string& path);
  222. bool DeleteCache(const std::string& path);
  223. void PreLoadCMakeFiles();
  224. //! Create a GlobalGenerator
  225. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  226. const std::string& name, bool allowArch = true);
  227. //! Create a GlobalGenerator and set it as our own
  228. bool CreateAndSetGlobalGenerator(const std::string& name, bool allowArch);
  229. #ifndef CMAKE_BOOTSTRAP
  230. //! Print list of configure presets
  231. void PrintPresetList(const cmCMakePresetsGraph& graph) const;
  232. #endif
  233. //! Return the global generator assigned to this instance of cmake
  234. cmGlobalGenerator* GetGlobalGenerator()
  235. {
  236. return this->GlobalGenerator.get();
  237. }
  238. //! Return the global generator assigned to this instance of cmake, const
  239. const cmGlobalGenerator* GetGlobalGenerator() const
  240. {
  241. return this->GlobalGenerator.get();
  242. }
  243. //! Return the full path to where the CMakeCache.txt file should be.
  244. static std::string FindCacheFile(const std::string& binaryDir);
  245. //! Return the global generator assigned to this instance of cmake
  246. void SetGlobalGenerator(std::unique_ptr<cmGlobalGenerator>);
  247. //! Get the names of the current registered generators
  248. void GetRegisteredGenerators(std::vector<GeneratorInfo>& generators,
  249. bool includeNamesWithPlatform = true) const;
  250. //! Set the name of the selected generator-specific instance.
  251. void SetGeneratorInstance(std::string const& instance)
  252. {
  253. this->GeneratorInstance = instance;
  254. this->GeneratorInstanceSet = true;
  255. }
  256. //! Set the name of the selected generator-specific platform.
  257. void SetGeneratorPlatform(std::string const& ts)
  258. {
  259. this->GeneratorPlatform = ts;
  260. this->GeneratorPlatformSet = true;
  261. }
  262. //! Set the name of the selected generator-specific toolset.
  263. void SetGeneratorToolset(std::string const& ts)
  264. {
  265. this->GeneratorToolset = ts;
  266. this->GeneratorToolsetSet = true;
  267. }
  268. bool IsAKnownSourceExtension(cm::string_view ext) const
  269. {
  270. return this->CLikeSourceFileExtensions.Test(ext) ||
  271. this->CudaFileExtensions.Test(ext) ||
  272. this->FortranFileExtensions.Test(ext) ||
  273. this->HipFileExtensions.Test(ext) || this->ISPCFileExtensions.Test(ext);
  274. }
  275. bool IsACLikeSourceExtension(cm::string_view ext) const
  276. {
  277. return this->CLikeSourceFileExtensions.Test(ext);
  278. }
  279. bool IsAKnownExtension(cm::string_view ext) const
  280. {
  281. return this->IsAKnownSourceExtension(ext) || this->IsAHeaderExtension(ext);
  282. }
  283. std::vector<std::string> GetAllExtensions() const;
  284. const std::vector<std::string>& GetHeaderExtensions() const
  285. {
  286. return this->HeaderFileExtensions.ordered;
  287. }
  288. bool IsAHeaderExtension(cm::string_view ext) const
  289. {
  290. return this->HeaderFileExtensions.Test(ext);
  291. }
  292. // Strips the extension (if present and known) from a filename
  293. std::string StripExtension(const std::string& file) const;
  294. /**
  295. * Given a variable name, return its value (as a string).
  296. */
  297. cmValue GetCacheDefinition(const std::string&) const;
  298. //! Add an entry into the cache
  299. void AddCacheEntry(const std::string& key, const char* value,
  300. const char* helpString, int type)
  301. {
  302. this->AddCacheEntry(key,
  303. value ? cmValue(std::string(value)) : cmValue(nullptr),
  304. helpString, type);
  305. }
  306. void AddCacheEntry(const std::string& key, const std::string& value,
  307. const char* helpString, int type)
  308. {
  309. this->AddCacheEntry(key, cmValue(value), helpString, type);
  310. }
  311. void AddCacheEntry(const std::string& key, cmValue value,
  312. const char* helpString, int type);
  313. bool DoWriteGlobVerifyTarget() const;
  314. std::string const& GetGlobVerifyScript() const;
  315. std::string const& GetGlobVerifyStamp() const;
  316. void AddGlobCacheEntry(bool recurse, bool listDirectories,
  317. bool followSymlinks, const std::string& relative,
  318. const std::string& expression,
  319. const std::vector<std::string>& files,
  320. const std::string& variable,
  321. cmListFileBacktrace const& bt);
  322. /**
  323. * Get the system information and write it to the file specified
  324. */
  325. int GetSystemInformation(std::vector<std::string>&);
  326. //! Parse environment variables
  327. void LoadEnvironmentPresets();
  328. //! Parse command line arguments
  329. void SetArgs(const std::vector<std::string>& args);
  330. //! Is this cmake running as a result of a TRY_COMPILE command
  331. bool GetIsInTryCompile() const;
  332. #ifndef CMAKE_BOOTSTRAP
  333. void SetWarningFromPreset(const std::string& name,
  334. const cm::optional<bool>& warning,
  335. const cm::optional<bool>& error);
  336. void ProcessPresetVariables();
  337. void PrintPresetVariables();
  338. void ProcessPresetEnvironment();
  339. void PrintPresetEnvironment();
  340. #endif
  341. //! Parse command line arguments that might set cache values
  342. bool SetCacheArgs(const std::vector<std::string>&);
  343. void ProcessCacheArg(const std::string& var, const std::string& value,
  344. cmStateEnums::CacheEntryType type);
  345. using ProgressCallbackType = std::function<void(const std::string&, float)>;
  346. /**
  347. * Set the function used by GUIs to receive progress updates
  348. * Function gets passed: message as a const char*, a progress
  349. * amount ranging from 0 to 1.0 and client data. The progress
  350. * number provided may be negative in cases where a message is
  351. * to be displayed without any progress percentage.
  352. */
  353. void SetProgressCallback(ProgressCallbackType f);
  354. //! this is called by generators to update the progress
  355. void UpdateProgress(const std::string& msg, float prog);
  356. #if !defined(CMAKE_BOOTSTRAP)
  357. //! Get the variable watch object
  358. cmVariableWatch* GetVariableWatch() { return this->VariableWatch.get(); }
  359. #endif
  360. std::vector<cmDocumentationEntry> GetGeneratorsDocumentation();
  361. //! Set/Get a property of this target file
  362. void SetProperty(const std::string& prop, const char* value);
  363. void SetProperty(const std::string& prop, cmValue value);
  364. void SetProperty(const std::string& prop, const std::string& value)
  365. {
  366. this->SetProperty(prop, cmValue(value));
  367. }
  368. void AppendProperty(const std::string& prop, const std::string& value,
  369. bool asString = false);
  370. cmValue GetProperty(const std::string& prop);
  371. bool GetPropertyAsBool(const std::string& prop);
  372. //! Get or create an cmInstalledFile instance and return a pointer to it
  373. cmInstalledFile* GetOrCreateInstalledFile(cmMakefile* mf,
  374. const std::string& name);
  375. cmInstalledFile const* GetInstalledFile(const std::string& name) const;
  376. InstalledFilesMap const& GetInstalledFiles() const
  377. {
  378. return this->InstalledFiles;
  379. }
  380. //! Do all the checks before running configure
  381. int DoPreConfigureChecks();
  382. void SetWorkingMode(WorkingMode mode) { this->CurrentWorkingMode = mode; }
  383. WorkingMode GetWorkingMode() { return this->CurrentWorkingMode; }
  384. //! Debug the try compile stuff by not deleting the files
  385. bool GetDebugTryCompile() const { return this->DebugTryCompile; }
  386. void DebugTryCompileOn() { this->DebugTryCompile = true; }
  387. /**
  388. * Generate CMAKE_ROOT and CMAKE_COMMAND cache entries
  389. */
  390. int AddCMakePaths();
  391. /**
  392. * Get the file comparison class
  393. */
  394. cmFileTimeCache* GetFileTimeCache() { return this->FileTimeCache.get(); }
  395. bool WasLogLevelSetViaCLI() const { return this->LogLevelWasSetViaCLI; }
  396. //! Get the selected log level for `message()` commands during the cmake run.
  397. LogLevel GetLogLevel() const { return this->MessageLogLevel; }
  398. void SetLogLevel(LogLevel level) { this->MessageLogLevel = level; }
  399. static LogLevel StringToLogLevel(const std::string& levelStr);
  400. static TraceFormat StringToTraceFormat(const std::string& levelStr);
  401. bool HasCheckInProgress() const
  402. {
  403. return !this->CheckInProgressMessages.empty();
  404. }
  405. std::size_t GetCheckInProgressSize() const
  406. {
  407. return this->CheckInProgressMessages.size();
  408. }
  409. std::string GetTopCheckInProgressMessage()
  410. {
  411. auto message = this->CheckInProgressMessages.top();
  412. this->CheckInProgressMessages.pop();
  413. return message;
  414. }
  415. void PushCheckInProgressMessage(std::string message)
  416. {
  417. this->CheckInProgressMessages.emplace(std::move(message));
  418. }
  419. //! Should `message` command display context.
  420. bool GetShowLogContext() const { return this->LogContext; }
  421. void SetShowLogContext(bool b) { this->LogContext = b; }
  422. //! Do we want debug output during the cmake run.
  423. bool GetDebugOutput() const { return this->DebugOutput; }
  424. void SetDebugOutputOn(bool b) { this->DebugOutput = b; }
  425. //! Do we want debug output from the find commands during the cmake run.
  426. bool GetDebugFindOutput() const { return this->DebugFindOutput; }
  427. bool GetDebugFindOutput(std::string const& var) const;
  428. bool GetDebugFindPkgOutput(std::string const& pkg) const;
  429. void SetDebugFindOutput(bool b) { this->DebugFindOutput = b; }
  430. void SetDebugFindOutputPkgs(std::string const& args);
  431. void SetDebugFindOutputVars(std::string const& args);
  432. //! Do we want trace output during the cmake run.
  433. bool GetTrace() const { return this->Trace; }
  434. void SetTrace(bool b) { this->Trace = b; }
  435. bool GetTraceExpand() const { return this->TraceExpand; }
  436. void SetTraceExpand(bool b) { this->TraceExpand = b; }
  437. TraceFormat GetTraceFormat() const { return this->TraceFormatVar; }
  438. void SetTraceFormat(TraceFormat f) { this->TraceFormatVar = f; }
  439. void AddTraceSource(std::string const& file)
  440. {
  441. this->TraceOnlyThisSources.push_back(file);
  442. }
  443. std::vector<std::string> const& GetTraceSources() const
  444. {
  445. return this->TraceOnlyThisSources;
  446. }
  447. cmGeneratedFileStream& GetTraceFile() { return this->TraceFile; }
  448. void SetTraceFile(std::string const& file);
  449. void PrintTraceFormatVersion();
  450. bool GetWarnUninitialized() const { return this->WarnUninitialized; }
  451. void SetWarnUninitialized(bool b) { this->WarnUninitialized = b; }
  452. bool GetWarnUnusedCli() const { return this->WarnUnusedCli; }
  453. void SetWarnUnusedCli(bool b) { this->WarnUnusedCli = b; }
  454. bool GetCheckSystemVars() const { return this->CheckSystemVars; }
  455. void SetCheckSystemVars(bool b) { this->CheckSystemVars = b; }
  456. bool GetIgnoreWarningAsError() const { return this->IgnoreWarningAsError; }
  457. void SetIgnoreWarningAsError(bool b) { this->IgnoreWarningAsError = b; }
  458. void MarkCliAsUsed(const std::string& variable);
  459. /** Get the list of configurations (in upper case) considered to be
  460. debugging configurations.*/
  461. std::vector<std::string> GetDebugConfigs();
  462. void SetCMakeEditCommand(std::string const& s)
  463. {
  464. this->CMakeEditCommand = s;
  465. }
  466. std::string const& GetCMakeEditCommand() const
  467. {
  468. return this->CMakeEditCommand;
  469. }
  470. cmMessenger* GetMessenger() const { return this->Messenger.get(); }
  471. /**
  472. * Get the state of the suppression of developer (author) warnings.
  473. * Returns false, by default, if developer warnings should be shown, true
  474. * otherwise.
  475. */
  476. bool GetSuppressDevWarnings() const;
  477. /**
  478. * Set the state of the suppression of developer (author) warnings.
  479. */
  480. void SetSuppressDevWarnings(bool v);
  481. /**
  482. * Get the state of the suppression of deprecated warnings.
  483. * Returns false, by default, if deprecated warnings should be shown, true
  484. * otherwise.
  485. */
  486. bool GetSuppressDeprecatedWarnings() const;
  487. /**
  488. * Set the state of the suppression of deprecated warnings.
  489. */
  490. void SetSuppressDeprecatedWarnings(bool v);
  491. /**
  492. * Get the state of treating developer (author) warnings as errors.
  493. * Returns false, by default, if warnings should not be treated as errors,
  494. * true otherwise.
  495. */
  496. bool GetDevWarningsAsErrors() const;
  497. /**
  498. * Set the state of treating developer (author) warnings as errors.
  499. */
  500. void SetDevWarningsAsErrors(bool v);
  501. /**
  502. * Get the state of treating deprecated warnings as errors.
  503. * Returns false, by default, if warnings should not be treated as errors,
  504. * true otherwise.
  505. */
  506. bool GetDeprecatedWarningsAsErrors() const;
  507. /**
  508. * Set the state of treating developer (author) warnings as errors.
  509. */
  510. void SetDeprecatedWarningsAsErrors(bool v);
  511. /** Display a message to the user. */
  512. void IssueMessage(
  513. MessageType t, std::string const& text,
  514. cmListFileBacktrace const& backtrace = cmListFileBacktrace()) const;
  515. //! run the --build option
  516. int Build(int jobs, std::string dir, std::vector<std::string> targets,
  517. std::string config, std::vector<std::string> nativeOptions,
  518. cmBuildOptions& buildOptions, bool verbose,
  519. const std::string& presetName, bool listPresets);
  520. //! run the --open option
  521. bool Open(const std::string& dir, bool dryRun);
  522. void UnwatchUnusedCli(const std::string& var);
  523. void WatchUnusedCli(const std::string& var);
  524. cmState* GetState() const { return this->State.get(); }
  525. void SetCurrentSnapshot(cmStateSnapshot const& snapshot)
  526. {
  527. this->CurrentSnapshot = snapshot;
  528. }
  529. cmStateSnapshot GetCurrentSnapshot() const { return this->CurrentSnapshot; }
  530. bool GetRegenerateDuringBuild() const { return this->RegenerateDuringBuild; }
  531. #if !defined(CMAKE_BOOTSTRAP)
  532. cmMakefileProfilingData& GetProfilingOutput();
  533. bool IsProfilingEnabled() const;
  534. #endif
  535. protected:
  536. void RunCheckForUnusedVariables();
  537. int HandleDeleteCacheVariables(const std::string& var);
  538. using RegisteredGeneratorsVector =
  539. std::vector<std::unique_ptr<cmGlobalGeneratorFactory>>;
  540. RegisteredGeneratorsVector Generators;
  541. using RegisteredExtraGeneratorsVector =
  542. std::vector<cmExternalMakefileProjectGeneratorFactory*>;
  543. RegisteredExtraGeneratorsVector ExtraGenerators;
  544. void AddScriptingCommands() const;
  545. void AddProjectCommands() const;
  546. void AddDefaultGenerators();
  547. void AddDefaultExtraGenerators();
  548. std::map<std::string, DiagLevel> DiagLevels;
  549. std::string GeneratorInstance;
  550. std::string GeneratorPlatform;
  551. std::string GeneratorToolset;
  552. bool GeneratorInstanceSet = false;
  553. bool GeneratorPlatformSet = false;
  554. bool GeneratorToolsetSet = false;
  555. //! read in a cmake list file to initialize the cache
  556. void ReadListFile(const std::vector<std::string>& args,
  557. const std::string& path);
  558. bool FindPackage(const std::vector<std::string>& args);
  559. //! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  560. /// If it is set, truncate it to 50kb
  561. void TruncateOutputLog(const char* fname);
  562. /**
  563. * Method called to check build system integrity at build time.
  564. * Returns 1 if CMake should rerun and 0 otherwise.
  565. */
  566. int CheckBuildSystem();
  567. bool SetDirectoriesFromFile(const std::string& arg);
  568. //! Make sure all commands are what they say they are and there is no
  569. /// macros.
  570. void CleanupCommandsAndMacros();
  571. void GenerateGraphViz(const std::string& fileName) const;
  572. private:
  573. std::string CMakeWorkingDirectory;
  574. ProgressCallbackType ProgressCallback;
  575. WorkingMode CurrentWorkingMode = NORMAL_MODE;
  576. bool DebugOutput = false;
  577. bool DebugFindOutput = false;
  578. bool Trace = false;
  579. bool TraceExpand = false;
  580. TraceFormat TraceFormatVar = TRACE_HUMAN;
  581. cmGeneratedFileStream TraceFile;
  582. bool WarnUninitialized = false;
  583. bool WarnUnusedCli = true;
  584. bool CheckSystemVars = false;
  585. bool IgnoreWarningAsError = false;
  586. std::map<std::string, bool> UsedCliVariables;
  587. std::string CMakeEditCommand;
  588. std::string CXXEnvironment;
  589. std::string CCEnvironment;
  590. std::string CheckBuildSystemArgument;
  591. std::string CheckStampFile;
  592. std::string CheckStampList;
  593. std::string VSSolutionFile;
  594. std::string EnvironmentGenerator;
  595. FileExtensions CLikeSourceFileExtensions;
  596. FileExtensions HeaderFileExtensions;
  597. FileExtensions CudaFileExtensions;
  598. FileExtensions ISPCFileExtensions;
  599. FileExtensions FortranFileExtensions;
  600. FileExtensions HipFileExtensions;
  601. bool ClearBuildSystem = false;
  602. bool DebugTryCompile = false;
  603. bool FreshCache = false;
  604. bool RegenerateDuringBuild = false;
  605. std::unique_ptr<cmFileTimeCache> FileTimeCache;
  606. std::string GraphVizFile;
  607. InstalledFilesMap InstalledFiles;
  608. #ifndef CMAKE_BOOTSTRAP
  609. std::map<std::string, cm::optional<cmCMakePresetsGraph::CacheVariable>>
  610. UnprocessedPresetVariables;
  611. std::map<std::string, cm::optional<std::string>>
  612. UnprocessedPresetEnvironment;
  613. #endif
  614. #if !defined(CMAKE_BOOTSTRAP)
  615. std::unique_ptr<cmVariableWatch> VariableWatch;
  616. std::unique_ptr<cmFileAPI> FileAPI;
  617. #endif
  618. std::unique_ptr<cmState> State;
  619. cmStateSnapshot CurrentSnapshot;
  620. std::unique_ptr<cmMessenger> Messenger;
  621. std::vector<std::string> TraceOnlyThisSources;
  622. std::set<std::string> DebugFindPkgs;
  623. std::set<std::string> DebugFindVars;
  624. LogLevel MessageLogLevel = LogLevel::LOG_STATUS;
  625. bool LogLevelWasSetViaCLI = false;
  626. bool LogContext = false;
  627. std::stack<std::string> CheckInProgressMessages;
  628. std::unique_ptr<cmGlobalGenerator> GlobalGenerator;
  629. void UpdateConversionPathTable();
  630. //! Print a list of valid generators to stderr.
  631. void PrintGeneratorList();
  632. std::unique_ptr<cmGlobalGenerator> EvaluateDefaultGlobalGenerator();
  633. void CreateDefaultGlobalGenerator();
  634. void AppendGlobalGeneratorsDocumentation(std::vector<cmDocumentationEntry>&);
  635. void AppendExtraGeneratorsDocumentation(std::vector<cmDocumentationEntry>&);
  636. #if !defined(CMAKE_BOOTSTRAP)
  637. std::unique_ptr<cmMakefileProfilingData> ProfilingOutput;
  638. #endif
  639. };
  640. #define CMAKE_STANDARD_OPTIONS_TABLE \
  641. { "-S <path-to-source>", "Explicitly specify a source directory." }, \
  642. { "-B <path-to-build>", "Explicitly specify a build directory." }, \
  643. { "-C <initial-cache>", "Pre-load a script to populate the cache." }, \
  644. { "-D <var>[:<type>]=<value>", "Create or update a cmake cache entry." }, \
  645. { "-U <globbing_expr>", "Remove matching entries from CMake cache." }, \
  646. { "-G <generator-name>", "Specify a build system generator." }, \
  647. { "-T <toolset-name>", \
  648. "Specify toolset name if supported by generator." }, \
  649. { "-A <platform-name>", \
  650. "Specify platform name if supported by generator." }, \
  651. { "--toolchain <file>", \
  652. "Specify toolchain file [CMAKE_TOOLCHAIN_FILE]." }, \
  653. { "--install-prefix <directory>", \
  654. "Specify install directory [CMAKE_INSTALL_PREFIX]." }, \
  655. { "-Wdev", "Enable developer warnings." }, \
  656. { "-Wno-dev", "Suppress developer warnings." }, \
  657. { "-Werror=dev", "Make developer warnings errors." }, \
  658. { "-Wno-error=dev", "Make developer warnings not errors." }, \
  659. { "-Wdeprecated", "Enable deprecation warnings." }, \
  660. { "-Wno-deprecated", "Suppress deprecation warnings." }, \
  661. { "-Werror=deprecated", \
  662. "Make deprecated macro and function warnings " \
  663. "errors." }, \
  664. { \
  665. "-Wno-error=deprecated", \
  666. "Make deprecated macro and function warnings " \
  667. "not errors." \
  668. }
  669. #define FOR_EACH_C90_FEATURE(F) F(c_function_prototypes)
  670. #define FOR_EACH_C99_FEATURE(F) \
  671. F(c_restrict) \
  672. F(c_variadic_macros)
  673. #define FOR_EACH_C11_FEATURE(F) F(c_static_assert)
  674. #define FOR_EACH_C_FEATURE(F) \
  675. F(c_std_90) \
  676. F(c_std_99) \
  677. F(c_std_11) \
  678. F(c_std_17) \
  679. F(c_std_23) \
  680. FOR_EACH_C90_FEATURE(F) \
  681. FOR_EACH_C99_FEATURE(F) \
  682. FOR_EACH_C11_FEATURE(F)
  683. #define FOR_EACH_CXX98_FEATURE(F) F(cxx_template_template_parameters)
  684. #define FOR_EACH_CXX11_FEATURE(F) \
  685. F(cxx_alias_templates) \
  686. F(cxx_alignas) \
  687. F(cxx_alignof) \
  688. F(cxx_attributes) \
  689. F(cxx_auto_type) \
  690. F(cxx_constexpr) \
  691. F(cxx_decltype) \
  692. F(cxx_decltype_incomplete_return_types) \
  693. F(cxx_default_function_template_args) \
  694. F(cxx_defaulted_functions) \
  695. F(cxx_defaulted_move_initializers) \
  696. F(cxx_delegating_constructors) \
  697. F(cxx_deleted_functions) \
  698. F(cxx_enum_forward_declarations) \
  699. F(cxx_explicit_conversions) \
  700. F(cxx_extended_friend_declarations) \
  701. F(cxx_extern_templates) \
  702. F(cxx_final) \
  703. F(cxx_func_identifier) \
  704. F(cxx_generalized_initializers) \
  705. F(cxx_inheriting_constructors) \
  706. F(cxx_inline_namespaces) \
  707. F(cxx_lambdas) \
  708. F(cxx_local_type_template_args) \
  709. F(cxx_long_long_type) \
  710. F(cxx_noexcept) \
  711. F(cxx_nonstatic_member_init) \
  712. F(cxx_nullptr) \
  713. F(cxx_override) \
  714. F(cxx_range_for) \
  715. F(cxx_raw_string_literals) \
  716. F(cxx_reference_qualified_functions) \
  717. F(cxx_right_angle_brackets) \
  718. F(cxx_rvalue_references) \
  719. F(cxx_sizeof_member) \
  720. F(cxx_static_assert) \
  721. F(cxx_strong_enums) \
  722. F(cxx_thread_local) \
  723. F(cxx_trailing_return_types) \
  724. F(cxx_unicode_literals) \
  725. F(cxx_uniform_initialization) \
  726. F(cxx_unrestricted_unions) \
  727. F(cxx_user_literals) \
  728. F(cxx_variadic_macros) \
  729. F(cxx_variadic_templates)
  730. #define FOR_EACH_CXX14_FEATURE(F) \
  731. F(cxx_aggregate_default_initializers) \
  732. F(cxx_attribute_deprecated) \
  733. F(cxx_binary_literals) \
  734. F(cxx_contextual_conversions) \
  735. F(cxx_decltype_auto) \
  736. F(cxx_digit_separators) \
  737. F(cxx_generic_lambdas) \
  738. F(cxx_lambda_init_captures) \
  739. F(cxx_relaxed_constexpr) \
  740. F(cxx_return_type_deduction) \
  741. F(cxx_variable_templates)
  742. #define FOR_EACH_CXX_FEATURE(F) \
  743. F(cxx_std_98) \
  744. F(cxx_std_11) \
  745. F(cxx_std_14) \
  746. F(cxx_std_17) \
  747. F(cxx_std_20) \
  748. F(cxx_std_23) \
  749. FOR_EACH_CXX98_FEATURE(F) \
  750. FOR_EACH_CXX11_FEATURE(F) \
  751. FOR_EACH_CXX14_FEATURE(F)
  752. #define FOR_EACH_CUDA_FEATURE(F) \
  753. F(cuda_std_03) \
  754. F(cuda_std_11) \
  755. F(cuda_std_14) \
  756. F(cuda_std_17) \
  757. F(cuda_std_20) \
  758. F(cuda_std_23)
  759. #define FOR_EACH_HIP_FEATURE(F) \
  760. F(hip_std_98) \
  761. F(hip_std_11) \
  762. F(hip_std_14) \
  763. F(hip_std_17) \
  764. F(hip_std_20) \
  765. F(hip_std_23)