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
@, <output name>.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.
This commit is contained in:
Jorijn van der Graaf 2026-11-19 10:33:33 +01:00
commit 93eca17e57
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB

View file

@ -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<fs::path, std::shared_future<BuildResult>>& depResults, std::mutex& depMutex) {
@ -1146,7 +1162,9 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
const std::string& ltoLinkFlags = compile.ltoLinkFlags;
const bool isWasm = config.target.starts_with("wasm32");
std::string files;
// The objects this configuration is linked or archived from, in the
// order that step is given them.
std::vector<fs::path> objects;
std::set<std::string> libSet;
std::set<std::string> runtimeSet;
std::set<std::string> publicFlagSet;
@ -1204,8 +1222,8 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
cArchFlags += WasmCompileFlags(config);
}
for (const fs::path& cFile : config.cFiles) {
files += std::format(" {}_source.o ", (buildDir / cFile.filename()).string());
const std::string objPath = std::format("{}_source.o", (buildDir / cFile.filename()).string());
objects.emplace_back(objPath);
const std::string srcPath = std::format("{}.c", cFile.string());
// C sources carry their dependencies entirely in #includes, so the
// depfile the compile below writes is the whole record of what this
@ -1218,8 +1236,8 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
}
for (const fs::path& cFile : config.cuda) {
files += std::format(" {}_source.o ", (buildDir / cFile.filename()).string());
const std::string objPath = std::format("{}_source.o", (buildDir / cFile.filename()).string());
objects.emplace_back(objPath);
const std::string srcPath = std::format("{}.cu", cFile.string());
// nvcc writes the depfile clang writes for a C source (-MD -MF): the
// .cu and every header it read, the toolkit's and the system's among
@ -1391,9 +1409,9 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
if(interface->Check(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<ModulePartition>& 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<fs::path, s
buildResult.repack = true;
implementation.Schedule(jobs, command, pcmDir, buildDir);
}
files += std::format(" {}/{}_impl.o", buildDir.string(), implementation.path.filename().string());
objects.push_back(buildDir / std::format("{}_impl.o", implementation.path.filename().string()));
}
if (std::string failed = jobs.Wait(); !failed.empty()) {
@ -1553,6 +1571,18 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
}
}
// The step reads its objects from a file the command names with @, one
// to a line: a response file, which clang and llvm-ar both take
// arguments from. Written into the command itself, the objects' paths
// made it longer with every source, and the command is one argument to
// the shell that runs it, which Linux holds to 32 pages, 131,072 bytes
// where a page is 4 KiB. With objects whose paths came to more, no
// shell started and the build ended with "popen() failed!".
const fs::path objectList = buildDir / std::format("{}.objects", config.outputName);
std::string listed;
for (const fs::path& object : objects) listed += std::format("{}\n", ResponseFileArgument(object.string()));
const std::string files = std::format(" {}", ShellQuote(std::format("@{}", objectList.string())));
// 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
@ -1591,10 +1621,16 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
linkCommand = std::format("{}{} -shared -o {}.so -Wl,-rpath,'$ORIGIN' -fuse-ld=lld{}", command, files, (outputDir/(std::string("lib")+config.outputName)).string(), linkExtras);
}
const fs::path linkRecord = buildDir / std::format("{}.link", config.outputName);
// Kept with the command, under it: the objects it reads, which the
// command itself only names the file of. Taking a source out of the
// configuration can leave the command the one of the build before,
// with no object newer than the output: that list is then all that
// says the step has to run again.
const std::string linked = std::format("{}\n{}", linkCommand, listed);
if (!buildResult.repack) {
std::ifstream recorded(linkRecord, std::ios::binary);
const std::string previous((std::istreambuf_iterator<char>(recorded)), std::istreambuf_iterator<char>());
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<fs::path, s
}
}
}
{
// Written for every run of the step, and a list that cannot be
// written is the build's error: the list of an earlier build
// would be left in its place, and the step would read that one.
// Written beside its place and moved there, not over the list
// that is there: a step of another crafter-build that reads the
// list at that moment (WrittenBeside) would read none of it, or
// part, and archive or link that. With the move it reads the
// list it opened, whole, or this one.
const fs::path written = WrittenBeside(objectList);
std::ofstream list(written, std::ios::binary | std::ios::trunc);
list << listed;
list.close();
if (list.fail()) {
std::error_code ec;
fs::remove(written, ec);
return {std::format("cannot write {}, the list of objects {} is made from", written.string(), ExpectedOutput(config).filename().string()), false, {}};
}
if (std::string failed = MoveIntoPlace(written, objectList); !failed.empty()) return {std::move(failed), false, {}};
}
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
@ -1666,7 +1722,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
buildResult.result = RunBuildCommand(linkCommand);
}
if (buildResult.result.empty()) {
std::ofstream(linkRecord, std::ios::binary | std::ios::trunc) << linkCommand;
std::ofstream(linkRecord, std::ios::binary | std::ios::trunc) << linked;
}
}