cmCMakePresetsFile.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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 "cmCMakePresetsFile.h"
  4. #include <cstdlib>
  5. #include <functional>
  6. #include <utility>
  7. #include <cmext/string_view>
  8. #include <cm3p/json/reader.h>
  9. #include <cm3p/json/value.h>
  10. #include "cmsys/FStream.hxx"
  11. #include "cmJSONHelpers.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. #include "cmVersion.h"
  15. namespace {
  16. enum class CycleStatus
  17. {
  18. Unvisited,
  19. InProgress,
  20. Verified,
  21. };
  22. using ReadFileResult = cmCMakePresetsFile::ReadFileResult;
  23. using CacheVariable = cmCMakePresetsFile::CacheVariable;
  24. using UnexpandedPreset = cmCMakePresetsFile::UnexpandedPreset;
  25. using CMakeGeneratorConfig = cmCMakePresetsFile::CMakeGeneratorConfig;
  26. constexpr int MIN_VERSION = 1;
  27. constexpr int MAX_VERSION = 1;
  28. struct CMakeVersion
  29. {
  30. unsigned int Major = 0;
  31. unsigned int Minor = 0;
  32. unsigned int Patch = 0;
  33. };
  34. struct RootPresets
  35. {
  36. CMakeVersion CMakeMinimumRequired;
  37. std::vector<cmCMakePresetsFile::UnexpandedPreset> Presets;
  38. };
  39. cmJSONHelper<std::nullptr_t, ReadFileResult> VendorHelper(ReadFileResult error)
  40. {
  41. return [error](std::nullptr_t& /*out*/,
  42. const Json::Value* value) -> ReadFileResult {
  43. if (!value) {
  44. return ReadFileResult::READ_OK;
  45. }
  46. if (!value->isObject()) {
  47. return error;
  48. }
  49. return ReadFileResult::READ_OK;
  50. };
  51. }
  52. auto const VersionIntHelper = cmJSONIntHelper<ReadFileResult>(
  53. ReadFileResult::READ_OK, ReadFileResult::INVALID_VERSION);
  54. auto const VersionHelper = cmJSONRequiredHelper<int, ReadFileResult>(
  55. ReadFileResult::NO_VERSION, VersionIntHelper);
  56. auto const RootVersionHelper =
  57. cmJSONObjectHelper<int, ReadFileResult>(ReadFileResult::READ_OK,
  58. ReadFileResult::INVALID_ROOT)
  59. .Bind("version"_s, VersionHelper, false);
  60. auto const VariableStringHelper = cmJSONStringHelper<ReadFileResult>(
  61. ReadFileResult::READ_OK, ReadFileResult::INVALID_VARIABLE);
  62. ReadFileResult VariableValueHelper(std::string& out, const Json::Value* value)
  63. {
  64. if (!value) {
  65. out.clear();
  66. return ReadFileResult::READ_OK;
  67. }
  68. if (value->isBool()) {
  69. out = value->asBool() ? "TRUE" : "FALSE";
  70. return ReadFileResult::READ_OK;
  71. }
  72. return VariableStringHelper(out, value);
  73. }
  74. auto const VariableObjectHelper =
  75. cmJSONObjectHelper<CacheVariable, ReadFileResult>(
  76. ReadFileResult::READ_OK, ReadFileResult::INVALID_VARIABLE, false)
  77. .Bind("type"_s, &CacheVariable::Type, VariableStringHelper, false)
  78. .Bind("value"_s, &CacheVariable::Value, VariableValueHelper);
  79. ReadFileResult VariableHelper(cm::optional<CacheVariable>& out,
  80. const Json::Value* value)
  81. {
  82. if (value->isBool()) {
  83. out = CacheVariable{
  84. /*Type=*/"BOOL",
  85. /*Value=*/value->asBool() ? "TRUE" : "FALSE",
  86. };
  87. return ReadFileResult::READ_OK;
  88. }
  89. if (value->isString()) {
  90. out = CacheVariable{
  91. /*Type=*/"",
  92. /*Value=*/value->asString(),
  93. };
  94. return ReadFileResult::READ_OK;
  95. }
  96. if (value->isObject()) {
  97. out.emplace();
  98. return VariableObjectHelper(*out, value);
  99. }
  100. if (value->isNull()) {
  101. out = cm::nullopt;
  102. return ReadFileResult::READ_OK;
  103. }
  104. return ReadFileResult::INVALID_VARIABLE;
  105. }
  106. auto const VariablesHelper =
  107. cmJSONMapHelper<cm::optional<CacheVariable>, ReadFileResult>(
  108. ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET, VariableHelper);
  109. auto const PresetStringHelper = cmJSONStringHelper<ReadFileResult>(
  110. ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET);
  111. ReadFileResult EnvironmentHelper(cm::optional<std::string>& out,
  112. const Json::Value* value)
  113. {
  114. if (!value || value->isNull()) {
  115. out = cm::nullopt;
  116. return ReadFileResult::READ_OK;
  117. }
  118. if (value->isString()) {
  119. out = value->asString();
  120. return ReadFileResult::READ_OK;
  121. }
  122. return ReadFileResult::INVALID_PRESET;
  123. }
  124. auto const EnvironmentMapHelper =
  125. cmJSONMapHelper<cm::optional<std::string>, ReadFileResult>(
  126. ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET,
  127. EnvironmentHelper);
  128. auto const PresetVectorStringHelper =
  129. cmJSONVectorHelper<std::string, ReadFileResult>(
  130. ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET,
  131. PresetStringHelper);
  132. ReadFileResult PresetInheritsHelper(std::vector<std::string>& out,
  133. const Json::Value* value)
  134. {
  135. out.clear();
  136. if (!value) {
  137. return ReadFileResult::READ_OK;
  138. }
  139. if (value->isString()) {
  140. out.push_back(value->asString());
  141. return ReadFileResult::READ_OK;
  142. }
  143. return PresetVectorStringHelper(out, value);
  144. }
  145. auto const PresetBoolHelper = cmJSONBoolHelper<ReadFileResult>(
  146. ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET);
  147. auto const PresetOptionalBoolHelper =
  148. cmJSONOptionalHelper<bool, ReadFileResult>(ReadFileResult::READ_OK,
  149. PresetBoolHelper);
  150. auto const PresetWarningsHelper =
  151. cmJSONObjectHelper<UnexpandedPreset, ReadFileResult>(
  152. ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET, false)
  153. .Bind("dev"_s, &UnexpandedPreset::WarnDev, PresetOptionalBoolHelper, false)
  154. .Bind("deprecated"_s, &UnexpandedPreset::WarnDeprecated,
  155. PresetOptionalBoolHelper, false)
  156. .Bind("uninitialized"_s, &UnexpandedPreset::WarnUninitialized,
  157. PresetOptionalBoolHelper, false)
  158. .Bind("unusedCli"_s, &UnexpandedPreset::WarnUnusedCli,
  159. PresetOptionalBoolHelper, false)
  160. .Bind("systemVars"_s, &UnexpandedPreset::WarnSystemVars,
  161. PresetOptionalBoolHelper, false);
  162. auto const PresetErrorsHelper =
  163. cmJSONObjectHelper<UnexpandedPreset, ReadFileResult>(
  164. ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET, false)
  165. .Bind("dev"_s, &UnexpandedPreset::ErrorDev, PresetOptionalBoolHelper,
  166. false)
  167. .Bind("deprecated"_s, &UnexpandedPreset::ErrorDeprecated,
  168. PresetOptionalBoolHelper, false);
  169. auto const PresetDebugHelper =
  170. cmJSONObjectHelper<UnexpandedPreset, ReadFileResult>(
  171. ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET, false)
  172. .Bind("output"_s, &UnexpandedPreset::DebugOutput, PresetOptionalBoolHelper,
  173. false)
  174. .Bind("tryCompile"_s, &UnexpandedPreset::DebugTryCompile,
  175. PresetOptionalBoolHelper, false)
  176. .Bind("find"_s, &UnexpandedPreset::DebugFind, PresetOptionalBoolHelper,
  177. false);
  178. ReadFileResult CMakeGeneratorConfigHelper(
  179. cm::optional<CMakeGeneratorConfig>& out, const Json::Value* value)
  180. {
  181. if (!value) {
  182. out = cm::nullopt;
  183. return ReadFileResult::READ_OK;
  184. }
  185. if (!value->isString()) {
  186. return ReadFileResult::INVALID_PRESET;
  187. }
  188. if (value->asString() == "default") {
  189. out = CMakeGeneratorConfig::Default;
  190. return ReadFileResult::READ_OK;
  191. }
  192. if (value->asString() == "ignore") {
  193. out = CMakeGeneratorConfig::Ignore;
  194. return ReadFileResult::READ_OK;
  195. }
  196. return ReadFileResult::INVALID_PRESET;
  197. }
  198. auto const PresetHelper =
  199. cmJSONObjectHelper<UnexpandedPreset, ReadFileResult>(
  200. ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET, false)
  201. .Bind("name"_s, &UnexpandedPreset::Name, PresetStringHelper)
  202. .Bind("inherits"_s, &UnexpandedPreset::Inherits, PresetInheritsHelper,
  203. false)
  204. .Bind("hidden"_s, &UnexpandedPreset::Hidden, PresetBoolHelper, false)
  205. .Bind<std::nullptr_t>("vendor"_s, nullptr,
  206. VendorHelper(ReadFileResult::INVALID_PRESET), false)
  207. .Bind("displayName"_s, &UnexpandedPreset::DisplayName, PresetStringHelper,
  208. false)
  209. .Bind("description"_s, &UnexpandedPreset::Description, PresetStringHelper,
  210. false)
  211. .Bind("generator"_s, &UnexpandedPreset::Generator, PresetStringHelper,
  212. false)
  213. .Bind("architecture"_s, &UnexpandedPreset::Architecture,
  214. PresetStringHelper, false)
  215. .Bind("toolset"_s, &UnexpandedPreset::Toolset, PresetStringHelper, false)
  216. .Bind("cmakeGeneratorConfig"_s, &UnexpandedPreset::GeneratorConfig,
  217. CMakeGeneratorConfigHelper, false)
  218. .Bind("binaryDir"_s, &UnexpandedPreset::BinaryDir, PresetStringHelper,
  219. false)
  220. .Bind<std::string>("cmakeExecutable"_s, nullptr, PresetStringHelper, false)
  221. .Bind("cacheVariables"_s, &UnexpandedPreset::CacheVariables,
  222. VariablesHelper, false)
  223. .Bind("environment"_s, &UnexpandedPreset::Environment,
  224. EnvironmentMapHelper, false)
  225. .Bind("warnings"_s, PresetWarningsHelper, false)
  226. .Bind("errors"_s, PresetErrorsHelper, false)
  227. .Bind("debug"_s, PresetDebugHelper, false);
  228. auto const PresetsHelper =
  229. cmJSONVectorHelper<UnexpandedPreset, ReadFileResult>(
  230. ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESETS, PresetHelper);
  231. auto const CMakeVersionUIntHelper = cmJSONUIntHelper<ReadFileResult>(
  232. ReadFileResult::READ_OK, ReadFileResult::INVALID_VERSION);
  233. auto const CMakeVersionHelper =
  234. cmJSONObjectHelper<CMakeVersion, ReadFileResult>(
  235. ReadFileResult::READ_OK, ReadFileResult::INVALID_CMAKE_VERSION, false)
  236. .Bind("major"_s, &CMakeVersion::Major, CMakeVersionUIntHelper, false)
  237. .Bind("minor"_s, &CMakeVersion::Minor, CMakeVersionUIntHelper, false)
  238. .Bind("patch"_s, &CMakeVersion::Patch, CMakeVersionUIntHelper, false);
  239. auto const RootPresetsHelper =
  240. cmJSONObjectHelper<RootPresets, ReadFileResult>(
  241. ReadFileResult::READ_OK, ReadFileResult::INVALID_ROOT, false)
  242. .Bind<int>("version"_s, nullptr, VersionHelper)
  243. .Bind("configurePresets"_s, &RootPresets::Presets, PresetsHelper, false)
  244. .Bind("cmakeMinimumRequired"_s, &RootPresets::CMakeMinimumRequired,
  245. CMakeVersionHelper, false)
  246. .Bind<std::nullptr_t>("vendor"_s, nullptr,
  247. VendorHelper(ReadFileResult::INVALID_ROOT), false);
  248. void InheritString(std::string& child, const std::string& parent)
  249. {
  250. if (child.empty()) {
  251. child = parent;
  252. }
  253. }
  254. void InheritOptionalBool(cm::optional<bool>& child,
  255. const cm::optional<bool>& parent)
  256. {
  257. if (!child) {
  258. child = parent;
  259. }
  260. }
  261. /**
  262. * Check preset inheritance for cycles (using a DAG check algorithm) while
  263. * also bubbling up fields through the inheritance hierarchy, then verify
  264. * that each preset has the required fields, either directly or through
  265. * inheritance.
  266. */
  267. ReadFileResult VisitPreset(std::map<std::string, UnexpandedPreset>& presets,
  268. UnexpandedPreset& preset,
  269. std::map<std::string, CycleStatus> cycleStatus)
  270. {
  271. switch (cycleStatus[preset.Name]) {
  272. case CycleStatus::InProgress:
  273. return ReadFileResult::CYCLIC_PRESET_INHERITANCE;
  274. case CycleStatus::Verified:
  275. return ReadFileResult::READ_OK;
  276. default:
  277. break;
  278. }
  279. cycleStatus[preset.Name] = CycleStatus::InProgress;
  280. if (preset.CacheVariables.count("") != 0) {
  281. return ReadFileResult::INVALID_PRESET;
  282. }
  283. if (preset.Environment.count("") != 0) {
  284. return ReadFileResult::INVALID_PRESET;
  285. }
  286. for (auto const& i : preset.Inherits) {
  287. auto parent = presets.find(i);
  288. if (parent == presets.end()) {
  289. return ReadFileResult::INVALID_PRESET;
  290. }
  291. if (!preset.User && parent->second.User) {
  292. return ReadFileResult::USER_PRESET_INHERITANCE;
  293. }
  294. auto result = VisitPreset(presets, parent->second, cycleStatus);
  295. if (result != ReadFileResult::READ_OK) {
  296. return result;
  297. }
  298. InheritString(preset.Generator, parent->second.Generator);
  299. InheritString(preset.Architecture, parent->second.Architecture);
  300. InheritString(preset.Toolset, parent->second.Toolset);
  301. if (!preset.GeneratorConfig) {
  302. preset.GeneratorConfig = parent->second.GeneratorConfig;
  303. }
  304. InheritString(preset.BinaryDir, parent->second.BinaryDir);
  305. InheritOptionalBool(preset.WarnDev, parent->second.WarnDev);
  306. InheritOptionalBool(preset.ErrorDev, parent->second.ErrorDev);
  307. InheritOptionalBool(preset.WarnDeprecated, parent->second.WarnDeprecated);
  308. InheritOptionalBool(preset.ErrorDeprecated,
  309. parent->second.ErrorDeprecated);
  310. InheritOptionalBool(preset.WarnUninitialized,
  311. parent->second.WarnUninitialized);
  312. InheritOptionalBool(preset.WarnUnusedCli, parent->second.WarnUnusedCli);
  313. InheritOptionalBool(preset.WarnSystemVars, parent->second.WarnSystemVars);
  314. for (auto const& v : parent->second.CacheVariables) {
  315. preset.CacheVariables.insert(v);
  316. }
  317. for (auto const& v : parent->second.Environment) {
  318. preset.Environment.insert(v);
  319. }
  320. }
  321. if (!preset.Hidden) {
  322. if (preset.Generator.empty()) {
  323. return ReadFileResult::INVALID_PRESET;
  324. }
  325. if (preset.BinaryDir.empty()) {
  326. return ReadFileResult::INVALID_PRESET;
  327. }
  328. if (preset.WarnDev == false && preset.ErrorDev == true) {
  329. return ReadFileResult::INVALID_PRESET;
  330. }
  331. if (preset.WarnDeprecated == false && preset.ErrorDeprecated == true) {
  332. return ReadFileResult::INVALID_PRESET;
  333. }
  334. }
  335. cycleStatus[preset.Name] = CycleStatus::Verified;
  336. return ReadFileResult::READ_OK;
  337. }
  338. ReadFileResult ComputePresetInheritance(
  339. std::map<std::string, UnexpandedPreset>& presets)
  340. {
  341. std::map<std::string, CycleStatus> cycleStatus;
  342. for (auto const& it : presets) {
  343. cycleStatus[it.first] = CycleStatus::Unvisited;
  344. }
  345. for (auto& it : presets) {
  346. auto result = VisitPreset(presets, it.second, cycleStatus);
  347. if (result != ReadFileResult::READ_OK) {
  348. return result;
  349. }
  350. }
  351. return ReadFileResult::READ_OK;
  352. }
  353. constexpr const char* ValidPrefixes[] = {
  354. "",
  355. "env",
  356. "penv",
  357. "vendor",
  358. };
  359. bool PrefixesValidMacroNamespace(const std::string& str)
  360. {
  361. for (auto const& prefix : ValidPrefixes) {
  362. if (cmHasPrefix(prefix, str)) {
  363. return true;
  364. }
  365. }
  366. return false;
  367. }
  368. bool IsValidMacroNamespace(const std::string& str)
  369. {
  370. for (auto const& prefix : ValidPrefixes) {
  371. if (str == prefix) {
  372. return true;
  373. }
  374. }
  375. return false;
  376. }
  377. bool VisitEnv(const cmCMakePresetsFile& file,
  378. cmCMakePresetsFile::ExpandedPreset& preset,
  379. std::map<std::string, CycleStatus>& envCycles,
  380. std::string& value, CycleStatus& status);
  381. bool ExpandMacros(const cmCMakePresetsFile& file,
  382. cmCMakePresetsFile::ExpandedPreset& preset,
  383. std::map<std::string, CycleStatus>& envCycles,
  384. std::string& out);
  385. bool ExpandMacro(const cmCMakePresetsFile& file,
  386. cmCMakePresetsFile::ExpandedPreset& preset,
  387. std::map<std::string, CycleStatus>& envCycles,
  388. std::string& out, const std::string& macroNamespace,
  389. const std::string& macroName);
  390. bool VisitEnv(const cmCMakePresetsFile& file,
  391. cmCMakePresetsFile::ExpandedPreset& preset,
  392. std::map<std::string, CycleStatus>& envCycles,
  393. std::string& value, CycleStatus& status)
  394. {
  395. if (status == CycleStatus::Verified) {
  396. return true;
  397. }
  398. if (status == CycleStatus::InProgress) {
  399. return false;
  400. }
  401. status = CycleStatus::InProgress;
  402. if (!ExpandMacros(file, preset, envCycles, value)) {
  403. return false;
  404. }
  405. status = CycleStatus::Verified;
  406. return true;
  407. }
  408. bool ExpandMacros(const cmCMakePresetsFile& file,
  409. cmCMakePresetsFile::ExpandedPreset& preset,
  410. std::map<std::string, CycleStatus>& envCycles,
  411. std::string& out)
  412. {
  413. std::string result;
  414. std::string macroNamespace;
  415. std::string macroName;
  416. enum class State
  417. {
  418. Default,
  419. MacroNamespace,
  420. MacroName,
  421. } state = State::Default;
  422. for (auto c : out) {
  423. switch (state) {
  424. case State::Default:
  425. if (c == '$') {
  426. state = State::MacroNamespace;
  427. } else {
  428. result += c;
  429. }
  430. break;
  431. case State::MacroNamespace:
  432. if (c == '{') {
  433. if (IsValidMacroNamespace(macroNamespace)) {
  434. state = State::MacroName;
  435. } else {
  436. result += '$';
  437. result += macroNamespace;
  438. result += '{';
  439. macroNamespace.clear();
  440. state = State::Default;
  441. }
  442. } else {
  443. macroNamespace += c;
  444. if (!PrefixesValidMacroNamespace(macroNamespace)) {
  445. result += '$';
  446. result += macroNamespace;
  447. macroNamespace.clear();
  448. state = State::Default;
  449. }
  450. }
  451. break;
  452. case State::MacroName:
  453. if (c == '}') {
  454. if (!ExpandMacro(file, preset, envCycles, result, macroNamespace,
  455. macroName)) {
  456. return false;
  457. }
  458. macroNamespace.clear();
  459. macroName.clear();
  460. state = State::Default;
  461. } else {
  462. macroName += c;
  463. }
  464. break;
  465. }
  466. }
  467. switch (state) {
  468. case State::Default:
  469. break;
  470. case State::MacroNamespace:
  471. result += '$';
  472. result += macroNamespace;
  473. break;
  474. case State::MacroName:
  475. return false;
  476. }
  477. out = std::move(result);
  478. return true;
  479. }
  480. bool ExpandMacro(const cmCMakePresetsFile& file,
  481. cmCMakePresetsFile::ExpandedPreset& preset,
  482. std::map<std::string, CycleStatus>& envCycles,
  483. std::string& out, const std::string& macroNamespace,
  484. const std::string& macroName)
  485. {
  486. if (macroNamespace.empty()) {
  487. if (macroName == "sourceDir") {
  488. out += file.SourceDir;
  489. return true;
  490. }
  491. if (macroName == "sourceParentDir") {
  492. out += cmSystemTools::GetParentDirectory(file.SourceDir);
  493. return true;
  494. }
  495. if (macroName == "presetName") {
  496. out += preset.Name;
  497. return true;
  498. }
  499. if (macroName == "generator") {
  500. out += preset.Generator;
  501. return true;
  502. }
  503. if (macroName == "dollar") {
  504. out += '$';
  505. return true;
  506. }
  507. }
  508. if (macroNamespace == "env" && !macroName.empty()) {
  509. auto v = preset.Environment.find(macroName);
  510. if (v != preset.Environment.end() && v->second) {
  511. if (!VisitEnv(file, preset, envCycles, *v->second,
  512. envCycles[macroName])) {
  513. return false;
  514. }
  515. out += *v->second;
  516. return true;
  517. }
  518. }
  519. if (macroNamespace == "env" || macroNamespace == "penv") {
  520. if (macroName.empty()) {
  521. return false;
  522. }
  523. const char* value = std::getenv(macroName.c_str());
  524. if (value) {
  525. out += value;
  526. }
  527. return true;
  528. }
  529. // "vendor" falls through to here
  530. return false;
  531. }
  532. }
  533. std::string cmCMakePresetsFile::GetFilename(const std::string& sourceDir)
  534. {
  535. return cmStrCat(sourceDir, "/CMakePresets.json");
  536. }
  537. std::string cmCMakePresetsFile::GetUserFilename(const std::string& sourceDir)
  538. {
  539. return cmStrCat(sourceDir, "/CMakeUserPresets.json");
  540. }
  541. cmCMakePresetsFile::ReadFileResult cmCMakePresetsFile::ReadProjectPresets(
  542. const std::string& sourceDir, bool allowNoFiles)
  543. {
  544. bool haveOneFile = false;
  545. this->SourceDir = sourceDir;
  546. this->Presets.clear();
  547. this->PresetOrder.clear();
  548. std::vector<std::string> presetOrder;
  549. std::map<std::string, UnexpandedPreset> presetMap;
  550. std::string filename = GetUserFilename(this->SourceDir);
  551. if (cmSystemTools::FileExists(filename)) {
  552. auto result = this->ReadJSONFile(filename, presetOrder, presetMap, true);
  553. if (result != ReadFileResult::READ_OK) {
  554. return result;
  555. }
  556. haveOneFile = true;
  557. }
  558. filename = GetFilename(this->SourceDir);
  559. if (cmSystemTools::FileExists(filename)) {
  560. auto result = this->ReadJSONFile(filename, presetOrder, presetMap, false);
  561. if (result != ReadFileResult::READ_OK) {
  562. return result;
  563. }
  564. haveOneFile = true;
  565. }
  566. if (!haveOneFile) {
  567. return allowNoFiles ? ReadFileResult::READ_OK
  568. : ReadFileResult::FILE_NOT_FOUND;
  569. }
  570. auto result = ComputePresetInheritance(presetMap);
  571. if (result != ReadFileResult::READ_OK) {
  572. return result;
  573. }
  574. this->PresetOrder = std::move(presetOrder);
  575. this->Presets = std::move(presetMap);
  576. return ReadFileResult::READ_OK;
  577. }
  578. const char* cmCMakePresetsFile::ResultToString(ReadFileResult result)
  579. {
  580. switch (result) {
  581. case ReadFileResult::READ_OK:
  582. return "OK";
  583. case ReadFileResult::FILE_NOT_FOUND:
  584. return "File not found";
  585. case ReadFileResult::JSON_PARSE_ERROR:
  586. return "JSON parse error";
  587. case ReadFileResult::INVALID_ROOT:
  588. return "Invalid root object";
  589. case ReadFileResult::NO_VERSION:
  590. return "No \"version\" field";
  591. case ReadFileResult::INVALID_VERSION:
  592. return "Invalid \"version\" field";
  593. case ReadFileResult::UNRECOGNIZED_VERSION:
  594. return "Unrecognized \"version\" field";
  595. case ReadFileResult::INVALID_CMAKE_VERSION:
  596. return "Invalid \"cmakeMinimumRequired\" field";
  597. case ReadFileResult::UNRECOGNIZED_CMAKE_VERSION:
  598. return "\"cmakeMinimumRequired\" version too new";
  599. case ReadFileResult::INVALID_PRESETS:
  600. return "Invalid \"configurePresets\" field";
  601. case ReadFileResult::INVALID_PRESET:
  602. return "Invalid preset";
  603. case ReadFileResult::INVALID_VARIABLE:
  604. return "Invalid CMake variable definition";
  605. case ReadFileResult::DUPLICATE_PRESETS:
  606. return "Duplicate presets";
  607. case ReadFileResult::CYCLIC_PRESET_INHERITANCE:
  608. return "Cyclic preset inheritance";
  609. case ReadFileResult::USER_PRESET_INHERITANCE:
  610. return "Project preset inherits from user preset";
  611. default:
  612. return "Unknown error";
  613. }
  614. }
  615. cm::optional<cmCMakePresetsFile::ExpandedPreset>
  616. cmCMakePresetsFile::ExpandMacros(const UnexpandedPreset& preset) const
  617. {
  618. ExpandedPreset retval{ preset };
  619. std::map<std::string, CycleStatus> envCycles;
  620. for (auto const& v : retval.Environment) {
  621. envCycles[v.first] = CycleStatus::Unvisited;
  622. }
  623. for (auto& v : retval.Environment) {
  624. if (v.second &&
  625. !VisitEnv(*this, retval, envCycles, *v.second, envCycles[v.first])) {
  626. return cm::nullopt;
  627. }
  628. }
  629. std::string binaryDir = preset.BinaryDir;
  630. if (!::ExpandMacros(*this, retval, envCycles, binaryDir)) {
  631. return cm::nullopt;
  632. }
  633. if (!cmSystemTools::FileIsFullPath(binaryDir)) {
  634. binaryDir = cmStrCat(this->SourceDir, '/', binaryDir);
  635. }
  636. retval.BinaryDir = cmSystemTools::CollapseFullPath(binaryDir);
  637. cmSystemTools::ConvertToUnixSlashes(retval.BinaryDir);
  638. for (auto& variable : retval.CacheVariables) {
  639. if (variable.second &&
  640. !::ExpandMacros(*this, retval, envCycles, variable.second->Value)) {
  641. return cm::nullopt;
  642. }
  643. }
  644. return cm::make_optional(retval);
  645. }
  646. cmCMakePresetsFile::ReadFileResult cmCMakePresetsFile::ReadJSONFile(
  647. const std::string& filename, std::vector<std::string>& presetOrder,
  648. std::map<std::string, UnexpandedPreset>& presetMap, bool user)
  649. {
  650. cmsys::ifstream fin(filename.c_str());
  651. if (!fin) {
  652. return ReadFileResult::FILE_NOT_FOUND;
  653. }
  654. // If there's a BOM, toss it.
  655. cmsys::FStream::ReadBOM(fin);
  656. Json::Value root;
  657. Json::CharReaderBuilder builder;
  658. if (!Json::parseFromStream(builder, fin, &root, nullptr)) {
  659. return ReadFileResult::JSON_PARSE_ERROR;
  660. }
  661. int v = 0;
  662. auto result = RootVersionHelper(v, &root);
  663. if (result != ReadFileResult::READ_OK) {
  664. return result;
  665. }
  666. if (v < MIN_VERSION || v > MAX_VERSION) {
  667. return ReadFileResult::UNRECOGNIZED_VERSION;
  668. }
  669. RootPresets presets;
  670. if ((result = RootPresetsHelper(presets, &root)) !=
  671. ReadFileResult::READ_OK) {
  672. return result;
  673. }
  674. unsigned int currentMajor = cmVersion::GetMajorVersion();
  675. unsigned int currentMinor = cmVersion::GetMinorVersion();
  676. unsigned int currentPatch = cmVersion::GetPatchVersion();
  677. auto const& required = presets.CMakeMinimumRequired;
  678. if (required.Major > currentMajor ||
  679. (required.Major == currentMajor &&
  680. (required.Minor > currentMinor ||
  681. (required.Minor == currentMinor &&
  682. (required.Patch > currentPatch))))) {
  683. return ReadFileResult::UNRECOGNIZED_CMAKE_VERSION;
  684. }
  685. for (auto& preset : presets.Presets) {
  686. preset.User = user;
  687. if (preset.Name.empty()) {
  688. return ReadFileResult::INVALID_PRESET;
  689. }
  690. if (!presetMap.insert({ preset.Name, preset }).second) {
  691. return ReadFileResult::DUPLICATE_PRESETS;
  692. }
  693. presetOrder.push_back(preset.Name);
  694. }
  695. return ReadFileResult::READ_OK;
  696. }