Crafter.Build/tests/CudaMissing/main.cpp

73 lines
3 KiB
C++
Raw Permalink Normal View History

// 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;
}