cmake.h 34 KB

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