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.
73 lines
3 KiB
C++
73 lines
3 KiB
C++
// 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;
|
|
}
|