cmCMakePresetsFile.h 10.0 KB

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