Skip to content

cuda-bindings: handle CUDA header discovery errors - #88

Closed
yagna-1 wants to merge 2 commits into
NVIDIA:mainfrom
yagna-1:fix/cuda-bindings-header-discovery
Closed

yagna-1 wants to merge 2 commits into
NVIDIA:mainfrom
yagna-1:fix/cuda-bindings-header-discovery

Conversation

@yagna-1

@yagna-1 yagna-1 commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Make the cuda-bindings build script report missing or unreadable CUDA headers directly instead of falling through to a bindgen panic.
  • Discover CUDA headers in both the standard include/ layout and target-specific redistributable layouts such as targets/x86_64-linux/include/ and targets/sbsa-linux/include/.
  • Pass discovered include paths to bindgen and matching target-layout library paths to rustc.

Fixes #87.

Test plan

  • cargo fmt --check --all
  • cargo test -p cuda-device -p oxide-artifacts -p reserved-oxide-symbols --lib --tests
  • cargo test -p cargo-oxide --all-targets on a host without CUDA headers now fails with the direct cuda-bindings: could not find cuda.h... build-script error instead of a bindgen panic.

@yagna-1
yagna-1 force-pushed the fix/cuda-bindings-header-discovery branch from cfa4b39 to eb140b1 Compare May 26, 2026 05:40
@fbrv

fbrv commented May 27, 2026

Copy link
Copy Markdown
Contributor

I think this is not enough: in sbsa environment this wouldn’t work, even if cuda is installed.
Also it doesn’t take in consideration environments where cuda is not installed in the default path but I think it’s a project decision to support or not default paths (@nihalpasham)

@yagna-1

yagna-1 commented May 27, 2026

Copy link
Copy Markdown
Contributor Author

Updated to scan CUDA target-layout directories via targets/* instead of hardcoding targets/x86_64-linux, so SBSA layouts such as targets/sbsa-linux are covered as well.

Report missing or unreadable CUDA headers directly from the build script and
discover CUDA headers and libraries in both the standard toolkit layout and
target-specific layouts such as targets/x86_64-linux and targets/sbsa-linux.

Signed-off-by: yagna-1 <[email protected]>
@yagna-1
yagna-1 force-pushed the fix/cuda-bindings-header-discovery branch from eb140b1 to fbf7a7a Compare May 27, 2026 06:08
Discover CUDA headers and libraries across target-specific toolkit layouts
such as targets/x86_64-linux and targets/sbsa-linux instead of hardcoding a
single target directory.

Signed-off-by: yagna-1 <[email protected]>
@fbrv

fbrv commented May 28, 2026

Copy link
Copy Markdown
Contributor

I think this introduces another problem: if multiple CUDA targets are installed, we may end up mixing dependencies and likely cause build/link errors. We should probably first discover the CUDA host target, and only then look for cuda.h under that target.

@nihalpasham nihalpasham added bug Something isn't working cuda-bindings Raw FFI bindings to the CUDA driver/runtime build-related Build scripts, toolchain discovery, or compilation setup labels Jun 6, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR, the diagnosis is right and the error-message UX is a nice improvement. We can't accept external contributions to cuda-bindings though (it's governed by an NVIDIA license), so I've re-landed the idea NVIDIA-authored as #172

Two changes from your version:

The doctor half of #87 stays open. Closing as already-fixed. (Your #95 merged today, thanks for both.)

nihalpasham added a commit to Barrow099/cuda-oxide that referenced this pull request Jun 12, 2026
A missing cuda.h previously fell through to bindgen, which panicked
with raw clang diagnostics before cargo oxide doctor could diagnose
the host (issue NVIDIA#87). Discovery now happens up front: probe
{toolkit}/include/cuda.h and the build target's
{toolkit}/targets/<dir>/include/cuda.h, and exit through the existing
run()/main() error path with one message listing the probed paths,
CUDA_TOOLKIT_PATH/CUDA_HOME, and the /usr/local/cuda default.

The targets/ directory is chosen from cargo's TARGET env (x86_64 ->
x86_64-linux, aarch64 -> sbsa-linux) rather than globbing targets/*:
on multi-target installs a sorted glob puts sbsa-linux stubs ahead of
x86_64-linux. The same single-target selection feeds the bindgen -I
flag and the lib + lib/stubs link-search dirs.

A present but unreadable cuda.h keeps the warn-and-continue probe
behavior (bindgen reports the authoritative failure right after, now
mapped through the clean error path instead of an expect panic);
only a missing header is a hard discovery-time error.

Idea credited to @yagna-1's diagnosis in NVIDIA#88; re-landed
NVIDIA-authored because cuda-bindings cannot accept external
contributions.
@nihalpasham nihalpasham added the already-fixed Resolved on a newer commit or branch; no action needed label Jun 12, 2026
nihalpasham added a commit that referenced this pull request Aug 8, 2026
…egra (#700)

* fix(cuda-bindings): select targets/ by full build target, and reach Tegra

`toolkit_target_dir` matched only the first segment of the `TARGET`
triple, so the architecture alone decided the CUDA `targets/<dir>`
layout and the OS was never consulted. Every non-Linux target therefore
claimed a Linux directory: `aarch64-apple-darwin` resolved to
`sbsa-linux` and `x86_64-pc-windows-msvc` to `x86_64-linux`, both of
which then fed `find_cuda_include_dir` and `collect_lib_paths` a path
that cannot exist on those platforms.

The arch-only match also left CUDA for Tegra unreachable. Tegra installs
(Jetson, Drive) put their headers under `targets/aarch64-linux/`, while
`sbsa-linux` is the ARM *server* tree -- and both build natively as
`aarch64-unknown-linux-gnu`, so the triple cannot tell them apart.
aarch64 Linux now yields both names as ordered candidates and the
existing `cuda.h` existence probe picks the one actually installed.
`sbsa-linux` stays first, so an install carrying both trees resolves
exactly as it does today.

Selection keys off `CARGO_CFG_TARGET_ARCH`/`CARGO_CFG_TARGET_OS` rather
than splitting the triple: the triple's field count varies
(`arch-vendor-os-env` vs `arch-os-env`), and `aarch64-linux-android`
would otherwise read as a Linux target.

This is not the `targets/*` glob proposed in #88 and rejected. Every
candidate returned is for the requested architecture only, so a
multi-target install can never hand an x86_64 build the `sbsa-linux`
headers that sort ahead of it; `multi_target_install_never_hands_x86_64_
the_sbsa_headers` pins that.

The table and the candidate-path construction live in
`toolkit_target.rs` and are pulled into both `build.rs` and
`tests/toolkit_target.rs` with `include!`, because cargo never builds a
build script as a test target. Eight tests cover the selection table,
the Tegra and sbsa layouts, a multi-target install, the standard
top-level `include/`, and non-Linux targets. They fail against the
previous arch-only behavior.

Signed-off-by: Nils Homer <[email protected]>

* fix(cuda-bindings): add CUDA_TOOLKIT_TARGET_DIR override and mirror the targets/ selection

Two completions on top of the targets/ selection rework:

- CUDA_TOOLKIT_TARGET_DIR names one targets/ tree by hand, like nvcc's
  -target-dir flag and CMake's CUDAToolkit_TARGET_DIR. When set (and not
  blank) it is the single targets/ candidate; the directory is still
  probed for cuda.h, so a wrong value fails with the clear discovery
  error instead of silently falling back to the table. build.rs emits
  rerun-if-env-changed for it, and the synthetic-tree tests cover both
  the resolving and the deliberately-failing override.

- The two hand-kept mirrors catch up with the shared table in
  crates/cuda-bindings/toolkit_target.rs: doctor's cuda_header_candidates
  in cargo-oxide and the multicast probe in cuda-core/build.rs now key on
  arch+OS instead of the first triple segment, probe both aarch64 Linux
  trees (sbsa-linux, then aarch64-linux), and honor the same override.
  Their lockstep comments now name toolkit_target.rs.

---------

Signed-off-by: Nils Homer <[email protected]>
Co-authored-by: nihalpasham <[email protected]>
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

already-fixed Resolved on a newer commit or branch; no action needed bug Something isn't working build-related Build scripts, toolchain discovery, or compilation setup cuda-bindings Raw FFI bindings to the CUDA driver/runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cargo oxide doctor cannot diagnose missing CUDA headers because cuda-bindings panics first

3 participants