build: Switch from FindCUDA to FindCUDAToolkit (#2072)#2073
build: Switch from FindCUDA to FindCUDAToolkit (#2072)#2073MarkLeone wants to merge 1 commit intoAcademySoftwareFoundation:mainfrom
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. |
FindCUDA is obsolete. FindCUDAToolkit was added in CMake 3.17. Current cmake_minimum_required is already 3.19.