cmake.h 33 KB

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