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

@ -1054,6 +1054,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
std::string files;
std::set<std::string> libSet;
std::set<std::string> runtimeSet;
std::set<std::string> publicFlagSet;
std::set<std::string> runPathSet;
std::mutex fileMutex;
@ -1070,6 +1071,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
const BuildResult& result = built.get();
fileMutex.lock();
for (const std::string& lib : result.libs) libSet.insert(lib);
for (const std::string& lib : result.runtimeLibs) runtimeSet.insert(lib);
for (const std::string& f : result.publicCompileFlags) publicFlagSet.insert(f);
for (const std::string& dir : result.runPaths) runPathSet.insert(dir);
fileMutex.unlock();
@ -1270,10 +1272,31 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
buildResult.repack = true;
}
buildResult.libs = std::move(libSet);
buildResult.runtimeLibs = std::move(runtimeSet);
buildResult.runPaths = std::move(runPathSet);
for(const std::string& flag : config.linkFlags) {
buildResult.libs.insert(flag);
}
// CUDA sources link the runtime nvcc's own link takes, the static one, of
// the toolkit whose nvcc compiles them: Toolchain::cudaRuntime, with the
// -lrt -lpthread -ldl nvcc's link line puts after it. Static, so no run
// path into the toolkit, which in the Flatpak lies under /run/host, ends
// up in the program; libcuda.so.1, which the runtime loads, comes with the
// driver. Named by its path: a -L of the toolkit's lib64 would put its
// libraries, such as Arch's libcheckpoint.so, in the way of a project's
// own of the same name. A toolkit without one gets nothing linked, as
// before, and cfg.linkFlags names the runtime: a -lcudart_static the
// linker could not find would fail the link even of a project that names
// its own -lcudart. In runtimeLibs, which a configuration hands on as it
// does libs and every link puts after them: whatever is built on a library
// with CUDA sources links the runtime too, and a -lcudart a project names
// itself comes first. Without an nvcc there is no runtime either, and no
// object: VariantId keys the nvcc, and the .cu's compile above has already
// said where it looked.
if (const Toolchain& toolchain = GetToolchain(); !config.cuda.empty() && !toolchain.cudaRuntime.empty()) {
buildResult.runtimeLibs.insert(ShellQuote(toolchain.cudaRuntime.string()));
for (const std::string_view lib : {"-lrt", "-lpthread", "-ldl"}) buildResult.runtimeLibs.emplace(lib);
}
// Public compile flags propagated from sub-deps. Add them to this build's
// command so config sees the headers its deps expose, and re-publish them
// so config's own consumers see them transitively.
@ -1357,6 +1380,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
for(const std::string& flag : buildResult.libs) {
linkExtras += std::format(" {}", flag);
}
for (const std::string& lib : buildResult.runtimeLibs) linkExtras += std::format(" {}", lib);
// The program loads its shared dependencies from where they were built,
// the external cache among them. ExternalOutputs leaves the mingw target
// without any: PE resolves DLLs another way. A Flatpak has none of those
@ -2083,10 +2107,12 @@ 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.
// build that has some says where it looked. The static runtime beside it
// as well, which says whether one is linked. 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.cudaRuntime.empty()) std::println("cudart {}", tc.cudaRuntime.string());
};
if (!tc.error.empty()) {
std::println("compiler: {}", tc.error);