cmCMakePresetsGraph.h 15 KB

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