|
| 1 | +# 05 — Distributing a prebuilt library |
| 2 | + |
| 3 | +Both halves of one story, because they are only interesting together: |
| 4 | + |
| 5 | +``` |
| 6 | +producer/ a library with TWO interfaces — a C header and a C++ module |
| 7 | +consumer/ three programs using it: header only, module only, both |
| 8 | +``` |
| 9 | + |
| 10 | +```bash |
| 11 | +cd producer && mcpp pack mathkit # → target/dist/mathkit-0.1.0-<tag>/ |
| 12 | +cd ../consumer # point [dependencies] at it, then: |
| 13 | +mcpp run consume-header |
| 14 | +mcpp run consume-module |
| 15 | +mcpp run consume-both |
| 16 | +``` |
| 17 | + |
| 18 | +Full reference: [docs/12 — Distributing a Prebuilt Library](../../docs/12-binary-distribution.md). |
| 19 | + |
| 20 | +## There is no `--lib` flag |
| 21 | + |
| 22 | +What `mcpp pack` produces is decided by `[targets.<name>].kind` — `bin` gives an |
| 23 | +application bundle, `lib` and `shared` give library packages. That is already |
| 24 | +where mcpp records what an artifact is, and a flag would be a second place to |
| 25 | +say it. Publishing both forms means declaring both targets, which the producer |
| 26 | +does: |
| 27 | + |
| 28 | +```toml |
| 29 | +[targets.mathkit] kind = "lib" |
| 30 | +[targets.mathkit-shared] kind = "shared" soname = "libmathkit.so.1" |
| 31 | +``` |
| 32 | + |
| 33 | +## Read both lists it prints |
| 34 | + |
| 35 | +``` |
| 36 | + Packed leg x86_64-linux-gnu [x86_64-linux-gnu-gcc16-libstdcxx16-c++23] |
| 37 | + Interface mathkit.cppm, api.cppm |
| 38 | + Withheld capi.c, impl.cpp, secret.cppm |
| 39 | +``` |
| 40 | + |
| 41 | +If you are shipping closed source, **the second line is the one that matters**. |
| 42 | +Publishing too little fails loudly in your consumer's compile; publishing too |
| 43 | +much silently puts your implementation on someone's disk. |
| 44 | + |
| 45 | +`producer/src/secret.cppm` is an *implementation partition* |
| 46 | +(`module mathkit:secret;`, no `export`). It produces a BMI and an object exactly |
| 47 | +like the interface units do — so "publish every module unit" would leak it, and |
| 48 | +"publish every `.m.o`" is not a rule mcpp uses. What travels is the **module |
| 49 | +closure of the lib root**: `mathkit.cppm` and what its purview imports. |
| 50 | + |
| 51 | +Prove it: |
| 52 | + |
| 53 | +```bash |
| 54 | +cd producer && mcpp pack mathkit |
| 55 | +grep -r house_factor target/dist/*/interface/ ; echo "no match = nothing leaked" |
| 56 | +``` |
| 57 | + |
| 58 | +**Then break it on purpose.** Add `import :secret;` to `src/mathkit.cppm` and |
| 59 | +pack again: the interface now reaches the partition, so a consumer needs that |
| 60 | +source to compile at all — and `mcpp pack` says so instead of shipping a package |
| 61 | +nobody can build. |
| 62 | + |
| 63 | +## What the consumer's build checks |
| 64 | + |
| 65 | +Two things, both of which fail silently without a check. |
| 66 | + |
| 67 | +**The interface still matches its binaries.** Edit one line of |
| 68 | +`interface/api.cppm` inside the package and rebuild: |
| 69 | + |
| 70 | +``` |
| 71 | +error: mcpp.mathkit@0.1.0: 'interface' does not match what was packaged. |
| 72 | + recorded fnv1a:25b2cf2a79d71c40 |
| 73 | + found fnv1a:fe404d5be85118ff |
| 74 | +``` |
| 75 | + |
| 76 | +That refusal exists because the alternative was measured: swap two `int` members |
| 77 | +of a struct in a shipped interface — the Itanium ABI does not mangle field order |
| 78 | +— and the consumer compiles, links, runs, and prints transposed data, with no |
| 79 | +diagnostic from any tool. |
| 80 | + |
| 81 | +**The binaries were built for your toolchain.** Switch `[toolchain]` and rebuild; |
| 82 | +the refusal lists the tags the package *does* have, because "not found" would |
| 83 | +send you looking for a package already on your disk. |
| 84 | + |
| 85 | +## And one thing that will not work |
| 86 | + |
| 87 | +```bash |
| 88 | +cd producer/target/dist/mathkit-0.1.0-*/ && mcpp build |
| 89 | +error: … is a distribution package produced by `mcpp pack`, not a source tree. |
| 90 | +``` |
| 91 | + |
| 92 | +Its `interface/` holds declarations whose definitions are in the artifact beside |
| 93 | +them. Building there compiles the declarations, produces a near-empty library |
| 94 | +and reports success — a failure shaped exactly like a success. |
| 95 | + |
| 96 | +## Why the tag is shorter for a C-only library |
| 97 | + |
| 98 | +``` |
| 99 | +x86_64-linux-gnu-gcc16-libstdcxx16-c++23 # a C++ module interface |
| 100 | +x86_64-linux-gnu # an extern "C" interface only |
| 101 | +``` |
| 102 | + |
| 103 | +A tag names the dimensions the artifact actually constrains, and unnamed ones |
| 104 | +are don't-care. So an `extern "C"` library needs one tag per triple instead of |
| 105 | +one per triple per compiler — no flag, no mode: the shape *is* the statement. |
0 commit comments