// 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; // An interface unit takes two compiler runs: one writes its BMI, the other // the object made from the BMI. Whether a unit is stale is read off the BMI, // so a unit left with a current BMI and the object of its source before an // edit, or no object at all, was called built by every build after: the link // took the old object without a word, or failed on the missing file until the // source was touched. A build ends between the two runs when it is // interrupted, when the second run fails, and, with the two as jobs of their // own, when another step fails first and keeps the second from starting. // // The fixture is a module with a partition and a program that prints what // the module's function returns. Built without optimisation, so the program // gets that function from the unit's object alone and what it prints says // which object was linked. First the primary interface is edited and, once // that is built, its object put back as it was before the edit, dated before // the new BMI: once with the program still there, which has to be linked // again, and once with the program deleted, so that it is linked from // whatever object is there. Then both objects and the program are deleted. A // last build of the intact tree has to leave the objects alone. 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 edited during the run, so work on a copy outside the repo. fs::path StageFixture() { fs::path source = fs::current_path() / "tests" / "IncrementalInterfaceObject" / "fixture"; fs::path staged = fs::temp_directory_path() / "crafter-build-incremental-interface-object"; fs::remove_all(staged); fs::copy(source, staged, fs::copy_options::recursive); // An earlier run in the checkout may have left these; the copy starts cold. fs::remove_all(staged / "bin"); fs::remove_all(staged / "build"); return staged; } bool BuildOk(Configuration& app, std::string_view label) { // A fresh depResults per pass, so every pass checks everything again. std::unordered_map> depResults; std::mutex depMutex; const BuildResult built = Build(app, depResults, depMutex); Check(built.result.empty(), std::format("{}: the build failed: {}", label, built.result)); return built.result.empty(); } std::string Printed(const Configuration& app) { const auto run = RunCommandWithTimeout(ShellQuote(ExpectedOutput(app).string()), std::chrono::seconds(30)); return run.exitCode == 0 ? run.output : std::format("", run.exitCode, run.output); } } int main() { const fs::path staged = StageFixture(); Configuration app; app.path = staged; app.name = "value-app"; app.outputName = "value-app"; app.target = HostTarget(); app.type = ConfigurationType::Executable; app.debug = true; std::array interfaces = { "Value", "Value-Part" }; std::array implementations = { "main" }; app.GetInterfacesAndImplementations(interfaces, implementations); const fs::path primaryPcm = app.PcmDir() / "Value.pcm"; const fs::path primaryObject = app.BuildDir() / "Value.o"; const fs::path partitionObject = app.BuildDir() / "Value-Part.o"; const fs::path program = ExpectedOutput(app); if (!BuildOk(app, "the first build")) return 1; Check(Printed(app) == "11", std::format("the first build's program prints 11, got '{}'", Printed(app))); if (!fs::exists(primaryObject) || !fs::exists(partitionObject) || !fs::exists(primaryPcm)) { std::println(std::cerr, "FAIL: the first build left no {}, {} or {}", primaryObject.string(), partitionObject.string(), primaryPcm.string()); return 1; } const fs::path oldObject = staged / "Value.o.before-edit"; fs::copy_file(primaryObject, oldObject, fs::copy_options::overwrite_existing); // After an edit, the BMI of the new source next to the object of the old // one: what a build that ends between the two runs leaves of a unit that // was built before. fs::copy_file(staged / "Value-edited.cppm.in", staged / "Value.cppm", fs::copy_options::overwrite_existing); if (!BuildOk(app, "the build after the edit")) return 1; Check(Printed(app) == "12", std::format("the edited program prints 12, got '{}'", Printed(app))); const auto putOldObjectBack = [&] { fs::copy_file(oldObject, primaryObject, fs::copy_options::overwrite_existing); fs::last_write_time(primaryObject, fs::last_write_time(primaryPcm) - std::chrono::seconds(2)); }; // With the program the last build linked still there, as a build that // failed leaves it: nothing but the object made again is newer than it. putOldObjectBack(); if (BuildOk(app, "the build with the object of before the edit")) { Check(fs::last_write_time(primaryObject) >= fs::last_write_time(primaryPcm), "an object older than its BMI is made again"); Check(fs::last_write_time(program) >= fs::last_write_time(primaryObject), "the program is linked again from the object made again"); Check(Printed(app) == "12", std::format("the program linked again prints 12, got '{}'", Printed(app))); } // And with the program gone, so that the link takes whatever object is // there. putOldObjectBack(); fs::remove(program); if (BuildOk(app, "the build with the object of before the edit and no program")) { Check(fs::last_write_time(primaryObject) >= fs::last_write_time(primaryPcm), "an object older than its BMI is made again, with no program either"); Check(Printed(app) == "12", std::format("the program is linked from the object of the edited source and prints 12, got '{}'", Printed(app))); } // BMIs and no objects: what such a build leaves of units it built for // the first time. fs::remove(primaryObject); fs::remove(partitionObject); fs::remove(program); if (BuildOk(app, "the build after the objects went missing")) { Check(fs::exists(primaryObject) && fs::exists(partitionObject), "a unit with a BMI and no object gets its object back"); Check(Printed(app) == "12", std::format("the program linked from the restored objects prints 12, got '{}'", Printed(app))); } // What follows reads the objects the builds above should have left. if (Failures != 0) return 1; // With every object newer than its BMI there is nothing to make again. const fs::file_time_type primaryTime = fs::last_write_time(primaryObject); const fs::file_time_type partitionTime = fs::last_write_time(partitionObject); if (BuildOk(app, "the build of the intact tree")) { Check(fs::last_write_time(primaryObject) == primaryTime && fs::last_write_time(partitionObject) == partitionTime, "objects that are newer than their BMIs are left as they are"); } return Failures == 0 ? 0 : 1; }