1
0

cmake.h 35 KB

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