2020-07-04 05:11:11 +08:00
if ( ( WIN32 AND NOT MSVC ) OR OPENCV_CMAKE_FORCE_CUDA )
2013-01-23 19:32:49 +08:00
message ( STATUS "CUDA compilation is disabled (due to only Visual Studio compiler supported on your platform)." )
2012-11-24 03:11:44 +08:00
return ( )
endif ( )
2020-07-04 05:11:11 +08:00
if ( ( NOT UNIX AND CV_CLANG ) OR OPENCV_CMAKE_FORCE_CUDA )
2013-01-23 19:32:49 +08:00
message ( STATUS "CUDA compilation is disabled (due to Clang unsupported on your platform)." )
2012-09-21 21:42:37 +08:00
return ( )
endif ( )
2020-07-04 05:11:11 +08:00
#set(OPENCV_CMAKE_CUDA_DEBUG 1)
2018-07-11 18:47:56 +08:00
if ( ( ( NOT CMAKE_VERSION VERSION_LESS "3.9.0" ) # requires https://gitlab.kitware.com/cmake/cmake/merge_requests/663
O R O P E N C V _ C U D A _ F O R C E _ E X T E R N A L _ C M A K E _ M O D U L E )
A N D N O T O P E N C V _ C U D A _ F O R C E _ B U I L T I N _ C M A K E _ M O D U L E )
2019-12-13 22:52:40 +08:00
ocv_update ( CUDA_LINK_LIBRARIES_KEYWORD "PRIVATE" )
2018-07-11 18:47:56 +08:00
find_host_package ( CUDA "${MIN_VER_CUDA}" QUIET )
else ( )
# Use OpenCV's patched "FindCUDA" module
set ( CMAKE_MODULE_PATH "${OpenCV_SOURCE_DIR}/cmake" ${ CMAKE_MODULE_PATH } )
if ( ANDROID )
set ( CUDA_TARGET_OS_VARIANT "Android" )
endif ( )
find_host_package ( CUDA "${MIN_VER_CUDA}" QUIET )
2013-10-30 20:10:02 +08:00
2018-07-11 18:47:56 +08:00
list ( REMOVE_AT CMAKE_MODULE_PATH 0 )
endif ( )
2013-10-30 20:10:02 +08:00
2012-02-03 19:26:49 +08:00
if ( CUDA_FOUND )
2022-10-28 16:03:51 +08:00
unset ( CUDA_nvcuvenc_LIBRARY CACHE )
2012-02-03 19:26:49 +08:00
set ( HAVE_CUDA 1 )
2020-06-30 16:28:36 +08:00
if ( NOT CUDA_VERSION VERSION_LESS 11.0 )
2020-06-28 04:34:44 +08:00
# CUDA 11.0 removes nppicom
ocv_list_filterout ( CUDA_nppi_LIBRARY "nppicom" )
ocv_list_filterout ( CUDA_npp_LIBRARY "nppicom" )
endif ( )
2012-02-03 19:26:49 +08:00
if ( WITH_CUFFT )
set ( HAVE_CUFFT 1 )
endif ( )
if ( WITH_CUBLAS )
set ( HAVE_CUBLAS 1 )
endif ( )
2019-06-02 19:47:15 +08:00
if ( WITH_CUDNN )
set ( CMAKE_MODULE_PATH "${OpenCV_SOURCE_DIR}/cmake" ${ CMAKE_MODULE_PATH } )
find_host_package ( CUDNN "${MIN_VER_CUDNN}" )
list ( REMOVE_AT CMAKE_MODULE_PATH 0 )
if ( CUDNN_FOUND )
set ( HAVE_CUDNN 1 )
endif ( )
endif ( )
2022-10-28 16:03:51 +08:00
if ( WITH_NVCUVID OR WITH_NVCUVENC )
2020-07-04 05:11:11 +08:00
macro ( ocv_cuda_SEARCH_NVCUVID_HEADER _filename _result )
2020-06-11 22:06:18 +08:00
# place header file under CUDA_TOOLKIT_TARGET_DIR or CUDA_TOOLKIT_ROOT_DIR
find_path ( _header_result
$ { _ f i l e n a m e }
P A T H S " $ { C U D A _ T O O L K I T _ T A R G E T _ D I R } " " $ { C U D A _ T O O L K I T _ R O O T _ D I R } "
E N V C U D A _ P A T H
E N V C U D A _ I N C _ P A T H
P A T H _ S U F F I X E S i n c l u d e
N O _ D E F A U L T _ P A T H
)
if ( "x${_header_result}" STREQUAL "x_header_result-NOTFOUND" )
set ( ${ _result } 0 )
else ( )
set ( ${ _result } 1 )
endif ( )
2020-06-16 10:14:04 +08:00
unset ( _header_result CACHE )
2020-06-11 22:06:18 +08:00
endmacro ( )
2022-10-28 16:03:51 +08:00
if ( WITH_NVCUVID )
ocv_cuda_SEARCH_NVCUVID_HEADER ( "nvcuvid.h" HAVE_NVCUVID_HEADER )
ocv_cuda_SEARCH_NVCUVID_HEADER ( "dynlink_nvcuvid.h" HAVE_DYNLINK_NVCUVID_HEADER )
find_cuda_helper_libs ( nvcuvid )
if ( CUDA_nvcuvid_LIBRARY AND ( ${ HAVE_NVCUVID_HEADER } OR ${ HAVE_DYNLINK_NVCUVID_HEADER } ) )
# make sure to have both header and library before enabling
set ( HAVE_NVCUVID 1 )
endif ( )
2016-01-31 00:28:15 +08:00
endif ( )
2022-10-28 16:03:51 +08:00
if ( WITH_NVCUVENC )
ocv_cuda_SEARCH_NVCUVID_HEADER ( "nvEncodeAPI.h" HAVE_NVCUVENC_HEADER )
if ( WIN32 )
find_cuda_helper_libs ( nvencodeapi )
else ( )
find_cuda_helper_libs ( nvidia-encode )
endif ( )
if ( ( CUDA_nvencodeapi_LIBRARY OR CUDA_nvidia-encode_LIBRARY ) AND ${ HAVE_NVCUVENC_HEADER } )
set ( HAVE_NVCUVENC 1 )
endif ( )
2016-01-31 00:28:15 +08:00
endif ( )
2013-01-23 19:32:49 +08:00
endif ( )
2012-02-03 19:26:49 +08:00
message ( STATUS "CUDA detected: " ${ CUDA_VERSION } )
2020-07-29 16:31:39 +08:00
OCV_OPTION ( CUDA_ENABLE_DEPRECATED_GENERATION "Enable deprecated generations in the list" OFF )
set ( _generations "Maxwell" "Pascal" "Volta" "Turing" "Ampere" )
if ( CUDA_ENABLE_DEPRECATED_GENERATION )
set ( _generations "Fermi" "${_generations}" )
set ( _generations "Kepler" "${_generations}" )
endif ( )
2020-06-02 04:07:53 +08:00
set ( _arch_fermi "2.0" )
set ( _arch_kepler "3.0;3.5;3.7" )
set ( _arch_maxwell "5.0;5.2" )
set ( _arch_pascal "6.0;6.1" )
set ( _arch_volta "7.0" )
set ( _arch_turing "7.5" )
2020-09-25 21:33:55 +08:00
set ( _arch_ampere "8.0;8.6" )
2013-02-26 17:02:54 +08:00
if ( NOT CMAKE_CROSSCOMPILING )
list ( APPEND _generations "Auto" )
endif ( )
set ( CUDA_GENERATION "" CACHE STRING "Build CUDA device code only for specific GPU architecture. Leave empty to build for all architectures." )
if ( CMAKE_VERSION VERSION_GREATER "2.8" )
set_property ( CACHE CUDA_GENERATION PROPERTY STRINGS "" ${ _generations } )
endif ( )
if ( CUDA_GENERATION )
if ( NOT ";${_generations};" MATCHES ";${CUDA_GENERATION};" )
string ( REPLACE ";" ", " _generations "${_generations}" )
2021-07-26 12:54:27 +08:00
message ( FATAL_ERROR "ERROR: ${_generations} Generations are supported." )
2013-02-26 17:02:54 +08:00
endif ( )
unset ( CUDA_ARCH_BIN CACHE )
unset ( CUDA_ARCH_PTX CACHE )
endif ( )
2020-07-04 05:11:11 +08:00
if ( OPENCV_CUDA_DETECTION_NVCC_FLAGS MATCHES "-ccbin" )
# already specified by user
elseif ( CUDA_HOST_COMPILER AND EXISTS "${CUDA_HOST_COMPILER}" )
2020-07-12 20:22:12 +08:00
get_filename_component ( c_compiler_realpath "${CMAKE_C_COMPILER}" REALPATH )
# C compiler doesn't work with --run option, forcing C++ compiler instead
if ( CUDA_HOST_COMPILER STREQUAL c_compiler_realpath OR CUDA_HOST_COMPILER STREQUAL CMAKE_C_COMPILER )
if ( DEFINED CMAKE_CXX_COMPILER )
get_filename_component ( cxx_compiler_realpath "${CMAKE_CXX_COMPILER}" REALPATH )
LIST ( APPEND OPENCV_CUDA_DETECTION_NVCC_FLAGS -ccbin "${cxx_compiler_realpath}" )
else ( )
message ( STATUS "CUDA: CMAKE_CXX_COMPILER is not available. You may need to specify CUDA_HOST_COMPILER." )
endif ( )
else ( )
LIST ( APPEND OPENCV_CUDA_DETECTION_NVCC_FLAGS -ccbin "${CUDA_HOST_COMPILER}" )
endif ( )
2020-07-04 05:11:11 +08:00
elseif ( WIN32 AND CMAKE_LINKER ) # Workaround for VS cl.exe not being in the env. path
get_filename_component ( host_compiler_bindir ${ CMAKE_LINKER } DIRECTORY )
LIST ( APPEND OPENCV_CUDA_DETECTION_NVCC_FLAGS -ccbin "${host_compiler_bindir}" )
2020-06-11 15:34:31 +08:00
else ( )
2020-07-04 05:11:11 +08:00
if ( CUDA_HOST_COMPILER )
message ( STATUS "CUDA: CUDA_HOST_COMPILER='${CUDA_HOST_COMPILER}' is not valid, autodetection may not work. Specify OPENCV_CUDA_DETECTION_NVCC_FLAGS with -ccbin option for fix that" )
2020-06-11 15:34:31 +08:00
endif ( )
2018-08-16 14:43:52 +08:00
endif ( )
2020-06-02 04:07:53 +08:00
macro ( ocv_filter_available_architecture result_list )
2020-07-04 05:11:11 +08:00
set ( __cache_key_check "${ARGN} : ${CUDA_NVCC_EXECUTABLE} ${OPENCV_CUDA_DETECTION_NVCC_FLAGS}" )
if ( DEFINED OPENCV_CACHE_CUDA_SUPPORTED_CC AND OPENCV_CACHE_CUDA_SUPPORTED_CC_check STREQUAL __cache_key_check )
set ( ${ result_list } "${OPENCV_CACHE_CUDA_SUPPORTED_CC}" )
2020-06-02 04:07:53 +08:00
else ( )
set ( CC_LIST ${ ARGN } )
foreach ( target_arch ${ CC_LIST } )
2020-06-17 21:02:51 +08:00
string ( REPLACE "." "" target_arch_short "${target_arch}" )
2020-06-02 04:07:53 +08:00
set ( NVCC_OPTION "-gencode;arch=compute_${target_arch_short},code=sm_${target_arch_short}" )
2020-07-04 05:11:11 +08:00
set ( _cmd "${CUDA_NVCC_EXECUTABLE}" ${ OPENCV_CUDA_DETECTION_NVCC_FLAGS } ${ NVCC_OPTION } "${OpenCV_SOURCE_DIR}/cmake/checks/OpenCVDetectCudaArch.cu" --compile )
execute_process (
C O M M A N D $ { _ c m d }
W O R K I N G _ D I R E C T O R Y " $ { C M A K E _ B I N A R Y _ D I R } $ { C M A K E _ F I L E S _ D I R E C T O R Y } / C M a k e T m p / "
R E S U L T _ V A R I A B L E _ n v c c _ r e s
O U T P U T _ V A R I A B L E _ n v c c _ o u t
E R R O R _ V A R I A B L E _ n v c c _ e r r
#ERROR_QUIET
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
)
if ( OPENCV_CMAKE_CUDA_DEBUG )
message ( WARNING "COMMAND: ${_cmd}" )
message ( STATUS "Result: ${_nvcc_res}" )
message ( STATUS "Out: ${_nvcc_out}" )
message ( STATUS "Err: ${_nvcc_err}" )
endif ( )
2020-06-02 04:07:53 +08:00
if ( _nvcc_res EQUAL 0 )
2020-07-04 05:11:11 +08:00
LIST ( APPEND ${ result_list } "${target_arch}" )
2020-06-02 04:07:53 +08:00
endif ( )
endforeach ( )
2020-06-17 21:02:51 +08:00
string ( STRIP "${${result_list}}" ${ result_list } )
2020-07-04 05:11:11 +08:00
if ( " ${${result_list}}" STREQUAL " " )
message ( WARNING "CUDA: Autodetection arch list is empty. Please enable OPENCV_CMAKE_CUDA_DEBUG=1 and check/specify OPENCV_CUDA_DETECTION_NVCC_FLAGS variable" )
endif ( )
# cache detected values
set ( OPENCV_CACHE_CUDA_SUPPORTED_CC ${ ${result_list } } CACHE INTERNAL "" )
set ( OPENCV_CACHE_CUDA_SUPPORTED_CC_check "${__cache_key_check}" CACHE INTERNAL "" )
2020-06-02 04:07:53 +08:00
endif ( )
endmacro ( )
macro ( ocv_detect_native_cuda_arch status output )
2020-07-04 05:11:11 +08:00
set ( OPENCV_CUDA_DETECT_ARCHS_COMMAND "${CUDA_NVCC_EXECUTABLE}" ${ OPENCV_CUDA_DETECTION_NVCC_FLAGS } "${OpenCV_SOURCE_DIR}/cmake/checks/OpenCVDetectCudaArch.cu" "--run" )
set ( __cache_key_check "${OPENCV_CUDA_DETECT_ARCHS_COMMAND}" )
if ( DEFINED OPENCV_CACHE_CUDA_ACTIVE_CC AND OPENCV_CACHE_CUDA_ACTIVE_CC_check STREQUAL __cache_key_check )
set ( ${ output } "${OPENCV_CACHE_CUDA_ACTIVE_CC}" )
set ( ${ status } 0 )
else ( )
execute_process (
C O M M A N D $ { O P E N C V _ C U D A _ D E T E C T _ A R C H S _ C O M M A N D }
W O R K I N G _ D I R E C T O R Y " $ { C M A K E _ B I N A R Y _ D I R } $ { C M A K E _ F I L E S _ D I R E C T O R Y } / C M a k e T m p / "
R E S U L T _ V A R I A B L E $ { s t a t u s }
O U T P U T _ V A R I A B L E _ n v c c _ o u t
E R R O R _ V A R I A B L E _ n v c c _ e r r
E R R O R _ Q U I E T
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
)
if ( OPENCV_CMAKE_CUDA_DEBUG )
message ( WARNING "COMMAND: ${OPENCV_CUDA_DETECT_ARCHS_COMMAND}" )
message ( STATUS "Result: ${${status}}" )
message ( STATUS "Out: ${_nvcc_out}" )
message ( STATUS "Err: ${_nvcc_err}" )
endif ( )
string ( REGEX REPLACE ".*\n" "" ${ output } "${_nvcc_out}" ) #Strip leading warning messages, if any
if ( ${ status } EQUAL 0 )
# cache detected values
2020-10-10 06:00:02 +08:00
set ( OPENCV_CACHE_CUDA_ACTIVE_CC ${ ${output } } CACHE INTERNAL "" )
2020-07-04 05:11:11 +08:00
set ( OPENCV_CACHE_CUDA_ACTIVE_CC_check "${__cache_key_check}" CACHE INTERNAL "" )
endif ( )
endif ( )
2020-06-02 04:07:53 +08:00
endmacro ( )
2013-02-26 17:02:54 +08:00
set ( __cuda_arch_ptx "" )
if ( CUDA_GENERATION STREQUAL "Fermi" )
2020-06-02 04:07:53 +08:00
set ( __cuda_arch_bin ${ _arch_fermi } )
2013-02-26 17:02:54 +08:00
elseif ( CUDA_GENERATION STREQUAL "Kepler" )
2020-06-02 04:07:53 +08:00
set ( __cuda_arch_bin ${ _arch_kepler } )
2016-07-13 18:03:16 +08:00
elseif ( CUDA_GENERATION STREQUAL "Maxwell" )
2020-06-02 04:07:53 +08:00
set ( __cuda_arch_bin ${ _arch_maxwell } )
2016-07-13 18:03:16 +08:00
elseif ( CUDA_GENERATION STREQUAL "Pascal" )
2020-06-02 04:07:53 +08:00
set ( __cuda_arch_bin ${ _arch_pascal } )
2017-08-24 15:11:44 +08:00
elseif ( CUDA_GENERATION STREQUAL "Volta" )
2020-06-02 04:07:53 +08:00
set ( __cuda_arch_bin ${ _arch_volta } )
2018-10-16 01:40:24 +08:00
elseif ( CUDA_GENERATION STREQUAL "Turing" )
2020-06-02 04:07:53 +08:00
set ( __cuda_arch_bin ${ _arch_turing } )
2020-06-19 19:46:18 +08:00
elseif ( CUDA_GENERATION STREQUAL "Ampere" )
set ( __cuda_arch_bin ${ _arch_ampere } )
2013-02-26 17:02:54 +08:00
elseif ( CUDA_GENERATION STREQUAL "Auto" )
2020-06-02 04:07:53 +08:00
ocv_detect_native_cuda_arch ( _nvcc_res _nvcc_out )
2013-02-26 17:02:54 +08:00
if ( NOT _nvcc_res EQUAL 0 )
message ( STATUS "Automatic detection of CUDA generation failed. Going to build for all known architectures." )
else ( )
2020-06-02 04:07:53 +08:00
string ( REGEX MATCHALL "[0-9]+\\.[0-9]" __cuda_arch_bin "${_nvcc_out}" )
2013-02-26 17:02:54 +08:00
endif ( )
2020-07-04 05:11:11 +08:00
elseif ( CUDA_ARCH_BIN )
message ( STATUS "CUDA: Using CUDA_ARCH_BIN=${CUDA_ARCH_BIN}" )
set ( __cuda_arch_bin ${ CUDA_ARCH_BIN } )
2013-02-26 17:02:54 +08:00
endif ( )
if ( NOT DEFINED __cuda_arch_bin )
2015-10-31 01:28:19 +08:00
if ( ARM )
set ( __cuda_arch_bin "3.2" )
set ( __cuda_arch_ptx "" )
elseif ( AARCH64 )
2020-07-04 05:11:11 +08:00
if ( NOT CMAKE_CROSSCOMPILING )
ocv_detect_native_cuda_arch ( _nvcc_res _nvcc_out )
else ( )
set ( _nvcc_res -1 ) # emulate error, see below
endif ( )
2017-04-10 21:19:17 +08:00
if ( NOT _nvcc_res EQUAL 0 )
message ( STATUS "Automatic detection of CUDA generation failed. Going to build for all known architectures." )
2022-06-29 19:35:27 +08:00
# TX1 (5.3) TX2 (6.2) Xavier (7.2) V100 (7.0) Orin (8.7)
2020-06-21 06:20:47 +08:00
ocv_filter_available_architecture ( __cuda_arch_bin
5 . 3
6 . 2
7 . 2
7 . 0
2022-06-29 19:35:27 +08:00
8 . 7
2020-06-21 06:20:47 +08:00
)
2017-04-10 21:19:17 +08:00
else ( )
set ( __cuda_arch_bin "${_nvcc_out}" )
endif ( )
2015-10-31 01:28:19 +08:00
set ( __cuda_arch_ptx "" )
2013-07-18 13:44:00 +08:00
else ( )
2020-06-02 04:07:53 +08:00
ocv_filter_available_architecture ( __cuda_arch_bin
$ { _ a r c h _ f e r m i }
$ { _ a r c h _ k e p l e r }
$ { _ a r c h _ m a x w e l l }
$ { _ a r c h _ p a s c a l }
$ { _ a r c h _ v o l t a }
$ { _ a r c h _ t u r i n g }
2020-06-19 19:46:18 +08:00
$ { _ a r c h _ a m p e r e }
2020-06-02 04:07:53 +08:00
)
2013-07-18 13:44:00 +08:00
endif ( )
2013-02-26 17:02:54 +08:00
endif ( )
set ( CUDA_ARCH_BIN ${ __cuda_arch_bin } CACHE STRING "Specify 'real' GPU architectures to build binaries for, BIN(PTX) format is supported" )
set ( CUDA_ARCH_PTX ${ __cuda_arch_ptx } CACHE STRING "Specify 'virtual' PTX architectures to build PTX intermediate code for" )
2012-06-20 13:41:16 +08:00
2012-02-03 19:26:49 +08:00
string ( REGEX REPLACE "\\." "" ARCH_BIN_NO_POINTS "${CUDA_ARCH_BIN}" )
string ( REGEX REPLACE "\\." "" ARCH_PTX_NO_POINTS "${CUDA_ARCH_PTX}" )
2020-07-29 16:31:39 +08:00
# Check if user specified 1.0/2.1 compute capability: we don't support it
macro ( ocv_wipeout_deprecated_cc target_cc )
if ( " ${CUDA_ARCH_BIN} ${CUDA_ARCH_PTX}" MATCHES " ${target_cc}" )
message ( SEND_ERROR "CUDA: ${target_cc} compute capability is not supported - exclude it from ARCH/PTX list and re-run CMake" )
endif ( )
endmacro ( )
ocv_wipeout_deprecated_cc ( "1.0" )
ocv_wipeout_deprecated_cc ( "2.1" )
2012-02-03 19:26:49 +08:00
# NVCC flags to be set
set ( NVCC_FLAGS_EXTRA "" )
# These vars will be passed into the templates
set ( OPENCV_CUDA_ARCH_BIN "" )
set ( OPENCV_CUDA_ARCH_PTX "" )
set ( OPENCV_CUDA_ARCH_FEATURES "" )
# Tell NVCC to add binaries for the specified GPUs
string ( REGEX MATCHALL "[0-9()]+" ARCH_LIST "${ARCH_BIN_NO_POINTS}" )
foreach ( ARCH IN LISTS ARCH_LIST )
if ( ARCH MATCHES "([0-9]+)\\(([0-9]+)\\)" )
# User explicitly specified PTX for the concrete BIN
set ( NVCC_FLAGS_EXTRA ${ NVCC_FLAGS_EXTRA } -gencode arch=compute_ ${ CMAKE_MATCH_2 } ,code=sm_ ${ CMAKE_MATCH_1 } )
set ( OPENCV_CUDA_ARCH_BIN "${OPENCV_CUDA_ARCH_BIN} ${CMAKE_MATCH_1}" )
set ( OPENCV_CUDA_ARCH_FEATURES "${OPENCV_CUDA_ARCH_FEATURES} ${CMAKE_MATCH_2}" )
else ( )
# User didn't explicitly specify PTX for the concrete BIN, we assume PTX=BIN
set ( NVCC_FLAGS_EXTRA ${ NVCC_FLAGS_EXTRA } -gencode arch=compute_ ${ ARCH } ,code=sm_ ${ ARCH } )
set ( OPENCV_CUDA_ARCH_BIN "${OPENCV_CUDA_ARCH_BIN} ${ARCH}" )
set ( OPENCV_CUDA_ARCH_FEATURES "${OPENCV_CUDA_ARCH_FEATURES} ${ARCH}" )
endif ( )
endforeach ( )
2016-05-26 18:45:50 +08:00
set ( NVCC_FLAGS_EXTRA ${ NVCC_FLAGS_EXTRA } -D_FORCE_INLINES )
2012-02-03 19:26:49 +08:00
# Tell NVCC to add PTX intermediate code for the specified architectures
string ( REGEX MATCHALL "[0-9]+" ARCH_LIST "${ARCH_PTX_NO_POINTS}" )
2013-01-23 19:32:49 +08:00
foreach ( ARCH IN LISTS ARCH_LIST )
set ( NVCC_FLAGS_EXTRA ${ NVCC_FLAGS_EXTRA } -gencode arch=compute_ ${ ARCH } ,code=compute_ ${ ARCH } )
set ( OPENCV_CUDA_ARCH_PTX "${OPENCV_CUDA_ARCH_PTX} ${ARCH}" )
set ( OPENCV_CUDA_ARCH_FEATURES "${OPENCV_CUDA_ARCH_FEATURES} ${ARCH}" )
endforeach ( )
2012-02-03 19:26:49 +08:00
# These vars will be processed in other scripts
set ( CUDA_NVCC_FLAGS ${ CUDA_NVCC_FLAGS } ${ NVCC_FLAGS_EXTRA } )
set ( OpenCV_CUDA_CC "${NVCC_FLAGS_EXTRA}" )
2013-10-30 14:21:55 +08:00
if ( ANDROID )
set ( CUDA_NVCC_FLAGS ${ CUDA_NVCC_FLAGS } "-Xptxas;-dlcm=ca" )
endif ( )
2012-02-03 19:26:49 +08:00
message ( STATUS "CUDA NVCC target flags: ${CUDA_NVCC_FLAGS}" )
2012-10-17 07:18:30 +08:00
2013-01-23 19:32:49 +08:00
OCV_OPTION ( CUDA_FAST_MATH "Enable --use_fast_math for CUDA compiler " OFF )
2012-10-17 07:18:30 +08:00
2012-10-12 16:45:50 +08:00
if ( CUDA_FAST_MATH )
2012-10-17 07:18:30 +08:00
set ( CUDA_NVCC_FLAGS ${ CUDA_NVCC_FLAGS } --use_fast_math )
2012-10-11 02:16:42 +08:00
endif ( )
2012-10-17 07:18:30 +08:00
2012-10-11 02:16:42 +08:00
mark_as_advanced ( CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_VERBOSE_BUILD CUDA_SDK_ROOT_DIR )
2012-02-03 19:26:49 +08:00
2016-12-04 06:51:28 +08:00
macro ( ocv_cuda_filter_options )
2012-08-23 19:46:19 +08:00
foreach ( var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG )
set ( ${ var } _backup_in_cuda_compile_ "${${var}}" )
2012-10-17 07:18:30 +08:00
2018-08-03 14:42:28 +08:00
if ( CV_CLANG )
# we remove -Winconsistent-missing-override and -Qunused-arguments
# just in case we are compiling CUDA with gcc but OpenCV with clang
string ( REPLACE "-Winconsistent-missing-override" "" ${ var } "${${var}}" )
string ( REPLACE "-Qunused-arguments" "" ${ var } "${${var}}" )
endif ( )
2013-04-03 17:29:04 +08:00
# we remove /EHa as it generates warnings under windows
2012-08-23 19:46:19 +08:00
string ( REPLACE "/EHa" "" ${ var } "${${var}}" )
2012-10-17 07:18:30 +08:00
2012-08-23 19:46:19 +08:00
# we remove -ggdb3 flag as it leads to preprocessor errors when compiling CUDA files (CUDA 4.1)
string ( REPLACE "-ggdb3" "" ${ var } "${${var}}" )
2013-04-03 17:29:04 +08:00
# we remove -Wsign-promo as it generates warnings under linux
string ( REPLACE "-Wsign-promo" "" ${ var } "${${var}}" )
2013-06-07 17:34:33 +08:00
2014-02-14 21:56:03 +08:00
# we remove -Wno-sign-promo as it generates warnings under linux
string ( REPLACE "-Wno-sign-promo" "" ${ var } "${${var}}" )
2013-12-03 19:50:44 +08:00
2013-10-30 14:21:30 +08:00
# we remove -Wno-delete-non-virtual-dtor because it's used for C++ compiler
# but NVCC uses C compiler by default
string ( REPLACE "-Wno-delete-non-virtual-dtor" "" ${ var } "${${var}}" )
# we remove -frtti because it's used for C++ compiler
# but NVCC uses C compiler by default
string ( REPLACE "-frtti" "" ${ var } "${${var}}" )
2014-08-13 22:08:48 +08:00
string ( REPLACE "-fvisibility-inlines-hidden" "" ${ var } "${${var}}" )
2018-04-26 20:06:59 +08:00
# cc1: warning: command line option '-Wsuggest-override' is valid for C++/ObjC++ but not for C
string ( REPLACE "-Wsuggest-override" "" ${ var } "${${var}}" )
2018-05-21 22:54:11 +08:00
# issue: #11552 (from OpenCVCompilerOptions.cmake)
string ( REGEX REPLACE "-Wimplicit-fallthrough(=[0-9]+)? " "" ${ var } "${${var}}" )
# removal of custom specified options
if ( OPENCV_CUDA_NVCC_FILTEROUT_OPTIONS )
foreach ( __flag ${ OPENCV_CUDA_NVCC_FILTEROUT_OPTIONS } )
string ( REPLACE "${__flag}" "" ${ var } "${${var}}" )
endforeach ( )
endif ( )
2012-08-23 19:46:19 +08:00
endforeach ( )
2016-12-04 06:51:28 +08:00
endmacro ( )
macro ( ocv_cuda_compile VAR )
ocv_cuda_filter_options ( )
2012-10-17 07:18:30 +08:00
2013-01-23 19:32:49 +08:00
if ( BUILD_SHARED_LIBS )
2012-02-03 19:26:49 +08:00
set ( CUDA_NVCC_FLAGS ${ CUDA_NVCC_FLAGS } -Xcompiler -DCVAPI_EXPORTS )
endif ( )
if ( UNIX OR APPLE )
2020-06-29 06:49:00 +08:00
set ( CUDA_NVCC_FLAGS ${ CUDA_NVCC_FLAGS } -Xcompiler -fPIC )
if ( NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_DEBUG} ${CUDA_NVCC_FLAGS}" MATCHES "-std=" )
if ( CUDA_VERSION VERSION_LESS "11.0" )
list ( APPEND CUDA_NVCC_FLAGS "--std=c++11" )
else ( )
list ( APPEND CUDA_NVCC_FLAGS "--std=c++14" )
endif ( )
endif ( )
2012-02-03 19:26:49 +08:00
endif ( )
if ( APPLE )
2013-01-23 19:32:49 +08:00
set ( CUDA_NVCC_FLAGS ${ CUDA_NVCC_FLAGS } -Xcompiler -fno-finite-math-only )
2012-02-03 19:26:49 +08:00
endif ( )
2012-08-21 03:35:29 +08:00
2015-10-31 01:28:19 +08:00
if ( CMAKE_CROSSCOMPILING AND ( ARM OR AARCH64 ) )
set ( CUDA_NVCC_FLAGS ${ CUDA_NVCC_FLAGS } -Xlinker --unresolved-symbols=ignore-in-shared-libs )
endif ( )
2012-08-21 03:35:29 +08:00
# disabled because of multiple warnings during building nvcc auto generated files
2018-03-26 19:30:49 +08:00
if ( CV_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "4.6.0" )
2012-08-21 03:35:29 +08:00
ocv_warnings_disable ( CMAKE_CXX_FLAGS -Wunused-but-set-variable )
endif ( )
2012-02-03 19:26:49 +08:00
2012-10-17 07:18:30 +08:00
CUDA_COMPILE ( ${ VAR } ${ ARGN } )
2012-08-23 19:46:19 +08:00
foreach ( var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG )
set ( ${ var } "${${var}_backup_in_cuda_compile_}" )
unset ( ${ var } _backup_in_cuda_compile_ )
2012-10-17 07:18:30 +08:00
endforeach ( )
2012-02-03 19:26:49 +08:00
endmacro ( )
else ( )
unset ( CUDA_ARCH_BIN CACHE )
unset ( CUDA_ARCH_PTX CACHE )
endif ( )
2014-02-28 22:18:20 +08:00
if ( HAVE_CUDA )
set ( CUDA_LIBS_PATH "" )
foreach ( p ${ CUDA_LIBRARIES } ${ CUDA_npp_LIBRARY } )
get_filename_component ( _tmp ${ p } PATH )
list ( APPEND CUDA_LIBS_PATH ${ _tmp } )
endforeach ( )
if ( HAVE_CUBLAS )
foreach ( p ${ CUDA_cublas_LIBRARY } )
get_filename_component ( _tmp ${ p } PATH )
list ( APPEND CUDA_LIBS_PATH ${ _tmp } )
endforeach ( )
endif ( )
2019-06-02 19:47:15 +08:00
if ( HAVE_CUDNN )
foreach ( p ${ CUDNN_LIBRARIES } )
get_filename_component ( _tmp ${ p } PATH )
list ( APPEND CUDA_LIBS_PATH ${ _tmp } )
endforeach ( )
endif ( )
2014-02-28 22:18:20 +08:00
if ( HAVE_CUFFT )
foreach ( p ${ CUDA_cufft_LIBRARY } )
get_filename_component ( _tmp ${ p } PATH )
list ( APPEND CUDA_LIBS_PATH ${ _tmp } )
endforeach ( )
endif ( )
list ( REMOVE_DUPLICATES CUDA_LIBS_PATH )
link_directories ( ${ CUDA_LIBS_PATH } )
set ( CUDA_LIBRARIES_ABS ${ CUDA_LIBRARIES } )
ocv_convert_to_lib_name ( CUDA_LIBRARIES ${ CUDA_LIBRARIES } )
set ( CUDA_npp_LIBRARY_ABS ${ CUDA_npp_LIBRARY } )
ocv_convert_to_lib_name ( CUDA_npp_LIBRARY ${ CUDA_npp_LIBRARY } )
if ( HAVE_CUBLAS )
set ( CUDA_cublas_LIBRARY_ABS ${ CUDA_cublas_LIBRARY } )
ocv_convert_to_lib_name ( CUDA_cublas_LIBRARY ${ CUDA_cublas_LIBRARY } )
endif ( )
2019-06-02 19:47:15 +08:00
if ( HAVE_CUDNN )
set ( CUDNN_LIBRARIES_ABS ${ CUDNN_LIBRARIES } )
ocv_convert_to_lib_name ( CUDNN_LIBRARIES ${ CUDNN_LIBRARIES } )
endif ( )
2014-02-28 22:18:20 +08:00
if ( HAVE_CUFFT )
set ( CUDA_cufft_LIBRARY_ABS ${ CUDA_cufft_LIBRARY } )
ocv_convert_to_lib_name ( CUDA_cufft_LIBRARY ${ CUDA_cufft_LIBRARY } )
endif ( )
2020-07-07 06:58:17 +08:00
if ( CMAKE_GENERATOR MATCHES "Visual Studio"
A N D N O T O P E N C V _ S K I P _ C U D A _ C M A K E _ S U P P R E S S _ R E G E N E R A T I O N
)
2020-07-04 05:11:11 +08:00
message ( STATUS "CUDA: MSVS generator is detected. Disabling CMake re-run checks (CMAKE_SUPPRESS_REGENERATION=ON). You need to run CMake manually if updates are required." )
2020-07-07 06:58:17 +08:00
set ( CMAKE_SUPPRESS_REGENERATION ON )
endif ( )
2014-03-24 19:47:56 +08:00
endif ( )
2019-07-06 07:10:41 +08:00
# ----------------------------------------------------------------------------
# Add CUDA libraries (needed for apps/tools, samples)
# ----------------------------------------------------------------------------
if ( HAVE_CUDA )
# details: https://github.com/NVIDIA/nvidia-docker/issues/775
if ( " ${CUDA_CUDA_LIBRARY}" MATCHES "/stubs/libcuda.so" AND NOT OPENCV_SKIP_CUDA_STUB_WORKAROUND )
set ( CUDA_STUB_ENABLED_LINK_WORKAROUND 1 )
if ( EXISTS "${CUDA_CUDA_LIBRARY}" AND NOT OPENCV_SKIP_CUDA_STUB_WORKAROUND_RPATH_LINK )
set ( CUDA_STUB_TARGET_PATH "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/" )
execute_process ( COMMAND ${ CMAKE_COMMAND } -E create_symlink "${CUDA_CUDA_LIBRARY}" "${CUDA_STUB_TARGET_PATH}/libcuda.so.1"
R E S U L T _ V A R I A B L E C U D A _ S T U B _ S Y M L I N K _ R E S U L T )
if ( NOT CUDA_STUB_SYMLINK_RESULT EQUAL 0 )
execute_process ( COMMAND ${ CMAKE_COMMAND } -E copy_if_different "${CUDA_CUDA_LIBRARY}" "${CUDA_STUB_TARGET_PATH}/libcuda.so.1"
R E S U L T _ V A R I A B L E C U D A _ S T U B _ C O P Y _ R E S U L T )
if ( NOT CUDA_STUB_COPY_RESULT EQUAL 0 )
set ( CUDA_STUB_ENABLED_LINK_WORKAROUND 0 )
endif ( )
endif ( )
if ( CUDA_STUB_ENABLED_LINK_WORKAROUND )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath-link,\" ${ CUDA_STUB_TARGET_PATH } \"")
endif ( )
else ( )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-shlib-undefined" )
endif ( )
if ( NOT CUDA_STUB_ENABLED_LINK_WORKAROUND )
message ( WARNING "CUDA: workaround for stubs/libcuda.so.1 is not applied" )
endif ( )
endif ( )
2019-08-06 00:07:48 +08:00
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CUDA_LIBRARIES } ${ CUDA_npp_LIBRARY } )
2019-07-06 07:10:41 +08:00
if ( HAVE_CUBLAS )
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CUDA_cublas_LIBRARY } )
endif ( )
if ( HAVE_CUDNN )
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CUDNN_LIBRARIES } )
endif ( )
if ( HAVE_CUFFT )
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CUDA_cufft_LIBRARY } )
endif ( )
foreach ( p ${ CUDA_LIBS_PATH } )
if ( MSVC AND CMAKE_GENERATOR MATCHES "Ninja|JOM" )
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CMAKE_LIBRARY_PATH_FLAG } "${p}" )
else ( )
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CMAKE_LIBRARY_PATH_FLAG } ${ p } )
endif ( )
endforeach ( )
endif ( )