@@ -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