// 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; // ObjectPath names the object of a source from where the source lies, so // that two sources have one object only when their paths, with . and .. // taken out of them, are one: by its path from the configuration's // directory under BuildDir()/objects, by its whole path under // BuildDir()/objects-outside when it lies outside that directory, and with // an ending of its own for a .cpp, a .c and a .cu. Named from the file name // alone, a/Thing.cpp and b/Thing.cpp shared an object (issue #50), and so // did a Thing.c and a Thing.cu in one directory. // // Nothing is built here: the rule is asked for the object of sources that // need not exist, which is how the CUDA ending is checked on a machine // without nvcc, and the tree of outside sources against a directory of the // configuration's own that is named like it. SameNamedSources builds // configurations with such sources. namespace { std::int32_t Failures = 0; void Check(bool cond, std::string_view msg) { if (!cond) { std::println(std::cerr, "FAIL: {}", msg); ++Failures; } } Configuration Named(const fs::path& path) { Configuration cfg; cfg.path = path; cfg.name = "paths"; cfg.outputName = "paths"; cfg.target = HostTarget(); return cfg; } } int main() { // Never made: the rule reads no file. const fs::path root = fs::temp_directory_path() / "crafter-build-object-path"; const Configuration cfg = Named(root / "project"); const fs::path inside = cfg.BuildDir() / "objects"; const fs::path outside = cfg.BuildDir() / "objects-outside"; // Every source asked about below with the object it was given, to see // at the end that no two of them were given one. std::vector> given; const auto expect = [&](const fs::path& source, const fs::path& object, std::string_view what) { const fs::path got = ObjectPath(cfg, source); Check(got == object, std::format("{}: {} has the object {}, expected {}", what, source.string(), got.string(), object.string())); given.emplace_back(source, got); }; expect(cfg.path / "main.cpp", inside / "main_impl.o", "a source in the configuration's directory itself"); expect(cfg.path / "a" / "Thing.cpp", inside / "a" / "Thing_impl.o", "a source in a directory of it"); expect(cfg.path / "b" / "Thing.cpp", inside / "b" / "Thing_impl.o", "a source of the same file name in another directory"); expect(cfg.path / "a" / "Thing.c", inside / "a" / "Thing_source.o", "a C source beside a C++ one of its name"); expect(cfg.path / "a" / "Thing.cu", inside / "a" / "Thing_cuda.o", "a CUDA source beside a C one of its name"); expect(cfg.path / "implementations" / "Crafter.Build-Clang.cpp", inside / "implementations" / "Crafter.Build-Clang_impl.o", "a file name with a dot in it"); // Outside the configuration's directory: by the whole path, in a tree // of its own. const fs::path shared = root / "shared" / "Thing.cpp"; expect(shared, outside / (root / "shared" / "Thing_impl.o").relative_path(), "a source beside the configuration's directory"); expect(root / "project-more" / "Thing.cpp", outside / (root / "project-more" / "Thing_impl.o").relative_path(), "a source in a directory whose name begins with the configuration's"); // A directory of the configuration's own named like that tree, with // the whole path of the outside source in it: an object of the first // tree all the same. const fs::path mimic = cfg.path / "objects-outside" / shared.relative_path(); expect(mimic, inside / "objects-outside" / (root / "shared" / "Thing_impl.o").relative_path(), "a source under a directory of the configuration's named objects-outside"); for (std::size_t i = 0; i < given.size(); ++i) { for (std::size_t j = i + 1; j < given.size(); ++j) { Check(given[i].second != given[j].second, std::format("{} and {} have one object, {}", given[i].first.string(), given[j].first.string(), given[i].second.string())); } } // One source however its path is spelled. Check(ObjectPath(cfg, cfg.path / "b" / ".." / "a" / "." / "Thing.cpp") == inside / "a" / "Thing_impl.o", "a path with . and .. in it names the source it leads to"); Check(ObjectPath(cfg, cfg.path / ".." / "shared" / "Thing.cpp") == ObjectPath(cfg, shared), "a path that leaves the configuration's directory through .. names the source outside it"); Check(ObjectPath(cfg, fs::path("sources") / "Thing.c") == ObjectPath(cfg, fs::current_path() / "sources" / "Thing.c"), "a relative source is taken against the working directory"); // The configuration's directory however it is spelled: with a // separator at its end, or relative as a project file's "./" is. const Configuration trailing = Named(root / "project" / ""); Check(ObjectPath(trailing, root / "project" / "a" / "Thing.cpp") == trailing.BuildDir() / "objects" / "a" / "Thing_impl.o", std::format("a configuration whose path ends in a separator: {}", ObjectPath(trailing, root / "project" / "a" / "Thing.cpp").string())); const Configuration relative = Named("./"); Check(ObjectPath(relative, fs::path("lib") / "unit.c") == relative.BuildDir() / "objects" / "lib" / "unit_source.o", std::format("a relative source of a configuration whose path is ./: {}", ObjectPath(relative, fs::path("lib") / "unit.c").string())); Check(ObjectPath(relative, fs::current_path() / "lib" / "unit.c") == relative.BuildDir() / "objects" / "lib" / "unit_source.o", "and the same source by its whole path"); // An interface unit's object is not ObjectPath's to name, nor is any // other file's. for (const std::string_view name : {"Thing.cppm", "Thing.h", "Thing"}) { bool refused = false; try { ObjectPath(cfg, cfg.path / name); } catch (const std::invalid_argument&) { refused = true; } Check(refused, std::format("{} is refused: not a .cpp, .c or .cu", name)); } if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }