cmCMakePresetsGraph.h 11 KB

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