From 4b031345f0c49b183d5e2b849188572984d44e10 Mon Sep 17 00:00:00 2001 From: SamareshSingh <97642706+ssam18@users.noreply.github.com> Date: Thu, 14 May 2026 07:43:59 -0500 Subject: [PATCH] Merge pull request #28954 from ssam18:fix-cuda13-empty-arch-detection-28952 Fix CMake crash when CUDA arch autodetection comes up empty #28954 With CUDA 13 and a system gcc that the toolkit does not support, every NVCC probe inside ocv_filter_available_architecture fails and the result list ends up empty. The next line then calls list(GET ... -1) on that empty list, so CMake dies with the unhelpful message about list GET given empty list and the user has no obvious next step. This change replaces that path with a fatal error that points at the three real fixes, namely setting CUDA_ARCH_BIN, setting CUDA_HOST_COMPILER, or enabling OPENCV_CMAKE_CUDA_DEBUG to see what NVCC actually printed. It also forwards CMAKE_CUDA_HOST_COMPILER into CUDA_HOST_COMPILER since the issue reporter passed the standard CMake variable and it was being silently ignored. Fixes #28952 --- cmake/OpenCVDetectCUDA.cmake | 4 ++++ cmake/OpenCVDetectCUDAUtils.cmake | 3 +++ 2 files changed, 7 insertions(+) diff --git a/cmake/OpenCVDetectCUDA.cmake b/cmake/OpenCVDetectCUDA.cmake index c2179da804..fd08dfd470 100644 --- a/cmake/OpenCVDetectCUDA.cmake +++ b/cmake/OpenCVDetectCUDA.cmake @@ -14,6 +14,10 @@ if(CUDA_TOOLKIT_ROOT_DIR) set(CUDA_TOOLKIT_TARGET_DIR ${CUDA_TOOLKIT_ROOT_DIR}) endif() +if(CMAKE_CUDA_HOST_COMPILER AND NOT CUDA_HOST_COMPILER) + set(CUDA_HOST_COMPILER "${CMAKE_CUDA_HOST_COMPILER}" CACHE FILEPATH "Host side compiler used by NVCC") +endif() + if(((NOT CMAKE_VERSION VERSION_LESS "3.9.0") # requires https://gitlab.kitware.com/cmake/cmake/merge_requests/663 OR OPENCV_CUDA_FORCE_EXTERNAL_CMAKE_MODULE) AND NOT OPENCV_CUDA_FORCE_BUILTIN_CMAKE_MODULE) diff --git a/cmake/OpenCVDetectCUDAUtils.cmake b/cmake/OpenCVDetectCUDAUtils.cmake index 8d2c87b443..808f6bb550 100644 --- a/cmake/OpenCVDetectCUDAUtils.cmake +++ b/cmake/OpenCVDetectCUDAUtils.cmake @@ -300,6 +300,9 @@ macro(ocv_set_cuda_arch_bin_and_ptx nvcc_executable) ${_arch_hopper} ${_arch_blackwell} ) + if(NOT __cuda_arch_bin) + message(FATAL_ERROR "CUDA: No compatible CUDA architecture found. Please enable OPENCV_CMAKE_CUDA_DEBUG=1 for investigation.") + endif() list(GET __cuda_arch_bin -1 __cuda_arch_ptx) endif() endif()