cmake.h 35 KB

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