fix(cuda): rebuild a .cu when a header it includes changes

A .cu was compiled again only when it was newer than its object, so one
whose header changed kept the object built from the old header. The C
and C++ paths ask the compiler for a depfile and check every
prerequisite in it; nvcc was left out because its -MD and -MF had never
been run here.

They work with CUDA 13.4 as they do with clang: the depfile lists the
.cu and every header it read, the toolkit's and the system's included.
nvcc puts a space before the rule's colon, which NewestPrerequisite
reads as a target token like any other. An object from before has no
depfile and is compiled once more.
This commit is contained in:
Jorijn van der Graaf 2026-11-11 19:34:52 +01:00
commit c2338d1122
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB

View file

@ -1142,12 +1142,14 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
files += std::format(" {}_source.o ", (buildDir / cFile.filename()).string());
const std::string objPath = std::format("{}_source.o", (buildDir / cFile.filename()).string());
const std::string srcPath = std::format("{}.cu", cFile.string());
// Still .cu-mtime only: the header tracking the C and C++ paths get
// comes from asking the compiler for a depfile, and nvcc's -MD/-MF are
// untested here (no CUDA toolkit in CI), so a .cu that only sees a
// changed header does not rebuild yet. cfg.cudaArch needs no check:
// it is part of VariantId, so another one builds in another directory.
if (!fs::exists(objPath) || (fs::exists(srcPath) && fs::last_write_time(srcPath) > fs::last_write_time(objPath))) {
// nvcc writes the depfile clang writes for a C source (-MD -MF): the
// .cu and every header it read, the toolkit's and the system's among
// them, as a Make rule NewestPrerequisite reads as it reads clang's,
// although nvcc puts a space before the colon. Without it, a .cu
// whose header changed kept its old object. cfg.cudaArch needs no
// check: it is part of VariantId, so another one builds in another
// directory.
if (!fs::exists(objPath) || (fs::exists(srcPath) && fs::last_write_time(srcPath) > fs::last_write_time(objPath)) || NewestPrerequisite(std::format("{}.d", objPath)) > fs::last_write_time(objPath)) {
const Toolchain& toolchain = GetToolchain();
if (toolchain.nvcc.empty()) {
bool expected = false;
@ -1160,7 +1162,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
Progress::Task task(std::format("Compiling {}.cu", cFile.filename().string()));
if (buildCancelled.load(std::memory_order_relaxed)) return;
std::string result = RunBuildCommand(std::format("{} {} -c -o {} -O3 -arch={}", ShellQuote(toolchain.nvcc.string()), ShellQuote(srcPath), ShellQuote(objPath), ShellQuote(config.cudaArch)));
std::string result = RunBuildCommand(std::format("{} {} -c -MD -MF {} -o {} -O3 -arch={}", ShellQuote(toolchain.nvcc.string()), ShellQuote(srcPath), ShellQuote(std::format("{}.d", objPath)), ShellQuote(objPath), ShellQuote(config.cudaArch)));
if (result.empty()) return;
bool expected = false;