Skip to content

Commit cc06741

Browse files
mcpp new --template platform; mcpp check platform (SPEC-016 §2, §3)
- The platform skeleton defines <arch>-<name> and the provider that serves it, with the llvm the built-in bare-metal rows pin. - `mcpp check platform [dir]` reads a platform's statement as the engine does: rows (segments, built-in redefinitions, repeats), providers (rows served, shared priorities), and the identities served with provider and priority. Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent af4f204 commit cc06741

6 files changed

Lines changed: 296 additions & 2 deletions

File tree

‎src/cli.cppm‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import mcpplibs.cmdline;
2020
import mcpp.cli.cmd_build;
2121
import mcpp.cli.cmd_cache;
2222
import mcpp.cli.cmd_patch;
23+
import mcpp.cli.cmd_check;
2324
import mcpp.cli.cmd_new;
2425
import mcpp.cli.cmd_publish;
2526
import mcpp.cli.cmd_xpkg;
@@ -77,6 +78,7 @@ void print_usage() {
7778
std::println(" mcpp add [ns.]pkg@ver Add an exact dependency to mcpp.toml");
7879
std::println(" mcpp remove [ns.]pkg Remove an exact dependency from mcpp.toml");
7980
std::println(" mcpp patch edit|save <dependency> Edit a dependency's sources; save the change as its patch");
81+
std::println(" mcpp check platform [dir] Check a platform package's rows, providers and served targets");
8082
std::println(" mcpp update [pkg] Re-resolve deps and rewrite mcpp.lock");
8183
std::println(" mcpp search <keyword> Search packages in registries");
8284
std::println(" mcpp publish [--dry-run] Publish package to default registry");
@@ -682,7 +684,7 @@ int run(int argc, char** argv) {
682684
// (cmd_new validates presence for project creation itself).
683685
.arg(cl::Arg("name").help("Package directory name"))
684686
.option(cl::Option("template").short_name('t').takes_value().value_name("SPEC")
685-
.help("bin (default) | lib | [ns.]pkg[@ver][:template] — exact package template"))
687+
.help("bin (default) | lib | platform | [ns.]pkg[@ver][:template] — exact package template"))
686688
.option(cl::Option("list-templates").takes_value().value_name("PKG")
687689
.help("List templates from exact [ns.]pkg[@ver]"))
688690
.action(wrap_rc(cmd_new)))
@@ -1223,6 +1225,16 @@ int run(int argc, char** argv) {
12231225
.option(cl::Option("target").takes_value().help(
12241226
"Remove the payload for <triple> instead of the host one")))
12251227
.action(wrap_rc(cmd_toolchain_any)))
1228+
.subcommand(cl::App("check")
1229+
.description("Check a package's statement as the engine reads it")
1230+
.subcommand(cl::App("platform")
1231+
.description("Check a platform package: its rows, its providers and the identities it serves")
1232+
.arg(cl::Arg("dir").help("The platform package's directory (default: this one)")))
1233+
.action(wrap_rc([&dispatch_sub](const cl::ParsedArgs& p) {
1234+
return dispatch_sub("check", p, {
1235+
{"platform", cmd_check_platform},
1236+
});
1237+
})))
12261238
.subcommand(cl::App("patch")
12271239
.description("Edit a dependency's sources and save the change as its patch")
12281240
.subcommand(cl::App("edit")
@@ -1940,7 +1952,7 @@ int run(int argc, char** argv) {
19401952
static constexpr std::array known = std::to_array<std::string_view>({
19411953
"new", "build", "run", "test", "clean", "add", "remove",
19421954
"update", "search", "publish", "pack", "emit", "xpkg",
1943-
"toolchain", "cache", "patch", "index", "self", "explain",
1955+
"toolchain", "cache", "patch", "check", "index", "self", "explain",
19441956
"version", "dyndep", "why", "resolve", "stage", "bmi-equal", "coff-def",
19451957
"place-dlls", "link-output", "platform-step",
19461958
"bmi-compile", "bmi-supervise", "bmi-await",

‎src/cli/cmd_check.cpp‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

‎src/cli/cmd_check.cppm‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// mcpp.cli.cmd_check — `mcpp check platform [<dir>]` (SPEC-016 §2, §3).
2+
//
3+
// A platform package's statement checked as the engine reads it: the rows it
4+
// defines (their segments, that they name no built-in row, that none repeats),
5+
// the providers (that each serves a row, and which share a row at one
6+
// priority), and the identities it serves, each with its provider and
7+
// priority. Errors end the command with 1; warnings do not.
8+
9+
export module mcpp.cli.cmd_check;
10+
11+
import std;
12+
import mcpplibs.cmdline;
13+
14+
export namespace mcpp::cli {
15+
16+
int cmd_check_platform(const mcpplibs::cmdline::ParsedArgs& parsed);
17+
18+
} // namespace mcpp::cli

‎src/cli/cmd_new.cppm‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export int cmd_new(const mcpplibs::cmdline::ParsedArgs& parsed) {
6262
"--template gui is deprecated; use `--template imgui` "
6363
"(the template then ships with — and version-tracks — the library)");
6464
}
65+
if (tmpl == "platform") return mcpp::scaffold::create_platform_project(*project);
6566
if (tmpl != "bin" && tmpl != "gui" && tmpl != "lib") {
6667
auto spec = mcpp::scaffold::parse_template_spec(tmpl);
6768
if (!spec) {

‎src/scaffold/create.cppm‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import mcpp.scaffold;
1717
import mcpp.scaffold.project_name;
1818
import mcpp.ui;
1919
import mcpp.build.test_protocol; // the release of mtest the skeleton's tests use
20+
import mcpp.toolchain.target_schema;
21+
import mcpp.toolchain.target_rows; // the llvm a platform skeleton pins, as the built-in rows do
2022

2123
namespace mcpp::scaffold {
2224

@@ -300,6 +302,11 @@ export int new_from_package_template(
300302
// gui alias) and a library (lib).
301303
export enum class builtin { bin, gui, lib };
302304

305+
// `mcpp new --template platform` (SPEC-016 §2, §3): a platform package that
306+
// defines one row, `<arch>-<name>`, and the provider that serves it, with the
307+
// llvm the built-in bare-metal rows pin. `mcpp check platform` reads it.
308+
export int create_platform_project(const mcpp::scaffold::PortableProjectName& project);
309+
303310
// What a project's library is named in C++: its module, the qualified name
304311
// with `-` written `_` (`acme.my-lib` -> `acme.my_lib`), and its namespace
305312
// (`acme::my_lib`). Empty when a component does not begin with a letter or
@@ -564,4 +571,81 @@ UTest greeting{"hello greets by name", [] {
564571
return 0;
565572
}
566573

574+
int create_platform_project(const mcpp::scaffold::PortableProjectName& project) {
575+
// The system's word: the package's name, letters and digits.
576+
std::string os;
577+
for (char c : project.name)
578+
if (std::isalnum(static_cast<unsigned char>(c))) os += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
579+
if (os.empty() || !std::isalpha(static_cast<unsigned char>(os.front()))) os = "example";
580+
std::string llvm;
581+
for (auto const* p : mcpp::toolchain::target::providers())
582+
if (p->toolchain.starts_with("llvm@")) { llvm = std::string(p->toolchain); break; }
583+
const std::string arch(mcpp::toolchain::platform::host_arch);
584+
const std::string triple = std::format("{}-{}", arch, os);
585+
586+
auto transaction = mcpp::scaffold::ScaffoldTransaction::begin(
587+
std::filesystem::current_path(), project.directoryName);
588+
if (!transaction) {
589+
mcpp::ui::error(transaction.error());
590+
return 1;
591+
}
592+
auto tx = std::move(*transaction);
593+
const auto& root = tx.staging_path();
594+
const std::string manifest = std::format(R"(# A platform (SPEC-016): the rows it defines and the provider that serves them.
595+
# A project selects it for a target with [target.<triple>] platform = {{ path = "..." }}.
596+
[package]
597+
name = "{0}"
598+
version = "0.1.0"
599+
description = "The platform of {1}"
600+
601+
[platform]
602+
toolchain = "{2}"
603+
604+
# The row: the system's triple, what the compiler is told, the object format,
605+
# and the system its programs run on.
606+
[[platform.define]]
607+
triple = "{1}"
608+
clang-triple = "{3}-unknown-none-elf"
609+
object-format = "elf"
610+
execution = "{4}"
611+
612+
[[platform.provider]]
613+
serves = ["{1}"]
614+
615+
[platform.build]
616+
cflags = ["-ffreestanding"]
617+
618+
[platform.link]
619+
driver = "compiler"
620+
621+
# What the platform provides for the target side; `false`: nothing yet.
622+
[platform.layers]
623+
"kernel-abi" = false
624+
"c-abi" = false
625+
"c++-abi" = false
626+
"compiler-runtime" = false
627+
)", project.qualifiedName, triple, llvm, arch, os);
628+
if (auto written = mcpp::scaffold::write_text_file(root / "mcpp.toml", manifest); !written) {
629+
mcpp::ui::error(written.error());
630+
return 1;
631+
}
632+
if (auto written = mcpp::scaffold::write_text_file(root / ".gitignore", "target/\n.mcpp/\n"); !written) {
633+
mcpp::ui::error(written.error());
634+
return 1;
635+
}
636+
auto generated = mcpp::manifest::load(root / "mcpp.toml");
637+
if (!generated) {
638+
mcpp::ui::error(std::format("generated manifest validation failed: {}", generated.error().format()));
639+
return 1;
640+
}
641+
if (auto committed = tx.commit(); !committed) {
642+
mcpp::ui::error(committed.error());
643+
return 1;
644+
}
645+
mcpp::ui::line(std::format("Created platform package '{}' at {}, defining {}", project.qualifiedName,
646+
tx.final_path().string(), triple));
647+
mcpp::ui::line(std::format("Next: cd {} && mcpp check platform", project.directoryName));
648+
return 0;
649+
}
650+
567651
} // namespace mcpp::scaffold
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bash
2+
# requires: elf llvm
3+
source "$(dirname "${BASH_SOURCE[0]}")/_toolchain_env.sh"
4+
# 1049 -- a platform skeleton is made and checked (SPEC-016 §2, §3).
5+
#
6+
# `mcpp new <name> --template platform` writes a platform package that defines `<arch>-<name>` and
7+
# the provider that serves it; `mcpp check platform` reads it as the engine does and lists the
8+
# identities it serves. A row that names a built-in row is refused, and a provider that serves
9+
# nothing is said.
10+
#
11+
# Criteria: the skeleton checks clean and serves x86_64-<name> (on x86_64); a program builds for
12+
# the row; a redefined built-in row ends the check with 1; a provider serving nothing is a warning.
13+
set -e
14+
t=$(mktemp -d); trap 'rm -rf "$t"' EXIT
15+
fail() { echo "FAIL: $1"; [ -n "${2:-}" ] && printf '%s\n' "$2" | tail -20; exit 1; }
16+
cd "$t"
17+
arch=$(uname -m); [ "$arch" = arm64 ] && arch=aarch64
18+
out=$("$MCPP" new myos --template platform 2>&1) || fail "the skeleton was not made" "$out"
19+
out=$(cd myos && "$MCPP" check platform 2>&1) || fail "the skeleton does not check" "$out"
20+
grep -Eq "serves $arch-myos +myos +priority 100" <<<"$out" || fail "the check does not list the served row" "$out"
21+
echo " ok the skeleton checks and serves $arch-myos"
22+
23+
mkdir -p app/src
24+
cat > app/mcpp.toml <<EOF
25+
[package]
26+
name = "app"
27+
version = "0.1.0"
28+
29+
[build]
30+
sources = ["src/*.c"]
31+
ldflags = ["-nostdlib", "-static"]
32+
33+
[targets.app]
34+
kind = "bin"
35+
main = "src/main.c"
36+
37+
[target.$arch-myos]
38+
platform = { path = "../myos" }
39+
EOF
40+
echo 'void _start(void) { for (;;) {} }' > app/src/main.c
41+
out=$(cd app && "$MCPP" build --target "$arch-myos" 2>&1) || fail "a program did not build for the row" "$out"
42+
echo " ok a program builds for the platform's row"
43+
44+
cat >> myos/mcpp.toml <<'EOF'
45+
46+
[[platform.define]]
47+
triple = "x86_64-linux-gnu"
48+
49+
[[platform.provider]]
50+
name = "nothing"
51+
serves = ["riscv64-nosuch"]
52+
EOF
53+
set +e; out=$(cd myos && "$MCPP" check platform 2>&1); rc=$?; set -e
54+
[ "$rc" = 1 ] || fail "a redefined built-in row did not fail the check (exit $rc)" "$out"
55+
grep -q "'x86_64-linux-gnu' is a built-in row" <<<"$out" || fail "the redefinition is not named" "$out"
56+
grep -q "the provider 'nothing' serves no row" <<<"$out" || fail "a provider serving nothing is not said" "$out"
57+
echo " ok a redefined built-in row fails the check; a provider serving nothing is said"
58+
echo "PASS: 1049 a platform skeleton is made and checked"

0 commit comments

Comments
 (0)