From 2fd45133a0afa6a9cdb74080a64e8b90d63858eb Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Wed, 11 Nov 2026 19:34:52 +0100 Subject: [PATCH] test(cuda): resolve nvcc on staged toolkits ToolchainResolve stages toolkit roots, a PATH and a host root, and checks the order (CUDA_PATH, then the roots, then PATH), that a CUDA_PATH without an nvcc is an error and not a fallback, that a relative one names an absolute nvcc, that a file that cannot be run is no nvcc and a relative PATH entry no place to look, what the error says when there is none, and that a toolkit resolves under the host root through a link: /usr/local/cuda in the host's /usr to an absolute path, named with a trailing slash too, through /etc/alternatives and to a relative path, and a link outside the host's /usr that leads into it. A link to itself finds nothing. --- tests/ToolchainResolve/main.cpp | 114 ++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/tests/ToolchainResolve/main.cpp b/tests/ToolchainResolve/main.cpp index 2ef5abf..849c36a 100644 --- a/tests/ToolchainResolve/main.cpp +++ b/tests/ToolchainResolve/main.cpp @@ -26,6 +26,12 @@ namespace { 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; } @@ -188,6 +194,114 @@ int main() { 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())); + } + } + { + // 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-. + 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); + } { // The cache key separates toolchains that would build different // PCMs and nothing else.