cmCMakePresetsFile.h 11 KB

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