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:
parent
6b2da3b1d6
commit
22ea97406d
6 changed files with 58 additions and 7 deletions
|
|
@ -23,7 +23,7 @@ crafter-build is a C++26 modules build system. A project is described by a `proj
|
|||
|
||||
Partitions and what they own:
|
||||
|
||||
- `:Toolchain` resolves the compiler once per process (`GetToolchain`): clang++, clang, llvm-ar, llvm-readelf, libclang, libc++'s std module source and the default sysroots, from `CRAFTER_BUILD_LLVM_DIR`, else a toolchain installed beside crafter-build, else PATH. It also holds the version baked in at build time, and the arch and branch of the Flatpak runtime crafter-build runs on, from `/.flatpak-info` or the file `CRAFTER_BUILD_FLATPAK_INFO` names, and the nvcc `cfg.cuda` compiles with: `$CUDA_PATH/bin/nvcc`, else `bin/nvcc` under `/opt/cuda` or `/usr/local/cuda`, else the first in an absolute entry of PATH, which it searches itself. In the Flatpak the host's `/usr` and `/etc` are under `/run/host`, and a link to an absolute path names a host path, so where the path of a toolkit is a link, it follows that link, and the link its target is in turn, each as the host's (`HostPath`); a link in a directory partway along that path, or inside the toolkit, is the kernel's to resolve, in the sandbox's root. `SysrootStdModule` picks the std module source for a sysroot build by the sysroot's libc++ release. `MingwSysroot` settles the mingw target's `--sysroot`: the mingw-w64 prefix itself, or, for a prefix that also holds a Linux system (the host's `/usr`, anything whose `include/` has `features.h`), a view in the cache of its mingw-w64 directories, since clang would search its `include/` and `lib/` too (issue #43). `MingwViewToPrefix` turns paths into the view back into paths into the prefix for messages.
|
||||
- `:Toolchain` resolves the compiler once per process (`GetToolchain`): clang++, clang, llvm-ar, llvm-readelf, libclang, libc++'s std module source and the default sysroots, from `CRAFTER_BUILD_LLVM_DIR`, else a toolchain installed beside crafter-build, else PATH. It also holds the version baked in at build time, and the arch and branch of the Flatpak runtime crafter-build runs on, from `/.flatpak-info` or the file `CRAFTER_BUILD_FLATPAK_INFO` names, and the nvcc `cfg.cuda` compiles with: `$CUDA_PATH/bin/nvcc`, else `bin/nvcc` under `/opt/cuda` or `/usr/local/cuda`, else the first in an absolute entry of PATH, which it searches itself, and `cudaRuntime`, the `lib64/libcudart_static.a` beside that nvcc's `bin` where there is one, which a configuration with CUDA sources links. In the Flatpak the host's `/usr` and `/etc` are under `/run/host`, and a link to an absolute path names a host path, so where the path of a toolkit is a link, it follows that link, and the link its target is in turn, each as the host's (`HostPath`); a link in a directory partway along that path, or inside the toolkit, is the kernel's to resolve, in the sandbox's root. `SysrootStdModule` picks the std module source for a sysroot build by the sysroot's libc++ release. `MingwSysroot` settles the mingw target's `--sysroot`: the mingw-w64 prefix itself, or, for a prefix that also holds a Linux system (the host's `/usr`, anything whose `include/` has `features.h`), a view in the cache of its mingw-w64 directories, since clang would search its `include/` and `lib/` too (issue #43). `MingwViewToPrefix` turns paths into the view back into paths into the prefix for messages.
|
||||
- `:Clang` holds `Configuration`, `Build()`, `VariantId()`, `ApplyStandardArgs`, `HostTarget`, the CLI entry point `Crafter::Run`, and the lint, test and script data types.
|
||||
- `:Platform` runs commands, implements `LoadProject` (compile and load `project.cpp`), the host PCM cache, `#pragma crafter dependency`, and all OS-specific code.
|
||||
- `:Interface` and `:Implementation` model module units, their staleness checks and their compiles.
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -348,6 +348,14 @@ Toolchain Crafter::ResolveToolchain(const ToolchainInputs& in) {
|
|||
const auto spelled = [](const std::optional<std::string>& value) { return value ? std::format("={}", *value) : std::string("unset"); };
|
||||
tc.cudaNativeKey = std::format("{}|{}|{}", std::views::join_with(gpus, std::string(",")) | std::ranges::to<std::string>(), spelled(in.cudaVisibleDevices), spelled(in.cudaDeviceOrder));
|
||||
}
|
||||
// The static runtime beside that nvcc, for the link of a configuration
|
||||
// with CUDA sources. Looked for without throwing: a lib64 that cannot be
|
||||
// looked into, through a link loop or for want of permission, counts as
|
||||
// one without it.
|
||||
if (!tc.nvcc.empty()) {
|
||||
const fs::path archive = tc.nvcc.parent_path().parent_path() / "lib64" / "libcudart_static.a";
|
||||
if (std::error_code ec; fs::exists(archive, ec)) tc.cudaRuntime = archive;
|
||||
}
|
||||
std::tie(tc.flatpakArch, tc.flatpakBranch) = FlatpakRuntimeOf(in.flatpakInfo);
|
||||
return tc;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,13 @@ export namespace Crafter {
|
|||
// recorded last time, so they come out the same however the
|
||||
// dependency threads that fill them happen to finish.
|
||||
std::set<std::string> libs;
|
||||
// The CUDA runtime that a configuration with CUDA sources, or one it
|
||||
// is built on, links: Toolchain::cudaRuntime by its path and the -lrt
|
||||
// -lpthread -ldl it needs, or nothing where the toolkit has none. By
|
||||
// its path, so no directory of the toolkit's is searched for a
|
||||
// project's own libraries; after libs on every link, so a -lcudart a
|
||||
// project names itself is the one the linker takes.
|
||||
std::set<std::string> runtimeLibs;
|
||||
// Compile flags (typically -I include paths) the dep wants its
|
||||
// consumers to see — sourced from its external dependencies' include
|
||||
// dirs so headers a dep exposes in its public modules are reachable
|
||||
|
|
|
|||
|
|
@ -78,6 +78,12 @@ export namespace Crafter {
|
|||
// them and number them, each unset apart from empty. Empty without an
|
||||
// nvcc.
|
||||
std::string cudaNativeKey;
|
||||
// The static CUDA runtime a configuration with CUDA sources links:
|
||||
// libcudart_static.a in the lib64 beside nvcc's bin. Empty without an
|
||||
// nvcc, and where that lib64 holds none, as where a distribution keeps
|
||||
// the toolkit's libraries in its own directories, or cannot be looked
|
||||
// into: then nothing is linked, and cfg.linkFlags names the runtime.
|
||||
fs::path cudaRuntime;
|
||||
// The Flatpak runtime crafter-build runs on, from the
|
||||
// runtime=runtime/<id>/<arch>/<branch> line of /.flatpak-info:
|
||||
// "x86_64" and "26.08" in the Flatpak, both empty outside one. A
|
||||
|
|
|
|||
Loading…
Reference in a new issue