// 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 static library holds the objects of the sources its configuration lists // and no others. Its archive used to be updated where it lay: `llvm-ar rcs` // into an archive that is there replaces the members it is given and leaves // every other member as it is, so the object of a source that had left the // project stayed in the library. The link takes a symbol from the first // member that defines it, and a member left behind comes before the ones // added after it: once a source had been renamed, whatever was built on the // library went on linking the object of the file under its old name. // // The fixture's library has three sources. lib/first.cpp and lib/second.cpp // each define Value(), returning 1 and 2, and lib/extra.cpp defines a // function nothing calls. The library is built three times, by // configurations of one name and directory, so into one archive: from the // first and the extra source; from the first alone, the extra one taken out // of the project and nothing else changed; and from the second in the // first's place, as the project of a renamed source lists it. Each time the // archive has to hold the objects of the sources listed and no other, and // the program built on the library to print what the Value() listed returns. // // The archive also has to be in its place for as long as it is written // anew: a library that another project is built on may be linked by that // project's crafter-build at any moment, where both build it in one clone // in the cache. While the second and the third build run, a thread looks // for the archive again and again. namespace { std::int32_t Failures = 0; void Check(bool cond, std::string_view msg) { if (!cond) { std::println(std::cerr, "FAIL: {}", msg); ++Failures; } } // Built into the fixture's own bin/ and build/, which an earlier run // would have left with the last archive in them: a copy that starts // with neither. fs::path StageFixture() { const fs::path source = fs::current_path() / "tests" / "StaticLibSourceRemoved" / "fixture"; const fs::path staged = fs::temp_directory_path() / "crafter-build-static-lib-source-removed"; fs::remove_all(staged); fs::copy(source, staged, fs::copy_options::recursive); return staged; } // The library, of the lib/.cpp listed. One name and one // directory whichever sources it lists, so one build directory and one // archive: the same library before and after a source leaves it. std::unique_ptr MakeLib(const fs::path& staged, std::vector sources) { std::unique_ptr lib = std::make_unique(); lib->path = staged / "lib"; lib->name = "value"; lib->outputName = "value"; lib->target = HostTarget(); lib->type = ConfigurationType::LibraryStatic; std::array ifaces = {}; lib->GetInterfacesAndImplementations(ifaces, sources); return lib; } Configuration MakeApp(const fs::path& staged, Configuration* lib) { Configuration app; app.path = staged; app.name = "value-app"; app.outputName = "value-app"; app.target = HostTarget(); app.type = ConfigurationType::Executable; std::array ifaces = {}; std::array impls = { "main" }; app.GetInterfacesAndImplementations(ifaces, impls); app.dependencies = { lib }; return app; } bool BuildOk(Configuration& app, std::string_view label) { // A fresh record for each pass: one that already holds the library's // build would hand it to the next pass unbuilt. std::unordered_map> depResults; std::mutex depMutex; const BuildResult r = Build(app, depResults, depMutex); if (!r.result.empty()) { std::println(std::cerr, "FAIL: {} failed: {}", label, r.result); ++Failures; return false; } return true; } // Builds `app` as BuildOk does, with a thread looking for `archive` from // before the build starts until it has ended: whether the build // succeeded and the archive was there every time. bool BuildOkWithArchiveInPlace(Configuration& app, const fs::path& archive, std::string_view label) { std::atomic missing{false}; bool built = false; { std::jthread watcher([&archive, &missing](std::stop_token stop) { while (!stop.stop_requested()) { std::error_code ec; if (!fs::exists(archive, ec)) missing = true; } }); built = BuildOk(app, label); // Leaving the scope stops the watcher and waits for it. } Check(!missing, std::format("{}: the archive is in its place for as long as the build writes it anew", label)); return built; } void CheckPrints(const Configuration& app, std::string_view expected, std::string_view label) { const auto r = RunCommandWithTimeout(ShellQuote(ExpectedOutput(app).string()), std::chrono::seconds(30)); Check(r.exitCode == 0 && r.output == expected, std::format("{}: the program printed '{}' (exit {}), expected '{}'", label, r.output, r.exitCode, expected)); } // The members of `lib`'s archive, as `llvm-ar t` lists them, have to be // `expected`, one to a line. void CheckMembers(const Configuration& lib, std::string_view expected, std::string_view label) { const auto members = RunCommandWithTimeout(std::format("{} t {}", ShellQuote(GetToolchain().llvmAr.string()), ShellQuote(ExpectedOutput(lib).string())), std::chrono::seconds(30)); Check(members.exitCode == 0 && members.output == expected, std::format("{}: llvm-ar t lists '{}' (exit {}), expected '{}'", label, members.output, members.exitCode, expected)); } } int main() { const fs::path staged = StageFixture(); const std::unique_ptr both = MakeLib(staged, { "first", "extra" }); Configuration withExtra = MakeApp(staged, both.get()); if (!BuildOk(withExtra, "the build with lib/first.cpp and lib/extra.cpp")) return 1; CheckPrints(withExtra, "1", "built with lib/first.cpp and lib/extra.cpp"); CheckMembers(*both, "first_impl.o\nextra_impl.o\n", "the archive of lib/first.cpp and lib/extra.cpp"); // What the passes below rest on: were the configurations built into // directories of their own, each archive would be made from nothing and // the later passes would prove nothing. const fs::path archive = ExpectedOutput(*both); // No source is compiled for this one and no object is newer than the // archive: that the library lists one object fewer is all that changed. const std::unique_ptr alone = MakeLib(staged, { "first" }); Check(ExpectedOutput(*alone) == archive, "the library is one archive whichever sources it lists"); Configuration withoutExtra = MakeApp(staged, alone.get()); if (BuildOkWithArchiveInPlace(withoutExtra, archive, "the build with lib/extra.cpp taken out")) { CheckPrints(withoutExtra, "1", "built with lib/extra.cpp taken out"); CheckMembers(*alone, "first_impl.o\n", "the archive with lib/extra.cpp taken out"); } const std::unique_ptr renamed = MakeLib(staged, { "second" }); Check(ExpectedOutput(*renamed) == archive, "and when another source is in the first's place"); Configuration afterRename = MakeApp(staged, renamed.get()); if (BuildOkWithArchiveInPlace(afterRename, archive, "the build with lib/second.cpp in the place of lib/first.cpp")) { CheckPrints(afterRename, "2", "built with lib/second.cpp in the place of lib/first.cpp"); CheckMembers(*renamed, "second_impl.o\n", "the archive with lib/second.cpp in the place of lib/first.cpp"); } if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }