GetInterfacesAndImplementations found the module declarations and the imports of a configuration's sources by searching their text with regular expressions, after deleting the comments that fit on one line. The expressions skipped neither string literals nor comments that span lines, knew nothing of the preprocessor, and wanted one space after `import` and none before its semicolon (#45). An implementation source that held "export module std;" in a string literal was taken for a unit of a module std, and listing it threw "Module std not found not found". An import in a comment that spans lines or under #if 0 was followed. `import X;` and `import X ;` were missed, so their unit had no edge to the module it imports and could be compiled before that module's BMI was written. What a .cppm or .cpp declares and imports now comes from clang-scan-deps -format=p1689, which preprocesses the source with the configuration's C++ compile command, the one Build() compiles every unit of it with: strings, comments, spacing, macros and #if come out as they do for the compiler. GetInterfacesAndImplementations only lists the sources, in the new interfaceSources and implementationSources. Build() reads them (ReadSources) once the configuration's dependencies and external dependencies are built: the command takes their include paths, and a source may include headers of an external dependency, which are not there before it is fetched. From what it read, Build() makes the configuration's modules and units, its `interfaces` and `implementations`, and places each import on a module of the configuration or of what it is built on, whose own Build() has read its sources by then. It makes those of its own configuration alone, as the reset of the units' state did before it. A build pass builds a directory once, for whichever configuration of it asks first, and the loader can make two configurations of one directory: one project loaded along two paths with its args in two orders, which it keeps apart and VariantId, sorting the args, does not. Only the one that was built has the modules that build read, so the result of a build now names the configuration it ran on (BuildResult::built), and a dependency's modules are looked up through the pass's record of its directory. 6.4.1 had every configuration's modules from the listing and took such a graph for one with two builds of a module, which it refused; it is now built, once, and the units of both sides have their edges. A scan is kept in the build directory as an object is, in scans/ or, for a source outside cfg.path, in scans-outside/, as JSON of the command it was taken with and of what clang-scan-deps wrote, with the dependency file clang-scan-deps wrote beside it, which lists the source and every file it included. A build scans a source again unless its scan is newer than every file that dependency file lists, was taken with the same command and can be read, so a build with nothing changed runs no clang-scan-deps. Both files are written beside their places and renamed into them, as the archive and the list of objects are, for another crafter-build that may be building the same clone. A configuration's modules are known only once its build has read its sources, so the refusals of a module that two configurations of the graph build (DuplicateModuleError, #41) and of interface units that import each other (ImportCycleError) now come there: once the dependencies are built, before any .cppm or .cpp of the configuration is compiled. They came before the configuration made a directory or built a dependency. The contributor guide names clang-scan-deps among the prerequisites and among what :Toolchain resolves, has a test look at the modules of a configuration only after building it, and has the gotcha for the new order. ResolvePendingImports and the pendingImports of the unit classes go. They placed the imports that a scan at listing time could not, once the dependencies were wired up, and with the imports placed when the dependencies are built there is nothing pending. The tests that looked at a configuration's modules and units before building now look after it (DependencyLink, ModuleInterface, StaticLib, ConcurrentDependencyReset and the disabled TransitiveInterfaceChange), IncrementalInterfaceChange checks the edge that the build of a consumer gives it when its dependency was assigned after its sources were listed, and ImportCycle and DuplicateModule check that the refused configuration compiled nothing of its own. The workarounds for the old scanner are not needed: MingwSysroot writes its stand-in std module as a plain literal again, JobLimit writes the declarations of the sources it generates as plain literals, and Lint's comment on its fixture file no longer gives the scanner as the reason for it. The messages of the regex code stay where they still apply. The one for a source that declares a unit of a module the configuration does not have loses its doubled "not found", and applies to an implementation source that declares one, such as an implementation partition (`module X:P;`). An implementation unit written `module X;` declares no unit for clang-scan-deps but imports X, and an import needs no module of the configuration: the compile reports a module no one builds. A .cppm that declares an implementation partition is built as a partition of its module, where it was refused as declaring no module. The Toolchain gains clangScanDeps, resolved as llvm-ar is: in the bin/ of the LLVM prefix, else by name on PATH. The Flatpak has /app/bin/clang-scan-deps. Fixes #45
448 lines
28 KiB
C++
448 lines
28 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;
|
|
|
|
// ResolveToolchain is pure path resolution, so this test needs no compiler:
|
|
// it stages fake prefixes under the temp dir and checks which one wins,
|
|
// what is picked up beside it, and what the defaults are when nothing is.
|
|
// The last block runs the real GetToolchain() once, which is the only part
|
|
// that spawns clang.
|
|
namespace {
|
|
std::int32_t Failures = 0;
|
|
|
|
void Check(bool cond, std::string_view msg) {
|
|
if (!cond) {
|
|
std::println(std::cerr, "FAIL: {}", msg);
|
|
++Failures;
|
|
}
|
|
}
|
|
|
|
void Touch(const fs::path& file) {
|
|
fs::create_directories(file.parent_path());
|
|
std::ofstream(file) << "";
|
|
}
|
|
|
|
// An empty file that may be run, as an nvcc found on PATH has to be.
|
|
void Program(const fs::path& file) {
|
|
Touch(file);
|
|
fs::permissions(file, fs::perms::owner_exec, fs::perm_options::add);
|
|
}
|
|
|
|
bool Contains(std::string_view haystack, std::string_view needle) {
|
|
return haystack.find(needle) != std::string_view::npos;
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
const fs::path scratch = fs::temp_directory_path() / "crafter-build-toolchain-resolve";
|
|
std::error_code ec;
|
|
fs::remove_all(scratch, ec);
|
|
fs::create_directories(scratch);
|
|
|
|
// A prefix with everything crafter-build looks for beside the compiler,
|
|
// and one with the compiler alone.
|
|
const fs::path full = scratch / "full";
|
|
for (const char* rel : {"bin/clang++", "bin/clang", "bin/llvm-ar", "lib/libclang.so", "include/clang-c/Index.h", "include/c++/v1/__config", "share/libc++/v1/std.cppm", "share/wasi-sysroot/include/.keep"}) {
|
|
Touch(full / rel);
|
|
}
|
|
// The prefix's libc++ headers state its release.
|
|
std::ofstream(full / "include/c++/v1/__config") << "# define _LIBCPP_VERSION 230102\n";
|
|
const fs::path bare = scratch / "bare";
|
|
Touch(bare / "bin/clang++");
|
|
|
|
{
|
|
// Nothing set and nothing beside crafter-build: bare names on PATH
|
|
// and the system locations.
|
|
Toolchain tc = ResolveToolchain({});
|
|
Check(tc.error.empty(), "the PATH fallback is not an error");
|
|
Check(tc.prefix.empty(), "the PATH fallback has no prefix");
|
|
Check(tc.clangxx == "clang++" && tc.clang == "clang" && tc.llvmAr == "llvm-ar", "the PATH fallback uses bare tool names");
|
|
Check(!tc.libclang.has_value(), "the PATH fallback searches libclang by name");
|
|
Check(tc.llvmIncludeDir.empty(), "the PATH fallback adds no include dir");
|
|
Check(tc.stdCppm == "/usr/share/libc++/v1/std.cppm", "the system std module source is the default");
|
|
Check(tc.libcxxVersion == LibcxxVersion("/usr/include/c++/v1"), "the system libc++ headers state the release of the system module source");
|
|
Check(tc.stdModulesDir.empty(), "no crafter-build prefix, no shipped module sources");
|
|
Check(tc.wasiSysroot == "/usr/share/wasi-sysroot", "the system wasi sysroot is the default");
|
|
Check(tc.mingwPrefix.empty(), "outside a sandbox, clang finds mingw-w64 itself: no prefix");
|
|
Check(tc.LibcxxCompileFlags().empty() && tc.LibcxxLinkFlags().empty(), "the PATH fallback adds no libc++ flags");
|
|
}
|
|
{
|
|
// A toolchain installed beside crafter-build wins over PATH, and
|
|
// everything found beside it is used.
|
|
ToolchainInputs in;
|
|
in.crafterPrefix = full;
|
|
Toolchain tc = ResolveToolchain(in);
|
|
Check(tc.prefix == full, "the prefix beside crafter-build is picked");
|
|
Check(tc.clangxx == full / "bin/clang++" && tc.clang == full / "bin/clang" && tc.llvmAr == full / "bin/llvm-ar", "the tools come from that prefix");
|
|
Check(tc.libclang == full / "lib/libclang.so", "libclang beside the compiler is pinned");
|
|
Check(tc.llvmIncludeDir == full / "include", "clang-c headers beside the compiler are added to the include path");
|
|
Check(tc.stdCppm == full / "share/libc++/v1/std.cppm", "the prefix's std module source is used");
|
|
Check(tc.libcxxVersion == 230102, "the prefix's libc++ headers state its release");
|
|
Check(tc.stdModulesDir == full / "share/crafter-build/libc++-modules", "module sources for other releases are looked up in crafter-build's own share directory");
|
|
Check(tc.wasiSysroot == full / "share/wasi-sysroot", "the prefix's wasi sysroot is used");
|
|
Check(tc.LibcxxCompileFlags().empty(), "the compiler finds its own libc++ headers without flags");
|
|
Check(Contains(tc.LibcxxLinkFlags(), std::format("-L{}", (full / "lib").string())) && Contains(tc.LibcxxLinkFlags(), std::format("-Wl,-rpath,{}", (full / "lib").string())), "binaries get -L and an rpath for the prefix's lib dir");
|
|
}
|
|
{
|
|
ToolchainInputs in;
|
|
in.crafterPrefix = scratch / "nothing-here";
|
|
Toolchain tc = ResolveToolchain(in);
|
|
Check(tc.prefix.empty() && tc.clangxx == "clang++", "a crafter-build prefix without clang++ falls back to PATH");
|
|
}
|
|
{
|
|
// The environment wins over the prefix beside crafter-build. A
|
|
// prefix with nothing but the compiler keeps the system defaults for
|
|
// everything else.
|
|
ToolchainInputs in;
|
|
in.crafterPrefix = full;
|
|
in.env["CRAFTER_BUILD_LLVM_DIR"] = bare.string();
|
|
Toolchain tc = ResolveToolchain(in);
|
|
Check(tc.error.empty(), "an explicit prefix with a clang++ is accepted");
|
|
Check(tc.prefix == bare, "CRAFTER_BUILD_LLVM_DIR wins over the prefix beside crafter-build");
|
|
Check(!tc.libclang.has_value(), "no libclang.so in the prefix: search by name");
|
|
Check(tc.llvmIncludeDir.empty(), "no clang-c headers in the prefix: nothing added");
|
|
Check(tc.stdCppm == "/usr/share/libc++/v1/std.cppm", "no std module in the prefix: the system copy");
|
|
Check(tc.wasiSysroot == "/usr/share/wasi-sysroot", "no wasi sysroot in the prefix: the system one");
|
|
Check(Contains(tc.LibcxxLinkFlags(), std::format("-L{}", (bare / "lib").string())) && Contains(tc.LibcxxLinkFlags(), std::format("-Wl,-rpath,{}", (bare / "lib").string())), "the link flags follow the explicit prefix");
|
|
}
|
|
{
|
|
ToolchainInputs in;
|
|
in.env["CRAFTER_BUILD_LLVM_DIR"] = (scratch / "missing").string();
|
|
Toolchain tc = ResolveToolchain(in);
|
|
Check(!tc.error.empty() && Contains(tc.error, "CRAFTER_BUILD_LLVM_DIR"), "an explicit prefix without clang++ is an error, not a fallback");
|
|
}
|
|
{
|
|
ToolchainInputs in;
|
|
in.env["CRAFTER_BUILD_LLVM_DIR"] = "/usr";
|
|
Toolchain tc = ResolveToolchain(in);
|
|
Check(tc.LibcxxLinkFlags().empty(), "the system prefix needs no rpath");
|
|
Check(tc.llvmIncludeDir.empty(), "the system prefix needs no include dir");
|
|
}
|
|
{
|
|
ToolchainInputs in;
|
|
in.crafterPrefix = full;
|
|
in.env["CRAFTER_BUILD_LIBCLANG"] = "/nonexistent/libclang.so";
|
|
Check(ResolveToolchain(in).libclang == fs::path("/nonexistent/libclang.so"), "CRAFTER_BUILD_LIBCLANG is taken verbatim, even when it does not exist");
|
|
}
|
|
{
|
|
// An explicit libc++ install: headers, library and std module all
|
|
// come from it, whatever the compiler would have used.
|
|
const fs::path libcxx = scratch / "libcxx";
|
|
fs::create_directories(libcxx / "include/c++/v1");
|
|
std::ofstream(libcxx / "include/c++/v1/__config") << "# define _LIBCPP_VERSION 210108\n";
|
|
ToolchainInputs in;
|
|
in.crafterPrefix = full;
|
|
in.env["CRAFTER_BUILD_LIBCXX_DIR"] = libcxx.string();
|
|
Toolchain tc = ResolveToolchain(in);
|
|
Check(tc.libcxxDir == libcxx, "CRAFTER_BUILD_LIBCXX_DIR is recorded");
|
|
Check(tc.stdCppm == libcxx / "share/libc++/v1/std.cppm", "the std module source follows the explicit libc++");
|
|
Check(tc.libcxxVersion == 210108, "the release is read from the explicit libc++, not the compiler's");
|
|
Check(Contains(tc.LibcxxCompileFlags(), std::format("-nostdinc++ -isystem {}", (libcxx / "include/c++/v1").string())), "compiles use the explicit libc++ headers");
|
|
Check(Contains(tc.LibcxxLinkFlags(), std::format("-L{}", (libcxx / "lib").string())) && Contains(tc.LibcxxLinkFlags(), std::format("-Wl,-rpath,{}", (libcxx / "lib").string())), "links use the explicit libc++ library dir, with an rpath");
|
|
}
|
|
{
|
|
ToolchainInputs in;
|
|
in.env["CRAFTER_BUILD_WASI_SYSROOT"] = "/opt/wasi";
|
|
in.env["CRAFTER_MINGW_DIR"] = "/opt/mingw";
|
|
Toolchain tc = ResolveToolchain(in);
|
|
Check(tc.wasiSysroot == "/opt/wasi", "CRAFTER_BUILD_WASI_SYSROOT overrides the wasi sysroot");
|
|
Check(tc.mingwPrefix == "/opt/mingw", "CRAFTER_MINGW_DIR names the mingw prefix");
|
|
in.env["CRAFTER_MINGW_DIR"] = "mingw";
|
|
Check(ResolveToolchain(in).mingwPrefix == fs::absolute("mingw"), "a relative CRAFTER_MINGW_DIR is resolved against the working directory once, when the toolchain is");
|
|
// The prefix names directories, so one directory has one spelling:
|
|
// otherwise a trailing slash rebuilds every mingw build for nothing.
|
|
for (const char* spelling : {"/opt/mingw/", "/opt//mingw", "/opt/./mingw", "/opt/x/../mingw"}) {
|
|
in.env["CRAFTER_MINGW_DIR"] = spelling;
|
|
Check(ResolveToolchain(in).mingwPrefix == "/opt/mingw", std::format("{} is spelled /opt/mingw (got '{}')", spelling, ResolveToolchain(in).mingwPrefix.string()));
|
|
}
|
|
}
|
|
{
|
|
// A prefix that holds a Linux system's C library, as /usr does, is
|
|
// shown to clang through a view; one that holds mingw-w64 alone is
|
|
// the sysroot itself, Windows libraries in its include/ and lib/
|
|
// included.
|
|
const fs::path staged = fs::temp_directory_path() / "crafter-build-toolchain-mingw-prefixes";
|
|
fs::remove_all(staged);
|
|
Touch(staged / "system" / "include" / "features.h");
|
|
Touch(staged / "system" / "x86_64-w64-mingw32" / "include" / "_mingw.h");
|
|
Touch(staged / "dedicated" / "include" / "SDL2" / "SDL.h");
|
|
Touch(staged / "dedicated" / "x86_64-w64-mingw32" / "include" / "_mingw.h");
|
|
ToolchainInputs in;
|
|
in.env["CRAFTER_MINGW_DIR"] = (staged / "system").string();
|
|
Check(ResolveToolchain(in).mingwPrefixIsSystem, "a prefix with a C library's features.h holds a Linux system");
|
|
in.env["CRAFTER_MINGW_DIR"] = (staged / "dedicated").string();
|
|
Check(!ResolveToolchain(in).mingwPrefixIsSystem, "a prefix of mingw-w64 and Windows libraries does not");
|
|
Check(!ResolveToolchain({}).mingwPrefixIsSystem, "no prefix, nothing to show");
|
|
fs::remove_all(staged);
|
|
}
|
|
{
|
|
// Sandboxed, the host's /usr stands in for the missing mingw-w64,
|
|
// unless CRAFTER_MINGW_DIR says otherwise.
|
|
const fs::path host = fs::temp_directory_path() / "crafter-build-toolchain-host";
|
|
fs::remove_all(host);
|
|
ToolchainInputs in;
|
|
in.hostRoot = host;
|
|
Check(ResolveToolchain(in).mingwPrefix.empty(), "a host without mingw-w64 gives no mingw prefix");
|
|
fs::create_directories(host / "usr" / "x86_64-w64-mingw32");
|
|
Check(ResolveToolchain(in).mingwPrefix == host / "usr", "a host with mingw-w64 gives its /usr as the mingw prefix");
|
|
Check(ResolveToolchain(in).mingwPrefixIsSystem, "the host's /usr is the host's system, with or without its C library's headers");
|
|
in.env["CRAFTER_MINGW_DIR"] = "/opt/mingw";
|
|
Check(ResolveToolchain(in).mingwPrefix == "/opt/mingw", "CRAFTER_MINGW_DIR wins over the host's");
|
|
fs::remove_all(host);
|
|
}
|
|
{
|
|
// nvcc, for cfg.cuda: the toolkit CUDA_PATH names, else the first
|
|
// root with a bin/nvcc, else the first nvcc that can be run in an
|
|
// absolute PATH entry.
|
|
const fs::path cuda = scratch / "cuda";
|
|
const fs::path first = cuda / "first";
|
|
const fs::path second = cuda / "second";
|
|
const fs::path onPath = cuda / "on-path";
|
|
Program(second / "bin/nvcc");
|
|
Program(onPath / "nvcc");
|
|
Program(cuda / "named/bin/nvcc");
|
|
Touch(cuda / "not-a-program/nvcc");
|
|
ToolchainInputs in;
|
|
in.cudaRoots = { first, second };
|
|
in.env["PATH"] = std::format("{}::{}:{}", (cuda / "not-a-program").string(), (cuda / "nothing").string(), onPath.string());
|
|
Toolchain tc = ResolveToolchain(in);
|
|
Check(tc.nvcc == second / "bin/nvcc" && tc.nvccError.empty(), std::format("the first toolkit root with a bin/nvcc wins over PATH (got '{}')", tc.nvcc.string()));
|
|
Program(first / "bin/nvcc");
|
|
Check(ResolveToolchain(in).nvcc == first / "bin/nvcc", "the toolkit roots are tried in order");
|
|
in.cudaRoots.clear();
|
|
Check(ResolveToolchain(in).nvcc == onPath / "nvcc", std::format("without a toolkit root, the first nvcc that can be run in an absolute PATH entry (got '{}')", ResolveToolchain(in).nvcc.string()));
|
|
{
|
|
// A relative entry names a directory under whatever the
|
|
// working directory is: passed over, as an empty one is.
|
|
const fs::path cwd = fs::current_path();
|
|
fs::current_path(cuda);
|
|
ToolchainInputs relative;
|
|
relative.env["PATH"] = std::format("on-path:{}", onPath.string());
|
|
Check(ResolveToolchain(relative).nvcc == onPath / "nvcc", std::format("a relative PATH entry is passed over (got '{}')", ResolveToolchain(relative).nvcc.string()));
|
|
relative.env["PATH"] = "on-path";
|
|
Check(ResolveToolchain(relative).nvcc.empty(), "a PATH of relative entries finds no nvcc");
|
|
fs::current_path(cwd);
|
|
}
|
|
in.cudaRoots = { first };
|
|
in.env["CUDA_PATH"] = (cuda / "named").string();
|
|
Check(ResolveToolchain(in).nvcc == cuda / "named/bin/nvcc", "CUDA_PATH wins over the toolkit roots and PATH");
|
|
in.env["CUDA_PATH"] = (cuda / "missing").string();
|
|
tc = ResolveToolchain(in);
|
|
Check(tc.nvcc.empty() && tc.nvccError == std::format("no nvcc at {} (CUDA_PATH)", (cuda / "missing/bin/nvcc").string()), std::format("a CUDA_PATH without bin/nvcc is an error naming it, not a fallback (got '{}')", tc.nvccError));
|
|
Check(tc.error.empty(), "no nvcc is no toolchain error: only a build with CUDA sources needs one");
|
|
{
|
|
// A relative CUDA_PATH is taken against the working directory
|
|
// once, as CRAFTER_MINGW_DIR is: nvcc runs later, from whatever
|
|
// directory a build is in.
|
|
const fs::path cwd = fs::current_path();
|
|
fs::current_path(cuda);
|
|
ToolchainInputs relative;
|
|
relative.env["CUDA_PATH"] = "named";
|
|
const fs::path found = ResolveToolchain(relative).nvcc;
|
|
fs::current_path(cwd);
|
|
Check(found.is_absolute() && fs::equivalent(found, cuda / "named/bin/nvcc"), std::format("a relative CUDA_PATH names an absolute nvcc (got '{}')", found.string()));
|
|
}
|
|
}
|
|
{
|
|
// The static runtime beside the nvcc found: lib64/libcudart_static.a
|
|
// where it is there, else none, as for a lib64 that holds none or
|
|
// cannot be looked into, here one that links to itself, which has to
|
|
// be looked into without throwing.
|
|
const fs::path kits = scratch / "runtime";
|
|
Program(kits / "with/bin/nvcc");
|
|
Touch(kits / "with/lib64/libcudart_static.a");
|
|
Program(kits / "without/bin/nvcc");
|
|
fs::create_directories(kits / "without/lib64");
|
|
Program(kits / "looped/bin/nvcc");
|
|
fs::create_directory_symlink("lib64", kits / "looped/lib64");
|
|
ToolchainInputs in;
|
|
in.env["CUDA_PATH"] = (kits / "with").string();
|
|
const fs::path found = ResolveToolchain(in).cudaRuntime;
|
|
std::error_code ec;
|
|
Check(!found.empty() && fs::equivalent(found, kits / "with/lib64/libcudart_static.a", ec), std::format("the static runtime in the lib64 beside nvcc's bin (got '{}')", found.string()));
|
|
in.env["CUDA_PATH"] = (kits / "without").string();
|
|
Check(ResolveToolchain(in).cudaRuntime.empty(), "none where that lib64 holds none");
|
|
in.env["CUDA_PATH"] = (kits / "looped").string();
|
|
Check(ResolveToolchain(in).cudaRuntime.empty(), "none where the lib64 cannot be looked into");
|
|
in.env["CUDA_PATH"] = (kits / "missing").string();
|
|
Check(ResolveToolchain(in).cudaRuntime.empty(), "and none without an nvcc");
|
|
}
|
|
{
|
|
// Nothing anywhere: the error says where it looked and what to set.
|
|
ToolchainInputs in;
|
|
in.cudaRoots = { "/nonexistent/opt-cuda", "/nonexistent/local-cuda" };
|
|
in.env["PATH"] = (scratch / "no-nvcc").string();
|
|
const Toolchain tc = ResolveToolchain(in);
|
|
const std::string expected = std::format("no CUDA toolkit found: looked for /nonexistent/opt-cuda/bin/nvcc, /nonexistent/local-cuda/bin/nvcc and nvcc on PATH ({}); set CUDA_PATH to the directory a toolkit is installed in", (scratch / "no-nvcc").string());
|
|
Check(tc.nvcc.empty() && tc.nvccError == expected, std::format("the error names every place looked at (got '{}')", tc.nvccError));
|
|
Check(ResolveToolchain({}).nvccError.contains("nvcc on PATH (unset)"), "an unset PATH is said to be unset");
|
|
}
|
|
{
|
|
// Sandboxed, the host's /usr and /etc show under the host root, and
|
|
// a link there to an absolute path names a path of the host, which
|
|
// the sandbox would look up in its own root.
|
|
const fs::path host = fs::temp_directory_path() / "crafter-build-toolchain-cuda-host";
|
|
fs::remove_all(host);
|
|
const fs::path installed = host / "usr/local/cuda-13.0/bin/nvcc";
|
|
Program(installed);
|
|
ToolchainInputs in;
|
|
in.hostRoot = host;
|
|
in.cudaRoots = { "/usr/local/cuda" };
|
|
fs::create_directory_symlink("/usr/local/cuda-13.0", host / "usr/local/cuda");
|
|
Check(ResolveToolchain(in).nvcc == installed, std::format("an absolute link is followed under the host root (got '{}')", ResolveToolchain(in).nvcc.string()));
|
|
in.cudaRoots = { "/usr/local/cuda/" };
|
|
Check(ResolveToolchain(in).nvcc == installed, std::format("and so is one named with a trailing separator, which readlink follows instead of reading (got '{}')", ResolveToolchain(in).nvcc.string()));
|
|
in.cudaRoots = { "/usr/local/cuda" };
|
|
fs::remove(host / "usr/local/cuda");
|
|
fs::create_directories(host / "etc/alternatives");
|
|
fs::create_directory_symlink("/usr/local/cuda-13.0", host / "etc/alternatives/cuda");
|
|
fs::create_directory_symlink("/etc/alternatives/cuda", host / "usr/local/cuda");
|
|
Check(ResolveToolchain(in).nvcc == installed, "so is a link through /etc/alternatives");
|
|
fs::remove(host / "usr/local/cuda");
|
|
fs::create_directory_symlink("cuda-13.0", host / "usr/local/cuda");
|
|
Check(ResolveToolchain(in).nvcc == installed, "and a relative link");
|
|
in.cudaRoots.clear();
|
|
in.env["CUDA_PATH"] = "/usr/local/cuda";
|
|
Check(ResolveToolchain(in).nvcc == installed, "CUDA_PATH names a path of the host too");
|
|
// Outside its /usr and /etc, the host's files are where the host has
|
|
// them, as /opt/cuda is.
|
|
in.env["CUDA_PATH"] = (scratch / "cuda/named").string();
|
|
Check(ResolveToolchain(in).nvcc == scratch / "cuda/named/bin/nvcc", "a toolkit outside the host's /usr is taken where it is");
|
|
// A link there that names a path of the host's /usr is followed
|
|
// under the host root, as one in it is: an /opt/cuda pointing at
|
|
// NVIDIA's /usr/local/cuda-<version>.
|
|
fs::create_directory_symlink("/usr/local/cuda-13.0", scratch / "opt-cuda");
|
|
in.env["CUDA_PATH"] = (scratch / "opt-cuda").string();
|
|
Check(ResolveToolchain(in).nvcc == installed, std::format("a link outside the host's /usr into it is followed under the host root (got '{}')", ResolveToolchain(in).nvcc.string()));
|
|
// A link to itself, under a name no machine's own root has: the
|
|
// kernel, which follows it in this process's root, must not reach a
|
|
// toolkit through it either.
|
|
fs::create_directory_symlink("/usr/local/crafter-build-cuda-loop", host / "usr/local/crafter-build-cuda-loop");
|
|
in.env["CUDA_PATH"] = "/usr/local/crafter-build-cuda-loop";
|
|
Check(ResolveToolchain(in).nvcc.empty(), "a link to itself ends, and finds nothing");
|
|
fs::remove_all(host);
|
|
}
|
|
{
|
|
// What VariantId keys a CUDA configuration's nvcc by: its canonical
|
|
// path, size and time, which an nvcc replaced at its path changes,
|
|
// as it does not change the path.
|
|
const fs::path toolkit = scratch / "stamped";
|
|
Program(toolkit / "bin/nvcc");
|
|
ToolchainInputs in;
|
|
in.env["CUDA_PATH"] = toolkit.string();
|
|
const std::string before = ResolveToolchain(in).nvccStamp;
|
|
Check(before.starts_with(std::format("{}|", fs::canonical(toolkit / "bin/nvcc").string())), std::format("the stamp names the nvcc where its links lead (got '{}')", before));
|
|
std::ofstream(toolkit / "bin/nvcc", std::ios::app) << "13.5";
|
|
Check(ResolveToolchain(in).nvccStamp != before, "an nvcc replaced at its path stamps differently");
|
|
Check(ResolveToolchain({}).nvccStamp.empty(), "no nvcc, no stamp");
|
|
}
|
|
{
|
|
// What VariantId keys -arch=native by: the NVIDIA display devices the
|
|
// PCI bus lists, by slot and model, and CUDA_VISIBLE_DEVICES and
|
|
// CUDA_DEVICE_ORDER. Another GPU, the same GPUs in each other's slots,
|
|
// or another choice or numbering among them keys it differently; a
|
|
// GPU's audio function, another NVIDIA class, and another vendor's GPU
|
|
// do not count.
|
|
const fs::path toolkit = scratch / "native/toolkit";
|
|
Program(toolkit / "bin/nvcc");
|
|
const fs::path pci = scratch / "native/pci";
|
|
const auto device = [&pci](std::string_view address, std::string_view vendor, std::string_view id, std::string_view type) {
|
|
fs::create_directories(pci / address);
|
|
std::ofstream(pci / address / "vendor") << vendor << "\n";
|
|
std::ofstream(pci / address / "device") << id << "\n";
|
|
std::ofstream(pci / address / "class") << type << "\n";
|
|
};
|
|
device("0000:34:00.0", "0x10de", "0x2684", "0x030000");
|
|
device("0000:34:00.1", "0x10de", "0x22ba", "0x040300");
|
|
device("0000:00:02.0", "0x8086", "0xa780", "0x030000");
|
|
ToolchainInputs in;
|
|
in.env["CUDA_PATH"] = toolkit.string();
|
|
in.pciDevices = pci;
|
|
const std::string one = ResolveToolchain(in).cudaNativeKey;
|
|
Check(one.contains("0x2684") && !one.contains("0x22ba") && !one.contains("0xa780"), std::format("native is keyed by the NVIDIA display devices alone (got '{}')", one));
|
|
device("0000:ca:00.0", "0x10de", "0x2b85", "0x030200");
|
|
const std::string two = ResolveToolchain(in).cudaNativeKey;
|
|
Check(two != one && two.contains("0x2b85"), std::format("another GPU keys it differently (got '{}')", two));
|
|
in.cudaVisibleDevices = "1";
|
|
const std::string chosen = ResolveToolchain(in).cudaNativeKey;
|
|
Check(chosen != two, "and so does CUDA_VISIBLE_DEVICES");
|
|
in.cudaVisibleDevices = "";
|
|
const std::string hidden = ResolveToolchain(in).cudaNativeKey;
|
|
Check(hidden != two && hidden != chosen, std::format("an empty CUDA_VISIBLE_DEVICES, which hides every GPU, keys apart from an unset one (got '{}')", hidden));
|
|
in.cudaVisibleDevices.reset();
|
|
Check(ResolveToolchain(in).cudaNativeKey == two, "unset again, the key is as it was");
|
|
in.cudaDeviceOrder = "PCI_BUS_ID";
|
|
Check(ResolveToolchain(in).cudaNativeKey != two, "CUDA_DEVICE_ORDER, which numbers the GPUs, keys it differently");
|
|
in.cudaDeviceOrder.reset();
|
|
device("0000:34:00.0", "0x10de", "0x2b85", "0x030200");
|
|
device("0000:ca:00.0", "0x10de", "0x2684", "0x030000");
|
|
const std::string swapped = ResolveToolchain(in).cudaNativeKey;
|
|
Check(swapped != two, std::format("so do the same GPUs in each other's slots (got '{}')", swapped));
|
|
in.env.erase("CUDA_PATH");
|
|
Check(ResolveToolchain(in).cudaNativeKey.empty(), "no nvcc, no key");
|
|
}
|
|
{
|
|
// The cache key separates toolchains that would build different
|
|
// PCMs and nothing else.
|
|
Toolchain a = ResolveToolchain({});
|
|
a.clangVersion = "clang version 22.1.8";
|
|
Toolchain b = a;
|
|
b.clangVersion = "Arch Linux clang version 22.1.9";
|
|
Toolchain c = a;
|
|
c.libcxxDir = "/opt/libcxx";
|
|
Toolchain d = ResolveToolchain({});
|
|
d.clangVersion = a.clangVersion;
|
|
Check(a.Key().starts_with("22.1.8-"), std::format("the key starts with the short clang version (got '{}')", a.Key()));
|
|
Check(b.Key().starts_with("22.1.9-"), "the short version is parsed past a distro prefix");
|
|
Check(a.Key() != b.Key(), "a different clang version changes the key");
|
|
Check(a.Key() != c.Key(), "a different libc++ location changes the key");
|
|
Check(a.Key() == d.Key(), "the key is deterministic");
|
|
}
|
|
{
|
|
// The real toolchain on this machine resolves and the probes work;
|
|
// HostTarget is the probe's answer.
|
|
const Toolchain& real = GetToolchain();
|
|
Check(real.error.empty(), std::format("the real toolchain resolves (error: '{}')", real.error));
|
|
Check(!real.hostTriple.empty() && real.hostTriple == HostTarget(), "HostTarget comes from the toolchain probe");
|
|
Check(!real.clangVersion.empty(), "the clang version was probed");
|
|
Check(!real.Key().starts_with("unknown-"), std::format("the real clang version parses (got '{}')", real.Key()));
|
|
}
|
|
|
|
{
|
|
// The runtime crafter-build runs on is the runtime= ref in
|
|
// /.flatpak-info's [Application] group: what a packaged program
|
|
// links against, and so the runtime it is packaged for.
|
|
ToolchainInputs in;
|
|
in.flatpakInfo = "[Application]\nname=net.catcrafts.CrafterBuild\nruntime=runtime/org.freedesktop.Sdk/x86_64/26.08\n\n[Instance]\nruntime=runtime/org.example.Other/aarch64/1\n";
|
|
const Toolchain tc = ResolveToolchain(in);
|
|
Check(tc.flatpakArch == "x86_64" && tc.flatpakBranch == "26.08", std::format("the runtime's arch and branch are read (got '{}' '{}')", tc.flatpakArch, tc.flatpakBranch));
|
|
Check(ResolveToolchain({}).flatpakBranch.empty(), "outside a Flatpak there is no runtime");
|
|
ToolchainInputs elsewhere;
|
|
elsewhere.flatpakInfo = "[Instance]\nruntime=runtime/org.freedesktop.Sdk/x86_64/26.08\n";
|
|
Check(ResolveToolchain(elsewhere).flatpakBranch.empty(), "a runtime= outside [Application] is not the runtime");
|
|
ToolchainInputs malformed;
|
|
malformed.flatpakInfo = "[Application]\nruntime=org.freedesktop.Sdk/26.08\n";
|
|
Check(ResolveToolchain(malformed).flatpakBranch.empty(), "a runtime= that is not a runtime ref is ignored");
|
|
|
|
// llvm-readelf, which reads what a packaged program needs, sits
|
|
// beside the compiler like llvm-ar; LibcxxSearchFlags is the link
|
|
// flags without the rpath a Flatpak does not use.
|
|
Check(ResolveToolchain({}).llvmReadelf == "llvm-readelf", "the PATH fallback names llvm-readelf bare");
|
|
ToolchainInputs prefixed;
|
|
prefixed.env.emplace("CRAFTER_BUILD_LLVM_DIR", full.string());
|
|
const Toolchain withPrefix = ResolveToolchain(prefixed);
|
|
Check(withPrefix.llvmReadelf == full / "bin" / "llvm-readelf", "llvm-readelf comes from the prefix");
|
|
// So does clang-scan-deps, which reads what a source declares and
|
|
// imports.
|
|
Check(ResolveToolchain({}).clangScanDeps == "clang-scan-deps", "the PATH fallback names clang-scan-deps bare");
|
|
Check(withPrefix.clangScanDeps == full / "bin" / "clang-scan-deps", "clang-scan-deps comes from the prefix");
|
|
Check(withPrefix.LibcxxSearchFlags() == std::format(" -L{}", (full / "lib").string()), std::format("the search flags are -L alone (got '{}')", withPrefix.LibcxxSearchFlags()));
|
|
}
|
|
|
|
fs::remove_all(scratch, ec);
|
|
return Failures == 0 ? 0 : 1;
|
|
}
|