feat(cuda): link the CUDA runtime of the toolkit that compiles the .cu

A program with CUDA sources needs the CUDA runtime, which cfg.linkFlags
had to name: -L/opt/cuda/lib64 -lcudart for Arch's toolkit, a path into
it. In the Flatpak that holds for /opt/cuda but not for the host's
/usr/local/cuda, which is under /run/host there, at a path crafter-build
resolves for nvcc and a project.cpp does not know. Jorijn chose to have
crafter-build link it.

A configuration with CUDA sources links the static runtime of the
toolkit whose nvcc compiles them, by its path, with the -lrt -lpthread
-ldl that nvcc's own link line puts after it. ResolveToolchain finds it
with nvcc, as Toolchain::cudaRuntime: libcudart_static.a in the lib64
beside nvcc's bin, looked for without starting a process, so a test can
stage a toolkit for it as for nvcc, and crafter-build --version prints
it. The static one, so no run path into the toolkit ends up in the
program; libcuda.so.1, which the runtime loads, comes with the driver.
By its path, not through a -L of the lib64, which would put the
toolkit's libraries, such as Arch's libcheckpoint.so, in the way of a
project's own of the same name. It goes into BuildResult::runtimeLibs,
which a configuration hands its consumers as it does libs and every link
puts after them, so whatever is built on a library with CUDA sources
links the runtime too, and a -lcudart a project names itself comes
first.

Where that lib64 holds none, as where a distribution keeps the toolkit's
libraries in its own directories, cudaRuntime is empty and nothing is
linked, as before: a -lcudart_static the linker could not find would
fail the link even of a project that names its own -lcudart. The archive
is looked for without throwing, so a lib64 that cannot be looked into,
through a link loop or for want of permission, counts as one without it.
This commit is contained in:
Jorijn van der Graaf 2026-11-11 19:34:52 +01:00
commit 22ea97406d
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
6 changed files with 58 additions and 7 deletions

View file

@ -507,10 +507,11 @@ namespace {
struct ProjectFileDependencies {
std::vector<Configuration*> configs;
std::string compileFlags; // " -fprebuilt-module-path=…" for each dependency and its transitive deps
// Every dependency's BuildResult.libs, verbatim, then a run path per
// directory of its runPaths (RunPathFlags), as Build() links a
// program: the shared libraries a pragma dependency is built on are
// found there when LoadProject dlopens project.so.
// Every dependency's BuildResult.libs, verbatim, then its runtimeLibs,
// the CUDA runtime, then a run path per directory of its runPaths
// (RunPathFlags), as Build() links a program: the shared libraries a
// pragma dependency is built on are found there when LoadProject
// dlopens project.so.
std::string linkFlags;
fs::file_time_type newestArtifact = fs::file_time_type::min();
};
@ -529,6 +530,7 @@ namespace {
std::unordered_set<Configuration*> forced;
std::unordered_set<Configuration*> walked;
std::set<std::string> libs;
std::set<std::string> runtimeLibs;
std::set<std::string> runPaths;
for (const PragmaDependency& pragma : pragmas) {
@ -574,6 +576,7 @@ namespace {
if (!built.result.empty()) PragmaError(absProject, pragma.line, std::format("project dependency '{}' failed to build: {}", cfg->name, built.result));
out.configs.push_back(cfg);
libs.insert(built.libs.begin(), built.libs.end());
runtimeLibs.insert(built.runtimeLibs.begin(), built.runtimeLibs.end());
runPaths.insert(built.runPaths.begin(), built.runPaths.end());
// The module path needs the dependency and everything it depends
@ -590,6 +593,7 @@ namespace {
walk(cfg);
}
for (const std::string& lib : libs) out.linkFlags += std::format(" {}", lib);
for (const std::string& lib : runtimeLibs) out.linkFlags += std::format(" {}", lib);
for (const std::string& dir : runPaths) out.linkFlags += RunPathFlags(dir);
return out;
}