cmCMakePresetsFile.h 10 KB

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