cmCMakePresetsGraph.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. class cmCMakePresetsGraph
  14. {
  15. public:
  16. enum class ReadFileResult
  17. {
  18. READ_OK,
  19. FILE_NOT_FOUND,
  20. JSON_PARSE_ERROR,
  21. INVALID_ROOT,
  22. NO_VERSION,
  23. INVALID_VERSION,
  24. UNRECOGNIZED_VERSION,
  25. INVALID_CMAKE_VERSION,
  26. UNRECOGNIZED_CMAKE_VERSION,
  27. INVALID_PRESETS,
  28. INVALID_PRESET,
  29. INVALID_VARIABLE,
  30. DUPLICATE_PRESETS,
  31. CYCLIC_PRESET_INHERITANCE,
  32. PRESET_UNREACHABLE_FROM_FILE,
  33. INVALID_MACRO_EXPANSION,
  34. BUILD_TEST_PRESETS_UNSUPPORTED,
  35. INCLUDE_UNSUPPORTED,
  36. INVALID_INCLUDE,
  37. INVALID_CONFIGURE_PRESET,
  38. INSTALL_PREFIX_UNSUPPORTED,
  39. INVALID_CONDITION,
  40. CONDITION_UNSUPPORTED,
  41. TOOLCHAIN_FILE_UNSUPPORTED,
  42. CYCLIC_INCLUDE,
  43. INCLUDE_OUTSIDE_PROJECT,
  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. ReadFileResult VisitPresetInherit(const Preset& parent) override;
  163. ReadFileResult VisitPresetAfterInherit(int /* version */) override;
  164. };
  165. class TestPreset : public Preset
  166. {
  167. public:
  168. TestPreset() = default;
  169. TestPreset(TestPreset&& /*other*/) = default;
  170. TestPreset(const TestPreset& /*other*/) = default;
  171. TestPreset& operator=(const TestPreset& /*other*/) = default;
  172. ~TestPreset() override = default;
  173. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  174. TestPreset& operator=(TestPreset&& /*other*/) = default;
  175. #else
  176. // The move assignment operators for several STL classes did not become
  177. // noexcept until C++17, which causes some tools to warn about this move
  178. // assignment operator throwing an exception when it shouldn't.
  179. TestPreset& operator=(TestPreset&& /*other*/) = delete;
  180. #endif
  181. struct OutputOptions
  182. {
  183. enum class VerbosityEnum
  184. {
  185. Default,
  186. Verbose,
  187. Extra
  188. };
  189. cm::optional<bool> ShortProgress;
  190. cm::optional<VerbosityEnum> Verbosity;
  191. cm::optional<bool> Debug;
  192. cm::optional<bool> OutputOnFailure;
  193. cm::optional<bool> Quiet;
  194. std::string OutputLogFile;
  195. cm::optional<bool> LabelSummary;
  196. cm::optional<bool> SubprojectSummary;
  197. cm::optional<int> MaxPassedTestOutputSize;
  198. cm::optional<int> MaxFailedTestOutputSize;
  199. cm::optional<int> MaxTestNameWidth;
  200. };
  201. struct IncludeOptions
  202. {
  203. struct IndexOptions
  204. {
  205. cm::optional<int> Start;
  206. cm::optional<int> End;
  207. cm::optional<int> Stride;
  208. std::vector<int> SpecificTests;
  209. std::string IndexFile;
  210. };
  211. std::string Name;
  212. std::string Label;
  213. cm::optional<IndexOptions> Index;
  214. cm::optional<bool> UseUnion;
  215. };
  216. struct ExcludeOptions
  217. {
  218. struct FixturesOptions
  219. {
  220. std::string Any;
  221. std::string Setup;
  222. std::string Cleanup;
  223. };
  224. std::string Name;
  225. std::string Label;
  226. cm::optional<FixturesOptions> Fixtures;
  227. };
  228. struct FilterOptions
  229. {
  230. cm::optional<IncludeOptions> Include;
  231. cm::optional<ExcludeOptions> Exclude;
  232. };
  233. struct ExecutionOptions
  234. {
  235. enum class ShowOnlyEnum
  236. {
  237. Human,
  238. JsonV1
  239. };
  240. struct RepeatOptions
  241. {
  242. enum class ModeEnum
  243. {
  244. UntilFail,
  245. UntilPass,
  246. AfterTimeout
  247. };
  248. ModeEnum Mode;
  249. int Count;
  250. };
  251. enum class NoTestsActionEnum
  252. {
  253. Default,
  254. Error,
  255. Ignore
  256. };
  257. cm::optional<bool> StopOnFailure;
  258. cm::optional<bool> EnableFailover;
  259. cm::optional<int> Jobs;
  260. std::string ResourceSpecFile;
  261. cm::optional<int> TestLoad;
  262. cm::optional<ShowOnlyEnum> ShowOnly;
  263. cm::optional<RepeatOptions> Repeat;
  264. cm::optional<bool> InteractiveDebugging;
  265. cm::optional<bool> ScheduleRandom;
  266. cm::optional<int> Timeout;
  267. cm::optional<NoTestsActionEnum> NoTestsAction;
  268. };
  269. std::string ConfigurePreset;
  270. cm::optional<bool> InheritConfigureEnvironment;
  271. std::string Configuration;
  272. std::vector<std::string> OverwriteConfigurationFile;
  273. cm::optional<OutputOptions> Output;
  274. cm::optional<FilterOptions> Filter;
  275. cm::optional<ExecutionOptions> Execution;
  276. ReadFileResult VisitPresetInherit(const Preset& parent) override;
  277. ReadFileResult VisitPresetAfterInherit(int /* version */) override;
  278. };
  279. template <class T>
  280. class PresetPair
  281. {
  282. public:
  283. T Unexpanded;
  284. cm::optional<T> Expanded;
  285. };
  286. std::map<std::string, PresetPair<ConfigurePreset>> ConfigurePresets;
  287. std::map<std::string, PresetPair<BuildPreset>> BuildPresets;
  288. std::map<std::string, PresetPair<TestPreset>> TestPresets;
  289. std::vector<std::string> ConfigurePresetOrder;
  290. std::vector<std::string> BuildPresetOrder;
  291. std::vector<std::string> TestPresetOrder;
  292. std::string SourceDir;
  293. std::vector<std::unique_ptr<File>> Files;
  294. int GetVersion(const Preset& preset) const
  295. {
  296. return preset.OriginFile->Version;
  297. }
  298. static std::string GetFilename(const std::string& sourceDir);
  299. static std::string GetUserFilename(const std::string& sourceDir);
  300. ReadFileResult ReadProjectPresets(const std::string& sourceDir,
  301. bool allowNoFiles = false);
  302. static const char* ResultToString(ReadFileResult result);
  303. std::string GetGeneratorForPreset(const std::string& presetName) const
  304. {
  305. auto configurePresetName = presetName;
  306. auto buildPresetIterator = this->BuildPresets.find(presetName);
  307. if (buildPresetIterator != this->BuildPresets.end()) {
  308. configurePresetName =
  309. buildPresetIterator->second.Unexpanded.ConfigurePreset;
  310. } else {
  311. auto testPresetIterator = this->TestPresets.find(presetName);
  312. if (testPresetIterator != this->TestPresets.end()) {
  313. configurePresetName =
  314. testPresetIterator->second.Unexpanded.ConfigurePreset;
  315. }
  316. }
  317. auto configurePresetIterator =
  318. this->ConfigurePresets.find(configurePresetName);
  319. if (configurePresetIterator != this->ConfigurePresets.end()) {
  320. return configurePresetIterator->second.Unexpanded.Generator;
  321. }
  322. // This should only happen if the preset is hidden
  323. // or (for build or test presets) if ConfigurePreset is invalid.
  324. return "";
  325. }
  326. static void PrintPresets(
  327. const std::vector<const cmCMakePresetsGraph::Preset*>& presets);
  328. void PrintConfigurePresetList() const;
  329. void PrintConfigurePresetList(
  330. const std::function<bool(const ConfigurePreset&)>& filter) const;
  331. void PrintBuildPresetList() const;
  332. void PrintTestPresetList() const;
  333. void PrintAllPresets() const;
  334. private:
  335. enum class RootType
  336. {
  337. Project,
  338. User,
  339. };
  340. enum class ReadReason
  341. {
  342. Root,
  343. Included,
  344. };
  345. ReadFileResult ReadProjectPresetsInternal(bool allowNoFiles);
  346. ReadFileResult ReadJSONFile(const std::string& filename, RootType rootType,
  347. ReadReason readReason,
  348. std::vector<File*>& inProgressFiles,
  349. File*& file);
  350. void ClearPresets();
  351. };