test(cuda): build a .cu with the toolkit CUDA_PATH names, and with none
CudaCompile puts an nvcc on PATH that fails, names a toolkit through CUDA_PATH whose nvcc writes down its arguments and runs the real one, and builds an executable from a C++ unit and a .cu: with the default architecture, then with another one, which compiles the unchanged .cu again in a directory of its own, then with nothing changed, which compiles nothing. With the bare "nvcc" of before, the first build runs the nvcc on PATH and fails. It needs a real toolkit, which it looks for as crafter-build does, with ResolveToolchain and the inputs GetToolchain() gives it, and it skips without one. Nothing in CI installs one, so AGENTS.template.md says to run it in the native loop. CudaMissing needs none, so CI runs it: with CUDA_PATH naming an empty directory, a build with a .cu fails with "cannot compile kernel.cu: no nvcc at <directory>/bin/nvcc (CUDA_PATH)", and a build without CUDA sources in the same process still succeeds. With the bare "nvcc" of before, that build ran whatever nvcc PATH held, and where PATH held none, as in the Flatpak, failed with "sh: line 1: nvcc: command not found". AGENTS.template.md names it beside CudaCompile.
This commit is contained in:
parent
967dd12b06
commit
ad021ee652
8 changed files with 251 additions and 0 deletions
|
|
@ -207,5 +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/<triple>/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.
|
||||
- `VariantId()` names the output directory, so CI globs `bin/crafter.build-exe-<target>-<march>-*/` 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.
|
||||
|
|
|
|||
|
|
@ -260,6 +260,8 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
|
|||
cfg.AddTest("ShaderCompile").Dependencies({ CrafterBuildLib.get() });
|
||||
cfg.AddTest("ShaderVariantBuild").Dependencies({ CrafterBuildLib.get() });
|
||||
cfg.AddTest("ShaderIncludeChange").Dependencies({ CrafterBuildLib.get() });
|
||||
cfg.AddTest("CudaCompile").Dependencies({ CrafterBuildLib.get() });
|
||||
cfg.AddTest("CudaMissing").Dependencies({ CrafterBuildLib.get() });
|
||||
cfg.AddTest("MingwMissing").Dependencies({ CrafterBuildLib.get() });
|
||||
cfg.AddTest("MingwSysroot").Dependencies({ CrafterBuildLib.get() });
|
||||
cfg.AddTest("GitSubmodules").Dependencies({ CrafterBuildLib.get() });
|
||||
|
|
|
|||
20
tests/CudaCompile/fixture/kernel.cu
Normal file
20
tests/CudaCompile/fixture/kernel.cu
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
#include <cstdint>
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
__global__ void Double(float* values, std::int32_t count) {
|
||||
const std::int32_t i = static_cast<std::int32_t>(blockIdx.x * blockDim.x + threadIdx.x);
|
||||
if (i < count) values[i] = 2.0f * static_cast<float>(i);
|
||||
}
|
||||
|
||||
extern "C" std::int32_t LaunchDouble(std::int32_t count, float* values) {
|
||||
float* device = nullptr;
|
||||
cudaError_t status = cudaMalloc(&device, sizeof(float) * count);
|
||||
if (status != cudaSuccess) return status;
|
||||
Double<<<1, 32>>>(device, count);
|
||||
status = cudaMemcpy(values, device, sizeof(float) * count, cudaMemcpyDeviceToHost);
|
||||
cudaFree(device);
|
||||
return status;
|
||||
}
|
||||
13
tests/CudaCompile/fixture/main.cpp
Normal file
13
tests/CudaCompile/fixture/main.cpp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
import std;
|
||||
|
||||
// In kernel.cu: doubles every index into `values` on the GPU, and returns
|
||||
// cudaSuccess (0) or the CUDA error.
|
||||
extern "C" std::int32_t LaunchDouble(std::int32_t count, float* values);
|
||||
|
||||
int main() {
|
||||
std::array<float, 8> values{};
|
||||
return LaunchDouble(static_cast<std::int32_t>(values.size()), values.data());
|
||||
}
|
||||
129
tests/CudaCompile/main.cpp
Normal file
129
tests/CudaCompile/main.cpp
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
#include <stdlib.h>
|
||||
import std;
|
||||
import Crafter.Build;
|
||||
namespace fs = std::filesystem;
|
||||
using namespace Crafter;
|
||||
|
||||
// cfg.cuda is compiled with the nvcc ResolveToolchain finds, for the GPU
|
||||
// architecture cfg.cudaArch names. It used to be whatever "nvcc" meant to the
|
||||
// shell, for sm_89 alone: crafter-build's Flatpak sets PATH to
|
||||
// /app/bin:/usr/bin, and with that PATH every CUDA build there stopped at
|
||||
// "nvcc: command not found" with the host's toolkit in reach at /opt/cuda.
|
||||
//
|
||||
// The test puts an nvcc on PATH that fails, names a toolkit through CUDA_PATH
|
||||
// whose nvcc writes down its arguments and runs the real one, and builds an
|
||||
// executable from a C++ unit and a .cu: with the default architecture, which
|
||||
// is native, then with another one, which compiles the unchanged .cu again in
|
||||
// a directory of its own, then once more with nothing changed, which compiles
|
||||
// nothing. 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.
|
||||
namespace {
|
||||
std::int32_t Failures = 0;
|
||||
|
||||
void Check(bool cond, std::string_view msg) {
|
||||
if (!cond) {
|
||||
std::println(std::cerr, "FAIL: {}", msg);
|
||||
++Failures;
|
||||
}
|
||||
}
|
||||
|
||||
// The nvcc crafter-build finds in this environment, looked for with the
|
||||
// inputs GetToolchain() gives ResolveToolchain, the host's /usr under
|
||||
// /run/host in a sandbox included, before the test changes CUDA_PATH and
|
||||
// PATH. ResolveToolchain is pure: GetToolchain()'s cache stays empty.
|
||||
Toolchain RealToolkit() {
|
||||
ToolchainInputs in;
|
||||
for (const std::string_view name : {"CUDA_PATH", "PATH"}) {
|
||||
if (const char* value = std::getenv(std::string(name).c_str()); value && *value) in.env.emplace(std::string(name), value);
|
||||
}
|
||||
if (fs::exists("/.flatpak-info")) in.hostRoot = "/run/host";
|
||||
in.cudaRoots = { "/opt/cuda", "/usr/local/cuda" };
|
||||
return ResolveToolchain(in);
|
||||
}
|
||||
|
||||
void WriteProgram(const fs::path& file, std::string_view text) {
|
||||
fs::create_directories(file.parent_path());
|
||||
std::ofstream(file) << text;
|
||||
fs::permissions(file, fs::perms::owner_exec, fs::perm_options::add);
|
||||
}
|
||||
|
||||
std::vector<std::string> Lines(const fs::path& file) {
|
||||
std::vector<std::string> lines;
|
||||
std::ifstream in(file);
|
||||
for (std::string line; std::getline(in, line);) lines.push_back(line);
|
||||
return lines;
|
||||
}
|
||||
|
||||
std::string Joined(const std::vector<std::string>& lines) {
|
||||
return std::views::join_with(lines, std::string(" | ")) | std::ranges::to<std::string>();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
const Toolchain real = RealToolkit();
|
||||
if (real.nvcc.empty()) {
|
||||
std::println("SKIP: {}", real.nvccError);
|
||||
return 77;
|
||||
}
|
||||
const fs::path nvcc = real.nvcc;
|
||||
|
||||
const fs::path scratch = fs::temp_directory_path() / "crafter-build-cuda-compile";
|
||||
std::error_code ec;
|
||||
fs::remove_all(scratch, ec);
|
||||
fs::create_directories(scratch);
|
||||
const fs::path project = scratch / "project";
|
||||
fs::copy(fs::current_path() / "tests" / "CudaCompile" / "fixture", project, fs::copy_options::recursive);
|
||||
|
||||
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())));
|
||||
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
|
||||
// once per process: the first Configuration does, for its target.
|
||||
setenv("CUDA_PATH", toolkit.c_str(), 1);
|
||||
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<std::string> arch) {
|
||||
Configuration app;
|
||||
app.path = project;
|
||||
app.name = "cuda";
|
||||
app.outputName = "cuda";
|
||||
app.type = ConfigurationType::Executable;
|
||||
if (arch) app.cudaArch = *arch;
|
||||
std::array<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> impls = { "main" };
|
||||
app.GetInterfacesAndImplementations(ifaces, impls);
|
||||
app.cuda = { project / "kernel" };
|
||||
app.linkFlags = { runtimeDir, "-lcudart" };
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
const BuildResult result = Build(app, depResults, depMutex);
|
||||
return std::pair{ result.result, app.BinDir() / "cuda" };
|
||||
};
|
||||
|
||||
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");
|
||||
std::vector<std::string> 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));
|
||||
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");
|
||||
|
||||
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))));
|
||||
|
||||
fs::remove_all(scratch, ec);
|
||||
return Failures == 0 ? 0 : 1;
|
||||
}
|
||||
5
tests/CudaMissing/fixture/kernel.cu
Normal file
5
tests/CudaMissing/fixture/kernel.cu
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
// Never compiled: CudaMissing's CUDA_PATH names a directory without an nvcc.
|
||||
__global__ void Kernel() {}
|
||||
8
tests/CudaMissing/fixture/main.cpp
Normal file
8
tests/CudaMissing/fixture/main.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
// CudaMissing's program: nothing in it needs CUDA, so it builds without an
|
||||
// nvcc where its configuration has no CUDA sources.
|
||||
int main() {
|
||||
return 0;
|
||||
}
|
||||
73
tests/CudaMissing/main.cpp
Normal file
73
tests/CudaMissing/main.cpp
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
import std;
|
||||
import Crafter.Build;
|
||||
namespace fs = std::filesystem;
|
||||
using namespace Crafter;
|
||||
|
||||
// A build with CUDA sources and no nvcc to compile them fails, saying where
|
||||
// nvcc was looked for. It used to run a bare "nvcc" through the shell, which
|
||||
// in crafter-build's Flatpak, whose PATH as its manifest sets it holds no host
|
||||
// toolkit, stopped such builds at "sh: line 1: nvcc: command not found",
|
||||
// naming no place to put one. CUDA_PATH names an empty directory here, so the
|
||||
// test needs no CUDA toolkit and runs in CI, where CudaCompile skips: the
|
||||
// build with a .cu fails naming that directory's bin/nvcc and CUDA_PATH, and a
|
||||
// build without CUDA sources in the same process still succeeds, as only a .cu
|
||||
// needs an nvcc.
|
||||
|
||||
extern "C" int setenv(const char* name, const char* value, int overwrite);
|
||||
|
||||
namespace {
|
||||
std::int32_t failures = 0;
|
||||
|
||||
void Check(bool cond, std::string_view msg) {
|
||||
if (!cond) {
|
||||
std::println(std::cerr, "FAIL: {}", msg);
|
||||
++failures;
|
||||
}
|
||||
}
|
||||
|
||||
BuildResult BuildOnce(const fs::path& project, std::string_view name, bool withCuda) {
|
||||
Configuration app;
|
||||
app.path = project;
|
||||
app.name = name;
|
||||
app.outputName = name;
|
||||
app.type = ConfigurationType::Executable;
|
||||
std::array<fs::path, 0> ifaces = {};
|
||||
std::array<fs::path, 1> impls = { "main" };
|
||||
app.GetInterfacesAndImplementations(ifaces, impls);
|
||||
if (withCuda) app.cuda = { project / "kernel" };
|
||||
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||||
std::mutex depMutex;
|
||||
return Build(app, depResults, depMutex);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
const fs::path scratch = fs::temp_directory_path() / "crafter-build-cuda-missing";
|
||||
fs::remove_all(scratch);
|
||||
fs::create_directories(scratch);
|
||||
const fs::path project = scratch / "project";
|
||||
fs::copy(fs::current_path() / "tests" / "CudaMissing" / "fixture", project, fs::copy_options::recursive);
|
||||
// A toolkit directory without a bin/nvcc, named before anything resolves
|
||||
// the toolchain, which reads CUDA_PATH once per process.
|
||||
const fs::path toolkit = scratch / "toolkit";
|
||||
fs::create_directories(toolkit);
|
||||
setenv("CUDA_PATH", toolkit.c_str(), 1);
|
||||
|
||||
const BuildResult missing = BuildOnce(project, "cuda", true);
|
||||
const std::string expected = std::format("cannot compile kernel.cu: no nvcc at {} (CUDA_PATH)", (toolkit.lexically_normal() / "bin" / "nvcc").string());
|
||||
Check(missing.result == expected, std::format("a build with a .cu and no nvcc fails, saying where it looked (expected '{}', got '{}')", expected, missing.result));
|
||||
|
||||
const BuildResult plain = BuildOnce(project, "plain", false);
|
||||
Check(plain.result.empty(), std::format("a build without CUDA sources needs no nvcc: {}", plain.result));
|
||||
|
||||
std::error_code ec;
|
||||
fs::remove_all(scratch, ec);
|
||||
if (failures > 0) {
|
||||
std::println(std::cerr, "{} assertions failed", failures);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue