cmCMakePresetsGraph.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 <functional>
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <unordered_set>
  10. #include <utility>
  11. #include <vector>
  12. #include <cm/optional>
  13. #include "cmJSONState.h"
  14. #include "cmStateTypes.h" // IWYU pragma: keep
  15. #include "CTest/cmCTestTypes.h" // IWYU pragma: keep
  16. enum class PackageResolveMode;
  17. class cmCMakePresetsGraph
  18. {
  19. public:
  20. std::string errors;
  21. cmJSONState parseState;
  22. enum class ArchToolsetStrategy
  23. {
  24. Set,
  25. External,
  26. };
  27. enum class TraceEnableMode
  28. {
  29. Disable,
  30. Default,
  31. Expand,
  32. };
  33. class CacheVariable
  34. {
  35. public:
  36. std::string Type;
  37. std::string Value;
  38. };
  39. class Condition;
  40. class File
  41. {
  42. public:
  43. std::string Filename;
  44. int Version;
  45. std::unordered_set<File*> ReachableFiles;
  46. };
  47. class Preset
  48. {
  49. public:
  50. Preset() = default;
  51. Preset(Preset&& /*other*/) = default;
  52. Preset(Preset const& /*other*/) = default;
  53. Preset& operator=(Preset const& /*other*/) = default;
  54. virtual ~Preset() = default;
  55. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  56. Preset& operator=(Preset&& /*other*/) = default;
  57. #else
  58. // The move assignment operators for several STL classes did not become
  59. // noexcept until C++17, which causes some tools to warn about this move
  60. // assignment operator throwing an exception when it shouldn't.
  61. Preset& operator=(Preset&& /*other*/) = delete;
  62. #endif
  63. std::string Name;
  64. std::vector<std::string> Inherits;
  65. bool Hidden = false;
  66. File* OriginFile;
  67. std::string DisplayName;
  68. std::string Description;
  69. std::shared_ptr<Condition> ConditionEvaluator;
  70. bool ConditionResult = true;
  71. std::map<std::string, cm::optional<std::string>> Environment;
  72. virtual bool VisitPresetInherit(Preset const& parent) = 0;
  73. virtual bool VisitPresetBeforeInherit() { return true; }
  74. virtual bool VisitPresetAfterInherit(int /* version */,
  75. cmJSONState* /*state*/)
  76. {
  77. return true;
  78. }
  79. };
  80. class ConfigurePreset : public Preset
  81. {
  82. public:
  83. ConfigurePreset() = default;
  84. ConfigurePreset(ConfigurePreset&& /*other*/) = default;
  85. ConfigurePreset(ConfigurePreset const& /*other*/) = default;
  86. ConfigurePreset& operator=(ConfigurePreset const& /*other*/) = default;
  87. ~ConfigurePreset() override = default;
  88. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  89. ConfigurePreset& operator=(ConfigurePreset&& /*other*/) = default;
  90. #else
  91. // The move assignment operators for several STL classes did not become
  92. // noexcept until C++17, which causes some tools to warn about this move
  93. // assignment operator throwing an exception when it shouldn't.
  94. ConfigurePreset& operator=(ConfigurePreset&& /*other*/) = delete;
  95. #endif
  96. std::string Generator;
  97. std::string Architecture;
  98. cm::optional<ArchToolsetStrategy> ArchitectureStrategy;
  99. std::string Toolset;
  100. cm::optional<ArchToolsetStrategy> ToolsetStrategy;
  101. std::string ToolchainFile;
  102. std::string GraphVizFile;
  103. std::string BinaryDir;
  104. std::string InstallDir;
  105. std::map<std::string, cm::optional<CacheVariable>> CacheVariables;
  106. cm::optional<bool> WarnDev;
  107. cm::optional<bool> ErrorDev;
  108. cm::optional<bool> WarnDeprecated;
  109. cm::optional<bool> ErrorDeprecated;
  110. cm::optional<bool> WarnUninitialized;
  111. cm::optional<bool> WarnUnusedCli;
  112. cm::optional<bool> WarnSystemVars;
  113. cm::optional<bool> DebugOutput;
  114. cm::optional<bool> DebugTryCompile;
  115. cm::optional<bool> DebugFind;
  116. cm::optional<TraceEnableMode> TraceMode;
  117. cm::optional<cmTraceEnums::TraceOutputFormat> TraceFormat;
  118. std::vector<std::string> TraceSource;
  119. std::string TraceRedirect;
  120. bool VisitPresetInherit(Preset const& parent) override;
  121. bool VisitPresetBeforeInherit() override;
  122. bool VisitPresetAfterInherit(int version, cmJSONState* state) override;
  123. };
  124. class BuildPreset : public Preset
  125. {
  126. public:
  127. BuildPreset() = default;
  128. BuildPreset(BuildPreset&& /*other*/) = default;
  129. BuildPreset(BuildPreset const& /*other*/) = default;
  130. BuildPreset& operator=(BuildPreset const& /*other*/) = default;
  131. ~BuildPreset() override = default;
  132. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  133. BuildPreset& operator=(BuildPreset&& /*other*/) = default;
  134. #else
  135. // The move assignment operators for several STL classes did not become
  136. // noexcept until C++17, which causes some tools to warn about this move
  137. // assignment operator throwing an exception when it shouldn't.
  138. BuildPreset& operator=(BuildPreset&& /*other*/) = delete;
  139. #endif
  140. std::string ConfigurePreset;
  141. cm::optional<bool> InheritConfigureEnvironment;
  142. cm::optional<unsigned int> Jobs;
  143. std::vector<std::string> Targets;
  144. std::string Configuration;
  145. cm::optional<bool> CleanFirst;
  146. cm::optional<bool> Verbose;
  147. std::vector<std::string> NativeToolOptions;
  148. cm::optional<PackageResolveMode> ResolvePackageReferences;
  149. bool VisitPresetInherit(Preset const& parent) override;
  150. bool VisitPresetAfterInherit(int /* version */,
  151. cmJSONState* /*state*/) override;
  152. };
  153. class TestPreset : public Preset
  154. {
  155. public:
  156. TestPreset() = default;
  157. TestPreset(TestPreset&& /*other*/) = default;
  158. TestPreset(TestPreset const& /*other*/) = default;
  159. TestPreset& operator=(TestPreset const& /*other*/) = default;
  160. ~TestPreset() override = default;
  161. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  162. TestPreset& operator=(TestPreset&& /*other*/) = default;
  163. #else
  164. // The move assignment operators for several STL classes did not become
  165. // noexcept until C++17, which causes some tools to warn about this move
  166. // assignment operator throwing an exception when it shouldn't.
  167. TestPreset& operator=(TestPreset&& /*other*/) = delete;
  168. #endif
  169. struct OutputOptions
  170. {
  171. enum class VerbosityEnum
  172. {
  173. Default,
  174. Verbose,
  175. Extra
  176. };
  177. cm::optional<bool> ShortProgress;
  178. cm::optional<VerbosityEnum> Verbosity;
  179. cm::optional<bool> Debug;
  180. cm::optional<bool> OutputOnFailure;
  181. cm::optional<bool> Quiet;
  182. std::string OutputLogFile;
  183. std::string OutputJUnitFile;
  184. cm::optional<bool> LabelSummary;
  185. cm::optional<bool> SubprojectSummary;
  186. cm::optional<int> MaxPassedTestOutputSize;
  187. cm::optional<int> MaxFailedTestOutputSize;
  188. cm::optional<cmCTestTypes::TruncationMode> TestOutputTruncation;
  189. cm::optional<int> MaxTestNameWidth;
  190. };
  191. struct IncludeOptions
  192. {
  193. struct IndexOptions
  194. {
  195. cm::optional<int> Start;
  196. cm::optional<int> End;
  197. cm::optional<int> Stride;
  198. std::vector<int> SpecificTests;
  199. std::string IndexFile;
  200. };
  201. std::string Name;
  202. std::string Label;
  203. cm::optional<IndexOptions> Index;
  204. cm::optional<bool> UseUnion;
  205. };
  206. struct ExcludeOptions
  207. {
  208. struct FixturesOptions
  209. {
  210. std::string Any;
  211. std::string Setup;
  212. std::string Cleanup;
  213. };
  214. std::string Name;
  215. std::string Label;
  216. cm::optional<FixturesOptions> Fixtures;
  217. };
  218. struct FilterOptions
  219. {
  220. cm::optional<IncludeOptions> Include;
  221. cm::optional<ExcludeOptions> Exclude;
  222. };
  223. struct ExecutionOptions
  224. {
  225. enum class ShowOnlyEnum
  226. {
  227. Human,
  228. JsonV1
  229. };
  230. struct RepeatOptions
  231. {
  232. enum class ModeEnum
  233. {
  234. UntilFail,
  235. UntilPass,
  236. AfterTimeout
  237. };
  238. ModeEnum Mode;
  239. int Count;
  240. };
  241. enum class NoTestsActionEnum
  242. {
  243. Default,
  244. Error,
  245. Ignore
  246. };
  247. cm::optional<bool> StopOnFailure;
  248. cm::optional<bool> EnableFailover;
  249. cm::optional<cm::optional<unsigned int>> Jobs;
  250. std::string ResourceSpecFile;
  251. cm::optional<int> TestLoad;
  252. cm::optional<ShowOnlyEnum> ShowOnly;
  253. cm::optional<RepeatOptions> Repeat;
  254. cm::optional<bool> InteractiveDebugging;
  255. cm::optional<bool> ScheduleRandom;
  256. cm::optional<int> Timeout;
  257. cm::optional<NoTestsActionEnum> NoTestsAction;
  258. };
  259. std::string ConfigurePreset;
  260. cm::optional<bool> InheritConfigureEnvironment;
  261. std::string Configuration;
  262. std::vector<std::string> OverwriteConfigurationFile;
  263. cm::optional<OutputOptions> Output;
  264. cm::optional<FilterOptions> Filter;
  265. cm::optional<ExecutionOptions> Execution;
  266. bool VisitPresetInherit(Preset const& parent) override;
  267. bool VisitPresetAfterInherit(int /* version */,
  268. cmJSONState* /*state*/) override;
  269. };
  270. class PackagePreset : public Preset
  271. {
  272. public:
  273. PackagePreset() = default;
  274. PackagePreset(PackagePreset&& /*other*/) = default;
  275. PackagePreset(PackagePreset const& /*other*/) = default;
  276. PackagePreset& operator=(PackagePreset const& /*other*/) = default;
  277. ~PackagePreset() override = default;
  278. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  279. PackagePreset& operator=(PackagePreset&& /*other*/) = default;
  280. #else
  281. // The move assignment operators for several STL classes did not become
  282. // noexcept until C++17, which causes some tools to warn about this move
  283. // assignment operator throwing an exception when it shouldn't.
  284. PackagePreset& operator=(PackagePreset&& /*other*/) = delete;
  285. #endif
  286. std::string ConfigurePreset;
  287. cm::optional<bool> InheritConfigureEnvironment;
  288. std::vector<std::string> Generators;
  289. std::vector<std::string> Configurations;
  290. std::map<std::string, std::string> Variables;
  291. std::string ConfigFile;
  292. cm::optional<bool> DebugOutput;
  293. cm::optional<bool> VerboseOutput;
  294. std::string PackageName;
  295. std::string PackageVersion;
  296. std::string PackageDirectory;
  297. std::string VendorName;
  298. bool VisitPresetInherit(Preset const& parent) override;
  299. bool VisitPresetAfterInherit(int /* version */,
  300. cmJSONState* /*state*/) override;
  301. };
  302. class WorkflowPreset : public Preset
  303. {
  304. public:
  305. WorkflowPreset() = default;
  306. WorkflowPreset(WorkflowPreset&& /*other*/) = default;
  307. WorkflowPreset(WorkflowPreset const& /*other*/) = default;
  308. WorkflowPreset& operator=(WorkflowPreset const& /*other*/) = default;
  309. ~WorkflowPreset() override = default;
  310. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  311. WorkflowPreset& operator=(WorkflowPreset&& /*other*/) = default;
  312. #else
  313. // The move assignment operators for several STL classes did not become
  314. // noexcept until C++17, which causes some tools to warn about this move
  315. // assignment operator throwing an exception when it shouldn't.
  316. WorkflowPreset& operator=(WorkflowPreset&& /*other*/) = delete;
  317. #endif
  318. class WorkflowStep
  319. {
  320. public:
  321. enum class Type
  322. {
  323. Configure,
  324. Build,
  325. Test,
  326. Package,
  327. };
  328. Type PresetType;
  329. std::string PresetName;
  330. };
  331. std::vector<WorkflowStep> Steps;
  332. bool VisitPresetInherit(Preset const& parent) override;
  333. bool VisitPresetAfterInherit(int /* version */,
  334. cmJSONState* /* state */) override;
  335. };
  336. template <class T>
  337. class PresetPair
  338. {
  339. public:
  340. T Unexpanded;
  341. cm::optional<T> Expanded;
  342. };
  343. std::map<std::string, PresetPair<ConfigurePreset>> ConfigurePresets;
  344. std::map<std::string, PresetPair<BuildPreset>> BuildPresets;
  345. std::map<std::string, PresetPair<TestPreset>> TestPresets;
  346. std::map<std::string, PresetPair<PackagePreset>> PackagePresets;
  347. std::map<std::string, PresetPair<WorkflowPreset>> WorkflowPresets;
  348. std::vector<std::string> ConfigurePresetOrder;
  349. std::vector<std::string> BuildPresetOrder;
  350. std::vector<std::string> TestPresetOrder;
  351. std::vector<std::string> PackagePresetOrder;
  352. std::vector<std::string> WorkflowPresetOrder;
  353. std::string SourceDir;
  354. std::vector<std::unique_ptr<File>> Files;
  355. int GetVersion(Preset const& preset) const
  356. {
  357. return preset.OriginFile->Version;
  358. }
  359. static std::string GetFilename(std::string const& sourceDir);
  360. static std::string GetUserFilename(std::string const& sourceDir);
  361. bool ReadProjectPresets(std::string const& sourceDir,
  362. bool allowNoFiles = false);
  363. std::string GetGeneratorForPreset(std::string const& presetName) const
  364. {
  365. auto configurePresetName = presetName;
  366. auto buildPresetIterator = this->BuildPresets.find(presetName);
  367. if (buildPresetIterator != this->BuildPresets.end()) {
  368. configurePresetName =
  369. buildPresetIterator->second.Unexpanded.ConfigurePreset;
  370. } else {
  371. auto testPresetIterator = this->TestPresets.find(presetName);
  372. if (testPresetIterator != this->TestPresets.end()) {
  373. configurePresetName =
  374. testPresetIterator->second.Unexpanded.ConfigurePreset;
  375. }
  376. }
  377. auto configurePresetIterator =
  378. this->ConfigurePresets.find(configurePresetName);
  379. if (configurePresetIterator != this->ConfigurePresets.end()) {
  380. return configurePresetIterator->second.Unexpanded.Generator;
  381. }
  382. // This should only happen if the preset is hidden
  383. // or (for build or test presets) if ConfigurePreset is invalid.
  384. return "";
  385. }
  386. enum class PrintPrecedingNewline
  387. {
  388. False,
  389. True,
  390. };
  391. static void printPrecedingNewline(PrintPrecedingNewline* p);
  392. static void PrintPresets(
  393. std::vector<cmCMakePresetsGraph::Preset const*> const& presets);
  394. void PrintConfigurePresetList(
  395. PrintPrecedingNewline* newline = nullptr) const;
  396. void PrintConfigurePresetList(
  397. std::function<bool(ConfigurePreset const&)> const& filter,
  398. PrintPrecedingNewline* newline = nullptr) const;
  399. void PrintBuildPresetList(PrintPrecedingNewline* newline = nullptr) const;
  400. void PrintTestPresetList(PrintPrecedingNewline* newline = nullptr) const;
  401. void PrintPackagePresetList(PrintPrecedingNewline* newline = nullptr) const;
  402. void PrintPackagePresetList(
  403. std::function<bool(PackagePreset const&)> const& filter,
  404. PrintPrecedingNewline* newline = nullptr) const;
  405. void PrintWorkflowPresetList(PrintPrecedingNewline* newline = nullptr) const;
  406. void PrintAllPresets() const;
  407. private:
  408. enum class RootType
  409. {
  410. Project,
  411. User,
  412. };
  413. enum class ReadReason
  414. {
  415. Root,
  416. Included,
  417. };
  418. bool ReadProjectPresetsInternal(bool allowNoFiles);
  419. bool ReadJSONFile(std::string const& filename, RootType rootType,
  420. ReadReason readReason, std::vector<File*>& inProgressFiles,
  421. File*& file, std::string& errMsg);
  422. void ClearPresets();
  423. };