|
| 1 | +// mcpp.cli.cmd_check — the implementation, an implementation unit for the |
| 2 | +// reason mcpp.cli.cmd_patch states. |
| 3 | +module mcpp.cli.cmd_check; |
| 4 | + |
| 5 | +import std; |
| 6 | +import mcpplibs.cmdline; |
| 7 | +import mcpp.manifest; |
| 8 | +import mcpp.toolchain.target_schema; |
| 9 | +import mcpp.toolchain.target_rows; |
| 10 | +import mcpp.ui; |
| 11 | + |
| 12 | +namespace mcpp::cli { |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +namespace fs = std::filesystem; |
| 17 | +namespace mp = mcpp::manifest::platform; |
| 18 | +namespace target = mcpp::toolchain::target; |
| 19 | + |
| 20 | +std::vector<std::string> segments(std::string_view triple) { |
| 21 | + std::vector<std::string> out; |
| 22 | + for (auto part : std::views::split(triple, '-')) out.emplace_back(part.begin(), part.end()); |
| 23 | + return out; |
| 24 | +} |
| 25 | + |
| 26 | +bool word(std::string_view w) { |
| 27 | + return !w.empty() && std::ranges::all_of(w, [](char c) { |
| 28 | + return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '.'; |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +} // namespace |
| 33 | + |
| 34 | +int cmd_check_platform(const mcpplibs::cmdline::ParsedArgs& parsed) { |
| 35 | + const fs::path dir = parsed.positional(0).empty() ? fs::current_path() : fs::path(parsed.positional(0)); |
| 36 | + auto m = mcpp::manifest::load(dir / "mcpp.toml"); |
| 37 | + if (!m) { |
| 38 | + mcpp::ui::error(m.error().format()); |
| 39 | + return 1; |
| 40 | + } |
| 41 | + if (!m->platformDeclared) { |
| 42 | + mcpp::ui::error(std::format("{} states no [platform]: it is not a platform package", |
| 43 | + (dir / "mcpp.toml").string())); |
| 44 | + return 1; |
| 45 | + } |
| 46 | + for (auto const& w : m->schemaWarnings) mcpp::ui::warning(w); |
| 47 | + auto const& s = m->platform; |
| 48 | + std::vector<std::string> errors, warnings; |
| 49 | + std::set<std::string> archs, own; |
| 50 | + for (auto const* r : target::rows()) archs.insert(std::string(r->arch)); |
| 51 | + |
| 52 | + for (auto const& d : s.defines) { |
| 53 | + const auto seg = segments(d.triple); |
| 54 | + if (seg.size() < 2 || seg.size() > 3 || !std::ranges::all_of(seg, word)) { |
| 55 | + errors.push_back(std::format("[[platform.define]] '{}' is not <arch>-<os>[-<env>] of words", d.triple)); |
| 56 | + continue; |
| 57 | + } |
| 58 | + if (!archs.contains(seg[0])) |
| 59 | + warnings.push_back(std::format("'{}': no built-in row has the architecture '{}'", d.triple, seg[0])); |
| 60 | + if (target::find_row(d.triple) && !own.contains(d.triple)) |
| 61 | + errors.push_back(std::format("'{}' is a built-in row; a platform serves it with " |
| 62 | + "[[platform.provider]] serves = [\"{}\"]", d.triple, d.triple)); |
| 63 | + if (!own.insert(d.triple).second) |
| 64 | + errors.push_back(std::format("'{}' is defined twice", d.triple)); |
| 65 | + if (d.clangTriple.empty()) |
| 66 | + warnings.push_back(std::format("'{}' states no clang-triple: the compiler is given the row's own spelling", |
| 67 | + d.triple)); |
| 68 | + } |
| 69 | + |
| 70 | + // Each identity this platform serves, with the provider and priority. |
| 71 | + struct Served { std::string identity, provider; int priority; }; |
| 72 | + std::vector<Served> served; |
| 73 | + auto rows_matching = [&](std::string_view pattern) { |
| 74 | + std::vector<std::string> out; |
| 75 | + for (auto const& t : own) if (target::serves_triple(pattern, t)) out.push_back(t); |
| 76 | + for (auto const* r : target::rows()) |
| 77 | + if (target::serves_triple(pattern, r->triple) && !own.contains(std::string(r->triple))) |
| 78 | + out.emplace_back(r->triple); |
| 79 | + return out; |
| 80 | + }; |
| 81 | + const std::string platformName = m->package.name; |
| 82 | + if (s.targets.set) |
| 83 | + for (auto const& t : s.targets.value) |
| 84 | + for (auto const& id : rows_matching(t)) |
| 85 | + served.push_back({ id, platformName, s.prioritySet ? s.priority : 100 }); |
| 86 | + for (auto const& p : s.providers) { |
| 87 | + const auto name = p.name.empty() ? platformName : p.name; |
| 88 | + std::size_t n = 0; |
| 89 | + for (auto const& pattern : p.serves) |
| 90 | + for (auto const& id : rows_matching(pattern)) { |
| 91 | + served.push_back({ id, name, p.priority }); |
| 92 | + ++n; |
| 93 | + } |
| 94 | + if (n == 0) |
| 95 | + warnings.push_back(std::format("the provider '{}' serves no row ({})", name, |
| 96 | + p.serves.empty() ? std::string("serves is empty") : p.serves.front())); |
| 97 | + } |
| 98 | + std::ranges::sort(served, [](auto const& a, auto const& b) { |
| 99 | + return std::tie(a.identity, b.priority, a.provider) < std::tie(b.identity, a.priority, b.provider); |
| 100 | + }); |
| 101 | + for (std::size_t i = 1; i < served.size(); ++i) |
| 102 | + if (served[i].identity == served[i - 1].identity && served[i].priority == served[i - 1].priority |
| 103 | + && served[i].provider != served[i - 1].provider) |
| 104 | + warnings.push_back(std::format("'{}' is served by '{}' and '{}' at priority {}: the name orders them", |
| 105 | + served[i].identity, served[i - 1].provider, served[i].provider, |
| 106 | + served[i].priority)); |
| 107 | + if (served.empty()) errors.push_back("the platform serves no identity: state targets or [[platform.provider]] serves"); |
| 108 | + |
| 109 | + mcpp::ui::info("Platform", std::format("{} {}", m->package.name, m->package.version)); |
| 110 | + for (auto const& e : served) |
| 111 | + mcpp::ui::line(std::format(" serves {:<28} {:<20} priority {}", e.identity, e.provider, e.priority)); |
| 112 | + for (auto const& w : warnings) mcpp::ui::warning(w); |
| 113 | + for (auto const& e : errors) mcpp::ui::error(e); |
| 114 | + if (!errors.empty()) return 1; |
| 115 | + mcpp::ui::info("Checked", std::format("{} row(s) defined, {} identit{} served{}", own.size(), served.size(), |
| 116 | + served.size() == 1 ? "y" : "ies", |
| 117 | + warnings.empty() ? "" : std::format(", {} warning(s)", warnings.size()))); |
| 118 | + return 0; |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace mcpp::cli |
0 commit comments