// 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; // Two sources of one configuration may have the same file name in different // directories: each has an object of its own. An object used to be named // from its source's file name alone, so a/Thing.cpp and b/Thing.cpp were // compiled into one Thing_impl.o, each over the other, and the link was // given that object twice (issue #50). A build of sources that define what // another file calls failed at the link, with duplicate symbols of one of // them and no word of the other. A debug build of sources that define // nothing another file names, and are in the program for what they do when // it starts, succeeded: the program had one of them twice and the other not // at all. // // The fixture has both. `names` is an executable of a/Thing.cpp and // b/Thing.cpp, the C sources a/unit.c and b/unit.c, a b/Thing.c beside the // b/Thing.cpp, and a Thing.cpp in a directory beside the configuration's // own; its program prints what each of them returns. A header only // a/Thing.cpp includes is then changed, and after that b/Thing.cpp and // b/unit.c: each time the program has to print the new values of the // sources that changed and the values the others had. `registry` is a debug // executable of a/Self.cpp and b/Self.cpp, which register themselves when // the program starts, and prints how often each did. `things` is a static // library of a/Thing.cpp, b/Thing.cpp, b/Thing.c and the two unit.c, whose // archive then holds members of one name, and `things-app` a program of // main.cpp and ../shared/Thing.cpp built on it. // // The object of a source outside its configuration's directory is named by // the source's whole path, so that path holds every directory above the // source. `outside`, which the test makes last, is a static library of one // such C source, listed relative to the working directory and built from a // working directory with a space in its name: the object's path then holds // the space, where neither the source as listed nor the build directory // does, and has to reach the compiler and the archive as one path. namespace { std::int32_t Failures = 0; void Check(bool cond, std::string_view msg) { if (!cond) { std::println(std::cerr, "FAIL: {}", msg); ++Failures; } } // The fixture is changed during the run, so it is a copy outside the // checkout that is built. fs::path StageFixture() { const fs::path source = fs::current_path() / "tests" / "SameNamedSources" / "fixture"; const fs::path staged = fs::temp_directory_path() / "crafter-build-same-named-sources"; fs::remove_all(staged); fs::copy(source, staged, fs::copy_options::recursive); return staged; } // Puts -grown.in in the place of `file`. The other // text is a file of the fixture rather than a string here, so that the // one under the source's name is the only one a build sees. void Grow(const fs::path& file) { const fs::path grown = file.parent_path() / std::format("{}-grown{}.in", file.stem().string(), file.extension().string()); fs::copy_file(grown, file, fs::copy_options::overwrite_existing); // copy_file gives the copy the time of what it copied, which would // leave the changed file older than the object built from it. fs::last_write_time(file, fs::file_time_type::clock::now()); } // The C sources of `names`, without their extensions and whole: cFiles // are read against the working directory, not against cfg.path. std::vector CSources(const fs::path& staged) { return { staged / "names" / "a" / "unit", staged / "names" / "b" / "unit", staged / "names" / "b" / "Thing" }; } // The issue's first case, and more of the kind: every source defines // something main.cpp calls. ../shared/Thing lies outside the // configuration's directory. Configuration MakeNames(const fs::path& staged) { Configuration cfg; cfg.path = staged / "names"; cfg.name = "names"; cfg.outputName = "names"; cfg.target = HostTarget(); cfg.type = ConfigurationType::Executable; std::array ifaces = {}; std::array impls = { "main", "a/Thing", "b/Thing", "../shared/Thing" }; cfg.GetInterfacesAndImplementations(ifaces, impls); cfg.cFiles = CSources(staged); return cfg; } // The issue's second case: a debug build of sources nothing names. Configuration MakeRegistry(const fs::path& staged) { Configuration cfg; cfg.path = staged / "registry"; cfg.name = "registry"; cfg.outputName = "registry"; cfg.target = HostTarget(); cfg.debug = true; cfg.type = ConfigurationType::Executable; std::array ifaces = {}; std::array impls = { "main", "a/Self", "b/Self" }; cfg.GetInterfacesAndImplementations(ifaces, impls); return cfg; } // a/Thing.cpp, b/Thing.cpp and the C sources of `names` as a static // library. std::unique_ptr MakeThings(const fs::path& staged) { std::unique_ptr lib = std::make_unique(); lib->path = staged / "names"; lib->name = "things"; lib->outputName = "things"; lib->target = HostTarget(); lib->type = ConfigurationType::LibraryStatic; std::array ifaces = {}; std::array impls = { "a/Thing", "b/Thing" }; lib->GetInterfacesAndImplementations(ifaces, impls); lib->cFiles = CSources(staged); return lib; } Configuration MakeThingsApp(const fs::path& staged, Configuration* things) { Configuration cfg; cfg.path = staged / "names"; cfg.name = "things-app"; cfg.outputName = "things-app"; cfg.target = HostTarget(); cfg.type = ConfigurationType::Executable; std::array ifaces = {}; std::array impls = { "main", "../shared/Thing" }; cfg.GetInterfacesAndImplementations(ifaces, impls); cfg.dependencies = { things }; return cfg; } bool BuildOk(Configuration& cfg, std::string_view label) { // A fresh record for each pass: one that holds a configuration's // build already would hand it to the next pass unbuilt. std::unordered_map> depResults; std::mutex depMutex; const BuildResult r = Build(cfg, depResults, depMutex); if (!r.result.empty()) { std::println(std::cerr, "FAIL: {} failed: {}", label, r.result); ++Failures; return false; } return true; } void CheckPrints(const Configuration& cfg, std::string_view expected, std::string_view label) { const auto r = RunCommandWithTimeout(ShellQuote(ExpectedOutput(cfg).string()), std::chrono::seconds(30)); Check(r.exitCode == 0 && r.output == expected, std::format("{}: the program printed '{}' (exit {}), expected '{}'", label, r.output, r.exitCode, expected)); } } int main() { const fs::path staged = StageFixture(); // A() B() AUnit() BUnit() BThingInC() Shared(), see the fixture's // names/main.cpp. Configuration names = MakeNames(staged); if (BuildOk(names, "the build of names")) CheckPrints(names, "1 2 4 8 16 32", "names"); Grow(staged / "names" / "a" / "limit.h"); if (BuildOk(names, "the build of names after a header of a/Thing.cpp changed")) CheckPrints(names, "100 2 4 8 16 32", "names after a header of a/Thing.cpp changed"); Grow(staged / "names" / "b" / "Thing.cpp"); Grow(staged / "names" / "b" / "unit.c"); if (BuildOk(names, "the build of names after b/Thing.cpp and b/unit.c changed")) CheckPrints(names, "100 200 4 800 16 32", "names after b/Thing.cpp and b/unit.c changed"); // How often a/Self.cpp registered, then b/Self.cpp. Configuration registry = MakeRegistry(staged); if (BuildOk(registry, "the debug build of registry")) CheckPrints(registry, "1 1", "registry"); // The sources as they are after the changes above. const std::unique_ptr things = MakeThings(staged); Configuration app = MakeThingsApp(staged, things.get()); if (BuildOk(app, "the build of things-app on the static library things")) CheckPrints(app, "100 200 4 800 16 32", "things-app"); // Last, because it changes the working directory of the test: a // relative cfg.path and a relative C source are read against it. const fs::path spaced = staged / "with space"; fs::create_directories(spaced / "app"); fs::create_directories(spaced / "shared"); std::ofstream(spaced / "shared" / "unit.c") << "int Outside(void) { return 64; }\n"; fs::current_path(spaced / "app"); Configuration outside; outside.path = "./"; outside.name = "outside"; outside.outputName = "outside"; outside.target = HostTarget(); outside.type = ConfigurationType::LibraryStatic; outside.cFiles = { "../shared/unit" }; if (BuildOk(outside, "the build of outside, from a working directory with a space in its name")) { const auto members = RunCommandWithTimeout(std::format("{} t {}", ShellQuote(GetToolchain().llvmAr.string()), ShellQuote(ExpectedOutput(outside).string())), std::chrono::seconds(30)); Check(members.exitCode == 0 && members.output == "unit_source.o\n", std::format("outside: llvm-ar t lists '{}' (exit {}), expected the object of ../shared/unit.c", members.output, members.exitCode)); } if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }