Skip to content

Commit 87ab153

Browse files
SPEC-015: target sets and legs -- a set expands to the table's rows of a system, the legs of one command are planned serially and built at once, run takes the first reachable leg, test runs every reachable leg and fails when none ran, tests are archived once and run elsewhere
- target sets (`openkal`, `linux-musl`, `none-eabihf`) are recognised by their first segment and expanded in the CLI against the target table; members must select one platform; a member the platform does not serve is left out, stated - mcpp::build::legs: plan (serial, mcpp.lock written by one planner), build and run_by_directory (concurrent across build directories, static job share, one Finished); the Android, universal, library and platform-test leg loops use it - build/run/test/self env take several --target; [pack] targets is parsed; [build] target refuses a set - mcpp test --no-run --archive <file> / --archive <file> - machine output: one leg unchanged; several legs add data.legs, the legs' status, triple on artifact/diagnostic events, legs in .last-result.json - unit tests (target sets, legs, manifest); e2e 963, 964 Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent 8bea56d commit 87ab153

17 files changed

Lines changed: 2397 additions & 360 deletions

‎modules/manifest/src/toml.cppm‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import std;
1212
import mcpp.invocation;
1313
import mcpp.source_kind;
1414
import mcpp.libs.toml;
15+
import mcpp.toolchain.triple;
1516
import mcpp.pm.dep_spec;
1617
import mcpp.version_req;
1718
import mcpp.pm.dependency_selector;
@@ -2950,7 +2951,17 @@ std::expected<Manifest, ManifestError> parse_document(const t::Document& documen
29502951
m.buildConfig.buildProgramTimeoutSecs = static_cast<int>(secs);
29512952
}
29522953
if (auto v = doc->get_string("build.c_standard")) m.buildConfig.cStandard = *v;
2953-
if (auto v = doc->get_string("build.target")) m.buildConfig.target = *v;
2954+
if (auto v = doc->get_string("build.target")) {
2955+
// One target: `build`, `run` and `test` act on it by default, and a
2956+
// default that built every architecture would charge each build for
2957+
// all of them (SPEC-015 §3.3).
2958+
if (mcpp::toolchain::target_set::is_set_spelling(*v))
2959+
return std::unexpected(error(origin, std::format(
2960+
"[build].target = '{}' names a target set; [build] target names one target "
2961+
"(`x86_64-openkal`). A set is written in [pack] targets, or as `--target {}`",
2962+
*v, *v)));
2963+
m.buildConfig.target = *v;
2964+
}
29542965
// `jobs` accepts a number or "auto"; both arrive as text and are validated
29552966
// where they are used, so a bad value warns at build time instead of making
29562967
// the whole manifest unloadable. (A published package carrying an unknown
@@ -3826,6 +3837,23 @@ std::expected<Manifest, ManifestError> parse_document(const t::Document& documen
38263837
m.packConfig.sign = *v;
38273838
}
38283839
if (auto v = doc->get_bool("pack.universal")) m.packConfig.universal = *v;
3840+
// `[pack] targets` (SPEC-015 §3.3): a string or a list; each a target or a
3841+
// target set. A set's members are known only with the platform the
3842+
// command selects, so a set is checked for its form here and expanded
3843+
// where the command reads it.
3844+
{
3845+
std::vector<std::string> targets;
3846+
if (auto v = doc->get_string("pack.targets")) targets.push_back(*v);
3847+
else if (auto a = doc->get_string_array("pack.targets")) targets = *a;
3848+
for (auto const& t : targets) {
3849+
if (mcpp::toolchain::target_set::is_set_spelling(t) || mcpp::toolchain::triple::parse(t))
3850+
continue;
3851+
return std::unexpected(error(origin, std::format(
3852+
"[pack].targets: '{}' is neither a target (`x86_64-linux-musl`) nor a target set "
3853+
"(`openkal`, `linux-musl`)", t)));
3854+
}
3855+
m.packConfig.targets = std::move(targets);
3856+
}
38293857

38303858
// [target.<triple>] — per-target overrides. We accept both GCC
38313859
// (x86_64-linux-musl) and Rust-style (x86_64-unknown-linux-musl)

‎modules/manifest/src/types.cppm‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,10 @@ struct PackConfig {
18801880
// `false`: a pack of a platform's target packs that target alone, where
18811881
// the platform states `universal = true` (SPEC-014 §3).
18821882
std::optional<bool> universal;
1883+
// The targets `mcpp pack` acts on when the command names none (SPEC-015
1884+
// §3.3): targets and target sets, as `--target` takes them (`openkal`,
1885+
// `x86_64-linux-musl`). Empty: unstated, the build's target.
1886+
std::vector<std::string> targets;
18831887
};
18841888

18851889
// `[workspace]` — multi-package workspace support (0.0.11+).

‎modules/toolchain-model/src/triple.cppm‎

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,3 +1584,218 @@ std::optional<std::string> did_you_mean(std::string_view input) {
15841584
}
15851585

15861586
} // namespace mcpp::toolchain::triple
1587+
1588+
// ── target sets (SPEC-015) ──────────────────────────────────────────────────
1589+
//
1590+
// A `--target` value is either a target or a set of targets. A target is a
1591+
// triple and means what it always meant. A SET is written by leaving the
1592+
// architecture out: `openkal`, `linux-musl`, `none-eabihf`. It names every row
1593+
// of the target table whose system and environment are the ones written, and
1594+
// a command over a set acts once per member, each member being one LEG.
1595+
//
1596+
// THE SET IS RECOGNISED BY ITS FIRST SEGMENT, NOT BY A FAILED PARSE. The
1597+
// triple parser does not validate the architecture: `unknown-linux-gnu` parses
1598+
// as a triple whose architecture is `unknown`, and the four-segment GNU forms
1599+
// put a vendor first. A spelling is a set exactly when its first segment is
1600+
// one of the system words below; everything else is a triple, refused or
1601+
// accepted where triples always were.
1602+
//
1603+
// THE ENVIRONMENT IS FILLED AS A WRITTEN TRIPLE FILLS IT. `linux` is
1604+
// `linux-gnu` and `windows` is `windows-gnu`, the fills docs/21 states for
1605+
// `<arch>-linux` and `<arch>-windows`; a system without an environment segment
1606+
// (`macos`, `openkal`, `emscripten`, `ios`) has nothing to fill. Bare metal has
1607+
// no default environment, so `none` alone names no set and the refusal lists
1608+
// the environments that do.
1609+
//
1610+
// WHICH PLATFORM SERVES THE MEMBERS IS ASKED, NOT ASSUMED. Each candidate is
1611+
// put to the platform selection the build uses (the flag, the environment,
1612+
// the project, this machine's configuration, the target table), through the
1613+
// callback the caller passes. A set whose members select different platforms
1614+
// is refused, naming the split: one command acting on two platforms at once
1615+
// is not what a set was written to mean. Whether the selected platform serves
1616+
// every member is a fact of the platform package, which is read when a leg is
1617+
// planned; a member it does not serve is left out there, and the leaving out
1618+
// is reported.
1619+
1620+
export namespace mcpp::toolchain::target_set {
1621+
1622+
// The words a set begins with. `android` is the one word that is an
1623+
// environment rather than a system (`<arch>-linux-android`); it is a word here
1624+
// because a user names the platform by it.
1625+
inline constexpr std::string_view kSystemWords[] = {
1626+
"linux", "macos", "windows", "openkal", "emscripten", "ios", "android", "none",
1627+
};
1628+
1629+
// Whether `spelling` names a set: its first segment is a system word.
1630+
bool is_set_spelling(std::string_view spelling);
1631+
1632+
// The platform a candidate selects, as one comparable string ("" = the
1633+
// engine's own conventions), or the selection's own error.
1634+
using PlatformOf =
1635+
std::function<std::expected<std::string, std::string>(const mcpp::toolchain::triple::Triple&)>;
1636+
1637+
struct Leg {
1638+
std::string triple; // canonical for a set's member; as written for a target
1639+
std::string fromSet; // the set spelling it came from; empty for a target
1640+
};
1641+
1642+
struct Expansion {
1643+
std::vector<std::string> members; // canonical triples, in target-table order
1644+
std::string platform; // the platform every member selected
1645+
};
1646+
1647+
// The members of one set spelling.
1648+
std::expected<Expansion, std::string> expand_set(std::string_view spelling,
1649+
const PlatformOf& platformOf);
1650+
1651+
// Every `--target` of one command: sets expanded, targets kept as written, the
1652+
// union in target-table order (targets the table does not hold keep the order
1653+
// they were written in, after the table's), a target named twice once.
1654+
std::expected<std::vector<Leg>, std::string> resolve(std::span<const std::string> requests,
1655+
const PlatformOf& platformOf);
1656+
1657+
} // namespace mcpp::toolchain::target_set
1658+
1659+
// ── implementation ──────────────────────────────────────────────────────────
1660+
1661+
namespace mcpp::toolchain::target_set {
1662+
1663+
namespace tt = mcpp::toolchain::triple;
1664+
1665+
namespace {
1666+
1667+
std::string_view first_segment(std::string_view s) {
1668+
auto d = s.find('-');
1669+
return d == std::string_view::npos ? s : s.substr(0, d);
1670+
}
1671+
1672+
// The table row index of a canonical triple; the table's size when absent.
1673+
std::size_t table_rank(std::string_view canonical) {
1674+
auto rows = tt::known_targets();
1675+
for (std::size_t i = 0; i < rows.size(); ++i)
1676+
if (rows[i].canonical == canonical) return i;
1677+
return rows.size();
1678+
}
1679+
1680+
struct Wanted {
1681+
std::string os;
1682+
std::string env;
1683+
};
1684+
1685+
std::expected<Wanted, std::string> wanted_of(std::string_view spelling) {
1686+
std::vector<std::string_view> seg;
1687+
for (std::size_t b = 0; b <= spelling.size();) {
1688+
auto d = spelling.find('-', b);
1689+
seg.push_back(spelling.substr(b, d == std::string_view::npos ? std::string_view::npos : d - b));
1690+
if (d == std::string_view::npos) break;
1691+
b = d + 1;
1692+
}
1693+
const auto word = seg.front();
1694+
if (seg.size() > 2 || (seg.size() == 2 && seg[1].empty()))
1695+
return std::unexpected(std::format(
1696+
"'{}' is not a target set: a set is a system and at most one environment "
1697+
"(`openkal`, `linux-musl`, `none-eabihf`)", spelling));
1698+
const std::string written = seg.size() == 2 ? std::string(seg[1]) : std::string{};
1699+
if (word == "android") {
1700+
if (!written.empty())
1701+
return std::unexpected(std::format(
1702+
"'{}' is not a target set: `android` is the environment of `<arch>-linux-android` "
1703+
"and takes no further segment", spelling));
1704+
return Wanted{"linux", "android"};
1705+
}
1706+
if (word == "linux" || word == "windows")
1707+
return Wanted{std::string(word), written.empty() ? std::string("gnu") : written};
1708+
if (word == "none") {
1709+
if (written.empty())
1710+
return std::unexpected(
1711+
"`none` names no target set: bare metal has no default environment.\n"
1712+
" use: none-elf | none-eabi | none-eabihf");
1713+
return Wanted{"none", written};
1714+
}
1715+
if (word == "ios") return Wanted{"ios", written};
1716+
if (!written.empty())
1717+
return std::unexpected(std::format(
1718+
"'{}' is not a target set: `{}` has no environment segment", spelling, word));
1719+
return Wanted{std::string(word), {}};
1720+
}
1721+
1722+
} // namespace
1723+
1724+
bool is_set_spelling(std::string_view spelling) {
1725+
const auto w = first_segment(spelling);
1726+
return std::ranges::find(kSystemWords, w) != std::end(kSystemWords);
1727+
}
1728+
1729+
std::expected<Expansion, std::string> expand_set(std::string_view spelling,
1730+
const PlatformOf& platformOf) {
1731+
auto want = wanted_of(spelling);
1732+
if (!want) return std::unexpected(want.error());
1733+
1734+
Expansion out;
1735+
std::vector<std::pair<std::string, std::string>> chosen; // member, platform
1736+
std::vector<std::string_view> planned;
1737+
for (auto const& row : tt::known_targets()) {
1738+
auto t = tt::parse(row.canonical);
1739+
if (!t || t->os != want->os || t->env != want->env) continue;
1740+
if (row.tier == "planned") { planned.push_back(row.canonical); continue; }
1741+
auto p = platformOf ? platformOf(*t) : std::expected<std::string, std::string>{};
1742+
if (!p) return std::unexpected(std::format("{}: {}", row.canonical, p.error()));
1743+
chosen.emplace_back(std::string(row.canonical), *p);
1744+
}
1745+
if (chosen.empty()) {
1746+
std::string rows;
1747+
for (auto r : planned) rows += std::format("{}{}", rows.empty() ? "" : ", ", r);
1748+
return std::unexpected(std::format(
1749+
"the target set '{}' names no target this mcpp builds{}.\n"
1750+
" `mcpp toolchain list` lists the targets", spelling,
1751+
rows.empty() ? std::string{} : std::format(" (planned, not yet built: {})", rows)));
1752+
}
1753+
for (auto const& [member, platform] : chosen) {
1754+
if (platform == chosen.front().second) continue;
1755+
std::string split;
1756+
for (auto const& [m, p] : chosen)
1757+
split += std::format("\n {} {}", m, p.empty() ? "(the engine's conventions)" : p);
1758+
return std::unexpected(std::format(
1759+
"the members of the target set '{}' select different platforms:{}\n"
1760+
" a set acts on one platform; name the targets one by one, or select one "
1761+
"platform for all of them ([target.<selector>] platform, --platform)",
1762+
spelling, split));
1763+
}
1764+
out.platform = chosen.front().second;
1765+
for (auto& [member, platform] : chosen) out.members.push_back(std::move(member));
1766+
return out;
1767+
}
1768+
1769+
std::expected<std::vector<Leg>, std::string> resolve(std::span<const std::string> requests,
1770+
const PlatformOf& platformOf) {
1771+
struct Ranked { std::size_t rank; std::size_t order; Leg leg; std::string key; };
1772+
std::vector<Ranked> all;
1773+
std::set<std::string> seen;
1774+
auto add = [&](Leg leg, std::string key) {
1775+
if (!seen.insert(key).second) return;
1776+
all.push_back({table_rank(key), all.size(), std::move(leg), std::move(key)});
1777+
};
1778+
for (auto const& r : requests) {
1779+
if (is_set_spelling(r)) {
1780+
auto e = expand_set(r, platformOf);
1781+
if (!e) return std::unexpected(e.error());
1782+
for (auto& m : e->members) {
1783+
std::string key = m;
1784+
add(Leg{std::move(m), r}, std::move(key));
1785+
}
1786+
continue;
1787+
}
1788+
// A target keeps its spelling: the leg is planned as the command
1789+
// would have planned it alone, completion and report included.
1790+
auto t = tt::parse(r);
1791+
add(Leg{r, {}}, t ? t->str() : r);
1792+
}
1793+
std::ranges::stable_sort(all, [](Ranked const& a, Ranked const& b) {
1794+
return a.rank != b.rank ? a.rank < b.rank : a.order < b.order;
1795+
});
1796+
std::vector<Leg> legs;
1797+
for (auto& r : all) legs.push_back(std::move(r.leg));
1798+
return legs;
1799+
}
1800+
1801+
} // namespace mcpp::toolchain::target_set

0 commit comments

Comments
 (0)