2026-07-31 11:11:49 +00:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
|
|
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
|
|
|
|
|
|
import std;
|
|
|
|
|
import Crafter.Build;
|
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
using namespace Crafter;
|
|
|
|
|
|
|
|
|
|
// Editing a header must rebuild everything that #includes it. The staleness
|
|
|
|
|
// check used to compare an artifact against its own source and its module
|
|
|
|
|
// imports only — and the module scanner reads `import` lines, so a header was
|
|
|
|
|
// invisible to it. A build after a header-only edit reported nothing to do and
|
|
|
|
|
// left objects compiled against the previous contents: the same silent
|
|
|
|
|
// mixed-layout binary as issue #27, reached through #include instead.
|
|
|
|
|
//
|
|
|
|
|
// Each pass below edits exactly one header and asserts both directions — the
|
|
|
|
|
// objects that include it are recompiled, the ones that don't are left alone —
|
|
|
|
|
// then runs the binary, because "was recompiled" is only interesting if the
|
|
|
|
|
// resulting program agrees with itself.
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
std::int32_t Failures = 0;
|
|
|
|
|
|
|
|
|
|
void Check(bool cond, std::string_view msg) {
|
|
|
|
|
if (!cond) {
|
|
|
|
|
std::println(std::cerr, "FAIL: {}", msg);
|
|
|
|
|
++Failures;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The fixture is mutated during the run, so work on a copy outside the repo.
|
|
|
|
|
fs::path StageFixture() {
|
|
|
|
|
fs::path source = fs::current_path() / "tests" / "IncrementalHeaderChange" / "fixture";
|
|
|
|
|
fs::path staged = fs::temp_directory_path() / "crafter-build-incremental-header-change";
|
|
|
|
|
fs::remove_all(staged);
|
|
|
|
|
fs::copy(source, staged, fs::copy_options::recursive);
|
|
|
|
|
return staged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Swap <name>-grown.h.in in for <name>.h. The replacement text lives in a
|
|
|
|
|
// file rather than a string literal so the header the fixture #includes is
|
|
|
|
|
// the only one under that name, whichever variant is in place.
|
|
|
|
|
void Grow(const fs::path& header) {
|
|
|
|
|
fs::path grown = header.parent_path() / std::format("{}-grown.h.in", header.stem().string());
|
|
|
|
|
fs::copy_file(grown, header, fs::copy_options::overwrite_existing);
|
|
|
|
|
// copy_file carries the source's mtime across, which would leave the
|
|
|
|
|
// rewritten header looking older than the objects built from it.
|
|
|
|
|
fs::last_write_time(header, fs::file_time_type::clock::now());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<Configuration> MakeLib(const fs::path& staged) {
|
|
|
|
|
auto lib = std::make_unique<Configuration>();
|
|
|
|
|
lib->path = staged / "lib";
|
|
|
|
|
lib->name = "widget";
|
|
|
|
|
lib->outputName = "widget";
|
|
|
|
|
lib->target = HostTarget();
|
|
|
|
|
lib->type = ConfigurationType::LibraryStatic;
|
|
|
|
|
std::array<fs::path, 1> ifaces = { "Widget" };
|
|
|
|
|
std::array<fs::path, 1> impls = { "Widget" };
|
|
|
|
|
lib->GetInterfacesAndImplementations(ifaces, impls);
|
|
|
|
|
// cFiles are resolved against the cwd at build time, so spell it out.
|
|
|
|
|
lib->cFiles = { staged / "lib" / "counter" };
|
|
|
|
|
return lib;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Configuration MakeApp(const fs::path& staged, Configuration* lib) {
|
|
|
|
|
Configuration app;
|
|
|
|
|
app.path = staged;
|
|
|
|
|
app.name = "widget-app";
|
|
|
|
|
app.outputName = "widget-app";
|
|
|
|
|
app.target = HostTarget();
|
|
|
|
|
app.type = ConfigurationType::Executable;
|
|
|
|
|
std::array<fs::path, 0> ifaces = {};
|
|
|
|
|
std::array<fs::path, 1> impls = { "main" };
|
|
|
|
|
app.GetInterfacesAndImplementations(ifaces, impls);
|
|
|
|
|
app.dependencies = { lib };
|
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BuildOk(Configuration& app, std::string_view label) {
|
|
|
|
|
// A fresh depResults per pass: the map memoizes each Configuration's
|
|
|
|
|
// build for the duration of one pass, so reusing it would skip the
|
|
|
|
|
// library's second build entirely.
|
|
|
|
|
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
|
|
|
|
std::mutex depMutex;
|
|
|
|
|
BuildResult r = Build(app, depResults, depMutex);
|
|
|
|
|
if (!r.result.empty()) {
|
|
|
|
|
std::println(std::cerr, "FAIL: {} build failed: {}", label, r.result);
|
|
|
|
|
++Failures;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// "<sizeof in the consumer> <sizeof in the library> <count> <limit>" — see
|
|
|
|
|
// the fixture's main.cpp.
|
|
|
|
|
void CheckRun(const fs::path& binary, std::string_view expected, std::string_view label) {
|
|
|
|
|
auto r = RunCommandWithTimeout(binary.string(), std::chrono::seconds(30));
|
|
|
|
|
Check(r.exitCode == 0 && !r.crashed && !r.timedOut && r.output == expected, std::format("{}: expected '{}', got '{}' (exit={})", label, expected, r.output, r.exitCode));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
fs::path staged = StageFixture();
|
|
|
|
|
std::unique_ptr<Configuration> lib = MakeLib(staged);
|
|
|
|
|
Configuration app = MakeApp(staged, lib.get());
|
|
|
|
|
|
|
|
|
|
fs::path binary = app.BinDir() / "widget-app";
|
|
|
|
|
// One artifact per compile path a header can reach: the module interface
|
|
|
|
|
// (BMI plus the object made from it), the module's implementation unit, a C
|
|
|
|
|
// source, and the consumer that imports the module.
|
|
|
|
|
fs::path interfacePcm = lib->PcmDir() / "Widget.pcm";
|
|
|
|
|
fs::path interfaceObject = lib->BuildDir() / "Widget.o";
|
fix(build): name a source's object from its path
The object of an implementation unit was named from its file's name
alone, <build dir>/<name>_impl.o, and that of a C or CUDA source
likewise, <name>_source.o. Two sources of one configuration with the
same file name in different directories were therefore compiled into
one object, each over the other, with one dependency file between
them, and the link was given that object twice (issue #50). A build of
an a/Thing.cpp and a b/Thing.cpp that each define a function failed at
the link, with duplicate symbols of one of the two and no word of the
other. A debug build of two such files that define nothing another
file names succeeded, and its program had one file's code twice and
the other's not at all. A .c and a .cu of one name shared an object in
one directory as well.
An object, with its dependency file beside it, is now named from where
its source lies: under <build dir>/objects by the source's path from
the configuration's directory, objects/a/Thing_impl.o for a/Thing.cpp,
or, for a source outside that directory, under
<build dir>/objects-outside by its whole path. A .cu's object ends in
_cuda.o; a .cpp's still ends in _impl.o and a .c's in _source.o. Two
sources then have one object only when their paths are one once . and
.. are taken out of them: any other two differ by path or by ending in
one of the two directories, and the two directories share nothing,
whatever a project calls its own directories. They are directories of
their own, and not the build directory itself, because that one holds
files named after other things: an interface unit's object, the record
of the link and the list of its objects and, for an executable, the
BMIs.
ObjectPath gives that name, exported beside ExpectedOutput for what
looks at an object from outside Build(). Build() names each object by
the same rule, once, as it goes over the sources, and the list it
keeps of them for the link is what the check whether an object is
newer than the output reads as well. Before, each put the name
together for itself: Implementation::Check, Implementation::Schedule,
and Build() where it compiles C and CUDA sources, lists the link's
objects and checks whether to link again. Implementation::Check and
Schedule are given the object, where they put its name together from
the build directory. The job that compiles a source makes the
directory of its object first, since the compiler makes none. The C
compile quotes its source, dependency file and object for the shell,
as the CUDA compile does: the object of a source outside the
configuration's directory holds that source's whole path, and so
directories the command did not name before, the working directory's
among them where the source is listed relative to it.
The progress line of the compile of a .cpp, .c or .cu names the source
by the path its object is named from, "Compiling a/Thing.cpp" where it
was "Compiling Thing.cpp" for both, and so does the error for a .cu
without an nvcc.
What else changes: every implementation unit and every C and CUDA
source has its object in a new place, so an existing build directory
compiles them once more. The objects under the old names stay where
they are, unused; a static library's archive does not keep them, being
written anew. Interface units are as they were: a BMI is found by its
module's name, so two interface files of one name would have to be one
module. A source that a configuration lists twice is also as it was,
compiled twice into its one object, which the link is given twice.
In IncrementalHeaderChange, IncrementalInterfaceChange, StdPcmRebuild
and TransitiveInterfaceChange, the lines that spelled out an object's
old name take it from ObjectPath.
Fixes #50
2026-11-19 11:02:39 +01:00
|
|
|
fs::path libraryObject = ObjectPath(*lib, staged / "lib" / "Widget.cpp");
|
|
|
|
|
fs::path counterObject = ObjectPath(*lib, staged / "lib" / "counter.c");
|
|
|
|
|
fs::path consumerObject = ObjectPath(app, staged / "main.cpp");
|
2026-07-31 11:11:49 +00:00
|
|
|
|
|
|
|
|
if (!BuildOk(app, "first pass")) {
|
|
|
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
CheckRun(binary, "8 8 1 5", "first pass");
|
|
|
|
|
|
|
|
|
|
auto stamps = [&]() {
|
|
|
|
|
return std::array<fs::file_time_type, 5>{
|
|
|
|
|
fs::last_write_time(interfacePcm),
|
|
|
|
|
fs::last_write_time(interfaceObject),
|
|
|
|
|
fs::last_write_time(libraryObject),
|
|
|
|
|
fs::last_write_time(counterObject),
|
|
|
|
|
fs::last_write_time(consumerObject),
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Nothing changed: the depfiles must not read as staleness of their own,
|
|
|
|
|
// or every build would recompile the world.
|
|
|
|
|
{
|
|
|
|
|
std::array<fs::file_time_type, 5> before = stamps();
|
|
|
|
|
if (BuildOk(app, "idle pass")) {
|
|
|
|
|
Check(stamps() == before, "an idle rebuild recompiles nothing");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A header only the module's implementation unit includes. Its name carries
|
|
|
|
|
// a space, so the depfile spells it escaped.
|
|
|
|
|
{
|
|
|
|
|
std::array<fs::file_time_type, 5> before = stamps();
|
|
|
|
|
Grow(staged / "lib" / "widget count.h");
|
|
|
|
|
if (BuildOk(app, "implementation header pass")) {
|
|
|
|
|
std::array<fs::file_time_type, 5> after = stamps();
|
|
|
|
|
Check(after[2] > before[2], "implementation object is recompiled after a header it includes changes");
|
|
|
|
|
Check(after[0] == before[0], "interface BMI is left alone by a header it does not include");
|
|
|
|
|
Check(after[3] == before[3], "C object is left alone by a header it does not include");
|
|
|
|
|
Check(after[4] == before[4], "consumer object is left alone by a header it does not include");
|
|
|
|
|
CheckRun(binary, "8 8 2 5", "implementation header pass");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A header only the C source includes.
|
|
|
|
|
{
|
|
|
|
|
std::array<fs::file_time_type, 5> before = stamps();
|
|
|
|
|
Grow(staged / "lib" / "counter-limit.h");
|
|
|
|
|
if (BuildOk(app, "C header pass")) {
|
|
|
|
|
std::array<fs::file_time_type, 5> after = stamps();
|
|
|
|
|
Check(after[3] > before[3], "C object is recompiled after a header it includes changes");
|
|
|
|
|
Check(after[2] == before[2], "implementation object is left alone by a header it does not include");
|
|
|
|
|
CheckRun(binary, "8 8 2 9", "C header pass");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A header the interface unit includes, changing the layout of an exported
|
|
|
|
|
// class: the BMI and everything compiled against it has to follow.
|
|
|
|
|
{
|
|
|
|
|
std::array<fs::file_time_type, 5> before = stamps();
|
|
|
|
|
Grow(staged / "lib" / "widget-layout.h");
|
|
|
|
|
if (BuildOk(app, "interface header pass")) {
|
|
|
|
|
std::array<fs::file_time_type, 5> after = stamps();
|
|
|
|
|
Check(after[0] > before[0], "interface BMI is rebuilt after a header it includes changes");
|
|
|
|
|
Check(after[1] > before[1], "interface object is rebuilt after a header it includes changes");
|
|
|
|
|
Check(after[2] > before[2], "implementation object follows the rebuilt BMI");
|
|
|
|
|
Check(after[4] > before[4], "consumer object follows the rebuilt BMI");
|
|
|
|
|
CheckRun(binary, "16 16 2 9", "interface header pass");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// An object built before depfiles were emitted at all — an upgrade over an
|
|
|
|
|
// existing build directory — has no record of what it included, so the one
|
|
|
|
|
// safe reading is "rebuild it once".
|
|
|
|
|
{
|
|
|
|
|
std::array<fs::file_time_type, 5> before = stamps();
|
fix(build): name a source's object from its path
The object of an implementation unit was named from its file's name
alone, <build dir>/<name>_impl.o, and that of a C or CUDA source
likewise, <name>_source.o. Two sources of one configuration with the
same file name in different directories were therefore compiled into
one object, each over the other, with one dependency file between
them, and the link was given that object twice (issue #50). A build of
an a/Thing.cpp and a b/Thing.cpp that each define a function failed at
the link, with duplicate symbols of one of the two and no word of the
other. A debug build of two such files that define nothing another
file names succeeded, and its program had one file's code twice and
the other's not at all. A .c and a .cu of one name shared an object in
one directory as well.
An object, with its dependency file beside it, is now named from where
its source lies: under <build dir>/objects by the source's path from
the configuration's directory, objects/a/Thing_impl.o for a/Thing.cpp,
or, for a source outside that directory, under
<build dir>/objects-outside by its whole path. A .cu's object ends in
_cuda.o; a .cpp's still ends in _impl.o and a .c's in _source.o. Two
sources then have one object only when their paths are one once . and
.. are taken out of them: any other two differ by path or by ending in
one of the two directories, and the two directories share nothing,
whatever a project calls its own directories. They are directories of
their own, and not the build directory itself, because that one holds
files named after other things: an interface unit's object, the record
of the link and the list of its objects and, for an executable, the
BMIs.
ObjectPath gives that name, exported beside ExpectedOutput for what
looks at an object from outside Build(). Build() names each object by
the same rule, once, as it goes over the sources, and the list it
keeps of them for the link is what the check whether an object is
newer than the output reads as well. Before, each put the name
together for itself: Implementation::Check, Implementation::Schedule,
and Build() where it compiles C and CUDA sources, lists the link's
objects and checks whether to link again. Implementation::Check and
Schedule are given the object, where they put its name together from
the build directory. The job that compiles a source makes the
directory of its object first, since the compiler makes none. The C
compile quotes its source, dependency file and object for the shell,
as the CUDA compile does: the object of a source outside the
configuration's directory holds that source's whole path, and so
directories the command did not name before, the working directory's
among them where the source is listed relative to it.
The progress line of the compile of a .cpp, .c or .cu names the source
by the path its object is named from, "Compiling a/Thing.cpp" where it
was "Compiling Thing.cpp" for both, and so does the error for a .cu
without an nvcc.
What else changes: every implementation unit and every C and CUDA
source has its object in a new place, so an existing build directory
compiles them once more. The objects under the old names stay where
they are, unused; a static library's archive does not keep them, being
written anew. Interface units are as they were: a BMI is found by its
module's name, so two interface files of one name would have to be one
module. A source that a configuration lists twice is also as it was,
compiled twice into its one object, which the link is given twice.
In IncrementalHeaderChange, IncrementalInterfaceChange, StdPcmRebuild
and TransitiveInterfaceChange, the lines that spelled out an object's
old name take it from ObjectPath.
Fixes #50
2026-11-19 11:02:39 +01:00
|
|
|
fs::remove(fs::path(libraryObject) += ".d");
|
2026-07-31 11:11:49 +00:00
|
|
|
if (BuildOk(app, "missing depfile pass")) {
|
|
|
|
|
std::array<fs::file_time_type, 5> after = stamps();
|
|
|
|
|
Check(after[2] > before[2], "an object whose dependency record is missing is rebuilt");
|
|
|
|
|
Check(after[3] == before[3], "the objects that still have one are left alone");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Failures > 0) {
|
|
|
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|