diff --git a/tests/DuplicateModule/fixture/p/project.cpp b/tests/DuplicateModule/fixture/p/project.cpp index 3f4a06b..c0eb440 100644 --- a/tests/DuplicateModule/fixture/p/project.cpp +++ b/tests/DuplicateModule/fixture/p/project.cpp @@ -8,13 +8,16 @@ using namespace Crafter; // Loads M itself and through A, which passes on all of its args. With // --split, the list P gives M leaves --wide out, as a filter meant for one -// sibling would, and the two paths load M with two different lists. +// sibling would, and the two paths load M with two different lists. With +// --reorder, P gives M its args in the other order: the same list, which +// the loader still takes for another. extern "C" Configuration CrafterBuildProject(std::span args) { const bool split = std::ranges::contains(args, std::string_view("--split")); std::vector mArgs; for (std::string_view arg : args) { if (!split || arg != "--wide") mArgs.emplace_back(arg); } + if (std::ranges::contains(args, std::string_view("--reorder"))) std::ranges::reverse(mArgs); Configuration* m = LocalProject({ .projectFile = "../m/project.cpp", .args = mArgs }); Configuration* a = LocalProject({ .projectFile = "../a/project.cpp" }); diff --git a/tests/DuplicateModule/main.cpp b/tests/DuplicateModule/main.cpp index 6a05d73..b755bee 100644 --- a/tests/DuplicateModule/main.cpp +++ b/tests/DuplicateModule/main.cpp @@ -67,6 +67,72 @@ int main() { Check(!fs::exists(ObjectPath(cfg, scratch / "p" / "main.cpp")) && !fs::exists(ExpectedOutput(cfg)), "P compiled and linked nothing"); } + // The same list for M along both paths, in two orders. The loader + // makes a configuration of M for each order, and VariantId, which + // takes the args sorted, gives both one directory, which a pass + // builds once, on whichever of the two asks first. That build's + // modules stand for both: P's import of M and A's have their edge to + // the module it read, and the graph is not taken for one with two + // builds of M. A second pass builds the other configuration on + // purpose, while the first still holds the modules it read: then + // both have modules, and they are still one build. + { + std::array args = { "--wide", "--reorder" }; + Configuration cfg = LoadProject("project.cpp", args); + const bool twoM = cfg.dependencies.size() == 2 && cfg.dependencies[1]->dependencies.size() == 1 && cfg.dependencies[0] != cfg.dependencies[1]->dependencies[0]; + Check(twoM, "the two orders load two configurations of M"); + if (twoM) { + Configuration& a = *cfg.dependencies[1]; + Configuration& mOfP = *cfg.dependencies[0]; + Configuration& mOfA = *a.dependencies[0]; + Check(mOfP.PcmDir() == mOfA.PcmDir(), "which are built into one directory"); + // The module M as a build of `m` read it; nullptr where no + // build of `m` has. + const auto moduleOf = [](const Configuration& m) -> const Module* { + return m.interfaces.size() == 1 ? m.interfaces[0].get() : nullptr; + }; + // The module a unit's edge to M leads to; nullptr without one. + const auto edgeToM = [](const auto& unit) -> const Module* { + for (const auto& [imported, bmi] : unit.externalModuleDependencies) { + if (imported->name == "M") return imported; + } + return nullptr; + }; + // P's main.cpp and A's interface lead to the module of `built`. + const auto checkEdges = [&](const Configuration& built, std::string_view pass) { + const Module* m = moduleOf(built); + Check(m != nullptr, std::format("{}: the configuration of M built has its module", pass)); + Check(cfg.implementations.size() == 1 && edgeToM(cfg.implementations[0]) == m, std::format("{}: P's main.cpp has its edge to that module", pass)); + Check(a.interfaces.size() == 1 && edgeToM(*a.interfaces[0]) == m, std::format("{}: A's interface has its edge to that module", pass)); + }; + + // Pass 1, with a record of its own. + BuildResult built = BuildPass(cfg); + Check(built.result.empty(), std::format("pass 1: the graph with one M in two configurations builds (got: {})", built.result)); + if (built.result.empty()) { + Configuration& first = moduleOf(mOfP) != nullptr ? mOfP : mOfA; + Configuration& other = &first == &mOfP ? mOfA : mOfP; + Check(moduleOf(other) == nullptr, "pass 1: one of the two is built, and the other has no modules"); + checkEdges(first, "pass 1"); + + // Pass 2: the other one is built first and recorded for + // the directory, as BuildAll records a configuration it + // builds, and the graph after it, which finds that build. + std::unordered_map> depResults; + std::mutex depMutex; + std::promise otherBuilt; + otherBuilt.set_value(Build(other, depResults, depMutex)); + depResults.insert_or_assign(other.PcmDir(), otherBuilt.get_future().share()); + built = Build(cfg, depResults, depMutex); + Check(built.result.empty(), std::format("pass 2: the graph builds with the other configuration of M built, and the first one holding the module it read in pass 1 (got: {})", built.result)); + if (built.result.empty()) { + Check(moduleOf(first) != nullptr, "pass 2: the first one still holds the module it read in pass 1"); + checkEdges(other, "pass 2"); + } + } + } + } + // One list along both paths: one M, and the build goes through. { std::array args = { "--wide" };