DuplicateModule builds its project once more with --wide --reorder, with which P gives M the list that A passes on, in the other order. The loader takes the two orders for two lists and makes two configurations of M, which VariantId, taking the project's own flags sorted, builds into one directory, and a pass builds that directory once. The case checks that the two are two configurations with one directory, and then, in two passes, that the graph builds and that P's main.cpp and A's interface have their edges to the module of the configuration of M that the pass built. The first pass builds whichever asks first, and the other has no modules. The second builds the other one first and records it for the directory, as BuildAll records a configuration it builds, before it builds the graph, so that both configurations of M then have modules, the first still those it read in the first pass. On the engine of the range's base the graph is refused as "module M is built twice for P": every configuration had its modules from the listing, and the refusal told the builds apart by their configurations. On an engine that reads a dependency's modules off the configuration it is listed as, the range's fix without BuildResult::built, the first pass builds and A's interface has no edge to M, P's M being the one built and A's having no modules, and the second pass is refused as a graph with two builds of M.
153 lines
8.4 KiB
C++
153 lines
8.4 KiB
C++
// 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 program can hold one module of each name, so Build() refuses a graph in
|
|
// which two configurations export the same module: the same project loaded
|
|
// along two paths with two arg lists, say. It used to build both and compile
|
|
// the consumer with both on its module path. Clang took the first for every
|
|
// import of the module, the ones inside the other build's consumers
|
|
// included, and the program linked code compiled against two layouts of one
|
|
// struct: it built cleanly, then read garbage or crashed (issue #41). The
|
|
// fixture is P -> M and P -> A -> M. With --split, P gives M its args
|
|
// without --wide, which changes what M declares, while A passes them on
|
|
// whole.
|
|
namespace {
|
|
std::int32_t Failures = 0;
|
|
|
|
void Check(bool cond, std::string_view msg) {
|
|
if (!cond) {
|
|
std::println(std::cerr, "FAIL: {}", msg);
|
|
++Failures;
|
|
}
|
|
}
|
|
|
|
BuildResult BuildPass(Configuration& cfg) {
|
|
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
|
std::mutex depMutex;
|
|
return Build(cfg, depResults, depMutex);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
const fs::path installedHome = GetCrafterBuildHome();
|
|
if (!fs::exists(installedHome / "Crafter.Build.cppm")) {
|
|
std::println(std::cerr, "SKIP: no module sources at {}", installedHome.string());
|
|
return 77;
|
|
}
|
|
|
|
// Staged because LoadProject and Build drop build/ and bin/ trees next
|
|
// to the project files.
|
|
const fs::path fixture = fs::current_path() / "tests" / "DuplicateModule" / "fixture";
|
|
const fs::path scratch = fs::temp_directory_path() / "crafter-build-duplicate-module";
|
|
std::error_code ec;
|
|
fs::remove_all(scratch, ec);
|
|
fs::create_directories(scratch);
|
|
fs::copy(fixture, scratch, fs::copy_options::recursive);
|
|
|
|
// From P's own directory, as crafter-build runs it: the fixture's
|
|
// LocalProject paths are relative to the working directory.
|
|
const fs::path cwd = fs::current_path();
|
|
fs::current_path(scratch / "p");
|
|
try {
|
|
// Two lists for M: refused before P compiles anything of its own.
|
|
// The modules of a configuration are known once it is built, so M is
|
|
// built twice, and A on one of the two, before P can tell.
|
|
{
|
|
std::array<std::string_view, 2> args = { "--wide", "--split" };
|
|
Configuration cfg = LoadProject("project.cpp", args);
|
|
const BuildResult built = BuildPass(cfg);
|
|
Check(!built.result.empty(), "a graph with two builds of one module fails to build");
|
|
Check(built.result.contains("module M is built twice for P"), std::format("the error names the module and the program (got: {})", built.result));
|
|
Check(built.result.contains("P -> M (") && built.result.contains("P -> A -> M ("), std::format("the error names the path to each build (got: {})", built.result));
|
|
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<std::string_view, 2> 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<fs::path, std::shared_future<BuildResult>> depResults;
|
|
std::mutex depMutex;
|
|
std::promise<BuildResult> 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<std::string_view, 1> args = { "--wide" };
|
|
Configuration cfg = LoadProject("project.cpp", args);
|
|
const bool oneM = cfg.dependencies.size() == 2 && cfg.dependencies[1]->dependencies.size() == 1 && cfg.dependencies[0] == cfg.dependencies[1]->dependencies[0];
|
|
Check(oneM, "the same list along both paths loads one M");
|
|
const BuildResult built = BuildPass(cfg);
|
|
Check(built.result.empty(), std::format("the graph with one M builds (got: {})", built.result));
|
|
}
|
|
} catch (const std::exception& e) {
|
|
fs::current_path(cwd);
|
|
std::println(std::cerr, "FAIL: threw: {}", e.what());
|
|
return 1;
|
|
}
|
|
fs::current_path(cwd);
|
|
|
|
return Failures > 0 ? 1 : 0;
|
|
}
|