cmake.h 36 KB

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