Crafter.Build/tests/StdPcmRebuild/main.cpp

85 lines
3.6 KiB
C++
Raw Permalink Normal View History

// 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;
// A std module rebuilt in place has to recompile the units that import it.
// The std PCM is rebuilt in its directory whenever its source changes under
// the same compiler and flags: a libc++ update, or mingw-w64's under the
// same prefix. A .cpp unit's depfile listed only its source and headers,
// because the driver records imported BMIs for --precompile alone, so such
// a unit kept its object and the next build only relinked it.
//
// The project is one main.cpp that imports std. Its object is dated before
// std.pcm and after its source, as a std module rebuilt after the object
// leaves them, and the next build has to compile it again. XDG_CACHE_HOME keeps the std PCM out of the real
// cache, whose other users the change of date would rebuild.
extern "C" int setenv(const char* name, const char* value, int overwrite);
namespace {
std::int32_t failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++failures;
}
}
BuildResult BuildOnce(Configuration& cfg) {
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
return Build(cfg, depResults, depMutex);
}
}
int main() {
const fs::path scratch = fs::temp_directory_path() / "crafter-build-std-pcm-rebuild";
std::error_code ec;
fs::remove_all(scratch, ec);
fs::create_directories(scratch);
const fs::path project = scratch / "project";
fs::copy(fs::current_path() / "tests" / "StdPcmRebuild" / "fixture", project, fs::copy_options::recursive);
setenv("XDG_CACHE_HOME", (scratch / "cache").c_str(), 1);
Configuration cfg;
cfg.path = project;
cfg.name = "rebuilt";
cfg.outputName = "rebuilt";
cfg.type = ConfigurationType::Executable;
std::array<fs::path, 0> ifaces = {};
std::array<fs::path, 1> impls = { "main" };
cfg.GetInterfacesAndImplementations(ifaces, impls);
BuildResult built = BuildOnce(cfg);
Check(built.result.empty(), std::format("the project builds: {}", built.result));
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
const fs::path object = ObjectPath(cfg, project / "main.cpp");
const fs::path stdPcm = StdPcmDir(cfg) / "std.pcm";
Check(fs::exists(object) && fs::exists(stdPcm), std::format("{} and {} exist", object.string(), stdPcm.string()));
if (!fs::exists(object) || !fs::exists(stdPcm)) return 1;
const fs::file_time_type first = fs::last_write_time(object);
built = BuildOnce(cfg);
Check(built.result.empty(), std::format("an idle build succeeds: {}", built.result));
Check(fs::last_write_time(object) == first, "and compiles nothing again");
// The source before the object and the object before std.pcm, so that
// nothing but the std module makes the object stale.
const fs::file_time_type older = fs::last_write_time(stdPcm) - std::chrono::seconds(10);
fs::last_write_time(project / "main.cpp", older - std::chrono::seconds(10));
fs::last_write_time(object, older);
built = BuildOnce(cfg);
Check(built.result.empty(), std::format("the build after the std module succeeds: {}", built.result));
Check(fs::last_write_time(object) > older, "a unit importing a std module newer than its object is compiled again");
fs::remove_all(scratch, ec);
if (failures > 0) {
std::println(std::cerr, "{} assertions failed", failures);
return 1;
}
return 0;
}