cmake.h 32 KB

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