// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® #include import std; import Crafter.Build; namespace fs = std::filesystem; using namespace Crafter; // cfg.cuda is compiled with the nvcc ResolveToolchain finds, for the GPU // architecture cfg.cudaArch names. It used to be whatever "nvcc" meant to the // shell, for sm_89 alone: crafter-build's Flatpak sets PATH to // /app/bin:/usr/bin, and with that PATH every CUDA build there stopped at // "nvcc: command not found" with the host's toolkit in reach at /opt/cuda. // // The test puts an nvcc on PATH that fails, names a toolkit through CUDA_PATH // whose nvcc writes down its arguments and runs the real one, and builds an // executable from a C++ unit and a .cu: with the default architecture, which // is native, then with another one, which compiles the unchanged .cu again in // a directory of its own, then once more with nothing changed, which compiles // nothing. Where nvcc is looked for is ToolchainResolve's to check; this is // the build that uses it. Skips without a CUDA toolkit, which CI does not // have. namespace { std::int32_t Failures = 0; void Check(bool cond, std::string_view msg) { if (!cond) { std::println(std::cerr, "FAIL: {}", msg); ++Failures; } } // The nvcc crafter-build finds in this environment, looked for with the // inputs GetToolchain() gives ResolveToolchain, the host's /usr under // /run/host in a sandbox included, before the test changes CUDA_PATH and // PATH. ResolveToolchain is pure: GetToolchain()'s cache stays empty. Toolchain RealToolkit() { ToolchainInputs in; for (const std::string_view name : {"CUDA_PATH", "PATH"}) { if (const char* value = std::getenv(std::string(name).c_str()); value && *value) in.env.emplace(std::string(name), value); } if (fs::exists("/.flatpak-info")) in.hostRoot = "/run/host"; in.cudaRoots = { "/opt/cuda", "/usr/local/cuda" }; return ResolveToolchain(in); } void WriteProgram(const fs::path& file, std::string_view text) { fs::create_directories(file.parent_path()); std::ofstream(file) << text; fs::permissions(file, fs::perms::owner_exec, fs::perm_options::add); } std::vector Lines(const fs::path& file) { std::vector lines; std::ifstream in(file); for (std::string line; std::getline(in, line);) lines.push_back(line); return lines; } std::string Joined(const std::vector& lines) { return std::views::join_with(lines, std::string(" | ")) | std::ranges::to(); } } int main() { const Toolchain real = RealToolkit(); if (real.nvcc.empty()) { std::println("SKIP: {}", real.nvccError); return 77; } const fs::path nvcc = real.nvcc; const fs::path scratch = fs::temp_directory_path() / "crafter-build-cuda-compile"; std::error_code ec; fs::remove_all(scratch, ec); fs::create_directories(scratch); const fs::path project = scratch / "project"; fs::copy(fs::current_path() / "tests" / "CudaCompile" / "fixture", project, fs::copy_options::recursive); const fs::path log = scratch / "nvcc.log"; const fs::path toolkit = scratch / "toolkit"; WriteProgram(toolkit / "bin" / "nvcc", std::format("#!/bin/sh\nprintf '%s\\n' \"$*\" >> {}\nexec {} \"$@\"\n", ShellQuote(log.string()), ShellQuote(nvcc.string()))); const fs::path decoy = scratch / "decoy"; WriteProgram(decoy / "nvcc", "#!/bin/sh\necho 'the nvcc on PATH ran' >&2\nexit 1\n"); // Before anything resolves the toolchain, which reads the environment // once per process: the first Configuration does, for its target. setenv("CUDA_PATH", toolkit.c_str(), 1); const char* path = std::getenv("PATH"); setenv("PATH", std::format("{}:{}", decoy.string(), path ? path : "").c_str(), 1); // The CUDA runtime the .cu calls into, from the real toolkit. const std::string runtimeDir = std::format("-L{}", (nvcc.parent_path().parent_path() / "lib64").string()); const auto build = [&](std::optional arch) { Configuration app; app.path = project; app.name = "cuda"; app.outputName = "cuda"; app.type = ConfigurationType::Executable; if (arch) app.cudaArch = *arch; std::array ifaces = {}; std::array impls = { "main" }; app.GetInterfacesAndImplementations(ifaces, impls); app.cuda = { project / "kernel" }; app.linkFlags = { runtimeDir, "-lcudart" }; std::unordered_map> depResults; std::mutex depMutex; const BuildResult result = Build(app, depResults, depMutex); return std::pair{ result.result, app.BinDir() / "cuda" }; }; const auto [native, nativeBinary] = build(std::nullopt); Check(native.empty(), std::format("the build with the default architecture succeeds: {}", native)); Check(fs::exists(nativeBinary), "it links the executable"); std::vector runs = Lines(log); Check(runs.size() == 1 && runs.front().contains("-arch=native"), std::format("CUDA_PATH's nvcc compiles the .cu once, with -arch=native (ran: {})", Joined(runs))); const auto [other, otherBinary] = build("sm_86"); Check(other.empty(), std::format("the build for sm_86 succeeds: {}", other)); runs = Lines(log); Check(runs.size() == 2 && runs.back().contains("-arch=sm_86"), std::format("another architecture compiles the unchanged .cu again, for it (ran: {})", Joined(runs))); Check(fs::exists(otherBinary) && otherBinary.parent_path() != nativeBinary.parent_path(), "in a directory of its own"); const auto [again, againBinary] = build("sm_86"); Check(again.empty() && Lines(log).size() == 2, std::format("an unchanged build compiles nothing (ran: {})", Joined(Lines(log)))); fs::remove_all(scratch, ec); return Failures == 0 ? 0 : 1; }