// 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; // Interface units that import each other are refused before any .cppm or // .cpp of their configuration is compiled, with an error naming them. No // order builds them. Before the refusal such a build hung with no compiler // running, each unit waiting for the units it imports, which is how this // test failed then: by running into its timeout. // // Three ways round: two partitions that import each other, a partition that // imports its own module, whose primary interface is built after all of its // partitions, and two modules of one configuration that import each other. namespace { std::int32_t Failures = 0; void Check(bool cond, std::string_view msg) { if (!cond) { std::println(std::cerr, "FAIL: {}", msg); ++Failures; } } // Builds the interfaces of the fixture in `dir` as a library and returns // Build()'s error. On a copy outside the checkout, made anew each run: a // build that gets past the refusal can compile units before it fails, and // in the checkout the next run would find them there and report them // against a refusal that works. std::string BuildError(std::string_view dir, std::span interfaces) { const fs::path staged = fs::temp_directory_path() / "crafter-build-import-cycle" / dir; fs::remove_all(staged); fs::create_directories(staged.parent_path()); fs::copy(fs::current_path() / "tests" / "ImportCycle" / "fixture" / dir, staged, fs::copy_options::recursive); Configuration cfg; cfg.path = staged; cfg.name = std::format("cycle-{}", dir); cfg.outputName = cfg.name; cfg.target = HostTarget(); cfg.type = ConfigurationType::LibraryStatic; std::array implementations = {}; cfg.GetInterfacesAndImplementations(interfaces, implementations); std::unordered_map> depResults; std::mutex depMutex; const BuildResult built = Build(cfg, depResults, depMutex); // Refused once the build has read what the units import, which is // before it compiles any of them: no unit has a BMI or an object. for (const fs::path& interface : interfaces) { const fs::path bmi = cfg.PcmDir() / std::format("{}.pcm", interface.string()); const fs::path object = cfg.BuildDir() / std::format("{}.o", interface.string()); Check(!fs::exists(bmi) && !fs::exists(object), std::format("{}: the refused build compiled {}", dir, interface.string())); } return built.result; } } int main() { { std::array interfaces = { "Ring", "Ring-First", "Ring-Second" }; const std::string error = BuildError("partitions", interfaces); Check(error.contains("Ring:First -> Ring:Second -> Ring:First"), std::format("partitions that import each other are named in the order they wait in, got: '{}'", error)); } { std::array interfaces = { "Own", "Own-Part" }; const std::string error = BuildError("own-module", interfaces); Check(error.contains("Own -> Own:Part -> Own"), std::format("a partition that imports its own module is named with it, got: '{}'", error)); } { std::array interfaces = { "East", "West" }; const std::string error = BuildError("siblings", interfaces); Check(error.contains("East -> West -> East"), std::format("modules that import each other are named, got: '{}'", error)); } return Failures == 0 ? 0 : 1; }