cmake.h 34 KB

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