cmake.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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 <stack>
  11. #include <string>
  12. #include <unordered_set>
  13. #include <utility>
  14. #include <vector>
  15. #include <cm/string_view>
  16. #include <cmext/string_view>
  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. #endif
  30. class cmExternalMakefileProjectGeneratorFactory;
  31. class cmFileAPI;
  32. class cmFileTimeCache;
  33. class cmGlobalGenerator;
  34. class cmGlobalGeneratorFactory;
  35. class cmMakefile;
  36. #if !defined(CMAKE_BOOTSTRAP)
  37. class cmMakefileProfilingData;
  38. #endif
  39. class cmMessenger;
  40. class cmVariableWatch;
  41. struct cmBuildOptions;
  42. struct cmDocumentationEntry;
  43. /** \brief Represents a cmake invocation.
  44. *
  45. * This class represents a cmake invocation. It is the top level class when
  46. * running cmake. Most cmake based GUIs should primarily create an instance
  47. * of this class and communicate with it.
  48. *
  49. * The basic process for a GUI is as follows:
  50. *
  51. * -# Create a cmake instance
  52. * -# Set the Home directories, generator, and cmake command. this
  53. * can be done using the Set methods or by using SetArgs and passing in
  54. * command line arguments.
  55. * -# Load the cache by calling LoadCache (duh)
  56. * -# if you are using command line arguments with -D or -C flags then
  57. * call SetCacheArgs (or if for some other reason you want to modify the
  58. * cache), do it now.
  59. * -# Finally call Configure
  60. * -# Let the user change values and go back to step 5
  61. * -# call Generate
  62. * If your GUI allows the user to change the home directories then
  63. * you must at a minimum redo steps 2 through 7.
  64. */
  65. class cmake
  66. {
  67. public:
  68. enum Role
  69. {
  70. RoleInternal, // no commands
  71. RoleScript, // script commands
  72. RoleProject // all commands
  73. };
  74. enum DiagLevel
  75. {
  76. DIAG_IGNORE,
  77. DIAG_WARN,
  78. DIAG_ERROR
  79. };
  80. /** \brief Describes the working modes of cmake */
  81. enum WorkingMode
  82. {
  83. NORMAL_MODE, ///< Cmake runs to create project files
  84. /** \brief Script mode (started by using -P).
  85. *
  86. * In script mode there is no generator and no cache. Also,
  87. * languages are not enabled, so add_executable and things do
  88. * nothing.
  89. */
  90. SCRIPT_MODE,
  91. /** \brief Help mode
  92. *
  93. * Used to print help for things that can only be determined after finding
  94. * the source directory, for example, the list of presets.
  95. */
  96. HELP_MODE,
  97. /** \brief A pkg-config like mode
  98. *
  99. * In this mode cmake just searches for a package and prints the results to
  100. * stdout. This is similar to SCRIPT_MODE, but commands like add_library()
  101. * work too, since they may be used e.g. in exported target files. Started
  102. * via --find-package.
  103. */
  104. FIND_PACKAGE_MODE
  105. };
  106. /** \brief Define supported trace formats **/
  107. enum TraceFormat
  108. {
  109. TRACE_UNDEFINED,
  110. TRACE_HUMAN,
  111. TRACE_JSON_V1,
  112. };
  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 const int NO_BUILD_PARALLEL_LEVEL = -1;
  135. static const int 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(const std::string& dir);
  170. std::string const& GetHomeDirectory() const;
  171. void SetHomeOutputDirectory(const std::string& 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(const std::vector<std::string>& args)
  185. {
  186. return this->Run(args, false);
  187. }
  188. int Run(const std::vector<std::string>& 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(const std::string& entry, std::string& var,
  203. std::string& value,
  204. cmStateEnums::CacheEntryType& type);
  205. int LoadCache();
  206. bool LoadCache(const std::string& path);
  207. bool LoadCache(const std::string& path, bool internal,
  208. std::set<std::string>& excludes,
  209. std::set<std::string>& includes);
  210. bool SaveCache(const std::string& path);
  211. bool DeleteCache(const std::string& path);
  212. void PreLoadCMakeFiles();
  213. //! Create a GlobalGenerator
  214. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  215. const std::string& name, bool allowArch = true);
  216. //! Create a GlobalGenerator and set it as our own
  217. bool CreateAndSetGlobalGenerator(const std::string& name, bool allowArch);
  218. #ifndef CMAKE_BOOTSTRAP
  219. //! Print list of configure presets
  220. void PrintPresetList(const cmCMakePresetsGraph& 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. const cmGlobalGenerator* 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(const std::string& 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,
  238. bool includeNamesWithPlatform = true) const;
  239. //! Set the name of the selected generator-specific instance.
  240. void SetGeneratorInstance(std::string const& instance)
  241. {
  242. this->GeneratorInstance = instance;
  243. this->GeneratorInstanceSet = true;
  244. }
  245. //! Set the name of the selected generator-specific platform.
  246. void SetGeneratorPlatform(std::string const& ts)
  247. {
  248. this->GeneratorPlatform = ts;
  249. this->GeneratorPlatformSet = true;
  250. }
  251. //! Set the name of the selected generator-specific toolset.
  252. void SetGeneratorToolset(std::string const& ts)
  253. {
  254. this->GeneratorToolset = ts;
  255. this->GeneratorToolsetSet = true;
  256. }
  257. bool IsAKnownSourceExtension(cm::string_view ext) const
  258. {
  259. return this->CLikeSourceFileExtensions.Test(ext) ||
  260. this->CudaFileExtensions.Test(ext) ||
  261. this->FortranFileExtensions.Test(ext) ||
  262. this->HipFileExtensions.Test(ext) || this->ISPCFileExtensions.Test(ext);
  263. }
  264. bool IsACLikeSourceExtension(cm::string_view ext) const
  265. {
  266. return this->CLikeSourceFileExtensions.Test(ext);
  267. }
  268. bool IsAKnownExtension(cm::string_view ext) const
  269. {
  270. return this->IsAKnownSourceExtension(ext) || this->IsAHeaderExtension(ext);
  271. }
  272. std::vector<std::string> GetAllExtensions() const;
  273. const std::vector<std::string>& GetHeaderExtensions() const
  274. {
  275. return this->HeaderFileExtensions.ordered;
  276. }
  277. bool IsAHeaderExtension(cm::string_view ext) const
  278. {
  279. return this->HeaderFileExtensions.Test(ext);
  280. }
  281. // Strips the extension (if present and known) from a filename
  282. std::string StripExtension(const std::string& file) const;
  283. /**
  284. * Given a variable name, return its value (as a string).
  285. */
  286. cmValue GetCacheDefinition(const std::string&) const;
  287. //! Add an entry into the cache
  288. void AddCacheEntry(const std::string& key, const char* value,
  289. const char* helpString, int type)
  290. {
  291. this->AddCacheEntry(key,
  292. value ? cmValue(std::string(value)) : cmValue(nullptr),
  293. helpString, type);
  294. }
  295. void AddCacheEntry(const std::string& key, const std::string& value,
  296. const char* helpString, int type)
  297. {
  298. this->AddCacheEntry(key, cmValue(value), helpString, type);
  299. }
  300. void AddCacheEntry(const std::string& key, cmValue value,
  301. const char* helpString, int type);
  302. bool DoWriteGlobVerifyTarget() const;
  303. std::string const& GetGlobVerifyScript() const;
  304. std::string const& GetGlobVerifyStamp() const;
  305. void AddGlobCacheEntry(bool recurse, bool listDirectories,
  306. bool followSymlinks, const std::string& relative,
  307. const std::string& expression,
  308. const std::vector<std::string>& files,
  309. const std::string& variable,
  310. cmListFileBacktrace const& bt);
  311. /**
  312. * Get the system information and write it to the file specified
  313. */
  314. int GetSystemInformation(std::vector<std::string>&);
  315. //! Parse environment variables
  316. void LoadEnvironmentPresets();
  317. //! Parse command line arguments
  318. void SetArgs(const std::vector<std::string>& args);
  319. //! Is this cmake running as a result of a TRY_COMPILE command
  320. bool GetIsInTryCompile() const;
  321. #ifndef CMAKE_BOOTSTRAP
  322. void SetWarningFromPreset(const std::string& name,
  323. const cm::optional<bool>& warning,
  324. const cm::optional<bool>& error);
  325. void ProcessPresetVariables();
  326. void PrintPresetVariables();
  327. void ProcessPresetEnvironment();
  328. void PrintPresetEnvironment();
  329. #endif
  330. //! Parse command line arguments that might set cache values
  331. bool SetCacheArgs(const std::vector<std::string>&);
  332. void ProcessCacheArg(const std::string& var, const std::string& value,
  333. cmStateEnums::CacheEntryType type);
  334. using ProgressCallbackType = std::function<void(const std::string&, float)>;
  335. /**
  336. * Set the function used by GUIs to receive progress updates
  337. * Function gets passed: message as a const char*, a progress
  338. * amount ranging from 0 to 1.0 and client data. The progress
  339. * number provided may be negative in cases where a message is
  340. * to be displayed without any progress percentage.
  341. */
  342. void SetProgressCallback(ProgressCallbackType f);
  343. //! this is called by generators to update the progress
  344. void UpdateProgress(const std::string& msg, float prog);
  345. #if !defined(CMAKE_BOOTSTRAP)
  346. //! Get the variable watch object
  347. cmVariableWatch* GetVariableWatch() { return this->VariableWatch.get(); }
  348. #endif
  349. std::vector<cmDocumentationEntry> GetGeneratorsDocumentation();
  350. //! Set/Get a property of this target file
  351. void SetProperty(const std::string& prop, const char* value);
  352. void SetProperty(const std::string& prop, cmValue value);
  353. void SetProperty(const std::string& prop, const std::string& value)
  354. {
  355. this->SetProperty(prop, cmValue(value));
  356. }
  357. void AppendProperty(const std::string& prop, const std::string& value,
  358. bool asString = false);
  359. cmValue GetProperty(const std::string& prop);
  360. bool GetPropertyAsBool(const std::string& prop);
  361. //! Get or create an cmInstalledFile instance and return a pointer to it
  362. cmInstalledFile* GetOrCreateInstalledFile(cmMakefile* mf,
  363. const std::string& name);
  364. cmInstalledFile const* GetInstalledFile(const std::string& 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(const std::string& 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.top();
  402. this->CheckInProgressMessages.pop();
  403. return message;
  404. }
  405. void PushCheckInProgressMessage(std::string message)
  406. {
  407. this->CheckInProgressMessages.emplace(std::move(message));
  408. }
  409. //! Should `message` command display context.
  410. bool GetShowLogContext() const { return this->LogContext; }
  411. void SetShowLogContext(bool b) { this->LogContext = b; }
  412. //! Do we want debug output during the cmake run.
  413. bool GetDebugOutput() const { return this->DebugOutput; }
  414. void SetDebugOutputOn(bool b) { this->DebugOutput = b; }
  415. //! Do we want debug output from the find commands during the cmake run.
  416. bool GetDebugFindOutput() const { return this->DebugFindOutput; }
  417. bool GetDebugFindOutput(std::string const& var) const;
  418. bool GetDebugFindPkgOutput(std::string const& pkg) const;
  419. void SetDebugFindOutput(bool b) { this->DebugFindOutput = b; }
  420. void SetDebugFindOutputPkgs(std::string const& args);
  421. void SetDebugFindOutputVars(std::string const& args);
  422. //! Do we want trace output during the cmake run.
  423. bool GetTrace() const { return this->Trace; }
  424. void SetTrace(bool b) { this->Trace = b; }
  425. bool GetTraceExpand() const { return this->TraceExpand; }
  426. void SetTraceExpand(bool b) { this->TraceExpand = b; }
  427. TraceFormat GetTraceFormat() const { return this->TraceFormatVar; }
  428. void SetTraceFormat(TraceFormat f) { this->TraceFormatVar = f; }
  429. void AddTraceSource(std::string const& file)
  430. {
  431. this->TraceOnlyThisSources.push_back(file);
  432. }
  433. std::vector<std::string> const& GetTraceSources() const
  434. {
  435. return this->TraceOnlyThisSources;
  436. }
  437. cmGeneratedFileStream& GetTraceFile() { return this->TraceFile; }
  438. void SetTraceFile(std::string const& file);
  439. void PrintTraceFormatVersion();
  440. bool GetWarnUninitialized() const { return this->WarnUninitialized; }
  441. void SetWarnUninitialized(bool b) { this->WarnUninitialized = b; }
  442. bool GetWarnUnusedCli() const { return this->WarnUnusedCli; }
  443. void SetWarnUnusedCli(bool b) { this->WarnUnusedCli = b; }
  444. bool GetCheckSystemVars() const { return this->CheckSystemVars; }
  445. void SetCheckSystemVars(bool b) { this->CheckSystemVars = b; }
  446. bool GetIgnoreWarningAsError() const { return this->IgnoreWarningAsError; }
  447. void SetIgnoreWarningAsError(bool b) { this->IgnoreWarningAsError = b; }
  448. void MarkCliAsUsed(const std::string& variable);
  449. /** Get the list of configurations (in upper case) considered to be
  450. debugging configurations.*/
  451. std::vector<std::string> GetDebugConfigs();
  452. void SetCMakeEditCommand(std::string const& s)
  453. {
  454. this->CMakeEditCommand = s;
  455. }
  456. std::string const& GetCMakeEditCommand() const
  457. {
  458. return this->CMakeEditCommand;
  459. }
  460. cmMessenger* GetMessenger() const { return this->Messenger.get(); }
  461. /**
  462. * Get the state of the suppression of developer (author) warnings.
  463. * Returns false, by default, if developer warnings should be shown, true
  464. * otherwise.
  465. */
  466. bool GetSuppressDevWarnings() const;
  467. /**
  468. * Set the state of the suppression of developer (author) warnings.
  469. */
  470. void SetSuppressDevWarnings(bool v);
  471. /**
  472. * Get the state of the suppression of deprecated warnings.
  473. * Returns false, by default, if deprecated warnings should be shown, true
  474. * otherwise.
  475. */
  476. bool GetSuppressDeprecatedWarnings() const;
  477. /**
  478. * Set the state of the suppression of deprecated warnings.
  479. */
  480. void SetSuppressDeprecatedWarnings(bool v);
  481. /**
  482. * Get the state of treating developer (author) warnings as errors.
  483. * Returns false, by default, if warnings should not be treated as errors,
  484. * true otherwise.
  485. */
  486. bool GetDevWarningsAsErrors() const;
  487. /**
  488. * Set the state of treating developer (author) warnings as errors.
  489. */
  490. void SetDevWarningsAsErrors(bool v);
  491. /**
  492. * Get the state of treating deprecated warnings as errors.
  493. * Returns false, by default, if warnings should not be treated as errors,
  494. * true otherwise.
  495. */
  496. bool GetDeprecatedWarningsAsErrors() const;
  497. /**
  498. * Set the state of treating developer (author) warnings as errors.
  499. */
  500. void SetDeprecatedWarningsAsErrors(bool v);
  501. /** Display a message to the user. */
  502. void IssueMessage(
  503. MessageType t, std::string const& text,
  504. cmListFileBacktrace const& backtrace = cmListFileBacktrace()) const;
  505. //! run the --build option
  506. int Build(int jobs, std::string dir, std::vector<std::string> targets,
  507. std::string config, std::vector<std::string> nativeOptions,
  508. cmBuildOptions& buildOptions, bool verbose,
  509. const std::string& presetName, bool listPresets);
  510. //! run the --open option
  511. bool Open(const std::string& dir, bool dryRun);
  512. //! run the --workflow option
  513. int Workflow(const std::string& presetName, bool listPresets);
  514. void UnwatchUnusedCli(const std::string& var);
  515. void WatchUnusedCli(const std::string& var);
  516. cmState* GetState() const { return this->State.get(); }
  517. void SetCurrentSnapshot(cmStateSnapshot const& snapshot)
  518. {
  519. this->CurrentSnapshot = snapshot;
  520. }
  521. cmStateSnapshot GetCurrentSnapshot() const { return this->CurrentSnapshot; }
  522. bool GetRegenerateDuringBuild() const { return this->RegenerateDuringBuild; }
  523. #if !defined(CMAKE_BOOTSTRAP)
  524. cmMakefileProfilingData& GetProfilingOutput();
  525. bool IsProfilingEnabled() const;
  526. #endif
  527. protected:
  528. void RunCheckForUnusedVariables();
  529. int HandleDeleteCacheVariables(const std::string& var);
  530. using RegisteredGeneratorsVector =
  531. std::vector<std::unique_ptr<cmGlobalGeneratorFactory>>;
  532. RegisteredGeneratorsVector Generators;
  533. using RegisteredExtraGeneratorsVector =
  534. std::vector<cmExternalMakefileProjectGeneratorFactory*>;
  535. RegisteredExtraGeneratorsVector ExtraGenerators;
  536. void AddScriptingCommands() const;
  537. void AddProjectCommands() const;
  538. void AddDefaultGenerators();
  539. void AddDefaultExtraGenerators();
  540. std::map<std::string, DiagLevel> DiagLevels;
  541. std::string GeneratorInstance;
  542. std::string GeneratorPlatform;
  543. std::string GeneratorToolset;
  544. bool GeneratorInstanceSet = false;
  545. bool GeneratorPlatformSet = false;
  546. bool GeneratorToolsetSet = false;
  547. //! read in a cmake list file to initialize the cache
  548. void ReadListFile(const std::vector<std::string>& args,
  549. const std::string& path);
  550. bool FindPackage(const std::vector<std::string>& args);
  551. //! Check if CMAKE_CACHEFILE_DIR is set. If it is not, delete the log file.
  552. /// If it is set, truncate it to 50kb
  553. void TruncateOutputLog(const char* fname);
  554. /**
  555. * Method called to check build system integrity at build time.
  556. * Returns 1 if CMake should rerun and 0 otherwise.
  557. */
  558. int CheckBuildSystem();
  559. bool SetDirectoriesFromFile(const std::string& arg);
  560. //! Make sure all commands are what they say they are and there is no
  561. /// macros.
  562. void CleanupCommandsAndMacros();
  563. void GenerateGraphViz(const std::string& fileName) const;
  564. private:
  565. std::string CMakeWorkingDirectory;
  566. ProgressCallbackType ProgressCallback;
  567. WorkingMode CurrentWorkingMode = NORMAL_MODE;
  568. bool DebugOutput = false;
  569. bool DebugFindOutput = false;
  570. bool Trace = false;
  571. bool TraceExpand = false;
  572. TraceFormat TraceFormatVar = TRACE_HUMAN;
  573. cmGeneratedFileStream TraceFile;
  574. bool WarnUninitialized = false;
  575. bool WarnUnusedCli = true;
  576. bool CheckSystemVars = false;
  577. bool IgnoreWarningAsError = false;
  578. std::map<std::string, bool> UsedCliVariables;
  579. std::string CMakeEditCommand;
  580. std::string CXXEnvironment;
  581. std::string CCEnvironment;
  582. std::string CheckBuildSystemArgument;
  583. std::string CheckStampFile;
  584. std::string CheckStampList;
  585. std::string VSSolutionFile;
  586. std::string EnvironmentGenerator;
  587. FileExtensions CLikeSourceFileExtensions;
  588. FileExtensions HeaderFileExtensions;
  589. FileExtensions CudaFileExtensions;
  590. FileExtensions ISPCFileExtensions;
  591. FileExtensions FortranFileExtensions;
  592. FileExtensions HipFileExtensions;
  593. bool ClearBuildSystem = false;
  594. bool DebugTryCompile = false;
  595. bool FreshCache = false;
  596. bool RegenerateDuringBuild = false;
  597. std::unique_ptr<cmFileTimeCache> FileTimeCache;
  598. std::string GraphVizFile;
  599. InstalledFilesMap InstalledFiles;
  600. #ifndef CMAKE_BOOTSTRAP
  601. std::map<std::string, cm::optional<cmCMakePresetsGraph::CacheVariable>>
  602. UnprocessedPresetVariables;
  603. std::map<std::string, cm::optional<std::string>>
  604. UnprocessedPresetEnvironment;
  605. #endif
  606. #if !defined(CMAKE_BOOTSTRAP)
  607. std::unique_ptr<cmVariableWatch> VariableWatch;
  608. std::unique_ptr<cmFileAPI> FileAPI;
  609. #endif
  610. std::unique_ptr<cmState> State;
  611. cmStateSnapshot CurrentSnapshot;
  612. std::unique_ptr<cmMessenger> Messenger;
  613. std::vector<std::string> TraceOnlyThisSources;
  614. std::set<std::string> DebugFindPkgs;
  615. std::set<std::string> DebugFindVars;
  616. Message::LogLevel MessageLogLevel = Message::LogLevel::LOG_STATUS;
  617. bool LogLevelWasSetViaCLI = false;
  618. bool LogContext = false;
  619. std::stack<std::string> CheckInProgressMessages;
  620. std::unique_ptr<cmGlobalGenerator> GlobalGenerator;
  621. void UpdateConversionPathTable();
  622. //! Print a list of valid generators to stderr.
  623. void PrintGeneratorList();
  624. std::unique_ptr<cmGlobalGenerator> EvaluateDefaultGlobalGenerator();
  625. void CreateDefaultGlobalGenerator();
  626. void AppendGlobalGeneratorsDocumentation(std::vector<cmDocumentationEntry>&);
  627. void AppendExtraGeneratorsDocumentation(std::vector<cmDocumentationEntry>&);
  628. #if !defined(CMAKE_BOOTSTRAP)
  629. template <typename T>
  630. const T* FindPresetForWorkflow(
  631. cm::static_string_view type,
  632. const std::map<std::string, cmCMakePresetsGraph::PresetPair<T>>& presets,
  633. const cmCMakePresetsGraph::WorkflowPreset::WorkflowStep& step);
  634. std::function<int()> BuildWorkflowStep(const std::vector<std::string>& args);
  635. #endif
  636. #if !defined(CMAKE_BOOTSTRAP)
  637. std::unique_ptr<cmMakefileProfilingData> ProfilingOutput;
  638. #endif
  639. };
  640. #define CMAKE_STANDARD_OPTIONS_TABLE \
  641. { "-S <path-to-source>", "Explicitly specify a source directory." }, \
  642. { "-B <path-to-build>", "Explicitly specify a build directory." }, \
  643. { "-C <initial-cache>", "Pre-load a script to populate the cache." }, \
  644. { "-D <var>[:<type>]=<value>", "Create or update a cmake cache entry." }, \
  645. { "-U <globbing_expr>", "Remove matching entries from CMake cache." }, \
  646. { "-G <generator-name>", "Specify a build system generator." }, \
  647. { "-T <toolset-name>", \
  648. "Specify toolset name if supported by generator." }, \
  649. { "-A <platform-name>", \
  650. "Specify platform name if supported by generator." }, \
  651. { "--toolchain <file>", \
  652. "Specify toolchain file [CMAKE_TOOLCHAIN_FILE]." }, \
  653. { "--install-prefix <directory>", \
  654. "Specify install directory [CMAKE_INSTALL_PREFIX]." }, \
  655. { "-Wdev", "Enable developer warnings." }, \
  656. { "-Wno-dev", "Suppress developer warnings." }, \
  657. { "-Werror=dev", "Make developer warnings errors." }, \
  658. { "-Wno-error=dev", "Make developer warnings not errors." }, \
  659. { "-Wdeprecated", "Enable deprecation warnings." }, \
  660. { "-Wno-deprecated", "Suppress deprecation warnings." }, \
  661. { "-Werror=deprecated", \
  662. "Make deprecated macro and function warnings " \
  663. "errors." }, \
  664. { \
  665. "-Wno-error=deprecated", \
  666. "Make deprecated macro and function warnings " \
  667. "not errors." \
  668. }
  669. #define FOR_EACH_C90_FEATURE(F) F(c_function_prototypes)
  670. #define FOR_EACH_C99_FEATURE(F) \
  671. F(c_restrict) \
  672. F(c_variadic_macros)
  673. #define FOR_EACH_C11_FEATURE(F) F(c_static_assert)
  674. #define FOR_EACH_C_FEATURE(F) \
  675. F(c_std_90) \
  676. F(c_std_99) \
  677. F(c_std_11) \
  678. F(c_std_17) \
  679. F(c_std_23) \
  680. FOR_EACH_C90_FEATURE(F) \
  681. FOR_EACH_C99_FEATURE(F) \
  682. FOR_EACH_C11_FEATURE(F)
  683. #define FOR_EACH_CXX98_FEATURE(F) F(cxx_template_template_parameters)
  684. #define FOR_EACH_CXX11_FEATURE(F) \
  685. F(cxx_alias_templates) \
  686. F(cxx_alignas) \
  687. F(cxx_alignof) \
  688. F(cxx_attributes) \
  689. F(cxx_auto_type) \
  690. F(cxx_constexpr) \
  691. F(cxx_decltype) \
  692. F(cxx_decltype_incomplete_return_types) \
  693. F(cxx_default_function_template_args) \
  694. F(cxx_defaulted_functions) \
  695. F(cxx_defaulted_move_initializers) \
  696. F(cxx_delegating_constructors) \
  697. F(cxx_deleted_functions) \
  698. F(cxx_enum_forward_declarations) \
  699. F(cxx_explicit_conversions) \
  700. F(cxx_extended_friend_declarations) \
  701. F(cxx_extern_templates) \
  702. F(cxx_final) \
  703. F(cxx_func_identifier) \
  704. F(cxx_generalized_initializers) \
  705. F(cxx_inheriting_constructors) \
  706. F(cxx_inline_namespaces) \
  707. F(cxx_lambdas) \
  708. F(cxx_local_type_template_args) \
  709. F(cxx_long_long_type) \
  710. F(cxx_noexcept) \
  711. F(cxx_nonstatic_member_init) \
  712. F(cxx_nullptr) \
  713. F(cxx_override) \
  714. F(cxx_range_for) \
  715. F(cxx_raw_string_literals) \
  716. F(cxx_reference_qualified_functions) \
  717. F(cxx_right_angle_brackets) \
  718. F(cxx_rvalue_references) \
  719. F(cxx_sizeof_member) \
  720. F(cxx_static_assert) \
  721. F(cxx_strong_enums) \
  722. F(cxx_thread_local) \
  723. F(cxx_trailing_return_types) \
  724. F(cxx_unicode_literals) \
  725. F(cxx_uniform_initialization) \
  726. F(cxx_unrestricted_unions) \
  727. F(cxx_user_literals) \
  728. F(cxx_variadic_macros) \
  729. F(cxx_variadic_templates)
  730. #define FOR_EACH_CXX14_FEATURE(F) \
  731. F(cxx_aggregate_default_initializers) \
  732. F(cxx_attribute_deprecated) \
  733. F(cxx_binary_literals) \
  734. F(cxx_contextual_conversions) \
  735. F(cxx_decltype_auto) \
  736. F(cxx_digit_separators) \
  737. F(cxx_generic_lambdas) \
  738. F(cxx_lambda_init_captures) \
  739. F(cxx_relaxed_constexpr) \
  740. F(cxx_return_type_deduction) \
  741. F(cxx_variable_templates)
  742. #define FOR_EACH_CXX_FEATURE(F) \
  743. F(cxx_std_98) \
  744. F(cxx_std_11) \
  745. F(cxx_std_14) \
  746. F(cxx_std_17) \
  747. F(cxx_std_20) \
  748. F(cxx_std_23) \
  749. F(cxx_std_26) \
  750. FOR_EACH_CXX98_FEATURE(F) \
  751. FOR_EACH_CXX11_FEATURE(F) \
  752. FOR_EACH_CXX14_FEATURE(F)
  753. #define FOR_EACH_CUDA_FEATURE(F) \
  754. F(cuda_std_03) \
  755. F(cuda_std_11) \
  756. F(cuda_std_14) \
  757. F(cuda_std_17) \
  758. F(cuda_std_20) \
  759. F(cuda_std_23) \
  760. F(cuda_std_26)
  761. #define FOR_EACH_HIP_FEATURE(F) \
  762. F(hip_std_98) \
  763. F(hip_std_11) \
  764. F(hip_std_14) \
  765. F(hip_std_17) \
  766. F(hip_std_20) \
  767. F(hip_std_23) \
  768. F(hip_std_26)