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
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