cmCMakePresetsGraph.cxx 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCMakePresetsGraph.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cstdlib>
  7. #include <functional>
  8. #include <iostream>
  9. #include <iterator>
  10. #include <utility>
  11. #include <cm/string_view>
  12. #include "cmsys/RegularExpression.hxx"
  13. #include "cmCMakePresetsGraphInternal.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. #define CHECK_EXPAND(out, field, expanders, version) \
  17. do { \
  18. switch (ExpandMacros(field, expanders, version)) { \
  19. case ExpandMacroResult::Error: \
  20. return false; \
  21. case ExpandMacroResult::Ignore: \
  22. out.reset(); \
  23. return true; \
  24. case ExpandMacroResult::Ok: \
  25. break; \
  26. } \
  27. } while (false)
  28. namespace {
  29. enum class CycleStatus
  30. {
  31. Unvisited,
  32. InProgress,
  33. Verified,
  34. };
  35. using ReadFileResult = cmCMakePresetsGraph::ReadFileResult;
  36. using ConfigurePreset = cmCMakePresetsGraph::ConfigurePreset;
  37. using BuildPreset = cmCMakePresetsGraph::BuildPreset;
  38. using TestPreset = cmCMakePresetsGraph::TestPreset;
  39. using ExpandMacroResult = cmCMakePresetsGraphInternal::ExpandMacroResult;
  40. using MacroExpander = cmCMakePresetsGraphInternal::MacroExpander;
  41. void InheritString(std::string& child, const std::string& parent)
  42. {
  43. if (child.empty()) {
  44. child = parent;
  45. }
  46. }
  47. template <typename T>
  48. void InheritOptionalValue(cm::optional<T>& child,
  49. const cm::optional<T>& parent)
  50. {
  51. if (!child) {
  52. child = parent;
  53. }
  54. }
  55. template <typename T>
  56. void InheritVector(std::vector<T>& child, const std::vector<T>& parent)
  57. {
  58. if (child.empty()) {
  59. child = parent;
  60. }
  61. }
  62. /**
  63. * Check preset inheritance for cycles (using a DAG check algorithm) while
  64. * also bubbling up fields through the inheritance hierarchy, then verify
  65. * that each preset has the required fields, either directly or through
  66. * inheritance.
  67. */
  68. template <class T>
  69. ReadFileResult VisitPreset(
  70. T& preset,
  71. std::map<std::string, cmCMakePresetsGraph::PresetPair<T>>& presets,
  72. std::map<std::string, CycleStatus> cycleStatus,
  73. const cmCMakePresetsGraph& graph)
  74. {
  75. switch (cycleStatus[preset.Name]) {
  76. case CycleStatus::InProgress:
  77. return ReadFileResult::CYCLIC_PRESET_INHERITANCE;
  78. case CycleStatus::Verified:
  79. return ReadFileResult::READ_OK;
  80. default:
  81. break;
  82. }
  83. cycleStatus[preset.Name] = CycleStatus::InProgress;
  84. if (preset.Environment.count("") != 0) {
  85. return ReadFileResult::INVALID_PRESET;
  86. }
  87. CHECK_OK(preset.VisitPresetBeforeInherit());
  88. for (auto const& i : preset.Inherits) {
  89. auto parent = presets.find(i);
  90. if (parent == presets.end()) {
  91. return ReadFileResult::INVALID_PRESET;
  92. }
  93. auto& parentPreset = parent->second.Unexpanded;
  94. if (!preset.OriginFile->ReachableFiles.count(parentPreset.OriginFile)) {
  95. return ReadFileResult::PRESET_UNREACHABLE_FROM_FILE;
  96. }
  97. auto result = VisitPreset(parentPreset, presets, cycleStatus, graph);
  98. if (result != ReadFileResult::READ_OK) {
  99. return result;
  100. }
  101. CHECK_OK(preset.VisitPresetInherit(parentPreset));
  102. for (auto const& v : parentPreset.Environment) {
  103. preset.Environment.insert(v);
  104. }
  105. if (!preset.ConditionEvaluator) {
  106. preset.ConditionEvaluator = parentPreset.ConditionEvaluator;
  107. }
  108. }
  109. if (preset.ConditionEvaluator && preset.ConditionEvaluator->IsNull()) {
  110. preset.ConditionEvaluator.reset();
  111. }
  112. CHECK_OK(preset.VisitPresetAfterInherit(graph.GetVersion(preset)));
  113. cycleStatus[preset.Name] = CycleStatus::Verified;
  114. return ReadFileResult::READ_OK;
  115. }
  116. template <class T>
  117. ReadFileResult ComputePresetInheritance(
  118. std::map<std::string, cmCMakePresetsGraph::PresetPair<T>>& presets,
  119. const cmCMakePresetsGraph& graph)
  120. {
  121. std::map<std::string, CycleStatus> cycleStatus;
  122. for (auto const& it : presets) {
  123. cycleStatus[it.first] = CycleStatus::Unvisited;
  124. }
  125. for (auto& it : presets) {
  126. auto& preset = it.second.Unexpanded;
  127. auto result = VisitPreset<T>(preset, presets, cycleStatus, graph);
  128. if (result != ReadFileResult::READ_OK) {
  129. return result;
  130. }
  131. }
  132. return ReadFileResult::READ_OK;
  133. }
  134. constexpr const char* ValidPrefixes[] = {
  135. "",
  136. "env",
  137. "penv",
  138. "vendor",
  139. };
  140. bool PrefixesValidMacroNamespace(const std::string& str)
  141. {
  142. return std::any_of(
  143. std::begin(ValidPrefixes), std::end(ValidPrefixes),
  144. [&str](const char* prefix) -> bool { return cmHasPrefix(prefix, str); });
  145. }
  146. bool IsValidMacroNamespace(const std::string& str)
  147. {
  148. return std::any_of(
  149. std::begin(ValidPrefixes), std::end(ValidPrefixes),
  150. [&str](const char* prefix) -> bool { return str == prefix; });
  151. }
  152. ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
  153. const std::vector<MacroExpander>& macroExpanders,
  154. int version);
  155. ExpandMacroResult ExpandMacros(
  156. std::string& out, const std::vector<MacroExpander>& macroExpanders,
  157. int version);
  158. ExpandMacroResult ExpandMacro(std::string& out,
  159. const std::string& macroNamespace,
  160. const std::string& macroName,
  161. const std::vector<MacroExpander>& macroExpanders,
  162. int version);
  163. bool ExpandMacros(const cmCMakePresetsGraph& graph,
  164. const ConfigurePreset& preset,
  165. cm::optional<ConfigurePreset>& out,
  166. const std::vector<MacroExpander>& macroExpanders)
  167. {
  168. std::string binaryDir = preset.BinaryDir;
  169. CHECK_EXPAND(out, binaryDir, macroExpanders, graph.GetVersion(preset));
  170. if (!binaryDir.empty()) {
  171. if (!cmSystemTools::FileIsFullPath(binaryDir)) {
  172. binaryDir = cmStrCat(graph.SourceDir, '/', binaryDir);
  173. }
  174. out->BinaryDir = cmSystemTools::CollapseFullPath(binaryDir);
  175. cmSystemTools::ConvertToUnixSlashes(out->BinaryDir);
  176. }
  177. if (!preset.InstallDir.empty()) {
  178. std::string installDir = preset.InstallDir;
  179. CHECK_EXPAND(out, installDir, macroExpanders, graph.GetVersion(preset));
  180. if (!cmSystemTools::FileIsFullPath(installDir)) {
  181. installDir = cmStrCat(graph.SourceDir, '/', installDir);
  182. }
  183. out->InstallDir = cmSystemTools::CollapseFullPath(installDir);
  184. cmSystemTools::ConvertToUnixSlashes(out->InstallDir);
  185. }
  186. if (!preset.ToolchainFile.empty()) {
  187. std::string toolchain = preset.ToolchainFile;
  188. CHECK_EXPAND(out, toolchain, macroExpanders, graph.GetVersion(preset));
  189. out->ToolchainFile = toolchain;
  190. }
  191. for (auto& variable : out->CacheVariables) {
  192. if (variable.second) {
  193. CHECK_EXPAND(out, variable.second->Value, macroExpanders,
  194. graph.GetVersion(preset));
  195. }
  196. }
  197. return true;
  198. }
  199. bool ExpandMacros(const cmCMakePresetsGraph& graph, const BuildPreset& preset,
  200. cm::optional<BuildPreset>& out,
  201. const std::vector<MacroExpander>& macroExpanders)
  202. {
  203. for (auto& target : out->Targets) {
  204. CHECK_EXPAND(out, target, macroExpanders, graph.GetVersion(preset));
  205. }
  206. for (auto& nativeToolOption : out->NativeToolOptions) {
  207. CHECK_EXPAND(out, nativeToolOption, macroExpanders,
  208. graph.GetVersion(preset));
  209. }
  210. return true;
  211. }
  212. bool ExpandMacros(const cmCMakePresetsGraph& graph, const TestPreset& preset,
  213. cm::optional<TestPreset>& out,
  214. const std::vector<MacroExpander>& macroExpanders)
  215. {
  216. for (auto& overwrite : out->OverwriteConfigurationFile) {
  217. CHECK_EXPAND(out, overwrite, macroExpanders, graph.GetVersion(preset));
  218. }
  219. if (out->Output) {
  220. CHECK_EXPAND(out, out->Output->OutputLogFile, macroExpanders,
  221. graph.GetVersion(preset));
  222. }
  223. if (out->Filter) {
  224. if (out->Filter->Include) {
  225. CHECK_EXPAND(out, out->Filter->Include->Name, macroExpanders,
  226. graph.GetVersion(preset));
  227. CHECK_EXPAND(out, out->Filter->Include->Label, macroExpanders,
  228. graph.GetVersion(preset));
  229. if (out->Filter->Include->Index) {
  230. CHECK_EXPAND(out, out->Filter->Include->Index->IndexFile,
  231. macroExpanders, graph.GetVersion(preset));
  232. }
  233. }
  234. if (out->Filter->Exclude) {
  235. CHECK_EXPAND(out, out->Filter->Exclude->Name, macroExpanders,
  236. graph.GetVersion(preset));
  237. CHECK_EXPAND(out, out->Filter->Exclude->Label, macroExpanders,
  238. graph.GetVersion(preset));
  239. if (out->Filter->Exclude->Fixtures) {
  240. CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Any, macroExpanders,
  241. graph.GetVersion(preset));
  242. CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Setup,
  243. macroExpanders, graph.GetVersion(preset));
  244. CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Cleanup,
  245. macroExpanders, graph.GetVersion(preset));
  246. }
  247. }
  248. }
  249. if (out->Execution) {
  250. CHECK_EXPAND(out, out->Execution->ResourceSpecFile, macroExpanders,
  251. graph.GetVersion(preset));
  252. }
  253. return true;
  254. }
  255. template <class T>
  256. bool ExpandMacros(const cmCMakePresetsGraph& graph, const T& preset,
  257. cm::optional<T>& out)
  258. {
  259. out.emplace(preset);
  260. std::map<std::string, CycleStatus> envCycles;
  261. for (auto const& v : out->Environment) {
  262. envCycles[v.first] = CycleStatus::Unvisited;
  263. }
  264. std::vector<MacroExpander> macroExpanders;
  265. MacroExpander defaultMacroExpander =
  266. [&graph, &preset](const std::string& macroNamespace,
  267. const std::string& macroName, std::string& macroOut,
  268. int version) -> ExpandMacroResult {
  269. if (macroNamespace.empty()) {
  270. if (macroName == "sourceDir") {
  271. macroOut += graph.SourceDir;
  272. return ExpandMacroResult::Ok;
  273. }
  274. if (macroName == "sourceParentDir") {
  275. macroOut += cmSystemTools::GetParentDirectory(graph.SourceDir);
  276. return ExpandMacroResult::Ok;
  277. }
  278. if (macroName == "sourceDirName") {
  279. macroOut += cmSystemTools::GetFilenameName(graph.SourceDir);
  280. return ExpandMacroResult::Ok;
  281. }
  282. if (macroName == "presetName") {
  283. macroOut += preset.Name;
  284. return ExpandMacroResult::Ok;
  285. }
  286. if (macroName == "generator") {
  287. // Generator only makes sense if preset is not hidden.
  288. if (!preset.Hidden) {
  289. macroOut += graph.GetGeneratorForPreset(preset.Name);
  290. }
  291. return ExpandMacroResult::Ok;
  292. }
  293. if (macroName == "dollar") {
  294. macroOut += '$';
  295. return ExpandMacroResult::Ok;
  296. }
  297. if (macroName == "hostSystemName") {
  298. if (version < 3) {
  299. return ExpandMacroResult::Error;
  300. }
  301. macroOut += cmSystemTools::GetSystemName();
  302. return ExpandMacroResult::Ok;
  303. }
  304. }
  305. return ExpandMacroResult::Ignore;
  306. };
  307. MacroExpander environmentMacroExpander =
  308. [&macroExpanders, &out, &envCycles](
  309. const std::string& macroNamespace, const std::string& macroName,
  310. std::string& result, int version) -> ExpandMacroResult {
  311. if (macroNamespace == "env" && !macroName.empty() && out) {
  312. auto v = out->Environment.find(macroName);
  313. if (v != out->Environment.end() && v->second) {
  314. auto e =
  315. VisitEnv(*v->second, envCycles[macroName], macroExpanders, version);
  316. if (e != ExpandMacroResult::Ok) {
  317. return e;
  318. }
  319. result += *v->second;
  320. return ExpandMacroResult::Ok;
  321. }
  322. }
  323. if (macroNamespace == "env" || macroNamespace == "penv") {
  324. if (macroName.empty()) {
  325. return ExpandMacroResult::Error;
  326. }
  327. const char* value = std::getenv(macroName.c_str());
  328. if (value) {
  329. result += value;
  330. }
  331. return ExpandMacroResult::Ok;
  332. }
  333. return ExpandMacroResult::Ignore;
  334. };
  335. macroExpanders.push_back(defaultMacroExpander);
  336. macroExpanders.push_back(environmentMacroExpander);
  337. for (auto& v : out->Environment) {
  338. if (v.second) {
  339. switch (VisitEnv(*v.second, envCycles[v.first], macroExpanders,
  340. graph.GetVersion(preset))) {
  341. case ExpandMacroResult::Error:
  342. return false;
  343. case ExpandMacroResult::Ignore:
  344. out.reset();
  345. return true;
  346. case ExpandMacroResult::Ok:
  347. break;
  348. }
  349. }
  350. }
  351. if (preset.ConditionEvaluator) {
  352. cm::optional<bool> result;
  353. if (!preset.ConditionEvaluator->Evaluate(
  354. macroExpanders, graph.GetVersion(preset), result)) {
  355. return false;
  356. }
  357. if (!result) {
  358. out.reset();
  359. return true;
  360. }
  361. out->ConditionResult = *result;
  362. }
  363. return ExpandMacros(graph, preset, out, macroExpanders);
  364. }
  365. ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
  366. const std::vector<MacroExpander>& macroExpanders,
  367. int version)
  368. {
  369. if (status == CycleStatus::Verified) {
  370. return ExpandMacroResult::Ok;
  371. }
  372. if (status == CycleStatus::InProgress) {
  373. return ExpandMacroResult::Error;
  374. }
  375. status = CycleStatus::InProgress;
  376. auto e = ExpandMacros(value, macroExpanders, version);
  377. if (e != ExpandMacroResult::Ok) {
  378. return e;
  379. }
  380. status = CycleStatus::Verified;
  381. return ExpandMacroResult::Ok;
  382. }
  383. ExpandMacroResult ExpandMacros(
  384. std::string& out, const std::vector<MacroExpander>& macroExpanders,
  385. int version)
  386. {
  387. std::string result;
  388. std::string macroNamespace;
  389. std::string macroName;
  390. enum class State
  391. {
  392. Default,
  393. MacroNamespace,
  394. MacroName,
  395. } state = State::Default;
  396. for (auto c : out) {
  397. switch (state) {
  398. case State::Default:
  399. if (c == '$') {
  400. state = State::MacroNamespace;
  401. } else {
  402. result += c;
  403. }
  404. break;
  405. case State::MacroNamespace:
  406. if (c == '{') {
  407. if (IsValidMacroNamespace(macroNamespace)) {
  408. state = State::MacroName;
  409. } else {
  410. result += '$';
  411. result += macroNamespace;
  412. result += '{';
  413. macroNamespace.clear();
  414. state = State::Default;
  415. }
  416. } else {
  417. macroNamespace += c;
  418. if (!PrefixesValidMacroNamespace(macroNamespace)) {
  419. result += '$';
  420. result += macroNamespace;
  421. macroNamespace.clear();
  422. state = State::Default;
  423. }
  424. }
  425. break;
  426. case State::MacroName:
  427. if (c == '}') {
  428. auto e = ExpandMacro(result, macroNamespace, macroName,
  429. macroExpanders, version);
  430. if (e != ExpandMacroResult::Ok) {
  431. return e;
  432. }
  433. macroNamespace.clear();
  434. macroName.clear();
  435. state = State::Default;
  436. } else {
  437. macroName += c;
  438. }
  439. break;
  440. }
  441. }
  442. switch (state) {
  443. case State::Default:
  444. break;
  445. case State::MacroNamespace:
  446. result += '$';
  447. result += macroNamespace;
  448. break;
  449. case State::MacroName:
  450. return ExpandMacroResult::Error;
  451. }
  452. out = std::move(result);
  453. return ExpandMacroResult::Ok;
  454. }
  455. ExpandMacroResult ExpandMacro(std::string& out,
  456. const std::string& macroNamespace,
  457. const std::string& macroName,
  458. const std::vector<MacroExpander>& macroExpanders,
  459. int version)
  460. {
  461. for (auto const& macroExpander : macroExpanders) {
  462. auto result = macroExpander(macroNamespace, macroName, out, version);
  463. if (result != ExpandMacroResult::Ignore) {
  464. return result;
  465. }
  466. }
  467. if (macroNamespace == "vendor") {
  468. return ExpandMacroResult::Ignore;
  469. }
  470. return ExpandMacroResult::Error;
  471. }
  472. }
  473. bool cmCMakePresetsGraphInternal::EqualsCondition::Evaluate(
  474. const std::vector<MacroExpander>& expanders, int version,
  475. cm::optional<bool>& out) const
  476. {
  477. std::string lhs = this->Lhs;
  478. CHECK_EXPAND(out, lhs, expanders, version);
  479. std::string rhs = this->Rhs;
  480. CHECK_EXPAND(out, rhs, expanders, version);
  481. out = (lhs == rhs);
  482. return true;
  483. }
  484. bool cmCMakePresetsGraphInternal::InListCondition::Evaluate(
  485. const std::vector<MacroExpander>& expanders, int version,
  486. cm::optional<bool>& out) const
  487. {
  488. std::string str = this->String;
  489. CHECK_EXPAND(out, str, expanders, version);
  490. for (auto item : this->List) {
  491. CHECK_EXPAND(out, item, expanders, version);
  492. if (str == item) {
  493. out = true;
  494. return true;
  495. }
  496. }
  497. out = false;
  498. return true;
  499. }
  500. bool cmCMakePresetsGraphInternal::MatchesCondition::Evaluate(
  501. const std::vector<MacroExpander>& expanders, int version,
  502. cm::optional<bool>& out) const
  503. {
  504. std::string str = this->String;
  505. CHECK_EXPAND(out, str, expanders, version);
  506. std::string regexStr = this->Regex;
  507. CHECK_EXPAND(out, regexStr, expanders, version);
  508. cmsys::RegularExpression regex;
  509. if (!regex.compile(regexStr)) {
  510. return false;
  511. }
  512. out = regex.find(str);
  513. return true;
  514. }
  515. bool cmCMakePresetsGraphInternal::AnyAllOfCondition::Evaluate(
  516. const std::vector<MacroExpander>& expanders, int version,
  517. cm::optional<bool>& out) const
  518. {
  519. for (auto const& condition : this->Conditions) {
  520. cm::optional<bool> result;
  521. if (!condition->Evaluate(expanders, version, result)) {
  522. out.reset();
  523. return false;
  524. }
  525. if (!result) {
  526. out.reset();
  527. return true;
  528. }
  529. if (result == this->StopValue) {
  530. out = result;
  531. return true;
  532. }
  533. }
  534. out = !this->StopValue;
  535. return true;
  536. }
  537. bool cmCMakePresetsGraphInternal::NotCondition::Evaluate(
  538. const std::vector<MacroExpander>& expanders, int version,
  539. cm::optional<bool>& out) const
  540. {
  541. out.reset();
  542. if (!this->SubCondition->Evaluate(expanders, version, out)) {
  543. out.reset();
  544. return false;
  545. }
  546. if (out) {
  547. *out = !*out;
  548. }
  549. return true;
  550. }
  551. cmCMakePresetsGraph::ReadFileResult
  552. cmCMakePresetsGraph::ConfigurePreset::VisitPresetInherit(
  553. const cmCMakePresetsGraph::Preset& parentPreset)
  554. {
  555. auto& preset = *this;
  556. const ConfigurePreset& parent =
  557. static_cast<const ConfigurePreset&>(parentPreset);
  558. InheritString(preset.Generator, parent.Generator);
  559. InheritString(preset.Architecture, parent.Architecture);
  560. InheritString(preset.Toolset, parent.Toolset);
  561. if (!preset.ArchitectureStrategy) {
  562. preset.ArchitectureStrategy = parent.ArchitectureStrategy;
  563. }
  564. if (!preset.ToolsetStrategy) {
  565. preset.ToolsetStrategy = parent.ToolsetStrategy;
  566. }
  567. InheritString(preset.BinaryDir, parent.BinaryDir);
  568. InheritString(preset.InstallDir, parent.InstallDir);
  569. InheritString(preset.ToolchainFile, parent.ToolchainFile);
  570. InheritOptionalValue(preset.WarnDev, parent.WarnDev);
  571. InheritOptionalValue(preset.ErrorDev, parent.ErrorDev);
  572. InheritOptionalValue(preset.WarnDeprecated, parent.WarnDeprecated);
  573. InheritOptionalValue(preset.ErrorDeprecated, parent.ErrorDeprecated);
  574. InheritOptionalValue(preset.WarnUninitialized, parent.WarnUninitialized);
  575. InheritOptionalValue(preset.WarnUnusedCli, parent.WarnUnusedCli);
  576. InheritOptionalValue(preset.WarnSystemVars, parent.WarnSystemVars);
  577. for (auto const& v : parent.CacheVariables) {
  578. preset.CacheVariables.insert(v);
  579. }
  580. return ReadFileResult::READ_OK;
  581. }
  582. cmCMakePresetsGraph::ReadFileResult
  583. cmCMakePresetsGraph::ConfigurePreset::VisitPresetBeforeInherit()
  584. {
  585. auto& preset = *this;
  586. if (preset.Environment.count("") != 0) {
  587. return ReadFileResult::INVALID_PRESET;
  588. }
  589. return ReadFileResult::READ_OK;
  590. }
  591. cmCMakePresetsGraph::ReadFileResult
  592. cmCMakePresetsGraph::ConfigurePreset::VisitPresetAfterInherit(int version)
  593. {
  594. auto& preset = *this;
  595. if (!preset.Hidden) {
  596. if (version < 3) {
  597. if (preset.Generator.empty()) {
  598. return ReadFileResult::INVALID_PRESET;
  599. }
  600. if (preset.BinaryDir.empty()) {
  601. return ReadFileResult::INVALID_PRESET;
  602. }
  603. }
  604. if (preset.WarnDev == false && preset.ErrorDev == true) {
  605. return ReadFileResult::INVALID_PRESET;
  606. }
  607. if (preset.WarnDeprecated == false && preset.ErrorDeprecated == true) {
  608. return ReadFileResult::INVALID_PRESET;
  609. }
  610. if (preset.CacheVariables.count("") != 0) {
  611. return ReadFileResult::INVALID_PRESET;
  612. }
  613. }
  614. return ReadFileResult::READ_OK;
  615. }
  616. cmCMakePresetsGraph::ReadFileResult
  617. cmCMakePresetsGraph::BuildPreset::VisitPresetInherit(
  618. const cmCMakePresetsGraph::Preset& parentPreset)
  619. {
  620. auto& preset = *this;
  621. const BuildPreset& parent = static_cast<const BuildPreset&>(parentPreset);
  622. InheritString(preset.ConfigurePreset, parent.ConfigurePreset);
  623. InheritOptionalValue(preset.InheritConfigureEnvironment,
  624. parent.InheritConfigureEnvironment);
  625. InheritOptionalValue(preset.Jobs, parent.Jobs);
  626. InheritVector(preset.Targets, parent.Targets);
  627. InheritString(preset.Configuration, parent.Configuration);
  628. InheritOptionalValue(preset.CleanFirst, parent.CleanFirst);
  629. InheritOptionalValue(preset.Verbose, parent.Verbose);
  630. InheritVector(preset.NativeToolOptions, parent.NativeToolOptions);
  631. return ReadFileResult::READ_OK;
  632. }
  633. cmCMakePresetsGraph::ReadFileResult
  634. cmCMakePresetsGraph::BuildPreset::VisitPresetAfterInherit(int /* version */)
  635. {
  636. auto& preset = *this;
  637. if (!preset.Hidden && preset.ConfigurePreset.empty()) {
  638. return ReadFileResult::INVALID_PRESET;
  639. }
  640. return ReadFileResult::READ_OK;
  641. }
  642. cmCMakePresetsGraph::ReadFileResult
  643. cmCMakePresetsGraph::TestPreset::VisitPresetInherit(
  644. const cmCMakePresetsGraph::Preset& parentPreset)
  645. {
  646. auto& preset = *this;
  647. const TestPreset& parent = static_cast<const TestPreset&>(parentPreset);
  648. InheritString(preset.ConfigurePreset, parent.ConfigurePreset);
  649. InheritOptionalValue(preset.InheritConfigureEnvironment,
  650. parent.InheritConfigureEnvironment);
  651. InheritString(preset.Configuration, parent.Configuration);
  652. InheritVector(preset.OverwriteConfigurationFile,
  653. parent.OverwriteConfigurationFile);
  654. if (parent.Output) {
  655. if (preset.Output) {
  656. auto& output = preset.Output.value();
  657. const auto& parentOutput = parent.Output.value();
  658. InheritOptionalValue(output.ShortProgress, parentOutput.ShortProgress);
  659. InheritOptionalValue(output.Verbosity, parentOutput.Verbosity);
  660. InheritOptionalValue(output.Debug, parentOutput.Debug);
  661. InheritOptionalValue(output.OutputOnFailure,
  662. parentOutput.OutputOnFailure);
  663. InheritOptionalValue(output.Quiet, parentOutput.Quiet);
  664. InheritString(output.OutputLogFile, parentOutput.OutputLogFile);
  665. InheritOptionalValue(output.LabelSummary, parentOutput.LabelSummary);
  666. InheritOptionalValue(output.SubprojectSummary,
  667. parentOutput.SubprojectSummary);
  668. InheritOptionalValue(output.MaxPassedTestOutputSize,
  669. parentOutput.MaxPassedTestOutputSize);
  670. InheritOptionalValue(output.MaxFailedTestOutputSize,
  671. parentOutput.MaxFailedTestOutputSize);
  672. InheritOptionalValue(output.MaxTestNameWidth,
  673. parentOutput.MaxTestNameWidth);
  674. } else {
  675. preset.Output = parent.Output;
  676. }
  677. }
  678. if (parent.Filter) {
  679. if (parent.Filter->Include) {
  680. if (preset.Filter && preset.Filter->Include) {
  681. auto& include = *preset.Filter->Include;
  682. const auto& parentInclude = *parent.Filter->Include;
  683. InheritString(include.Name, parentInclude.Name);
  684. InheritString(include.Label, parentInclude.Label);
  685. InheritOptionalValue(include.Index, parentInclude.Index);
  686. } else {
  687. if (!preset.Filter) {
  688. preset.Filter.emplace();
  689. }
  690. preset.Filter->Include = parent.Filter->Include;
  691. }
  692. }
  693. if (parent.Filter->Exclude) {
  694. if (preset.Filter && preset.Filter->Exclude) {
  695. auto& exclude = *preset.Filter->Exclude;
  696. const auto& parentExclude = *parent.Filter->Exclude;
  697. InheritString(exclude.Name, parentExclude.Name);
  698. InheritString(exclude.Label, parentExclude.Label);
  699. InheritOptionalValue(exclude.Fixtures, parentExclude.Fixtures);
  700. } else {
  701. if (!preset.Filter) {
  702. preset.Filter.emplace();
  703. }
  704. preset.Filter->Exclude = parent.Filter->Exclude;
  705. }
  706. }
  707. }
  708. if (parent.Execution) {
  709. if (preset.Execution) {
  710. auto& execution = *preset.Execution;
  711. const auto& parentExecution = *parent.Execution;
  712. InheritOptionalValue(execution.StopOnFailure,
  713. parentExecution.StopOnFailure);
  714. InheritOptionalValue(execution.EnableFailover,
  715. parentExecution.EnableFailover);
  716. InheritOptionalValue(execution.Jobs, parentExecution.Jobs);
  717. InheritString(execution.ResourceSpecFile,
  718. parentExecution.ResourceSpecFile);
  719. InheritOptionalValue(execution.TestLoad, parentExecution.TestLoad);
  720. InheritOptionalValue(execution.ShowOnly, parentExecution.ShowOnly);
  721. InheritOptionalValue(execution.Repeat, parentExecution.Repeat);
  722. InheritOptionalValue(execution.InteractiveDebugging,
  723. parentExecution.InteractiveDebugging);
  724. InheritOptionalValue(execution.ScheduleRandom,
  725. parentExecution.ScheduleRandom);
  726. InheritOptionalValue(execution.Timeout, parentExecution.Timeout);
  727. InheritOptionalValue(execution.NoTestsAction,
  728. parentExecution.NoTestsAction);
  729. } else {
  730. preset.Execution = parent.Execution;
  731. }
  732. }
  733. return ReadFileResult::READ_OK;
  734. }
  735. cmCMakePresetsGraph::ReadFileResult
  736. cmCMakePresetsGraph::TestPreset::VisitPresetAfterInherit(int /* version */)
  737. {
  738. auto& preset = *this;
  739. if (!preset.Hidden && preset.ConfigurePreset.empty()) {
  740. return ReadFileResult::INVALID_PRESET;
  741. }
  742. return ReadFileResult::READ_OK;
  743. }
  744. std::string cmCMakePresetsGraph::GetFilename(const std::string& sourceDir)
  745. {
  746. return cmStrCat(sourceDir, "/CMakePresets.json");
  747. }
  748. std::string cmCMakePresetsGraph::GetUserFilename(const std::string& sourceDir)
  749. {
  750. return cmStrCat(sourceDir, "/CMakeUserPresets.json");
  751. }
  752. cmCMakePresetsGraph::ReadFileResult cmCMakePresetsGraph::ReadProjectPresets(
  753. const std::string& sourceDir, bool allowNoFiles)
  754. {
  755. this->SourceDir = sourceDir;
  756. this->ClearPresets();
  757. auto result = this->ReadProjectPresetsInternal(allowNoFiles);
  758. if (result != ReadFileResult::READ_OK) {
  759. this->ClearPresets();
  760. }
  761. return result;
  762. }
  763. cmCMakePresetsGraph::ReadFileResult
  764. cmCMakePresetsGraph::ReadProjectPresetsInternal(bool allowNoFiles)
  765. {
  766. bool haveOneFile = false;
  767. File* file;
  768. std::string filename = GetUserFilename(this->SourceDir);
  769. std::vector<File*> inProgressFiles;
  770. if (cmSystemTools::FileExists(filename)) {
  771. auto result = this->ReadJSONFile(filename, RootType::User,
  772. ReadReason::Root, inProgressFiles, file);
  773. if (result != ReadFileResult::READ_OK) {
  774. return result;
  775. }
  776. haveOneFile = true;
  777. } else {
  778. filename = GetFilename(this->SourceDir);
  779. if (cmSystemTools::FileExists(filename)) {
  780. auto result = this->ReadJSONFile(
  781. filename, RootType::Project, ReadReason::Root, inProgressFiles, file);
  782. if (result != ReadFileResult::READ_OK) {
  783. return result;
  784. }
  785. haveOneFile = true;
  786. }
  787. }
  788. assert(inProgressFiles.empty());
  789. if (!haveOneFile) {
  790. return allowNoFiles ? ReadFileResult::READ_OK
  791. : ReadFileResult::FILE_NOT_FOUND;
  792. }
  793. CHECK_OK(ComputePresetInheritance(this->ConfigurePresets, *this));
  794. CHECK_OK(ComputePresetInheritance(this->BuildPresets, *this));
  795. CHECK_OK(ComputePresetInheritance(this->TestPresets, *this));
  796. for (auto& it : this->ConfigurePresets) {
  797. if (!ExpandMacros(*this, it.second.Unexpanded, it.second.Expanded)) {
  798. return ReadFileResult::INVALID_MACRO_EXPANSION;
  799. }
  800. }
  801. for (auto& it : this->BuildPresets) {
  802. if (!it.second.Unexpanded.Hidden) {
  803. const auto configurePreset =
  804. this->ConfigurePresets.find(it.second.Unexpanded.ConfigurePreset);
  805. if (configurePreset == this->ConfigurePresets.end()) {
  806. return ReadFileResult::INVALID_CONFIGURE_PRESET;
  807. }
  808. if (it.second.Unexpanded.InheritConfigureEnvironment.value_or(true)) {
  809. it.second.Unexpanded.Environment.insert(
  810. configurePreset->second.Unexpanded.Environment.begin(),
  811. configurePreset->second.Unexpanded.Environment.end());
  812. }
  813. }
  814. if (!ExpandMacros(*this, it.second.Unexpanded, it.second.Expanded)) {
  815. return ReadFileResult::INVALID_MACRO_EXPANSION;
  816. }
  817. }
  818. for (auto& it : this->TestPresets) {
  819. if (!it.second.Unexpanded.Hidden) {
  820. const auto configurePreset =
  821. this->ConfigurePresets.find(it.second.Unexpanded.ConfigurePreset);
  822. if (configurePreset == this->ConfigurePresets.end()) {
  823. return ReadFileResult::INVALID_CONFIGURE_PRESET;
  824. }
  825. if (it.second.Unexpanded.InheritConfigureEnvironment.value_or(true)) {
  826. it.second.Unexpanded.Environment.insert(
  827. configurePreset->second.Unexpanded.Environment.begin(),
  828. configurePreset->second.Unexpanded.Environment.end());
  829. }
  830. }
  831. if (!ExpandMacros(*this, it.second.Unexpanded, it.second.Expanded)) {
  832. return ReadFileResult::INVALID_MACRO_EXPANSION;
  833. }
  834. }
  835. return ReadFileResult::READ_OK;
  836. }
  837. const char* cmCMakePresetsGraph::ResultToString(ReadFileResult result)
  838. {
  839. switch (result) {
  840. case ReadFileResult::READ_OK:
  841. return "OK";
  842. case ReadFileResult::FILE_NOT_FOUND:
  843. return "File not found";
  844. case ReadFileResult::JSON_PARSE_ERROR:
  845. return "JSON parse error";
  846. case ReadFileResult::INVALID_ROOT:
  847. return "Invalid root object";
  848. case ReadFileResult::NO_VERSION:
  849. return "No \"version\" field";
  850. case ReadFileResult::INVALID_VERSION:
  851. return "Invalid \"version\" field";
  852. case ReadFileResult::UNRECOGNIZED_VERSION:
  853. return "Unrecognized \"version\" field";
  854. case ReadFileResult::INVALID_CMAKE_VERSION:
  855. return "Invalid \"cmakeMinimumRequired\" field";
  856. case ReadFileResult::UNRECOGNIZED_CMAKE_VERSION:
  857. return "\"cmakeMinimumRequired\" version too new";
  858. case ReadFileResult::INVALID_PRESETS:
  859. return "Invalid \"configurePresets\" field";
  860. case ReadFileResult::INVALID_PRESET:
  861. return "Invalid preset";
  862. case ReadFileResult::INVALID_VARIABLE:
  863. return "Invalid CMake variable definition";
  864. case ReadFileResult::DUPLICATE_PRESETS:
  865. return "Duplicate presets";
  866. case ReadFileResult::CYCLIC_PRESET_INHERITANCE:
  867. return "Cyclic preset inheritance";
  868. case ReadFileResult::PRESET_UNREACHABLE_FROM_FILE:
  869. return "Inherited preset is unreachable from preset's file";
  870. case ReadFileResult::INVALID_MACRO_EXPANSION:
  871. return "Invalid macro expansion";
  872. case ReadFileResult::BUILD_TEST_PRESETS_UNSUPPORTED:
  873. return "File version must be 2 or higher for build and test preset "
  874. "support.";
  875. case ReadFileResult::INCLUDE_UNSUPPORTED:
  876. return "File version must be 4 or higher for include support";
  877. case ReadFileResult::INVALID_INCLUDE:
  878. return "Invalid \"include\" field";
  879. case ReadFileResult::INVALID_CONFIGURE_PRESET:
  880. return "Invalid \"configurePreset\" field";
  881. case ReadFileResult::INSTALL_PREFIX_UNSUPPORTED:
  882. return "File version must be 3 or higher for installDir preset "
  883. "support.";
  884. case ReadFileResult::INVALID_CONDITION:
  885. return "Invalid preset condition";
  886. case ReadFileResult::CONDITION_UNSUPPORTED:
  887. return "File version must be 3 or higher for condition support";
  888. case ReadFileResult::TOOLCHAIN_FILE_UNSUPPORTED:
  889. return "File version must be 3 or higher for toolchainFile preset "
  890. "support.";
  891. case ReadFileResult::CYCLIC_INCLUDE:
  892. return "Cyclic include among preset files";
  893. case ReadFileResult::INCLUDE_OUTSIDE_PROJECT:
  894. return "File included from outside project directory";
  895. }
  896. return "Unknown error";
  897. }
  898. void cmCMakePresetsGraph::ClearPresets()
  899. {
  900. this->ConfigurePresets.clear();
  901. this->BuildPresets.clear();
  902. this->TestPresets.clear();
  903. this->ConfigurePresetOrder.clear();
  904. this->BuildPresetOrder.clear();
  905. this->TestPresetOrder.clear();
  906. this->Files.clear();
  907. }
  908. void cmCMakePresetsGraph::PrintPresets(
  909. const std::vector<const cmCMakePresetsGraph::Preset*>& presets)
  910. {
  911. if (presets.empty()) {
  912. return;
  913. }
  914. auto longestPresetName =
  915. std::max_element(presets.begin(), presets.end(),
  916. [](const cmCMakePresetsGraph::Preset* a,
  917. const cmCMakePresetsGraph::Preset* b) {
  918. return a->Name.length() < b->Name.length();
  919. });
  920. auto longestLength = (*longestPresetName)->Name.length();
  921. for (const auto* preset : presets) {
  922. std::cout << " \"" << preset->Name << '"';
  923. const auto& description = preset->DisplayName;
  924. if (!description.empty()) {
  925. for (std::size_t i = 0; i < longestLength - preset->Name.length(); ++i) {
  926. std::cout << ' ';
  927. }
  928. std::cout << " - " << description;
  929. }
  930. std::cout << '\n';
  931. }
  932. }
  933. void cmCMakePresetsGraph::PrintConfigurePresetList() const
  934. {
  935. PrintConfigurePresetList([](const ConfigurePreset&) { return true; });
  936. }
  937. void cmCMakePresetsGraph::PrintConfigurePresetList(
  938. const std::function<bool(const ConfigurePreset&)>& filter) const
  939. {
  940. std::vector<const cmCMakePresetsGraph::Preset*> presets;
  941. for (auto const& p : this->ConfigurePresetOrder) {
  942. auto const& preset = this->ConfigurePresets.at(p);
  943. if (!preset.Unexpanded.Hidden && preset.Expanded &&
  944. preset.Expanded->ConditionResult && filter(preset.Unexpanded)) {
  945. presets.push_back(
  946. static_cast<const cmCMakePresetsGraph::Preset*>(&preset.Unexpanded));
  947. }
  948. }
  949. if (!presets.empty()) {
  950. std::cout << "Available configure presets:\n\n";
  951. cmCMakePresetsGraph::PrintPresets(presets);
  952. }
  953. }
  954. void cmCMakePresetsGraph::PrintBuildPresetList() const
  955. {
  956. std::vector<const cmCMakePresetsGraph::Preset*> presets;
  957. for (auto const& p : this->BuildPresetOrder) {
  958. auto const& preset = this->BuildPresets.at(p);
  959. if (!preset.Unexpanded.Hidden && preset.Expanded &&
  960. preset.Expanded->ConditionResult) {
  961. presets.push_back(
  962. static_cast<const cmCMakePresetsGraph::Preset*>(&preset.Unexpanded));
  963. }
  964. }
  965. if (!presets.empty()) {
  966. std::cout << "Available build presets:\n\n";
  967. cmCMakePresetsGraph::PrintPresets(presets);
  968. }
  969. }
  970. void cmCMakePresetsGraph::PrintTestPresetList() const
  971. {
  972. std::vector<const cmCMakePresetsGraph::Preset*> presets;
  973. for (auto const& p : this->TestPresetOrder) {
  974. auto const& preset = this->TestPresets.at(p);
  975. if (!preset.Unexpanded.Hidden && preset.Expanded &&
  976. preset.Expanded->ConditionResult) {
  977. presets.push_back(
  978. static_cast<const cmCMakePresetsGraph::Preset*>(&preset.Unexpanded));
  979. }
  980. }
  981. if (!presets.empty()) {
  982. std::cout << "Available test presets:\n\n";
  983. cmCMakePresetsGraph::PrintPresets(presets);
  984. }
  985. }
  986. void cmCMakePresetsGraph::PrintAllPresets() const
  987. {
  988. this->PrintConfigurePresetList();
  989. std::cout << std::endl;
  990. this->PrintBuildPresetList();
  991. std::cout << std::endl;
  992. this->PrintTestPresetList();
  993. }