From 21380151785d5608dbaaef5f75295d3bdfed375c Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Thu, 19 Nov 2026 09:20:32 +0100 Subject: [PATCH] fix(build): write a static library's archive anew The archive step of a static library ran `llvm-ar rcs` on the archive the build before had left. `r` adds to an archive that is there: it replaces the members it is given and keeps every other member as it is. The object of a source that had left the configuration therefore stayed in the library, ahead of the members added after it. The link takes a symbol from the first archive member that defines it. So after a source of a static library had been renamed, the library held the object of the file under its old name and, after it, the one of the file under its new name, and whatever was built on the library went on linking the first. The object of a source taken out of the project stayed in the library the same way. The step now writes its archive under a name of its own, beside the archive's place, and Build() moves it there once the step has succeeded, so the archive holds the objects that step lists and no others. It is moved into place, and the old archive not removed first, because another crafter-build may be linking against the library at that moment: a project loaded with GitProject is built in its clone in the cache, which every project that names the same source shares, and nothing keeps two crafter-builds from building it at once. llvm-ar put the new archive in the place of the old one in one step, so such a link found one of the two, and with the move it still does. A step that fails leaves the archive of the build before, as it did, and the next build runs the step again. build.sh has written the bootstrap's archive anew since 046609f, for the same reason, by removing it first. --- implementations/Crafter.Build-Clang.cpp | 91 +++++++++++++++++++++---- 1 file changed, 79 insertions(+), 12 deletions(-) diff --git a/implementations/Crafter.Build-Clang.cpp b/implementations/Crafter.Build-Clang.cpp index f512509..85b860c 100644 --- a/implementations/Crafter.Build-Clang.cpp +++ b/implementations/Crafter.Build-Clang.cpp @@ -763,6 +763,35 @@ namespace { d.programEntries = placed; return WriteFlatpakFiles(d); } + + // The name a file is written under before MoveIntoPlace gives it its + // own: beside its place, so that the move is a rename, and no other + // write's, in this process or in another. A build writes files that a + // step of another crafter-build may be reading at that moment: a + // project loaded with GitProject is built in its clone in the cache, + // which every project that names the same source shares, and nothing + // keeps two crafter-builds from building it at once. The name is made + // of random bits and not of the process's id: a crafter-build that + // runs as a Flatpak has the process ids of its own sandbox, and two of + // them the same ones. + fs::path WrittenBeside(const fs::path& file) { + std::random_device random; + return fs::path(file) += std::format(".{:08x}{:08x}.tmp", random(), random()); + } + + // Moves `written` to `file`. The rename puts it there in one step: what + // opens `file` finds the file of before or this one, never none and + // never part of one, and what has the file of before open goes on + // reading that one. Returns what went wrong, with `written` removed; + // empty when nothing did. + std::string MoveIntoPlace(const fs::path& written, const fs::path& file) { + std::error_code ec; + fs::rename(written, file, ec); + if (!ec) return ""; + std::string failed = std::format("cannot move {} to {}: {}", written.string(), file.string(), ec.message()); + fs::remove(written, ec); + return failed; + } } BuildResult Crafter::Build(Configuration& config, std::unordered_map>& depResults, std::mutex& depMutex) { @@ -1524,11 +1553,23 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map