// 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 configuration is linked from however many objects it has. Build() used // to write the path of every object into the link or archive command, and a // command is handed to a shell as one argument, which Linux holds to 32 // pages: 131,072 bytes with pages of 4 KiB, the terminating byte included. // With objects whose paths came to more, no shell started, and the build // ended with "popen() failed!". // // The test makes a project six directories deep, each with a name of 200 // characters, so that the path of its build directory alone is long, and // gives it 120 C sources and a main.cpp that adds up what each of them // returns. Every object lies in the build directory, so the paths of the // 120 come to more than one argument holds. It builds the sources three // ways: as one executable, whose link is given the objects, as a static // library, whose archive step is, and as a shared library, the two // libraries each with an executable of main.cpp built on it. Every program // has to print the sum, which it does only when every object reached its // link. // // The step reads its objects from a list in the build directory, and a step // may be reading that list when the next build writes another: a library // that two projects are built on is built by the crafter-build of each, in // one clone in the cache. So the test holds the static library's list open, // as a step that reads it does, builds the library again with one source // fewer, and then reads what it has open: that has to be the whole list of // the first build, not the next list or part of either. namespace { std::int32_t Failures = 0; void Check(bool cond, std::string_view msg) { if (!cond) { std::println(std::cerr, "FAIL: {}", msg); ++Failures; } } void Write(const fs::path& file, std::string_view text) { std::ofstream(file, std::ios::binary | std::ios::trunc) << text; } constexpr std::int32_t Sources = 120; // What every program prints: source i returns i. constexpr std::int32_t Sum = Sources * (Sources - 1) / 2; // The most one argument of a command may be where a page is 4 KiB. constexpr std::size_t ArgumentLimit = 32 * 4096; // A configuration in `project` that is linked for the machine that // builds. Configuration Named(const fs::path& project, std::string_view name, ConfigurationType type) { Configuration cfg; cfg.path = project; cfg.name = std::string(name); cfg.outputName = std::string(name); cfg.target = HostTarget(); cfg.type = type; return cfg; } // An executable of main.cpp alone. Configuration Program(const fs::path& project, std::string_view name) { Configuration cfg = Named(project, name, ConfigurationType::Executable); std::array ifaces = {}; std::array impls = { "main" }; cfg.GetInterfacesAndImplementations(ifaces, impls); return cfg; } // What went wrong building `cfg`; empty when nothing did. Before the // fix the build threw where the shell did not start. std::string BuildError(Configuration& cfg) { try { std::unordered_map> depResults; std::mutex depMutex; return Build(cfg, depResults, depMutex).result; } catch (const std::exception& e) { return std::format("Build() threw: {}", e.what()); } } // Builds `cfg` and runs what it links. void CheckBuildsAndPrintsSum(Configuration& cfg, std::string_view label) { const std::string error = BuildError(cfg); Check(error.empty(), std::format("{} builds: {}", label, error)); if (!error.empty()) return; const auto run = RunCommandWithTimeout(ShellQuote(ExpectedOutput(cfg).string()), std::chrono::seconds(30)); Check(run.exitCode == 0 && run.output == std::to_string(Sum), std::format("{}: the program printed '{}' (exit {}), expected {}", label, run.output, run.exitCode, Sum)); } // How many lines `text` has. std::int64_t Lines(std::string_view text) { return std::ranges::count(text, '\n'); } } int main() { const fs::path root = fs::temp_directory_path() / "crafter-build-link-many-objects"; fs::remove_all(root); fs::path project = root; for (const char letter : std::string_view("abcdef")) project /= std::string(200, letter); fs::create_directories(project); // Without their extension and whole, as cFiles are read against the // working directory. std::vector sources; std::string declarations; std::string sum; for (std::int32_t i = 0; i < Sources; ++i) { const fs::path source = project / std::format("unit{}", i); Write(fs::path(source) += ".c", std::format("int unit_{0}(void) {{ return {0}; }}\n", i)); sources.push_back(source); declarations += std::format("extern \"C\" int unit_{}(void);\n", i); sum += std::format(" + unit_{}()", i); } // printf by its declaration, so that main.cpp, which is compiled once // for each of the three programs, needs no header and no module. Write(project / "main.cpp", std::format("extern \"C\" int printf(const char* format, ...);\n{}int main() {{ printf(\"%d\", 0{}); return 0; }}\n", declarations, sum)); Configuration whole = Program(project, "many"); whole.cFiles = sources; // What the test rests on: with a shorter list the builds below would // have passed before the fix as well. Each object lies in the build // directory, wherever in it, so its path is longer than that // directory's. const std::size_t listed = static_cast(Sources) * whole.BuildDir().string().size(); Check(listed > ArgumentLimit, std::format("the paths of the {} objects come to more than {} bytes, more than the {} one argument may be", Sources, listed, ArgumentLimit)); CheckBuildsAndPrintsSum(whole, "an executable of the sources"); Configuration archive = Named(project, "many-static", ConfigurationType::LibraryStatic); archive.cFiles = sources; Configuration onArchive = Program(project, "on-static"); onArchive.dependencies = { &archive }; CheckBuildsAndPrintsSum(onArchive, "an executable built on a static library of the sources"); Configuration shared = Named(project, "many-shared", ConfigurationType::LibraryDynamic); shared.cFiles = sources; Configuration onShared = Program(project, "on-shared"); onShared.dependencies = { &shared }; CheckBuildsAndPrintsSum(onShared, "an executable built on a shared library of the sources"); // The static library's list, opened as its archive step opens it, and // not read yet. const fs::path list = archive.BuildDir() / "many-static.objects"; std::ifstream opened(list, std::ios::binary); Check(opened.is_open(), std::format("the archive step read its objects from {}", list.string())); Configuration fewer = Named(project, "many-static", ConfigurationType::LibraryStatic); fewer.cFiles.assign(sources.begin(), sources.end() - 1); const std::string error = BuildError(fewer); Check(error.empty(), std::format("the static library builds again with one source fewer: {}", error)); const std::string held((std::istreambuf_iterator(opened)), std::istreambuf_iterator()); Check(Lines(held) == Sources, std::format("a list that is open when the next build writes its own is still the whole list it was: {} lines read, {} objects listed", Lines(held), Sources)); std::ifstream next(list, std::ios::binary); const std::string written((std::istreambuf_iterator(next)), std::istreambuf_iterator()); Check(Lines(written) == Sources - 1, std::format("and the list in its place is the next build's: {} lines, {} objects listed", Lines(written), Sources - 1)); if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }