diff --git a/AGENTS.template.md b/AGENTS.template.md index 941ff27..a99827a 100644 --- a/AGENTS.template.md +++ b/AGENTS.template.md @@ -207,6 +207,6 @@ Local builds are two scripts in `project.cpp`. Run them with the native binary: - The Flatpak carries the debugging tools for what it builds, because those programs run in its sandbox: lldb is a project of the `llvm` module, with `LLDB_ENABLE_PYTHON`, `LIBEDIT`, `CURSES`, `LIBXML2` and `LZMA` set ON so that a missing dependency fails the configure step (SWIG through Python, which needs it), and the `swig` and `libedit` modules come before it. The Khronos validation layer is the `vulkan-validation-layers` module plus four modules before it, build-only except for SPIRV-Tools' programs (`spirv-val` and the rest), all after the LLVM and WASI modules, so that bumping them reuses the cached LLVM and WASI builds. The layer follows the Sdk's Vulkan loader: when `runtime-version` moves to a runtime with a newer loader, move the layer to the Vulkan SDK release of that loader and take the other four revisions from that release's `scripts/known_good.json`. - The wasm compile flags live in one place, `WasmBaselineFlags` in `Crafter.Build-Clang.cppm`: the std module and every unit have to agree on them, and `VariantId()` and the std PCM directory hash them, so a change rebuilds instead of mixing old and new BMIs. They enable C++ exceptions, so the link takes the exception-enabled libc++ and libunwind from the sysroot's `lib//eh/` (wasi-sdk 33 and later). The Flatpak's sysroot has them and a distro's may not; where they are missing the wasm tests skip, so run those through the Flatpak. A browser page checks the engine with the `WasmBaselineProbes` named in `variants.json`. - `WasiBrowserRuntimeExit` runs `wasi-runtime/runtime.js` under node and skips where there is none. The Flatpak has no node, so CI never runs it: after touching `runtime.js`, run the suite in the native loop with node on `PATH`. -- `CudaCompile` builds with a real CUDA toolkit and skips where there is none, and nothing in CI installs one: after touching how `cfg.cuda` is compiled, run it in the native loop on a machine with a toolkit. Where nvcc is looked for, `/run/host` and its links included, is `ToolchainResolve`'s to check, on staged directories, so that one runs everywhere. `CudaMissing` checks what a build with a `.cu` says when no nvcc is found, and needs no toolkit either. +- `CudaCompile` builds with a real CUDA toolkit whose `lib64` beside `bin/nvcc` holds `libcudart_static.a`, and skips everywhere else, and nothing in CI installs one: after touching how `cfg.cuda` is compiled, or how the CUDA runtime is linked and handed on through `BuildResult::runtimeLibs`, run it in the native loop on a machine with such a toolkit, as Arch's `/opt/cuda` is. Where nvcc is looked for, `/run/host` and its links included, is `ToolchainResolve`'s to check, on staged directories, so that one runs everywhere, and so is whether the `lib64` beside it holds the static runtime. `CudaMissing` checks what a build with a `.cu` says when no nvcc is found, and needs no toolkit either. - `VariantId()` names the output directory, so CI globs `bin/crafter.build-exe---*/` rather than predicting the hash. The hash includes the compiler, so after a toolchain update the previous variant's directories stay in `bin/` and `build/` until `clean`, and such a glob matches both. - AGENTS.md is build output: the self-host build and `test` write it from `AGENTS.template.md`, with the `AGENTS.consumer.md` of each dependency that ships one (Crafter.Math, today) merged in, and it is gitignored. Edit the template; an edit made in AGENTS.md is gone after the next build. A fresh clone or worktree has no AGENTS.md until its first `./bin/crafter-build` or `test`, since `./build.sh` does not write it. A harness from before the template (3.0.0 and older) never creates one and keeps its section in one it finds; the next build with a current harness writes it over. diff --git a/tests/CudaCompile/fixture/checkpoint.cpp b/tests/CudaCompile/fixture/checkpoint.cpp new file mode 100644 index 0000000..ec756da --- /dev/null +++ b/tests/CudaCompile/fixture/checkpoint.cpp @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +// A library of the project's own, named like one a CUDA toolkit's lib64 +// can hold (Arch's holds a libcheckpoint.so): CudaCompile checks that the +// link takes this one. +extern "C" int SaveCheckpoint() { + return 0; +} diff --git a/tests/CudaCompile/main.cpp b/tests/CudaCompile/main.cpp index bfc6374..05531f9 100644 --- a/tests/CudaCompile/main.cpp +++ b/tests/CudaCompile/main.cpp @@ -20,9 +20,19 @@ using namespace Crafter; // a directory of its own, then once more with nothing changed, which compiles // nothing, and once its header is newer than its object, which compiles it // again: nvcc's depfile is the record of what the .cu includes, and before -// nvcc wrote one, a changed header left the old object in place. 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. +// nvcc wrote one, a changed header left the old object in place. Those builds +// name no CUDA library: the configuration links the runtime of the toolkit +// itself, the static archive in its lib64 by its path. Another build puts the +// .cu in a static library and the C++ unit in an executable built on it, which +// links only if the library hands the runtime on. That lib64 also holds a +// libcheckpoint.so and a libm.so that are no libraries, so a link that +// searched it would stop on one: an executable with CUDA sources built on a +// library of the project's named checkpoint has to take the project's, and +// clang++ adds -lm to every C++ link. A last build names the shared runtime +// itself, as a project with CUDA sources had to, and has its -lcudart ahead of +// the static archive. Where nvcc is looked for, and the runtime beside it, is +// ToolchainResolve's to check; this is the build that uses them. Skips without +// a CUDA toolkit whose lib64 holds the static runtime; CI has none. namespace { std::int32_t Failures = 0; @@ -53,6 +63,11 @@ namespace { fs::permissions(file, fs::perms::owner_exec, fs::perm_options::add); } + std::string Read(const fs::path& file) { + std::ifstream in(file, std::ios::binary); + return std::string((std::istreambuf_iterator(in)), std::istreambuf_iterator()); + } + std::vector Lines(const fs::path& file) { std::vector lines; std::ifstream in(file); @@ -63,6 +78,13 @@ namespace { std::string Joined(const std::vector& lines) { return std::views::join_with(lines, std::string(" | ")) | std::ranges::to(); } + + struct Built { + std::string error; + fs::path binary; + // The link command the build recorded, beside its objects. + fs::path linkRecord; + }; } int main() { @@ -72,6 +94,14 @@ int main() { return 77; } const fs::path nvcc = real.nvcc; + // The static runtime crafter-build links, from the lib64 beside nvcc's + // bin. A toolkit without one there gets none linked, and the runtime link + // has nothing to show. + const fs::path realArchive = real.cudaRuntime; + if (realArchive.empty()) { + std::println("SKIP: no libcudart_static.a in the lib64 beside {}, so crafter-build links no CUDA runtime for this toolkit", nvcc.parent_path().string()); + return 77; + } const fs::path scratch = fs::temp_directory_path() / "crafter-build-cuda-compile"; std::error_code ec; @@ -80,9 +110,23 @@ int main() { const fs::path project = scratch / "project"; fs::copy(fs::current_path() / "tests" / "CudaCompile" / "fixture", project, fs::copy_options::recursive); + // The toolkit CUDA_PATH names: an nvcc that writes down how it was run and + // runs the real one, and a lib64 with the real toolkit's static runtime + // and a libcheckpoint.so and a libm.so that are no libraries at all. A + // toolkit's lib64 can hold a library named like a project's own (Arch's + // holds a libcheckpoint.so) or like one the link takes from the system (a + // libOpenCL.so), and a link that searched it would take that one: the + // libm.so stands for those, as clang++ adds -lm to every C++ link. 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()))); + fs::create_directories(toolkit / "lib64"); + fs::create_symlink(realArchive, toolkit / "lib64" / "libcudart_static.a"); + std::ofstream(toolkit / "lib64" / "libcheckpoint.so") << "not a library\n"; + std::ofstream(toolkit / "lib64" / "libm.so") << "not a library\n"; + // What a link names the runtime by: the archive in the toolkit's lib64, by + // its path. + const std::string runtime = (toolkit / "lib64" / "libcudart_static.a").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 @@ -91,8 +135,6 @@ int main() { 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; @@ -104,34 +146,120 @@ int main() { 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" }; + return Built{ result.result, app.BinDir() / "cuda", app.BuildDir() / "cuda.link" }; }; - 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"); + const Built native = build(std::nullopt); + Check(native.error.empty(), std::format("the build with the default architecture succeeds: {}", native.error)); + Check(fs::exists(native.binary), "it links the executable"); + const std::string record = Read(native.linkRecord); + Check(record.contains(runtime), std::format("with the static runtime from the toolkit's lib64, though no link flag names it (link: {})", record)); 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)); + const Built other = build("sm_86"); + Check(other.error.empty(), std::format("the build for sm_86 succeeds: {}", other.error)); 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"); + Check(fs::exists(other.binary) && other.binary.parent_path() != native.binary.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)))); + const Built again = build("sm_86"); + Check(again.error.empty() && Lines(log).size() == 2, std::format("an unchanged build compiles nothing (ran: {})", Joined(Lines(log)))); fs::last_write_time(project / "kernel.h", fs::file_time_type::clock::now()); - const auto [touched, touchedBinary] = build("sm_86"); - Check(touched.empty(), std::format("the build after the header changed succeeds: {}", touched)); + const Built touched = build("sm_86"); + Check(touched.error.empty(), std::format("the build after the header changed succeeds: {}", touched.error)); runs = Lines(log); Check(runs.size() == 3 && runs.back().contains("-arch=sm_86"), std::format("a changed header compiles the .cu again (ran: {})", Joined(runs))); + { + // The .cu in a static library, the C++ unit in an executable built on + // it: neither names a CUDA library, and the executable's link has the + // runtime only from what the library hands on. + const fs::path split = scratch / "split"; + fs::copy(fs::current_path() / "tests" / "CudaCompile" / "fixture", split, fs::copy_options::recursive); + Configuration kernels; + kernels.path = split; + kernels.name = "kernels"; + kernels.outputName = "kernels"; + kernels.type = ConfigurationType::LibraryStatic; + kernels.cuda = { split / "kernel" }; + Configuration program; + program.path = split; + program.name = "program"; + program.outputName = "program"; + program.type = ConfigurationType::Executable; + std::array ifaces = {}; + std::array impls = { "main" }; + program.GetInterfacesAndImplementations(ifaces, impls); + program.dependencies = { &kernels }; + std::unordered_map> depResults; + std::mutex depMutex; + const BuildResult built = Build(program, depResults, depMutex); + Check(built.result.empty(), std::format("an executable built on a library with CUDA sources links: {}", built.result)); + Check(Read(program.BuildDir() / "program.link").contains(runtime), "with the runtime the library hands on"); + } + { + // An executable with CUDA sources built on a library of the + // project's named like one in the toolkit's lib64: the linker has to + // take the project's, and stops on the toolkit's file if it searches + // the toolkit's lib64 first. + const fs::path named = scratch / "named"; + fs::copy(fs::current_path() / "tests" / "CudaCompile" / "fixture", named, fs::copy_options::recursive); + Configuration checkpoint; + checkpoint.path = named; + checkpoint.name = "checkpoint"; + checkpoint.outputName = "checkpoint"; + checkpoint.type = ConfigurationType::LibraryStatic; + { + std::array ifaces = {}; + std::array impls = { "checkpoint" }; + checkpoint.GetInterfacesAndImplementations(ifaces, impls); + } + Configuration app; + app.path = named; + app.name = "named"; + app.outputName = "named"; + app.type = ConfigurationType::Executable; + std::array ifaces = {}; + std::array impls = { "main" }; + app.GetInterfacesAndImplementations(ifaces, impls); + app.cuda = { named / "kernel" }; + app.dependencies = { &checkpoint }; + std::unordered_map> depResults; + std::mutex depMutex; + const BuildResult built = Build(app, depResults, depMutex); + Check(built.result.empty(), std::format("an executable with CUDA sources links a library of its project's named like one in the toolkit's lib64: {}", built.result)); + } + { + // A project that names the shared runtime itself, as every project.cpp + // with CUDA sources had to: its -lcudart comes ahead of the static + // archive on the link line, so the linker takes the shared runtime. + const fs::path own = scratch / "own"; + fs::copy(fs::current_path() / "tests" / "CudaCompile" / "fixture", own, fs::copy_options::recursive); + Configuration app; + app.path = own; + app.name = "own"; + app.outputName = "own"; + app.type = ConfigurationType::Executable; + std::array ifaces = {}; + std::array impls = { "main" }; + app.GetInterfacesAndImplementations(ifaces, impls); + app.cuda = { own / "kernel" }; + app.linkFlags = { std::format("-L{}", ShellQuote(realArchive.parent_path().string())), "-lcudart" }; + std::unordered_map> depResults; + std::mutex depMutex; + const BuildResult built = Build(app, depResults, depMutex); + Check(built.result.empty(), std::format("a build that names -lcudart itself links: {}", built.result)); + const std::string link = Read(app.BuildDir() / "own.link"); + const std::size_t shared = link.find(" -lcudart "); + const std::size_t archive = link.find(runtime); + Check(shared != std::string::npos && archive != std::string::npos && shared < archive, std::format("with its -lcudart ahead of the static runtime (link: {})", link)); + } + fs::remove_all(scratch, ec); return Failures == 0 ? 0 : 1; } diff --git a/tests/ToolchainResolve/main.cpp b/tests/ToolchainResolve/main.cpp index 6ab8186..108ee5a 100644 --- a/tests/ToolchainResolve/main.cpp +++ b/tests/ToolchainResolve/main.cpp @@ -247,6 +247,30 @@ int main() { Check(found.is_absolute() && fs::equivalent(found, cuda / "named/bin/nvcc"), std::format("a relative CUDA_PATH names an absolute nvcc (got '{}')", found.string())); } } + { + // The static runtime beside the nvcc found: lib64/libcudart_static.a + // where it is there, else none, as for a lib64 that holds none or + // cannot be looked into, here one that links to itself, which has to + // be looked into without throwing. + const fs::path kits = scratch / "runtime"; + Program(kits / "with/bin/nvcc"); + Touch(kits / "with/lib64/libcudart_static.a"); + Program(kits / "without/bin/nvcc"); + fs::create_directories(kits / "without/lib64"); + Program(kits / "looped/bin/nvcc"); + fs::create_directory_symlink("lib64", kits / "looped/lib64"); + ToolchainInputs in; + in.env["CUDA_PATH"] = (kits / "with").string(); + const fs::path found = ResolveToolchain(in).cudaRuntime; + std::error_code ec; + Check(!found.empty() && fs::equivalent(found, kits / "with/lib64/libcudart_static.a", ec), std::format("the static runtime in the lib64 beside nvcc's bin (got '{}')", found.string())); + in.env["CUDA_PATH"] = (kits / "without").string(); + Check(ResolveToolchain(in).cudaRuntime.empty(), "none where that lib64 holds none"); + in.env["CUDA_PATH"] = (kits / "looped").string(); + Check(ResolveToolchain(in).cudaRuntime.empty(), "none where the lib64 cannot be looked into"); + in.env["CUDA_PATH"] = (kits / "missing").string(); + Check(ResolveToolchain(in).cudaRuntime.empty(), "and none without an nvcc"); + } { // Nothing anywhere: the error says where it looked and what to set. ToolchainInputs in;