cmCMakePresetsFile.h 10.0 KB

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