|
| 1 | +# Bare OS-alias sugar for `[target.*]` conditional tables (Design) |
| 2 | + |
| 3 | +Addresses the ergonomic complaint that `[target.'cfg(linux)'.dependencies.compat]` |
| 4 | +is visually noisy. Lets the common single-OS case drop the `cfg(...)` wrapper (and |
| 5 | +its mandatory TOML quotes) while keeping the full `cfg(...)` grammar for compound |
| 6 | +predicates. Ships in mcpp 0.0.80. |
| 7 | + |
| 8 | +## Before / after |
| 9 | + |
| 10 | +```toml |
| 11 | +# before — quotes (TOML-mandated around cfg(...)) + ceremony |
| 12 | +[target.'cfg(windows)'.dependencies.compat] |
| 13 | +openblas = "0.3.33" |
| 14 | +[target.'cfg(windows)'.build] |
| 15 | +ldflags = ["-Llib", "-llibopenblas"] |
| 16 | + |
| 17 | +# after — bare alias for the 90% case, no quotes |
| 18 | +[target.windows.dependencies.compat] |
| 19 | +openblas = "0.3.33" |
| 20 | +[target.windows.build] |
| 21 | +ldflags = ["-Llib", "-llibopenblas"] |
| 22 | + |
| 23 | +# compound predicates STILL use cfg(...) (arch / env / all / any / not) |
| 24 | +[target.'cfg(all(linux, not(arch = "aarch64")))'.build] |
| 25 | +cxxflags = ["-march=x86-64-v2"] |
| 26 | +``` |
| 27 | + |
| 28 | +## Why this is unambiguous |
| 29 | + |
| 30 | +A mcpp target triple is always `<arch>-<os>[-<env>]` (`x86_64-linux-musl`, |
| 31 | +`x86_64-pc-windows-msvc`). The bare OS/family aliases **`windows` / `linux` / |
| 32 | +`macos` / `unix` are never valid triples** (no dash), so `[target.linux]` can only |
| 33 | +mean the `cfg(linux)` predicate — there is no collision with the exact-triple |
| 34 | +namespace (`[target.x86_64-linux-musl]`). This is the same alias set already |
| 35 | +accepted *inside* `cfg(...)` (`cfgpred::match_alias`), now also accepted as a bare |
| 36 | +section key. |
| 37 | + |
| 38 | +This is a deliberate, small divergence from Cargo (which always requires |
| 39 | +`cfg()`/triple) — justified because mcpp's alias set is unambiguous, and the win is |
| 40 | +removing the quote-noise from the overwhelmingly common per-OS case. |
| 41 | + |
| 42 | +## Implementation (one evaluator branch) |
| 43 | + |
| 44 | +The parser already does the right thing: `manifest.cppm`'s `[target]` loop reads |
| 45 | +`build` / `dependencies` / … for **every** `[target.<key>]` and stores a |
| 46 | +`ConditionalConfig{ predicate = <key> }` (manifest.cppm:1242-1275). So |
| 47 | +`[target.linux.dependencies.compat]` is *already* parsed into a conditional config |
| 48 | +with `predicate = "linux"`. The only gap is evaluation. |
| 49 | + |
| 50 | +`cfgpred::matches()` (`prepare.cppm:139-146`) currently: |
| 51 | +```cpp |
| 52 | +inline bool matches(const std::string& predicate, const Ctx& c, std::string_view triple) { |
| 53 | + std::string_view k = predicate; |
| 54 | + if (k.starts_with("cfg(") && k.ends_with(")")) { Parser p{...}; return p.expr(); } |
| 55 | + return !triple.empty() && predicate == triple; // bare-triple exact match |
| 56 | +} |
| 57 | +``` |
| 58 | +A bare `"linux"` falls through to the triple branch and never matches (host build → |
| 59 | +`triple` empty; cross build → `"linux" != "x86_64-linux-gnu"`). Fix — add the |
| 60 | +alias branch **before** the triple fallback: |
| 61 | +```cpp |
| 62 | + if (predicate == "windows" || predicate == "linux" || |
| 63 | + predicate == "macos" || predicate == "unix") { |
| 64 | + Parser p{ predicate, 0, c }; // evaluate as the cfg bareword |
| 65 | + return p.expr(); |
| 66 | + } |
| 67 | +``` |
| 68 | +That's the whole behavioral change. Evaluation stays **target-resolved** (the |
| 69 | +`Ctx` is built from the resolved `--target`, else host) — identical semantics to |
| 70 | +`cfg(linux)`. No parser change, no schema change, fully backward-compatible |
| 71 | +(`cfg(...)` and exact triples unchanged). |
| 72 | + |
| 73 | +## Scope boundary (documented) |
| 74 | + |
| 75 | +The sugar covers the **L1 conditional config**: `.dependencies` / |
| 76 | +`.dev-dependencies` / `.build-dependencies` / `.build` (cflags/cxxflags/ldflags). |
| 77 | + |
| 78 | +`[target.<triple>].toolchain` and `.linkage` remain **exact-triple only** — they |
| 79 | +describe a *specific* cross target, not an OS family, and are looked up by exact |
| 80 | +triple in `prepare_build`. Writing `toolchain`/`linkage` under a bare alias (or a |
| 81 | +`cfg(...)` key) has no effect today; a follow-up may add a schema warning to flag |
| 82 | +that footgun. Out of scope here. |
| 83 | + |
| 84 | +## Tests |
| 85 | + |
| 86 | +`tests/e2e/91_target_bare_alias.sh`: a project with `[target.linux.build] cxxflags` |
| 87 | ++ `[target.windows.dependencies]`; assert on Linux the cxxflag define reaches the |
| 88 | +TU and the windows-only dep is NOT pulled; assert `[target.'cfg(linux)']` and the |
| 89 | +bare `[target.linux]` produce identical results (parity). |
| 90 | + |
| 91 | +## Docs |
| 92 | + |
| 93 | +`docs/05-mcpp-toml.md` (+ zh): the `[target.*]` section gains the bare-alias form |
| 94 | +as the recommended spelling for single-OS, with `cfg(...)` shown for compound |
| 95 | +predicates and the triple form for exact targets. |
0 commit comments