From 93eca17e575e428a6256d9b05dc4b67e68e5e382 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Thu, 19 Nov 2026 10:33:33 +0100 Subject: [PATCH] fix(build): give a link its objects in a response file Build() wrote the path of every object of a configuration into its link or archive command. A command is run by a shell, which is given it as one argument, and Linux holds one argument to 32 pages: 131,072 bytes with pages of 4 KiB, the terminating byte included. Once the objects' paths left the command longer than that, popen started no shell and the build ended with "popen() failed!". Issue #50 notes the limit, and that a response file for the objects would take it away. The step now reads its objects from a file that the command names with @, .objects in the build directory, one object to a line. clang and llvm-ar both take arguments from such a response file, so the link of an executable or a shared library and the archive step of a static library are handed their objects the same way, and the command is as long for a thousand sources as for one. Build() keeps the objects as a list for that, where it joined their paths into the command as it went over the sources. Each line is quoted for the reader of a response file, which differs from the shell's: it takes a backslash inside quotes to quote the character after it. The file is written before every run of the step, and one that cannot be written fails the build: the list of an earlier build would otherwise stay in its place and the step would read its objects. It is written under a name of its own beside its place and moved there, as the archive of a static library is, so that a step of another crafter-build which reads the list at that moment reads the list it opened, whole, or the new one. The record that decides whether an unchanged build links again held the command, which held the objects. It now holds the command and, under it, the list. The command only names the file, so taking a source out of a configuration can leave the command as it was, with no object newer than the output, as the second build of StaticLibSourceRemoved does: the list is then all that says the step has to run again. --- implementations/Crafter.Build-Clang.cpp | 72 ++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/implementations/Crafter.Build-Clang.cpp b/implementations/Crafter.Build-Clang.cpp index 85b860c..88aca5a 100644 --- a/implementations/Crafter.Build-Clang.cpp +++ b/implementations/Crafter.Build-Clang.cpp @@ -792,6 +792,22 @@ namespace { fs::remove(written, ec); return failed; } + + // `argument` as one argument in a response file, the file of further + // arguments a command names with @. clang and llvm-ar split such a file + // at whitespace, keep what stands in quotes together, and take a + // backslash to quote the character after it, inside quotes as well. + // That last is why this is not ShellQuote: the shell keeps a backslash + // in single quotes as it is. + std::string ResponseFileArgument(std::string_view argument) { + std::string quoted = "\""; + for (const char c : argument) { + if (c == '"' || c == '\\') quoted.push_back('\\'); + quoted.push_back(c); + } + quoted.push_back('"'); + return quoted; + } } BuildResult Crafter::Build(Configuration& config, std::unordered_map>& depResults, std::mutex& depMutex) { @@ -1146,7 +1162,9 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map objects; std::set libSet; std::set runtimeSet; std::set publicFlagSet; @@ -1204,8 +1222,8 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_mapCheck(pcmDir, externalFloor)) { buildResult.repack = true; } - files += std::format(" {}/{}.o", buildDir.string(), interface->path.filename().string()); + objects.push_back(buildDir / std::format("{}.o", interface->path.filename().string())); for(std::unique_ptr& part : interface->partitions) { - files += std::format(" {}/{}.o", buildDir.string(), part->path.filename().string()); + objects.push_back(buildDir / std::format("{}.o", part->path.filename().string())); } } @@ -1410,7 +1428,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map(recorded)), std::istreambuf_iterator()); - if (previous != linkCommand) buildResult.repack = true; + if (previous != linked) buildResult.repack = true; } if(buildResult.repack) { @@ -1633,6 +1669,26 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map