From b3e18d23a31313b4885f0c2fb8ca8afc0ff8199c Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Wed, 5 Mar 2014 11:25:27 +0400 Subject: [PATCH] Implicit CUDA and OpenCL control for module definition added. Feature allows to exclude CUDA or OpenCL optimizations at all even CUDA is used on build. Exclusion of CUDA or OpenCL cut unwanted dependencies. --- cmake/OpenCVModule.cmake | 66 +++++++++++++++++-------- modules/nonfree/CMakeLists.txt | 2 +- modules/superres/src/cuda/btv_l1_gpu.cu | 2 +- modules/ts/CMakeLists.txt | 2 +- 4 files changed, 49 insertions(+), 23 deletions(-) diff --git a/cmake/OpenCVModule.cmake b/cmake/OpenCVModule.cmake index 03818018d..c9c351113 100644 --- a/cmake/OpenCVModule.cmake +++ b/cmake/OpenCVModule.cmake @@ -479,39 +479,49 @@ endmacro() # finds and sets headers and sources for the standard OpenCV module # Usage: # ocv_glob_module_sources() -macro(ocv_glob_module_sources) +macro(ocv_glob_module_sources EXCLUDE_CUDA EXCLUDE_OPENCL) file(GLOB_RECURSE lib_srcs "src/*.cpp") file(GLOB_RECURSE lib_int_hdrs "src/*.hpp" "src/*.h") file(GLOB lib_hdrs "include/opencv2/${name}/*.hpp" "include/opencv2/${name}/*.h") file(GLOB lib_hdrs_detail "include/opencv2/${name}/detail/*.hpp" "include/opencv2/${name}/detail/*.h") - file(GLOB lib_cuda_srcs "src/cuda/*.cu") - set(cuda_objs "") - set(lib_cuda_hdrs "") - if(HAVE_CUDA) - ocv_include_directories(${CUDA_INCLUDE_DIRS}) - file(GLOB lib_cuda_hdrs "src/cuda/*.hpp") + if (NOT ${EXCLUDE_CUDA}) + file(GLOB lib_cuda_srcs "src/cuda/*.cu") + set(cuda_objs "") + set(lib_cuda_hdrs "") + if(HAVE_CUDA) + ocv_include_directories(${CUDA_INCLUDE_DIRS}) + file(GLOB lib_cuda_hdrs "src/cuda/*.hpp") - ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs}) - source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs}) + ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs}) + source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs}) + endif() + else() + set(cuda_objs "") + set(lib_cuda_srcs "") + set(lib_cuda_hdrs "") endif() source_group("Src" FILES ${lib_srcs} ${lib_int_hdrs}) - file(GLOB cl_kernels "src/opencl/*.cl") - if(HAVE_opencv_ocl AND cl_kernels) - ocv_include_directories(${OPENCL_INCLUDE_DIRS}) - add_custom_command( - OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp" - COMMAND ${CMAKE_COMMAND} -DCL_DIR="${CMAKE_CURRENT_SOURCE_DIR}/src/opencl" -DOUTPUT="${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" -P "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake" - DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake") - source_group("OpenCL" FILES ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp") - list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp") + if (NOT ${EXCLUDE_OPENCL}) + file(GLOB cl_kernels "src/opencl/*.cl") + if(HAVE_opencv_ocl AND cl_kernels) + ocv_include_directories(${OPENCL_INCLUDE_DIRS}) + add_custom_command( + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp" + COMMAND ${CMAKE_COMMAND} -DCL_DIR="${CMAKE_CURRENT_SOURCE_DIR}/src/opencl" -DOUTPUT="${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" -P "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake" + DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake") + source_group("OpenCL" FILES ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp") + list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp") + endif() endif() source_group("Include" FILES ${lib_hdrs}) source_group("Include\\detail" FILES ${lib_hdrs_detail}) + message(":${EXCLUDE_CUDA}: ${lib_cuda_srcs}") + ocv_set_module_sources(${ARGN} HEADERS ${lib_hdrs} ${lib_hdrs_detail} SOURCES ${lib_srcs} ${lib_int_hdrs} ${cuda_objs} ${lib_cuda_srcs} ${lib_cuda_hdrs}) endmacro() @@ -614,9 +624,25 @@ endmacro() # Usage: # ocv_define_module(module_name [INTERNAL] [REQUIRED] [] [OPTIONAL ]) macro(ocv_define_module module_name) - ocv_add_module(${module_name} ${ARGN}) + set(_tmp_argn ${ARGN}) + set(exclude_cuda 0) + set(exclude_opencl 0) + set(argv0 ${ARGV1}) + set(argv1 ${ARGV2}) + set(argv2 ${ARGV3}) + foreach(i RANGE 0 2) + if("${argv${i}}" STREQUAL "EXCLUDE_CUDA") + set(exclude_cuda 1) + list(REMOVE_AT _tmp_argn ${i}) + elseif ("${argv${i}}" STREQUAL "EXCLUDE_OPENCL") + set(exclude_opencl 1) + list(REMOVE_AT _tmp_argn ${i}) + endif() + endforeach() + + ocv_add_module(${module_name} ${_tmp_argn}) ocv_module_include_directories() - ocv_glob_module_sources() + ocv_glob_module_sources(${exclude_cuda} ${exclude_opencl}) ocv_create_module() ocv_add_precompiled_headers(${the_module}) diff --git a/modules/nonfree/CMakeLists.txt b/modules/nonfree/CMakeLists.txt index b43273bc8..571614be0 100644 --- a/modules/nonfree/CMakeLists.txt +++ b/modules/nonfree/CMakeLists.txt @@ -6,7 +6,7 @@ set(the_description "Functionality with possible limitations on the use") ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef -Wshadow) if(ENABLE_DYNAMIC_CUDA) add_definitions(-DDYNAMIC_CUDA_SUPPORT) - ocv_define_module(nonfree opencv_imgproc opencv_features2d opencv_calib3d OPTIONAL opencv_ocl) + ocv_define_module(nonfree EXCLUDE_CUDA opencv_imgproc opencv_features2d opencv_calib3d OPTIONAL opencv_ocl) else() ocv_define_module(nonfree opencv_imgproc opencv_features2d opencv_calib3d OPTIONAL opencv_gpu opencv_ocl) endif() diff --git a/modules/superres/src/cuda/btv_l1_gpu.cu b/modules/superres/src/cuda/btv_l1_gpu.cu index b4d96190a..4b0ebdc59 100644 --- a/modules/superres/src/cuda/btv_l1_gpu.cu +++ b/modules/superres/src/cuda/btv_l1_gpu.cu @@ -42,7 +42,7 @@ #include "opencv2/opencv_modules.hpp" -#ifdef HAVE_OPENCV_GPU +#if defined(HAVE_OPENCV_GPU) && !defined(DYNAMIC_CUDA_SUPPORT) #include "opencv2/gpu/device/common.hpp" #include "opencv2/gpu/device/transform.hpp" diff --git a/modules/ts/CMakeLists.txt b/modules/ts/CMakeLists.txt index bb56da2d9..dcd3e1563 100644 --- a/modules/ts/CMakeLists.txt +++ b/modules/ts/CMakeLists.txt @@ -11,7 +11,7 @@ ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef) ocv_add_module(ts opencv_core opencv_features2d) -ocv_glob_module_sources() +ocv_glob_module_sources(0 0) ocv_module_include_directories() ocv_create_module()