Skip to content

Commit 8db3d12

Browse files
test: a test keeps what it reaches (design 2026-11-09 §17.2)
Units are compiled with one section per function and per object, and a test program links with --gc-sections (-dead_strip on Mach-O): mcpp's unit test programs go from 32.6 MB to 13.7 MB each (code 17.3 MB to 1.6 MB), 5.1 GB to 2.2 GB in all. Static constructors are the collection's roots, so a case that registers itself stays; [test] link = "objects" keeps every object whole. Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent bdafae6 commit 8db3d12

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎modules/manifest/src/toml.cppm‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3704,10 +3704,18 @@ std::expected<Manifest, ManifestError> parse_document(const t::Document& documen
37043704
m.testPrograms = value.as_string();
37053705
continue;
37063706
}
3707+
if (key == "link") {
3708+
if (!value.is_string() || (value.as_string() != "used" && value.as_string() != "objects"))
3709+
return std::unexpected(error(origin,
3710+
"[test] link is \"used\" (the linker keeps what a test reaches, the default) or "
3711+
"\"objects\" (every object whole)"));
3712+
m.testLink = value.as_string();
3713+
continue;
3714+
}
37073715
if (key != "discover") {
37083716
m.schemaWarnings.push_back(std::format(
37093717
"[test] has unsupported key '{}' (ignored). Supported keys: "
3710-
"discover, main, programs, start, windows_code_page, windows_long_paths.", key));
3718+
"discover, link, main, programs, start, windows_code_page, windows_long_paths.", key));
37113719
continue;
37123720
}
37133721
bool ok = value.is_array();

‎modules/manifest/src/types.cppm‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,6 +2251,11 @@ struct Manifest {
22512251
// "directory" (the files of one directory below a discover glob's fixed
22522252
// directory are one program, named by the directory). Empty: "file".
22532253
std::string testPrograms;
2254+
// `[test] link` (design 2026-11-09 §17.2): "used" (the default) -- the
2255+
// linker keeps what a test reaches (`--gc-sections`, `-dead_strip`), so a
2256+
// test of one module does not carry the package's every function --, or
2257+
// "objects", every object whole.
2258+
std::string testLink;
22542259
// Set once a workspace root's own package has received `[workspace.package]`
22552260
// and `[workspace.build]` (`mcpp::project::inherit_as_root_package`). The
22562261
// manifest is read on two paths before it is planned, and an appended

‎src/build/flags.cppm‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,13 @@ CompileFlags compute_flags_impl(const BuildPlan& plan, bool packageLinkFlags) {
12131213
// `/LTCG` on the link and the archive; before, it was not spelled at all,
12141214
// and a build with `lto = true` reported `+ lto` while doing none.
12151215
if (prof.lto) opt_flag += isMsvcDialect ? " /GL" : " -flto";
1216+
// ONE SECTION PER FUNCTION AND PER OBJECT (design 2026-11-09 §17.2), so
1217+
// that a link that keeps what it reaches (a test, `--gc-sections`) drops
1218+
// the rest of an object a module initializer pulls in. Measured on mcpp's
1219+
// unit tests: with whole objects the collection kept 15 of 17 MB of code.
1220+
// The freestanding prefix states it for its own units; cl.exe's `/Gy` is
1221+
// its default under optimisation.
1222+
if (!isMsvcDialect && !isFreestandingTarget) opt_flag += " -ffunction-sections -fdata-sections";
12161223
// `ndebug` (design 2026-10-23 §15.4): the profiles that produce what is
12171224
// shipped define NDEBUG for every unit of the graph, as CMake's Release,
12181225
// Meson's `b_ndebug=if-release` and Cargo's release do.

‎src/build/plan.cppm‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,19 @@ make_plan(const mcpp::manifest::Manifest& manifest,
20392039
// than link.exe does, so "which compiler binary" is the wrong question.
20402040
return targetOs == "windows" && mcpp::toolchain::is_msvc_target(tc);
20412041
}();
2042+
// A TEST KEEPS WHAT IT REACHES (design 2026-11-09 §17.2): every object of
2043+
// the package is on its link line, and a test of one module used to carry
2044+
// all of them -- 32 MB for each of mcpp's 166 unit tests. The linker drops
2045+
// the sections nothing references; static constructors are its roots, so
2046+
// a case that registers itself stays. `[test] link = "objects"` keeps
2047+
// every object whole. ELF and Mach-O through the compiler driver; MSVC's
2048+
// link.exe, a freestanding image and wasm keep their own link lines.
2049+
auto keep_what_tests_reach = [&](LinkUnit& lu, const mcpp::manifest::Manifest& owner) {
2050+
if (owner.testLink == "objects" || msvcTarget || targetTriple.is_freestanding()) return;
2051+
if (!targetTriple.empty() && targetTriple.str().starts_with("wasm")) return;
2052+
const bool macho = targetTriple.empty() ? bool(mcpplibs::os::is_macos) : targetTriple.is_mach_o();
2053+
lu.linkFlags.push_back(macho ? "-Wl,-dead_strip" : "-Wl,--gc-sections");
2054+
};
20422055

20432056
bool experimentalStd = false;
20442057
if (auto stdCfg = mcpp::manifest::normalize_cpp_standard(manifest.package.standard)) {
@@ -3601,6 +3614,7 @@ make_plan(const mcpp::manifest::Manifest& manifest,
36013614
lu.kind = LinkUnit::TestBinary;
36023615
lu.output = target_output(t, naming);
36033616
if (!t.main.empty()) lu.entryMain = projectRoot / t.main;
3617+
keep_what_tests_reach(lu, manifest);
36043618
} else if (t.kind == mcpp::manifest::Target::Application
36053619
&& mcpp::toolchain::triple::application_form(targetTriple)
36063620
== mcpp::toolchain::triple::ApplicationForm::SharedObject) {
@@ -4153,6 +4167,7 @@ make_plan(const mcpp::manifest::Manifest& manifest,
41534167
: place(target_output(t, naming));
41544168
lu.windowsSubsystem = t.windowsSubsystem;
41554169
lu.windowsEntry = t.windowsEntry;
4170+
if (lu.kind == LinkUnit::TestBinary) keep_what_tests_reach(lu, pkg.manifest);
41564171
}
41574172
lu.loaderTagFlag = loader_tag_flag(lu.kind);
41584173
for (auto const& other : plan.linkUnits)

0 commit comments

Comments
 (0)