cmCMakePresetsGraph.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. enum class PackageResolveMode;
  14. class cmCMakePresetsGraph
  15. {
  16. public:
  17. enum class ReadFileResult
  18. {
  19. READ_OK,
  20. FILE_NOT_FOUND,
  21. JSON_PARSE_ERROR,
  22. INVALID_ROOT,
  23. NO_VERSION,
  24. INVALID_VERSION,
  25. UNRECOGNIZED_VERSION,
  26. INVALID_CMAKE_VERSION,
  27. UNRECOGNIZED_CMAKE_VERSION,
  28. INVALID_PRESETS,
  29. INVALID_PRESET,
  30. INVALID_VARIABLE,
  31. DUPLICATE_PRESETS,
  32. CYCLIC_PRESET_INHERITANCE,
  33. PRESET_UNREACHABLE_FROM_FILE,
  34. INVALID_MACRO_EXPANSION,
  35. BUILD_TEST_PRESETS_UNSUPPORTED,
  36. INCLUDE_UNSUPPORTED,
  37. INVALID_INCLUDE,
  38. INVALID_CONFIGURE_PRESET,
  39. INSTALL_PREFIX_UNSUPPORTED,
  40. INVALID_CONDITION,
  41. CONDITION_UNSUPPORTED,
  42. TOOLCHAIN_FILE_UNSUPPORTED,
  43. CYCLIC_INCLUDE,
  44. };
  45. enum class ArchToolsetStrategy
  46. {
  47. Set,
  48. External,
  49. };
  50. class CacheVariable
  51. {
  52. public:
  53. std::string Type;
  54. std::string Value;
  55. };
  56. class Condition;
  57. class File
  58. {
  59. public:
  60. std::string Filename;
  61. int Version;
  62. std::unordered_set<File*> ReachableFiles;
  63. };
  64. class Preset
  65. {
  66. public:
  67. Preset() = default;
  68. Preset(Preset&& /*other*/) = default;
  69. Preset(const Preset& /*other*/) = default;
  70. Preset& operator=(const Preset& /*other*/) = default;
  71. virtual ~Preset() = default;
  72. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  73. Preset& operator=(Preset&& /*other*/) = default;
  74. #else
  75. // The move assignment operators for several STL classes did not become
  76. // noexcept until C++17, which causes some tools to warn about this move
  77. // assignment operator throwing an exception when it shouldn't.
  78. Preset& operator=(Preset&& /*other*/) = delete;
  79. #endif
  80. std::string Name;
  81. std::vector<std::string> Inherits;
  82. bool Hidden;
  83. File* OriginFile;
  84. std::string DisplayName;
  85. std::string Description;
  86. std::shared_ptr<Condition> ConditionEvaluator;
  87. bool ConditionResult = true;
  88. std::map<std::string, cm::optional<std::string>> Environment;
  89. virtual ReadFileResult VisitPresetInherit(const Preset& parent) = 0;
  90. virtual ReadFileResult VisitPresetBeforeInherit()
  91. {
  92. return ReadFileResult::READ_OK;
  93. }
  94. virtual ReadFileResult VisitPresetAfterInherit(int /* version */)
  95. {
  96. return ReadFileResult::READ_OK;
  97. }
  98. };
  99. class ConfigurePreset : public Preset
  100. {
  101. public:
  102. ConfigurePreset() = default;
  103. ConfigurePreset(ConfigurePreset&& /*other*/) = default;
  104. ConfigurePreset(const ConfigurePreset& /*other*/) = default;
  105. ConfigurePreset& operator=(const ConfigurePreset& /*other*/) = default;
  106. ~ConfigurePreset() override = default;
  107. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  108. ConfigurePreset& operator=(ConfigurePreset&& /*other*/) = default;
  109. #else
  110. // The move assignment operators for several STL classes did not become
  111. // noexcept until C++17, which causes some tools to warn about this move
  112. // assignment operator throwing an exception when it shouldn't.
  113. ConfigurePreset& operator=(ConfigurePreset&& /*other*/) = delete;
  114. #endif
  115. std::string Generator;
  116. std::string Architecture;
  117. cm::optional<ArchToolsetStrategy> ArchitectureStrategy;
  118. std::string Toolset;
  119. cm::optional<ArchToolsetStrategy> ToolsetStrategy;
  120. std::string ToolchainFile;
  121. std::string BinaryDir;
  122. std::string InstallDir;
  123. std::map<std::string, cm::optional<CacheVariable>> CacheVariables;
  124. cm::optional<bool> WarnDev;
  125. cm::optional<bool> ErrorDev;
  126. cm::optional<bool> WarnDeprecated;
  127. cm::optional<bool> ErrorDeprecated;
  128. cm::optional<bool> WarnUninitialized;
  129. cm::optional<bool> WarnUnusedCli;
  130. cm::optional<bool> WarnSystemVars;
  131. cm::optional<bool> DebugOutput;
  132. cm::optional<bool> DebugTryCompile;
  133. cm::optional<bool> DebugFind;
  134. ReadFileResult VisitPresetInherit(const Preset& parent) override;
  135. ReadFileResult VisitPresetBeforeInherit() override;
  136. ReadFileResult VisitPresetAfterInherit(int version) override;
  137. };
  138. class BuildPreset : public Preset
  139. {
  140. public:
  141. BuildPreset() = default;
  142. BuildPreset(BuildPreset&& /*other*/) = default;
  143. BuildPreset(const BuildPreset& /*other*/) = default;
  144. BuildPreset& operator=(const BuildPreset& /*other*/) = default;
  145. ~BuildPreset() override = default;
  146. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  147. BuildPreset& operator=(BuildPreset&& /*other*/) = default;
  148. #else
  149. // The move assignment operators for several STL classes did not become
  150. // noexcept until C++17, which causes some tools to warn about this move
  151. // assignment operator throwing an exception when it shouldn't.
  152. BuildPreset& operator=(BuildPreset&& /*other*/) = delete;
  153. #endif
  154. std::string ConfigurePreset;
  155. cm::optional<bool> InheritConfigureEnvironment;
  156. cm::optional<int> Jobs;
  157. std::vector<std::string> Targets;
  158. std::string Configuration;
  159. cm::optional<bool> CleanFirst;
  160. cm::optional<bool> Verbose;
  161. std::vector<std::string> NativeToolOptions;
  162. cm::optional<PackageResolveMode> ResolvePackageReferences;
  163. ReadFileResult VisitPresetInherit(const Preset& parent) override;
  164. ReadFileResult VisitPresetAfterInherit(int /* version */) override;
  165. };
  166. class TestPreset : public Preset
  167. {
  168. public:
  169. TestPreset() = default;
  170. TestPreset(TestPreset&& /*other*/) = default;
  171. TestPreset(const TestPreset& /*other*/) = default;
  172. TestPreset& operator=(const TestPreset& /*other*/) = default;
  173. ~TestPreset() override = default;
  174. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  175. TestPreset& operator=(TestPreset&& /*other*/) = default;
  176. #else
  177. // The move assignment operators for several STL classes did not become
  178. // noexcept until C++17, which causes some tools to warn about this move
  179. // assignment operator throwing an exception when it shouldn't.
  180. TestPreset& operator=(TestPreset&& /*other*/) = delete;
  181. #endif
  182. struct OutputOptions
  183. {
  184. enum class VerbosityEnum
  185. {
  186. Default,
  187. Verbose,
  188. Extra
  189. };
  190. cm::optional<bool> ShortProgress;
  191. cm::optional<VerbosityEnum> Verbosity;
  192. cm::optional<bool> Debug;
  193. cm::optional<bool> OutputOnFailure;
  194. cm::optional<bool> Quiet;
  195. std::string OutputLogFile;
  196. cm::optional<bool> LabelSummary;
  197. cm::optional<bool> SubprojectSummary;
  198. cm::optional<int> MaxPassedTestOutputSize;
  199. cm::optional<int> MaxFailedTestOutputSize;
  200. cm::optional<int> MaxTestNameWidth;
  201. };
  202. struct IncludeOptions
  203. {
  204. struct IndexOptions
  205. {
  206. cm::optional<int> Start;
  207. cm::optional<int> End;
  208. cm::optional<int> Stride;
  209. std::vector<int> SpecificTests;
  210. std::string IndexFile;
  211. };
  212. std::string Name;
  213. std::string Label;
  214. cm::optional<IndexOptions> Index;
  215. cm::optional<bool> UseUnion;
  216. };
  217. struct ExcludeOptions
  218. {
  219. struct FixturesOptions
  220. {
  221. std::string Any;
  222. std::string Setup;
  223. std::string Cleanup;
  224. };
  225. std::string Name;
  226. std::string Label;
  227. cm::optional<FixturesOptions> Fixtures;
  228. };
  229. struct FilterOptions
  230. {
  231. cm::optional<IncludeOptions> Include;
  232. cm::optional<ExcludeOptions> Exclude;
  233. };
  234. struct ExecutionOptions
  235. {
  236. enum class ShowOnlyEnum
  237. {
  238. Human,
  239. JsonV1
  240. };
  241. struct RepeatOptions
  242. {
  243. enum class ModeEnum
  244. {
  245. UntilFail,
  246. UntilPass,
  247. AfterTimeout
  248. };
  249. ModeEnum Mode;
  250. int Count;
  251. };
  252. enum class NoTestsActionEnum
  253. {
  254. Default,
  255. Error,
  256. Ignore
  257. };
  258. cm::optional<bool> StopOnFailure;
  259. cm::optional<bool> EnableFailover;
  260. cm::optional<int> Jobs;
  261. std::string ResourceSpecFile;
  262. cm::optional<int> TestLoad;
  263. cm::optional<ShowOnlyEnum> ShowOnly;
  264. cm::optional<RepeatOptions> Repeat;
  265. cm::optional<bool> InteractiveDebugging;
  266. cm::optional<bool> ScheduleRandom;
  267. cm::optional<int> Timeout;
  268. cm::optional<NoTestsActionEnum> NoTestsAction;
  269. };
  270. std::string ConfigurePreset;
  271. cm::optional<bool> InheritConfigureEnvironment;
  272. std::string Configuration;
  273. std::vector<std::string> OverwriteConfigurationFile;
  274. cm::optional<OutputOptions> Output;
  275. cm::optional<FilterOptions> Filter;
  276. cm::optional<ExecutionOptions> Execution;
  277. ReadFileResult VisitPresetInherit(const Preset& parent) override;
  278. ReadFileResult VisitPresetAfterInherit(int /* version */) override;
  279. };
  280. template <class T>
  281. class PresetPair
  282. {
  283. public:
  284. T Unexpanded;
  285. cm::optional<T> Expanded;
  286. };
  287. std::map<std::string, PresetPair<ConfigurePreset>> ConfigurePresets;
  288. std::map<std::string, PresetPair<BuildPreset>> BuildPresets;
  289. std::map<std::string, PresetPair<TestPreset>> TestPresets;
  290. std::vector<std::string> ConfigurePresetOrder;
  291. std::vector<std::string> BuildPresetOrder;
  292. std::vector<std::string> TestPresetOrder;
  293. std::string SourceDir;
  294. std::vector<std::unique_ptr<File>> Files;
  295. int GetVersion(const Preset& preset) const
  296. {
  297. return preset.OriginFile->Version;
  298. }
  299. static std::string GetFilename(const std::string& sourceDir);
  300. static std::string GetUserFilename(const std::string& sourceDir);
  301. ReadFileResult ReadProjectPresets(const std::string& sourceDir,
  302. bool allowNoFiles = false);
  303. static const char* ResultToString(ReadFileResult result);
  304. std::string GetGeneratorForPreset(const std::string& presetName) const
  305. {
  306. auto configurePresetName = presetName;
  307. auto buildPresetIterator = this->BuildPresets.find(presetName);
  308. if (buildPresetIterator != this->BuildPresets.end()) {
  309. configurePresetName =
  310. buildPresetIterator->second.Unexpanded.ConfigurePreset;
  311. } else {
  312. auto testPresetIterator = this->TestPresets.find(presetName);
  313. if (testPresetIterator != this->TestPresets.end()) {
  314. configurePresetName =
  315. testPresetIterator->second.Unexpanded.ConfigurePreset;
  316. }
  317. }
  318. auto configurePresetIterator =
  319. this->ConfigurePresets.find(configurePresetName);
  320. if (configurePresetIterator != this->ConfigurePresets.end()) {
  321. return configurePresetIterator->second.Unexpanded.Generator;
  322. }
  323. // This should only happen if the preset is hidden
  324. // or (for build or test presets) if ConfigurePreset is invalid.
  325. return "";
  326. }
  327. static void PrintPresets(
  328. const std::vector<const cmCMakePresetsGraph::Preset*>& presets);
  329. void PrintConfigurePresetList() const;
  330. void PrintConfigurePresetList(
  331. const std::function<bool(const ConfigurePreset&)>& filter) const;
  332. void PrintBuildPresetList() const;
  333. void PrintTestPresetList() const;
  334. void PrintAllPresets() const;
  335. private:
  336. enum class RootType
  337. {
  338. Project,
  339. User,
  340. };
  341. enum class ReadReason
  342. {
  343. Root,
  344. Included,
  345. };
  346. ReadFileResult ReadProjectPresetsInternal(bool allowNoFiles);
  347. ReadFileResult ReadJSONFile(const std::string& filename, RootType rootType,
  348. ReadReason readReason,
  349. std::vector<File*>& inProgressFiles,
  350. File*& file);
  351. void ClearPresets();
  352. };