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.
This commit is contained in:
Jorijn van der Graaf 2026-11-19 09:20:32 +01:00
commit 2138015178
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB

View file

@ -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<fs::path, std::shared_future<BuildResult>>& depResults, std::mutex& depMutex) {
@ -1524,11 +1553,23 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
}
}
// The exact command the link (or archive) step runs, kept beside the
// objects once it has succeeded. A different one relinks even with no
// object newer than the output: a link flag changed in project.cpp, or a
// dependency's library moved, as an ExternalDependency's does when its
// options change and it is built into a new cache directory.
// The archive step of a static library, as it writes the archive to
// `archive`. ThinLTO emits LLVM bitcode objects; plain `ar` writes an
// archive index that omits their symbols, so a consumer's lld link
// can't pull the needed members. llvm-ar writes a bitcode-aware index
// and handles plain ELF and COFF objects just as well, so every build
// uses it.
const auto archiveCommand = [&](const fs::path& archive) {
return std::format("{} rcs {} {}", GetToolchain().llvmAr.string(), archive.string(), files);
};
// The command the link step runs, or the archive step with the archive
// itself for the name it is written under (see where the step runs),
// kept beside the objects once the step has succeeded. A different one
// relinks even with no object newer than the output: a link flag
// changed in project.cpp, or a dependency's library moved, as an
// ExternalDependency's does when its options change and it is built
// into a new cache directory.
std::string linkCommand;
if (config.type == ConfigurationType::Executable) {
if (config.target.starts_with("wasm32")) {
@ -1537,12 +1578,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
linkCommand = std::format("{}{} -o {} -fuse-ld=lld{}", command, files, (outputDir/config.outputName).string(), linkExtras);
}
} else if (config.type == ConfigurationType::LibraryStatic) {
// ThinLTO emits LLVM bitcode objects; plain `ar` writes an archive
// index that omits their symbols, so a consumer's lld link can't
// pull the needed members. llvm-ar writes a bitcode-aware index
// and handles plain ELF and COFF objects just as well, so every
// build uses it.
linkCommand = std::format("{} rcs {}.a {}", GetToolchain().llvmAr.string(), (outputDir/fs::path(std::string("lib")+config.outputName)).string(), files);
linkCommand = archiveCommand(ExpectedOutput(config));
} else if (config.target == "x86_64-w64-mingw32") {
// LibraryDynamic. Output names follow each target's convention so
// consumers can link via the standard linker search:
@ -1597,7 +1633,38 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
}
}
}
if (config.type == ConfigurationType::LibraryStatic) {
// llvm-ar's `r` adds to an archive that is there: it replaces
// the members it is given and keeps every other one, so the
// object of a source that had left the configuration stayed in
// the library, ahead of whatever was added after it. The link
// takes a symbol from the first member that defines it: once a
// source had been renamed, everything built on the library went
// on linking the object of the file under its old name. So the
// step writes its archive where there is none, and that one
// holds what the command lists and nothing else.
//
// Not in the archive's own place with what is there removed
// first: the library would be missing from there until llvm-ar
// had written the new one, and the link of another
// crafter-build that is built on the library at that moment
// (WrittenBeside) would not find it. llvm-ar put a new archive
// in the place of the old one in one step, and so does the
// move.
const fs::path archive = ExpectedOutput(config);
const fs::path written = WrittenBeside(archive);
buildResult.result = RunBuildCommand(archiveCommand(written));
if (buildResult.result.empty()) {
buildResult.result = MoveIntoPlace(written, archive);
} else {
// The archive of the build before stays, as it did when
// llvm-ar failed on it.
std::error_code ec;
fs::remove(written, ec);
}
} else {
buildResult.result = RunBuildCommand(linkCommand);
}
if (buildResult.result.empty()) {
std::ofstream(linkRecord, std::ios::binary | std::ios::trunc) << linkCommand;
}