From c82b2bb18c67572ec14ded7ab440f05a0dcee958 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Thu, 19 Nov 2026 09:20:32 +0100 Subject: [PATCH] test(build): a source that leaves a static library leaves its archive StaticLibSourceRemoved builds a static library and a program on it, on a fresh copy of its fixture outside the checkout. The 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. It is built three times, by configurations of one name and directory: from the first and the extra source, from the first alone, and from the second in the first's place, as the project of a renamed source lists it. The program prints 1, 1 and 2, and `llvm-ar t` lists the archive's members as first_impl.o and extra_impl.o, then first_impl.o, then second_impl.o. While the second and the third build run, a thread of the test looks for the archive again and again and has to find it every time: the new archive is moved into the place of the old one, where a build that removed the old one first would leave whatever links the library at that moment without it. Before the fix the second build's archive listed first_impl.o and extra_impl.o, and the third's those two and second_impl.o, with a program that printed 1. --- project.cpp | 1 + .../fixture/lib/extra.cpp | 6 + .../fixture/lib/first.cpp | 4 + .../fixture/lib/second.cpp | 4 + tests/StaticLibSourceRemoved/fixture/main.cpp | 13 ++ tests/StaticLibSourceRemoved/main.cpp | 168 ++++++++++++++++++ 6 files changed, 196 insertions(+) create mode 100644 tests/StaticLibSourceRemoved/fixture/lib/extra.cpp create mode 100644 tests/StaticLibSourceRemoved/fixture/lib/first.cpp create mode 100644 tests/StaticLibSourceRemoved/fixture/lib/second.cpp create mode 100644 tests/StaticLibSourceRemoved/fixture/main.cpp create mode 100644 tests/StaticLibSourceRemoved/main.cpp diff --git a/project.cpp b/project.cpp index adaaddf..294f04f 100644 --- a/project.cpp +++ b/project.cpp @@ -247,6 +247,7 @@ extern "C" Configuration CrafterBuildProject(std::span a if (linuxTarget) { cfg.AddTest("HelloWorld").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("StaticLib").Dependencies({ CrafterBuildLib.get() }); + cfg.AddTest("StaticLibSourceRemoved").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("ModuleInterface").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("DependencyLink").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("SharedLibraryRuns").Dependencies({ CrafterBuildLib.get() }); diff --git a/tests/StaticLibSourceRemoved/fixture/lib/extra.cpp b/tests/StaticLibSourceRemoved/fixture/lib/extra.cpp new file mode 100644 index 0000000..f4c38a0 --- /dev/null +++ b/tests/StaticLibSourceRemoved/fixture/lib/extra.cpp @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +// Nothing calls this: the test looks for the object of this file among the +// members of the library's archive. +int Extra() { return 3; } diff --git a/tests/StaticLibSourceRemoved/fixture/lib/first.cpp b/tests/StaticLibSourceRemoved/fixture/lib/first.cpp new file mode 100644 index 0000000..3d8e490 --- /dev/null +++ b/tests/StaticLibSourceRemoved/fixture/lib/first.cpp @@ -0,0 +1,4 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +int Value() { return 1; } diff --git a/tests/StaticLibSourceRemoved/fixture/lib/second.cpp b/tests/StaticLibSourceRemoved/fixture/lib/second.cpp new file mode 100644 index 0000000..4e04aac --- /dev/null +++ b/tests/StaticLibSourceRemoved/fixture/lib/second.cpp @@ -0,0 +1,4 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +int Value() { return 2; } diff --git a/tests/StaticLibSourceRemoved/fixture/main.cpp b/tests/StaticLibSourceRemoved/fixture/main.cpp new file mode 100644 index 0000000..7d987b8 --- /dev/null +++ b/tests/StaticLibSourceRemoved/fixture/main.cpp @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; + +int Value(); + +// Prints what the library's Value() returns: 1 from lib/first.cpp, 2 from +// lib/second.cpp, whichever the library was built from. +int main() { + std::printf("%d", Value()); + return 0; +} diff --git a/tests/StaticLibSourceRemoved/main.cpp b/tests/StaticLibSourceRemoved/main.cpp new file mode 100644 index 0000000..51f19a0 --- /dev/null +++ b/tests/StaticLibSourceRemoved/main.cpp @@ -0,0 +1,168 @@ +// 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; +}