fix(cuda): look for nvcc instead of leaving it to PATH

cfg.cuda compiled with a bare "nvcc", which the shell looked up on PATH.
In crafter-build's Flatpak PATH is /app/bin:/usr/bin, as its manifest
sets it, which never holds the host's toolkit, so with it every build
with CUDA sources stopped at "sh: line 1: nvcc: command not found",
although the sandbox sees /opt/cuda through --filesystem=host.

ResolveToolchain now finds nvcc as it finds clang++: CUDA_PATH, which is
exclusive as CRAFTER_BUILD_LLVM_DIR is, else /opt/cuda (Arch's package)
or /usr/local/cuda (NVIDIA's installer and packages), else the absolute
entries of PATH, which it searches itself so that a build without a
toolkit can say where it looked. A relative CUDA_PATH is made absolute
there, once, as CRAFTER_MINGW_DIR is, since nvcc runs later from
whatever working directory a build is in. The sandbox shows the host's
/usr and /etc under /run/host, with its own runtime at /usr, and
NVIDIA's /usr/local/cuda is a link, to /usr/local/cuda-<version> or
through /etc/alternatives. A link to an absolute path names a path of
the host, /opt/cuda pointing at /usr/local/cuda-<version> too, and the
sandbox would look it up in its own root, so where the path of a
toolkit, CUDA_PATH, /opt/cuda or /usr/local/cuda, is such a link,
HostPath follows it, and the link its target is in turn, each taken as
the host's; a link partway along that path, or inside the toolkit, is
the kernel's to resolve, in the sandbox's root.

A missing toolkit is reported by the build that has a .cu to compile and
not by GetToolchain(), so no other build fails for want of one.
crafter-build --version prints the nvcc it found, whether or not the
compiler could be resolved.
This commit is contained in:
Jorijn van der Graaf 2026-11-11 19:34:52 +01:00
commit fdba70d176
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
4 changed files with 132 additions and 4 deletions

View file

@ -1147,11 +1147,19 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
// untested here (no CUDA toolkit in CI), so a .cu that only sees a
// changed header does not rebuild yet.
if (!fs::exists(objPath) || (fs::exists(srcPath) && fs::last_write_time(srcPath) > fs::last_write_time(objPath))) {
threads.emplace_back([&cFile, &buildDir, &buildError, &buildCancelled]() {
const Toolchain& toolchain = GetToolchain();
if (toolchain.nvcc.empty()) {
bool expected = false;
if (buildCancelled.compare_exchange_strong(expected, true)) {
buildError = std::format("cannot compile {}.cu: {}", cFile.filename().string(), toolchain.nvccError);
}
break;
}
threads.emplace_back([&cFile, &buildError, &buildCancelled, &toolchain, srcPath, objPath]() {
Progress::Task task(std::format("Compiling {}.cu", cFile.filename().string()));
if (buildCancelled.load(std::memory_order_relaxed)) return;
std::string result = RunBuildCommand(std::format("nvcc {}.cu -c -o {}_source.o -O3 -arch=sm_89", cFile.string(), (buildDir / cFile.filename()).string()));
std::string result = RunBuildCommand(std::format("{} {} -c -o {} -O3 -arch=sm_89", ShellQuote(toolchain.nvcc.string()), ShellQuote(srcPath), ShellQuote(objPath)));
if (result.empty()) return;
bool expected = false;
@ -2049,8 +2057,15 @@ std::vector<fs::path> Crafter::CleanProject(const fs::path& projectFile) {
static void PrintVersion() {
std::println("crafter-build {}", CrafterBuildVersion());
const Toolchain& tc = GetToolchain();
// nvcc only when there is one: most projects have no CUDA sources, and the
// build that has some says where it looked. Found apart from the compiler,
// so printed whether or not that was resolved.
const auto cuda = [&tc]() {
if (!tc.nvcc.empty()) std::println("nvcc {}", tc.nvcc.string());
};
if (!tc.error.empty()) {
std::println("compiler: {}", tc.error);
cuda();
return;
}
std::println("compiler {} ({})", tc.clangVersion, tc.clangxx.string());
@ -2069,6 +2084,7 @@ static void PrintVersion() {
std::println("libc++ {}{}", LibcxxVersionString(tc.libcxxVersion), also.empty() ? std::string() : std::format(" (std module sources for sysroots also for{})", also.substr(1)));
}
std::println("host {}", tc.hostTriple);
cuda();
}
static void PrintHelp(std::string_view argv0) {
@ -2202,6 +2218,10 @@ Project args:
Environment:
CRAFTER_BUILD_MARCH Override -march (default: native).
CRAFTER_BUILD_MTUNE Override -mtune (default: native).
CUDA_PATH CUDA toolkit whose bin/nvcc compiles
cfg.cuda. Default: /opt/cuda, then
/usr/local/cuda (in the Flatpak, the
host's), then nvcc on PATH.
CRAFTER_BUILD_RUNNER_<TARGET> Default test runner for a target triple.
Replace '-' and '.' with '_' in the
triple. CLI --runner= overrides this.