Skip to content

Commit e6fdd89

Browse files
committed
A test plan that configures a package otherwise than mcpp build builds in a directory of its own; a build program's unchanged output keeps its time (#945)
The tests' configuration axis (#test=) enters the fingerprint when the tests ask a package for features ([test] features, or a dev-dependency entry that asks a package the manifest also depends on). One directory held both configurations, whose interfaces and objects are named by module and package, and each plan compiled over the other. A build program rerun when its cached answer was another configuration's rewrote its generated source with a new time. e2e 1107: one compile per configuration, none again.
1 parent b40a305 commit e6fdd89

6 files changed

Lines changed: 159 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@
7575
array of tables before the platform reader saw it, so a platform could define
7676
rows for a system but not state the system (`uname`, `resources`,
7777
`command-scripts`; SPEC-016 §2.3).
78+
- A test plan that configures a package otherwise than `mcpp build` (its
79+
`[test] features`, or a dev-dependency entry that asks a package the
80+
manifest also depends on for features) builds in a directory of its own
81+
(#945). The two configurations shared one directory, whose module
82+
interfaces and objects are named by module and package, and compiled each
83+
other's files again: three compiles of one module per `mcpp test
84+
--workspace`, and on Windows a rewrite of an interface another compile had
85+
open. Each configuration now compiles once (e2e 1107).
86+
- A file a build program writes again with the same content keeps its time,
87+
so a program that writes its outputs on every run does not recompile what
88+
imports them (#945).
7889
- A failed build whose ninja output names no failed step prints ninja's last
7990
lines rather than a bare `error: build failed` (#945).
8091
- A base package whose C++ layer another package supplies (a provider's

‎docs/08-testing.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,13 @@ A test plan turns them on beside the defaults and `--features`, in `mcpp test`,
227227
requested on the member's own edge, so another member that declares a feature
228228
of the same name is not asked for it. `mcpp build`, the programs the tests run
229229
(`MCPP_BIN_<NAME>`) and every other consumer of the package keep its defaults.
230-
The tests and `mcpp build` share one build directory, so building one after the
231-
other compiles again what the features change, as alternating `--features` does.
230+
A test plan that asks a package for features this way, or through a
231+
dev-dependency entry that asks a package the manifest also depends on for
232+
features (or for none of its defaults), configures that package otherwise than
233+
`mcpp build`. It builds in a build directory of its own, so each configuration
234+
is compiled once and neither compiles over the other's module interfaces; a test
235+
plan that asks for nothing shares the build's directory (2026.11.17.1+; before,
236+
the two shared one directory and compiled each other's files again, mcpp#945).
232237

233238
### Where a test program begins
234239

@@ -445,7 +450,7 @@ A dev-dependency reaches the test units and nothing else of the package
445450
the files under `tests/`; the package's own sources are compiled under
446451
`mcpp test` with the commands `mcpp build` gives them, so the code under test is
447452
the code that ships, and `mcpp build` and `mcpp test` share one build directory
448-
and one compile database. A kernel interface that only a dev-dependency reaches
453+
and one compile database while the tests ask the package for no features. A kernel interface that only a dev-dependency reaches
449454
(`mcpplibs.mtest` over `mcpplibs.os` reaches openkal's) is given to the
450455
dev-dependencies and the test units alone: they see `__OPENKAL__` and openkal's
451456
headers, and the package's own sources do not. A C library or a C++ runtime that

‎docs/zh/08-testing.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ features = ["ktx2"]
166166

167167
测试规划在默认 feature 与 `--features` 之外打开它们,见于 `mcpp test`、`mcpp test -p <member>` 与
168168
`mcpp test --workspace`。在工作空间中,它们在该成员自己的边上请求,声明了同名 feature 的另一个成员不被请求。
169-
`mcpp build`、测试运行的程序(`MCPP_BIN_<NAME>`)以及包的其它消费者保持默认 feature。测试与 `mcpp build`
170-
共用一个构建目录,所以先后构建两者会重新编译 feature 改变的部分,与交替使用 `--features` 相同。
169+
`mcpp build`、测试运行的程序(`MCPP_BIN_<NAME>`)以及包的其它消费者保持默认 feature。以这种方式,或经 dev 依赖条目
170+
(向本清单同时依赖的包要求 feature,或不要其默认 feature)向包要求 feature 的测试规划,以不同于 `mcpp build` 的配置构建该包。
171+
它在自己的构建目录中构建,因此每种配置只编译一次,互不覆盖对方的模块接口;不要求任何 feature 的测试规划与构建共用目录
172+
(2026.11.17.1 起;此前二者共用一个目录、互相重新编译,mcpp#945)。
171173

172174
### 测试程序的起点
173175

@@ -328,7 +330,7 @@ counters = { path = "../counters" }
328330

329331
dev 依赖只到达测试单元,不到达包的其他部分(2026.11.8.1+)。它的包含目录与接口定义只给
330332
`tests/` 下的文件;包自己的源文件在 `mcpp test` 下以 `mcpp build` 给出的命令编译,因此被
331-
测试的代码就是发布的代码,`mcpp build` 与 `mcpp test` 共用一个构建目录和一份编译数据库。
333+
测试的代码就是发布的代码,在测试不向包要求 feature 时,`mcpp build` 与 `mcpp test` 共用一个构建目录和一份编译数据库。
332334
只有 dev 依赖到达的内核接口(`mcpplibs.mtest` 经 `mcpplibs.os` 到达 openkal 的接口)只给
333335
dev 依赖与测试单元:它们看到 `__OPENKAL__` 与 openkal 的头文件,包自己的源文件看不到。
334336
dev 依赖提供的 C 库或 C++ 运行时是测试所链接的每个目标文件的属性,仍属于整个配置。

‎src/build/build_program.cppm‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,48 @@ install_hook_env(const BuildProgramEnv& env) {
11221122
};
11231123
}
11241124

1125+
// A FILE A RUN WROTE AGAIN AS IT WAS KEEPS ITS TIME (mcpp#945). A build program
1126+
// commonly writes its outputs on every run, and it runs again whenever its
1127+
// cached answer is another configuration's: the run record is one per
1128+
// package, the output directory one per configuration. A generated source
1129+
// rewritten with its old content but a new time compiled its module and every
1130+
// importer again. Each regular file of the output directory is stamped before
1131+
// the run; one the run left with the same size and content gets its time back,
1132+
// so the build sees no change.
1133+
struct OutputStamp {
1134+
std::uintmax_t size = 0;
1135+
fs::file_time_type time;
1136+
std::string hash;
1137+
};
1138+
1139+
std::map<fs::path, OutputStamp> stamp_outputs(const fs::path& outDir) {
1140+
std::map<fs::path, OutputStamp> out;
1141+
std::error_code ec;
1142+
if (!fs::is_directory(outDir, ec)) return out;
1143+
for (fs::recursive_directory_iterator it(outDir, fs::directory_options::skip_permission_denied, ec), end;
1144+
!ec && it != end; it.increment(ec)) {
1145+
if (!it->is_regular_file(ec)) continue;
1146+
const auto size = it->file_size(ec);
1147+
// A large output (an archive, an image) is not read twice per run.
1148+
if (ec || size > (std::uintmax_t{64} << 20)) continue;
1149+
const auto time = it->last_write_time(ec);
1150+
if (ec) continue;
1151+
out.emplace(it->path(), OutputStamp{size, time, mcpp::toolchain::hash_file(it->path())});
1152+
}
1153+
return out;
1154+
}
1155+
1156+
void keep_unchanged_times(const std::map<fs::path, OutputStamp>& before) {
1157+
for (auto const& [path, was] : before) {
1158+
std::error_code ec;
1159+
const auto time = fs::last_write_time(path, ec);
1160+
if (ec || time == was.time) continue;
1161+
if (fs::file_size(path, ec) != was.size || ec) continue;
1162+
if (mcpp::toolchain::hash_file(path) != was.hash) continue;
1163+
fs::last_write_time(path, was.time, ec);
1164+
}
1165+
}
1166+
11251167
std::expected<void, std::string> run_build_program_impl(
11261168
mcpp::manifest::Manifest& m,
11271169
const fs::path& root,
@@ -1278,8 +1320,10 @@ std::expected<void, std::string> run_build_program_impl(
12781320
}
12791321
if (!placed) argv.push_back(bin.string());
12801322
}
1323+
const auto stamps = stamp_outputs(outDir);
12811324
auto rres = mcpp::run::capture_exec_deadline(
12821325
argv, childEnv, deadline, &timedOut, root.string());
1326+
if (!timedOut && rres.exit_code == 0) keep_unchanged_times(stamps);
12831327
if (timedOut) {
12841328
// Name the manifest to edit. Without this the user edits their OWN
12851329
// mcpp.toml when a dependency's build program is what timed out, and

‎src/build/prepare/scan.cpp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,49 @@ static void step11_compute_fingerprint(PrepareState& state) {
10091009
fpi.compileFlags += " #accel=";
10101010
fpi.compileFlags += accel;
10111011
}
1012+
// THE TESTS' CONFIGURATION (mcpp#945). A test plan configures a package
1013+
// otherwise than `mcpp build` when the tests ask it for features: the
1014+
// package's `[test] features`, or a dev-dependency entry that asks a
1015+
// package the manifest also depends on for features (or for none of its
1016+
// defaults). The module interfaces and objects of a build directory are
1017+
// named by module and package, not by configuration, so two
1018+
// configurations in one directory compile each other's files again --
1019+
// three compiles of one module in a workspace run, and on Windows a
1020+
// rewrite of an interface another compile had open. Such a test plan
1021+
// builds in a directory of its own; one that asks for nothing shares the
1022+
// build's, which the tests' programs and `mcpp build` reuse.
1023+
if (state.includeDevDeps) {
1024+
std::vector<std::string> asks;
1025+
auto add = [&](const mcpp::manifest::Manifest& m) {
1026+
const auto who = m.package.namespace_.empty() ? m.package.name
1027+
: m.package.namespace_ + "." + m.package.name;
1028+
for (auto const& f : m.testFeatures) asks.push_back(who + "[test]" + f);
1029+
for (auto const& [key, dev] : m.devDependencies) {
1030+
auto dep = m.dependencies.find(key);
1031+
if (dep == m.dependencies.end()) continue;
1032+
std::vector<std::string> extra;
1033+
for (auto const& f : dev.features)
1034+
if (std::ranges::find(dep->second.features, f) == dep->second.features.end())
1035+
extra.push_back(f);
1036+
const bool fewerDefaults = !dev.defaultFeatures && dep->second.defaultFeatures;
1037+
if (extra.empty() && !fewerDefaults) continue;
1038+
std::ranges::sort(extra);
1039+
std::string w = who + ">" + key + ":";
1040+
for (auto const& f : extra) w += f + ",";
1041+
if (fewerDefaults) w += "-default";
1042+
asks.push_back(std::move(w));
1043+
}
1044+
};
1045+
if (state.workspacePlan())
1046+
for (auto const& [dir, member] : state.selectedMemberManifests) add(member);
1047+
else
1048+
add(*state.m);
1049+
if (!asks.empty()) {
1050+
std::ranges::sort(asks);
1051+
fpi.compileFlags += " #test=";
1052+
for (auto const& a : asks) { fpi.compileFlags += a; fpi.compileFlags += ';'; }
1053+
}
1054+
}
10121055
if (state.m->cppStandard.experimental) {
10131056
// c++fly gate flags are derived (not manifest-declared): fold them in
10141057
// so a cppfly table change across mcpp versions re-fingerprints.

‎tests/e2e/test/workspace.cpp‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,54 @@ int main() {
412412
expect(eq(gen_edges(), 1)) << "an unchanged shared module was compiled again";
413413
};
414414

415+
"1107 a member planned alone compiles a shared module once"_test[s::gcc] = [] {
416+
// mcpp#945: a member in a group of its own (its `[indices]` differ) whose test asks a
417+
// shared package for a feature through a dev-dependency. The tests' configuration of the
418+
// package is not the build's, so the test plan builds in a directory of its own: the
419+
// package's module -- one its build program writes on every run -- is compiled once per
420+
// configuration, and not again by an unchanged run, a member's tests or `mcpp build`.
421+
// Compiled over each other in one directory, it was compiled three times a run, and on
422+
// Windows rewritten while another compile had it open.
423+
s::project ws{{"mcpp.toml", "[workspace]\nmembers = [\"core\", \"app\"]\n"},
424+
{"core/mcpp.toml", "[package]\nname = \"core\"\nversion = \"0.1.0\"\n\n[features]\nextra = []\n\n[targets.core]\nkind = \"lib\"\n"},
425+
{"core/src/core.cppm", "export module s1107_core;\nexport import s1107_core.gen;\nexport int core_v() { return gen_v(); }\n"},
426+
{"core/build.mcpp", R"(import std;
427+
import mcpp;
428+
int main() {
429+
const std::string p = std::string(mcpp::out_dir()) + "/gen.cppm";
430+
std::ofstream(p) << "export module s1107_core.gen;\nexport int gen_v() { return 1; }\n";
431+
std::printf("mcpp:generated=%s\n", p.c_str());
432+
return 0;
433+
}
434+
)"},
435+
{"app/mcpp.toml", "[package]\nname = \"app\"\nversion = \"0.1.0\"\n\n[indices]\nlocal = { path = \"idx\" }\n\n"
436+
"[dependencies]\ncore = { path = \"../core\" }\n\n[dev-dependencies]\ncore = { path = \"../core\", features = [\"extra\"] }\n\n"
437+
"[targets.app]\nkind = \"bin\"\nmain = \"src/main.cpp\"\n"},
438+
{"app/idx/pkgs/.keep", ""},
439+
{"app/src/main.cpp", "import s1107_core;\nint main() { return core_v() == 1 ? 0 : 1; }\n"},
440+
{"app/tests/main.cpp", "import std;\nint main() { return std::getenv(\"MCPP_BIN_APP\") ? 0 : 1; }\n"}};
441+
auto gen_edges = [&] {
442+
std::ptrdiff_t n = 0;
443+
for (const auto& d : graph_dirs(ws))
444+
n += std::ranges::count_if(s::ninja_log_outputs(d), [](const std::string& o) {
445+
return std::regex_search(o, std::regex(R"(/core/gen\.m\.o$)"));
446+
});
447+
return n;
448+
};
449+
auto first = ws.mcpp({"test", "--workspace"});
450+
expect(fatal(e2e::succeeded(first))) << s::text(first);
451+
expect(eq(gen_edges(), 2)) << "the shared module was not compiled once per configuration: " << s::text(first);
452+
expect(eq(graph_dirs(ws).size(), std::size_t{2})) << "the tests' configuration has no directory of its own";
453+
expect(fatal(e2e::succeeded(ws.mcpp({"test", "--workspace"}))));
454+
expect(eq(gen_edges(), 2)) << "an unchanged shared module was compiled again";
455+
// One member's tests (`-p`) and the build take the same directories: nothing again.
456+
expect(fatal(e2e::succeeded(ws.mcpp({"test", "-p", "app"}))));
457+
expect(fatal(e2e::succeeded(ws.mcpp({"test", "-p", "app"}))));
458+
expect(eq(gen_edges(), 2)) << "one member's unchanged tests compiled the shared module again";
459+
expect(fatal(e2e::succeeded(ws.mcpp({"build"}))));
460+
expect(eq(gen_edges(), 2)) << "mcpp build after the tests compiled the shared module again";
461+
};
462+
415463
"1095 the guide at a workspace root plans nothing"_test[s::gcc] = [] {
416464
// #932: the guide reads the frameworks from the resolved graph -- every member's -- and
417465
// plans, scans and builds nothing.

0 commit comments

Comments
 (0)