Skip to content

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

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
nh13:draft/toolkit-target-triple
Aug 8, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
nh13:draft/toolkit-target-triple

Conversation

@nh13

@nh13 nh13 commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Closes #698.

Problem

toolkit_target_dir() matched only the first segment of the target triple, so architecture alone decided the CUDA targets/<dir> layout and the OS was never consulted:

  • aarch64-apple-darwin probed targets/sbsa-linux/ (a Linux-only tree) and x86_64-pc-windows-msvc probed targets/x86_64-linux/.
  • CUDA for Tegra was unreachable: Jetson and Drive install under targets/aarch64-linux/, while aarch64 was hard-wired to the ARM server tree (sbsa-linux).
# A CUDA-for-Tegra install ships only:
#   /usr/local/cuda/targets/aarch64-linux/include/cuda.h

Before: probes include/, then targets/sbsa-linux/  -> not found, error
After:  probes include/, targets/sbsa-linux/, targets/aarch64-linux/
        -> found; matching lib dirs join the link search path

Fix

  • Selection keys off CARGO_CFG_TARGET_ARCH + CARGO_CFG_TARGET_OS (robust against 3- vs 4-field triples; correctly excludes Android), returns nothing for non-Linux, and yields ordered candidates for aarch64 Linux: ["sbsa-linux", "aarch64-linux"], walked by the existing cuda.h existence probe. sbsa-linux stays first, so installs carrying both trees resolve exactly as today.
  • The table lives in one shared file (toolkit_target.rs) used by both build.rs consumers and a synthetic-tree integration suite. Preserves the single-architecture anti-glob decision from cuda-bindings: clean error for missing cuda.h, target-aware toolkit layout #172, with a test asserting it.
  • The shape matches CMake's FindCUDAToolkit selection almost exactly.

Maintainer commit on top (b65208b)

  • CUDA_TOOLKIT_TARGET_DIR env override (the nvcc -target-dir / CMake CUDAToolkit_TARGET_DIR equivalent): if set, it is the single targets/ candidate, still existence-probed so typos fail with the existing clear error; cargo:rerun-if-env-changed wired; two override tests added. This settles the dual-tree ambiguity (a Jetson cross sysroot carrying both trees can now pin aarch64-linux).
  • The two hand-lockstep copies of the old logic are mirrored to the new selection: cargo-oxide's doctor (cuda_header_candidates) and cuda-core's build.rs. Without this, a Tegra user would build fine and then cargo oxide doctor would exit 1 claiming cuda.h is missing. Their lockstep comments now name toolkit_target.rs.

…egra

`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 NVIDIA#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]>
@nh13
nh13 requested a review from nihalpasham as a code owner August 7, 2026 18:05
@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 Aug 8, 2026
…he 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.
@nihalpasham

Copy link
Copy Markdown
Collaborator

Gist: The toolkit directory is now chosen from both the target's CPU architecture and its operating system, instead of the first word of the triple. Concretely: Mac and Windows builds stop probing Linux-only directories, and Jetson/Drive installs (which live under targets/aarch64-linux/) finally resolve, with the ARM-server tree still preferred when both exist. Matches CMake's FindCUDAToolkit, with tests against synthetic toolkit trees.

I pushed one commit on top:

  • The CUDA_TOOLKIT_TARGET_DIR override you offered (same idea as nvcc's -target-dir), so a machine carrying both ARM trees can pin one; wired for rebuilds, two tests.
  • The same old logic existed hand-copied in two more places: cargo oxide doctor and cuda-core's build script. Mirrored the fix into both, so a Tegra user cannot end up with a working build and a doctor that says cuda.h is missing. The keep-in-lockstep comments now point at the shared file.

Verified here: 10/10 toolkit tests linking against the real toolkit on this box; both mirrors compile clean.

Closes #698. #139 (Windows lib paths) stays open, as you noted.

Merging.

@nihalpasham
nihalpasham merged commit 031e3f3 into NVIDIA:main Aug 8, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

toolkit_target_dir() matches only the triple's architecture, so non-Linux targets probe Linux paths and Tegra is unreachable

2 participants