Repository navigation
Conversation
…tion#2072) FindCUDA is obsolete. FindCUDAToolkit was added in CMake 3.17. Current cmake_minimum_required is already 3.19. Signed-off-by: Mark Leone <mleone@nvidia.com>
|
|
|
After kicking off a re-run of the failed CI test, it passes now, so I think that was unrelated to your PR. |
There was a problem hiding this comment.
Pull request overview
Updates the project’s CUDA discovery logic to use CMake’s modern FindCUDAToolkit module (since FindCUDA is deprecated/obsolete), aligning with the repo’s existing CMake minimum version.
Changes:
- Switch CUDA detection in
src/cmake/externalpackages.cmakefromCUDA(FindCUDA) toCUDAToolkit(FindCUDAToolkit) and add legacy-variable bridging. - Update the CUDA example testsuite to use
find_package(CUDAToolkit)and replace the removed FindCUDAcuda_compile_ptxfunctionality with a local CMake function. - Prefer imported CUDA targets (e.g.,
CUDA::cudart(_static),CUDA::nvrtc) when available.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| testsuite/example-cuda/CMakeLists.txt | Switches example to CUDAToolkit, adds a custom PTX compile function, and updates CUDA variables/targets used for linking. |
| src/cmake/externalpackages.cmake | Replaces FindCUDA with FindCUDAToolkit and adds a compatibility bridge for legacy CUDA_* variables/behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| checked_find_package(OpenImageIO 2.4 REQUIRED) | ||
| checked_find_package(OptiX REQUIRED) | ||
|
|
||
| set(CUDA_TOOLKIT_ROOT_DIR ${CUDAToolkit_ROOT_DIR}) |
There was a problem hiding this comment.
CUDAToolkit_ROOT_DIR doesn’t appear to be set anywhere in this repo and is not one of the variables exposed/used elsewhere for FindCUDAToolkit. As written, CUDA_TOOLKIT_ROOT_DIR will likely be empty, which then defeats the find_library(... HINTS ${CUDA_TOOLKIT_ROOT_DIR} ...) calls below. Consider deriving the toolkit root from CUDAToolkit_TARGET_DIR / CUDAToolkit_BIN_DIR (parent dir) / CUDAToolkit_LIBRARY_ROOT (if appropriate), similar to the logic used in src/cmake/externalpackages.cmake.
| set(CUDA_TOOLKIT_ROOT_DIR ${CUDAToolkit_ROOT_DIR}) | |
| # Derive a usable CUDA toolkit root directory from CUDAToolkit variables. | |
| set(CUDA_TOOLKIT_ROOT_DIR "") | |
| if (CUDAToolkit_LIBRARY_ROOT) | |
| # Use the library root directly when available. | |
| set(CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_LIBRARY_ROOT}") | |
| elseif (CUDAToolkit_BIN_DIR) | |
| # Fall back to the parent of the bin directory. | |
| get_filename_component(CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_BIN_DIR}" DIRECTORY) | |
| elseif (CUDAToolkit_ROOT) | |
| # As a last resort, use the generic CUDAToolkit root, if defined. | |
| set(CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_ROOT}") | |
| endif () |
| add_custom_command(OUTPUT ${cuda_ptx} | ||
| COMMAND ${CUDA_NVCC_EXECUTABLE} | ||
| ${_ccp_OPTIONS} | ||
| -ptx |
There was a problem hiding this comment.
cuda_compile_ptx() invokes ${CUDA_NVCC_EXECUTABLE}, but CUDAToolkit_NVCC_EXECUTABLE may be unset on systems that have CUDA runtime libraries/headers but no nvcc installed. Add a configure-time fallback/check (e.g., find_program(CUDA_NVCC_EXECUTABLE NAMES nvcc ...) and message(FATAL_ERROR ...) if still not found) so this fails during configuration rather than at build time.
| if (CUDAToolkit_LIBRARY_ROOT) | ||
| set (CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_LIBRARY_ROOT}") | ||
| elseif (CUDAToolkit_TARGET_DIR) | ||
| set (CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_TARGET_DIR}") | ||
| elseif (CUDAToolkit_BIN_DIR) | ||
| get_filename_component (CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_BIN_DIR}" DIRECTORY) |
There was a problem hiding this comment.
CUDA_TOOLKIT_ROOT_DIR is later treated as a full toolkit root (used for --cuda-path=..., .../bin nvcc lookup, and .../lib64 paths). Setting it from CUDAToolkit_LIBRARY_ROOT can be inconsistent with those uses on toolkits where the library root is not the same as the toolkit root. Consider preferring CUDAToolkit_TARGET_DIR (or deriving from CUDAToolkit_BIN_DIR) for CUDA_TOOLKIT_ROOT_DIR, and using CUDAToolkit_LIBRARY_ROOT only for library searches.
| if (CUDAToolkit_LIBRARY_ROOT) | |
| set (CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_LIBRARY_ROOT}") | |
| elseif (CUDAToolkit_TARGET_DIR) | |
| set (CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_TARGET_DIR}") | |
| elseif (CUDAToolkit_BIN_DIR) | |
| get_filename_component (CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_BIN_DIR}" DIRECTORY) | |
| if (CUDAToolkit_TARGET_DIR) | |
| set (CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_TARGET_DIR}") | |
| elseif (CUDAToolkit_BIN_DIR) | |
| get_filename_component (CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_BIN_DIR}" DIRECTORY) | |
| elseif (CUDAToolkit_LIBRARY_ROOT) | |
| # Fallback: library root may not always equal the full toolkit root, | |
| # but is used here as a last-resort compatibility measure. | |
| set (CUDA_TOOLKIT_ROOT_DIR "${CUDAToolkit_LIBRARY_ROOT}") |
|
@MarkLeone Before I merged this, I asked GitHub Copilot for a review, and it had a few comments. Do you have any thoughts on this? Ignore? Accept? Or do you want to amend on your own in some way? (Also, I rebased your patch on top of the current main to make sure there were no conflicts.) |
|
Also, I asked Claude Code to take a whack at it, and its reply was: ⏺ Now I have enough context. Here's my critique: PR #2073 Review: Switch from FindCUDA to FindCUDAToolkit Overall: The motivation is sound — FindCUDA has been deprecated since CMake 3.27, and Bug: CUDAToolkit_ROOT_DIR doesn't exist In testsuite/example-cuda/CMakeLists.txt: The downstream find_library calls for CUDA_nvrtc_LIBRARY and CUDA_cuda_LIBRARY use HINTS The same derivation logic that externalpackages.cmake uses (CUDAToolkit_LIBRARY_ROOT → Fragile: CUDA_INCLUDES silently truncated to first element set (CUDA_INCLUDES ${CUDAToolkit_INCLUDE_DIRS}) CUDAToolkit_INCLUDE_DIRS is a list, but cuda_macros.cmake uses it as a single string via Either document why only the first element is valid here, or better yet, fix cuda_macros.cmake to Concern: CUDA_EXTRA_LIBS redundancy with imported targets set(CUDA_EXTRA_LIBS ${CUDA_EXTRA_LIBS} dl rt) When cudart_lib is CUDA::cudart (an imported target), dl and rt should be carried as transitive Minor: CUDA_LIB_FLAGS relies on correctly-derived CUDA_TOOLKIT_ROOT_DIR cuda_macros.cmake uses ${CUDA_LIB_FLAGS} which is --cuda-path=${CUDA_TOOLKIT_ROOT_DIR}. The if (CUDAToolkit_LIBRARY_ROOT) On cross-compilation setups, CUDAToolkit_LIBRARY_ROOT can be the sysroot-relative target lib dir, Minor: testing.cmake relies on CUDA_TOOLKIT_ROOT_DIR src/cmake/testing.cmake:91 sets LD_LIBRARY_PATH=${CUDA_TOOLKIT_ROOT_DIR}/lib64. This is fine as What's good
|
|
BTW, I am neither particularly promoting nor eschewing these tools, but I do think it's instructive to give them a spin and see what they add to code review -- how much of the time it catches important things we miss, versus how much it's just noise. |
|
@MarkLeone You out there? No particular rush, but before I merged this I did want your feedback on the Copilot comments. |
|
Sorry for the delayed reply. I'll take a look. |
This PR replaces use of the deprecated `FindCUDA` module with CMake's newer `FindCUDAToolkit`. This change is a spiritual successor to Mark's old PR #2073. The overall direction remains the same, but has been reworked on top of the current state of `main`. We're including some additional fixes discovered while testing the build on Windows. #### FindCUDAToolkit Migration The build now uses `checked_find_package(CUDAToolkit)` and CMake's imported CUDA runtime targets. The migration: * Uses the canonical `CUDAToolkit_*` variables internally, while continuing to accept `CUDA_TOOLKIT_ROOT_DIR` as a legacy fallback input. * Links `CUDA::cudart` or `CUDA::cudart_static` rather than manually locating the CUDA runtime and appending Unix-specific system libraries. * Passes the complete CUDA toolkit include list to NVCC and Clang. * Adds the relocated CUDA C++ Core Libraries (`include/cccl`) when necessary for CUDA 13 and older supported CMake versions. * Updates the remaining build and test logic to use `CUDAToolkit_FOUND`, `CUDAToolkit_VERSION`, and purpose-specific OSL variables rather than recreating the old `FindCUDA` result variables. * Uses platform-aware CTest runtime paths so CUDA and NVRTC shared libraries can be found on both Windows and Unix-like systems. * Uses `VERBATIM` custom commands so paths containing spaces are handled correctly. #### Windows Device Alignment Fix Testing on Windows revealed a separate incompatibility in the CUDA renderer support library. When building with older MSVC versions, Clang CUDA builds can resolve `__builtin_assume_aligned` to an MSVC host-only declaration, producing an error when it is used in device code. We added a typed `device_assume_aligned` helper. It uses `__builtin_assume` to express the alignment guarantee on Windows and retains `__builtin_assume_aligned` on other platforms. The helper preserves the pointer type and evaluates each argument only once. #### Standalone CUDA Example We also migrated the standalone `example-cuda` project to `FindCUDAToolkit`. The build now reaches CUDA source compilation but encounters a pre-existing use of the now-defuct `OSL/device_string.h` API. Updating the example to OSL's current hash-based device string interface will be handled in a separate PR. The removed `cuda_compile_ptx` helper is replaced with a local NVCC custom command, and the example links directly against the `CUDA::cudart`, `CUDA::nvrtc`, and `CUDA::cuda_driver` imported targets. The example now propagates its complete dependency include paths into PTX compilation and uses `sm_75` as its default architecture with CUDA 13, which no longer supports the previous architectures. Older CUDA versions retain an `sm_60` default, and `CUDA_TARGET_ARCH` may still be overridden explicitly. ### Tests We did not add any new tests, and we didn't observe any change in the test results relative to the baseline. Linux configurations were exercised with CUDA 12.8 through 13.3 and LLVM 15.0.7 through 22.1.8, including the following combinations: * CUDA 12.8 / LLVM 15.0.7 * CUDA 12.9 / LLVM 18.1.0 * CUDA 13.0 and 13.1 / LLVM 20.1.8 * CUDA 13.3 / LLVM 22.1.8 * CUDA 13.3 / LLVM 20.1.8 * CUDA 13.3 / LLVM 15.0.7 Additional configuration and build checks covered: * CMake 3.24.4 and 4.0.4. * Dynamic and static CUDA runtime targets. * The legacy `CUDA_TOOLKIT_ROOT_DIR` input. * CUDA 13's relocated CCCL headers with an older supported CMake. * CUDA/OptiX test runtime paths on Linux and Windows. * CUDA 12.4 and 13.3 installations on Windows. * CUDA installation paths containing spaces. * Loading cudart and NVRTC with an otherwise clean Windows `PATH`. Assisted-by: OpenAI Codex / GPT-6 --------- Signed-off-by: Tim Grant <tgrant@nvidia.com>
|
Closing, replaced by #2185 |
FindCUDA is obsolete. FindCUDAToolkit was added in CMake 3.17. Current cmake_minimum_required is already 3.19.