From 122b9f8e17838c4c27394e1898eeb91c15b4557f Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Mon, 30 Mar 2015 14:58:37 +0300 Subject: [PATCH 1/3] Turn ocv_convert_to_lib_name into a function --- cmake/OpenCVUtils.cmake | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/cmake/OpenCVUtils.cmake b/cmake/OpenCVUtils.cmake index 4a33f3acdf..60501cba03 100644 --- a/cmake/OpenCVUtils.cmake +++ b/cmake/OpenCVUtils.cmake @@ -449,17 +449,15 @@ endmacro() # convert list of paths to libraries names without lib prefix -macro(ocv_convert_to_lib_name var) - set(__tmp "") +function(ocv_convert_to_lib_name var) + set(tmp "") foreach(path ${ARGN}) - get_filename_component(__tmp_name "${path}" NAME_WE) - string(REGEX REPLACE "^lib" "" __tmp_name ${__tmp_name}) - list(APPEND __tmp "${__tmp_name}") + get_filename_component(tmp_name "${path}" NAME_WE) + string(REGEX REPLACE "^lib" "" tmp_name "${tmp_name}") + list(APPEND tmp "${tmp_name}") endforeach() - set(${var} ${__tmp}) - unset(__tmp) - unset(__tmp_name) -endmacro() + set(${var} ${tmp} PARENT_SCOPE) +endfunction() # add install command From e6619cf580c40243d0b66faab71f1ba4de73ce8d Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Mon, 30 Mar 2015 15:04:27 +0300 Subject: [PATCH 2/3] Move the CUDA searching code in OpenCVConfig.cmake.in out of the foreach loop There's no reason to do it twice. --- cmake/templates/OpenCVConfig.cmake.in | 95 ++++++++++++++------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/cmake/templates/OpenCVConfig.cmake.in b/cmake/templates/OpenCVConfig.cmake.in index fcf3a2307a..660e26de29 100644 --- a/cmake/templates/OpenCVConfig.cmake.in +++ b/cmake/templates/OpenCVConfig.cmake.in @@ -234,55 +234,56 @@ endif() foreach(__opttype OPT DBG) SET(OpenCV_LIBS_${__opttype} "${OpenCV_LIBS}") SET(OpenCV_EXTRA_LIBS_${__opttype} "") - - # CUDA - if(OpenCV_CUDA_VERSION) - if(NOT CUDA_FOUND) - find_host_package(CUDA ${OpenCV_CUDA_VERSION} EXACT REQUIRED) - else() - if(NOT CUDA_VERSION_STRING VERSION_EQUAL OpenCV_CUDA_VERSION) - message(FATAL_ERROR "OpenCV static library was compiled with CUDA ${OpenCV_CUDA_VERSION} support. Please, use the same version or rebuild OpenCV with CUDA ${CUDA_VERSION_STRING}") - endif() - endif() - - set(OpenCV_CUDA_LIBS_ABSPATH ${CUDA_LIBRARIES}) - - if(${CUDA_VERSION} VERSION_LESS "5.5") - list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_npp_LIBRARY}) - else() - find_cuda_helper_libs(nppc) - find_cuda_helper_libs(nppi) - find_cuda_helper_libs(npps) - list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_nppc_LIBRARY} ${CUDA_nppi_LIBRARY} ${CUDA_npps_LIBRARY}) - endif() - - if(OpenCV_USE_CUBLAS) - list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_CUBLAS_LIBRARIES}) - endif() - - if(OpenCV_USE_CUFFT) - list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_CUFFT_LIBRARIES}) - endif() - - if(OpenCV_USE_NVCUVID) - list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_nvcuvid_LIBRARIES}) - endif() - - if(WIN32) - list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_nvcuvenc_LIBRARIES}) - endif() - - set(OpenCV_CUDA_LIBS_RELPATH "") - foreach(l ${OpenCV_CUDA_LIBS_ABSPATH}) - get_filename_component(_tmp ${l} PATH) - list(APPEND OpenCV_CUDA_LIBS_RELPATH ${_tmp}) - endforeach() - - list(REMOVE_DUPLICATES OpenCV_CUDA_LIBS_RELPATH) - link_directories(${OpenCV_CUDA_LIBS_RELPATH}) - endif() endforeach() +# CUDA +if(OpenCV_CUDA_VERSION) + if(NOT CUDA_FOUND) + find_host_package(CUDA ${OpenCV_CUDA_VERSION} EXACT REQUIRED) + else() + if(NOT CUDA_VERSION_STRING VERSION_EQUAL OpenCV_CUDA_VERSION) + message(FATAL_ERROR "OpenCV static library was compiled with CUDA ${OpenCV_CUDA_VERSION} support. Please, use the same version or rebuild OpenCV with CUDA ${CUDA_VERSION_STRING}") + endif() + endif() + + set(OpenCV_CUDA_LIBS_ABSPATH ${CUDA_LIBRARIES}) + + if(${CUDA_VERSION} VERSION_LESS "5.5") + list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_npp_LIBRARY}) + else() + find_cuda_helper_libs(nppc) + find_cuda_helper_libs(nppi) + find_cuda_helper_libs(npps) + list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_nppc_LIBRARY} ${CUDA_nppi_LIBRARY} ${CUDA_npps_LIBRARY}) + endif() + + if(OpenCV_USE_CUBLAS) + list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_CUBLAS_LIBRARIES}) + endif() + + if(OpenCV_USE_CUFFT) + list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_CUFFT_LIBRARIES}) + endif() + + if(OpenCV_USE_NVCUVID) + list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_nvcuvid_LIBRARIES}) + endif() + + if(WIN32) + list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_nvcuvenc_LIBRARIES}) + endif() + + set(OpenCV_CUDA_LIBS_RELPATH "") + foreach(l ${OpenCV_CUDA_LIBS_ABSPATH}) + get_filename_component(_tmp ${l} PATH) + list(APPEND OpenCV_CUDA_LIBS_RELPATH ${_tmp}) + endforeach() + + list(REMOVE_DUPLICATES OpenCV_CUDA_LIBS_RELPATH) + link_directories(${OpenCV_CUDA_LIBS_RELPATH}) +endif() + + # ============================================================== # Android camera helper macro # ============================================================== From 6e121b2e29dc66304d93ad4840e6bc8ff938115d Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Mon, 30 Mar 2015 15:16:32 +0300 Subject: [PATCH 3/3] Use imported targets for linking to CUDA This retains the desirable quality of not including paths to CUDA libraries from the build system into the config files, and has two major advantages: * It removes the need to use link_directories, which doesn't guarantee that the libraries from the supplied directory will be used (there may be libraries with the same names earlier in the search path). * It removes the need to put -L entries into OPENCV_LINKER_LIBS. This variable is used with target_link_libraries, where such entries are treated as linker flags, so doing this is unportable. I remove the support for -L entries from OpenCVGenPkgconfig.cmake, as well, to discourage adding them in the future. --- CMakeLists.txt | 3 --- cmake/OpenCVDetectCUDA.cmake | 32 +++++---------------------- cmake/OpenCVGenPkgconfig.cmake | 19 ++++++++-------- cmake/OpenCVModule.cmake | 23 ++++++++++--------- cmake/OpenCVUtils.cmake | 13 +++++++++++ cmake/templates/OpenCV.mk.in | 5 +++-- cmake/templates/OpenCVConfig.cmake.in | 15 ++++++------- 7 files changed, 51 insertions(+), 59 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a34fc65b0..665ffe3d62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -540,9 +540,6 @@ if(HAVE_CUDA AND NOT ENABLE_DYNAMIC_CUDA) if(HAVE_CUFFT) set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} ${CUDA_cufft_LIBRARY}) endif() - foreach(p ${CUDA_LIBS_PATH}) - set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} -L${p}) - endforeach() endif() # ---------------------------------------------------------------------------- diff --git a/cmake/OpenCVDetectCUDA.cmake b/cmake/OpenCVDetectCUDA.cmake index 9dfbe09a71..359cf526b3 100644 --- a/cmake/OpenCVDetectCUDA.cmake +++ b/cmake/OpenCVDetectCUDA.cmake @@ -216,40 +216,18 @@ else() endif() 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() - - 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}) + ocv_create_imported_targets(CUDA_LIBRARIES ${CUDA_LIBRARIES}) set(CUDA_npp_LIBRARY_ABS ${CUDA_npp_LIBRARY}) - ocv_convert_to_lib_name(CUDA_npp_LIBRARY ${CUDA_npp_LIBRARY}) + ocv_create_imported_targets(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}) + ocv_create_imported_targets(CUDA_cublas_LIBRARY ${CUDA_cublas_LIBRARY}) endif() if(HAVE_CUFFT) set(CUDA_cufft_LIBRARY_ABS ${CUDA_cufft_LIBRARY}) - ocv_convert_to_lib_name(CUDA_cufft_LIBRARY ${CUDA_cufft_LIBRARY}) + ocv_create_imported_targets(CUDA_cufft_LIBRARY ${CUDA_cufft_LIBRARY}) endif() endif() diff --git a/cmake/OpenCVGenPkgconfig.cmake b/cmake/OpenCVGenPkgconfig.cmake index 183c56d82d..4c46e66f49 100644 --- a/cmake/OpenCVGenPkgconfig.cmake +++ b/cmake/OpenCVGenPkgconfig.cmake @@ -63,22 +63,23 @@ endforeach() # add extra dependencies required for OpenCV if(OpenCV_EXTRA_COMPONENTS) foreach(extra_component ${OpenCV_EXTRA_COMPONENTS}) + if(TARGET "${extra_component}") + get_target_property(extra_component_is_imported "${extra_component}" IMPORTED) + if(extra_component_is_imported) + get_target_property(extra_component "${extra_component}" LOCATION) + endif() + endif() - if(extra_component MATCHES "^-[lL]") - set(libprefix "") - set(libname "${extra_component}") + if(extra_component MATCHES "^-l") + list(APPEND OpenCV_LIB_COMPONENTS_ "${extra_component}") elseif(extra_component MATCHES "[\\/]") get_filename_component(libdir "${extra_component}" PATH) - list(APPEND OpenCV_LIB_COMPONENTS_ "-L${libdir}") get_filename_component(libname "${extra_component}" NAME_WE) string(REGEX REPLACE "^lib" "" libname "${libname}") - set(libprefix "-l") + list(APPEND OpenCV_LIB_COMPONENTS_ "-L${libdir}" "-l${libname}") else() - set(libprefix "-l") - set(libname "${extra_component}") + list(APPEND OpenCV_LIB_COMPONENTS_ "-l${extra_component}") endif() - list(APPEND OpenCV_LIB_COMPONENTS_ "${libprefix}${libname}") - endforeach() endif() diff --git a/cmake/OpenCVModule.cmake b/cmake/OpenCVModule.cmake index 1ca1ac0a16..a70065750f 100644 --- a/cmake/OpenCVModule.cmake +++ b/cmake/OpenCVModule.cmake @@ -919,25 +919,28 @@ macro(__ocv_track_module_link_dependencies the_module optkind) list(REMOVE_AT __mod_depends 0) if(__dep STREQUAL the_module) set(__has_cycle TRUE) - else()#if("${OPENCV_MODULES_BUILD}" MATCHES "(^|;)${__dep}(;|$)") + else() ocv_regex_escape(__rdep "${__dep}") if(__resolved_deps MATCHES "(^|;)${__rdep}(;|$)") #all dependencies of this module are already resolved list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${__dep}") elseif(TARGET ${__dep}) - get_target_property(__module_type ${__dep} TYPE) - if(__module_type STREQUAL "STATIC_LIBRARY") - if(NOT DEFINED ${__dep}_LIB_DEPENDS_${optkind}) - ocv_split_libs_list(${__dep}_LIB_DEPENDS ${__dep}_LIB_DEPENDS_DBG ${__dep}_LIB_DEPENDS_OPT) + get_target_property(__dep_imported ${__dep} IMPORTED) + if(__dep_imported) + list(APPEND ${the_module}_EXTRA_DEPS_${optkind} "${__dep}") + else() + get_target_property(__module_type ${__dep} TYPE) + if(__module_type STREQUAL "STATIC_LIBRARY") + if(NOT DEFINED ${__dep}_LIB_DEPENDS_${optkind}) + ocv_split_libs_list(${__dep}_LIB_DEPENDS ${__dep}_LIB_DEPENDS_DBG ${__dep}_LIB_DEPENDS_OPT) + endif() + list(INSERT __mod_depends 0 ${${__dep}_LIB_DEPENDS_${optkind}} ${__dep}) + list(APPEND __resolved_deps "${__dep}") endif() - list(INSERT __mod_depends 0 ${${__dep}_LIB_DEPENDS_${optkind}} ${__dep}) - list(APPEND __resolved_deps "${__dep}") endif() else() list(APPEND ${the_module}_EXTRA_DEPS_${optkind} "${__dep}") endif() - #else() - # get_target_property(__dep_location "${__dep}" LOCATION) endif() endwhile() @@ -951,7 +954,7 @@ macro(__ocv_track_module_link_dependencies the_module optkind) list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${the_module}") endif() - unset(__dep_location) + unset(__dep_imported) unset(__mod_depends) unset(__resolved_deps) unset(__has_cycle) diff --git a/cmake/OpenCVUtils.cmake b/cmake/OpenCVUtils.cmake index 60501cba03..c23ddccebe 100644 --- a/cmake/OpenCVUtils.cmake +++ b/cmake/OpenCVUtils.cmake @@ -459,6 +459,19 @@ function(ocv_convert_to_lib_name var) set(${var} ${tmp} PARENT_SCOPE) endfunction() +# create imported targets for a list of external libraries +function(ocv_create_imported_targets var) + set(target_list "") + + foreach(library ${ARGN}) + ocv_convert_to_lib_name(libname "${library}") + add_library("opencv_dep_${libname}" UNKNOWN IMPORTED) + set_target_properties("opencv_dep_${libname}" PROPERTIES IMPORTED_LOCATION "${library}") + list(APPEND target_list "opencv_dep_${libname}") + endforeach() + + set("${var}" "${target_list}" PARENT_SCOPE) +endfunction() # add install command function(ocv_install_target) diff --git a/cmake/templates/OpenCV.mk.in b/cmake/templates/OpenCV.mk.in index 77229ecf5e..956062ed45 100644 --- a/cmake/templates/OpenCV.mk.in +++ b/cmake/templates/OpenCV.mk.in @@ -104,7 +104,7 @@ endef define add_cuda_module include $(CLEAR_VARS) LOCAL_MODULE:=$1 - LOCAL_SRC_FILES:=$(CUDA_TOOLKIT_DIR)/targets/armv7-linux-androideabi/lib/lib$1.so + LOCAL_SRC_FILES:=$(CUDA_TOOLKIT_DIR)/targets/armv7-linux-androideabi/lib/lib$(1:opencv_dep_%=%).so include $(PREBUILT_SHARED_LIBRARY) endef @@ -202,7 +202,8 @@ ifeq ($(OPENCV_USE_GPU_MODULE),on) ifeq ($(INSTALL_CUDA_LIBRARIES),on) LOCAL_SHARED_LIBRARIES += $(foreach mod, $(CUDA_RUNTIME_LIBS), $(mod)) else - LOCAL_LDLIBS += -L$(CUDA_TOOLKIT_DIR)/targets/armv7-linux-androideabi/lib $(foreach lib, $(CUDA_RUNTIME_LIBS), -l$(lib)) + LOCAL_LDLIBS += -L$(CUDA_TOOLKIT_DIR)/targets/armv7-linux-androideabi/lib \ + $(foreach lib, $(CUDA_RUNTIME_LIBS), -l$(lib:opencv_dep_%=%)) endif LOCAL_STATIC_LIBRARIES+=libopencv_gpu endif diff --git a/cmake/templates/OpenCVConfig.cmake.in b/cmake/templates/OpenCVConfig.cmake.in index 660e26de29..167dfa9097 100644 --- a/cmake/templates/OpenCVConfig.cmake.in +++ b/cmake/templates/OpenCVConfig.cmake.in @@ -236,7 +236,7 @@ foreach(__opttype OPT DBG) SET(OpenCV_EXTRA_LIBS_${__opttype} "") endforeach() -# CUDA +# Configure CUDA targets if(OpenCV_CUDA_VERSION) if(NOT CUDA_FOUND) find_host_package(CUDA ${OpenCV_CUDA_VERSION} EXACT REQUIRED) @@ -273,17 +273,16 @@ if(OpenCV_CUDA_VERSION) list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_nvcuvenc_LIBRARIES}) endif() - set(OpenCV_CUDA_LIBS_RELPATH "") foreach(l ${OpenCV_CUDA_LIBS_ABSPATH}) - get_filename_component(_tmp ${l} PATH) - list(APPEND OpenCV_CUDA_LIBS_RELPATH ${_tmp}) + get_filename_component(_tmp "${l}" NAME_WE) + string(REGEX REPLACE "^lib" "" _tmp "${_tmp}") + if(NOT TARGET "opencv_dep_${_tmp}") # protect against repeated inclusions + add_library("opencv_dep_${_tmp}" UNKNOWN IMPORTED) + set_target_properties("opencv_dep_${_tmp}" PROPERTIES IMPORTED_LOCATION "${l}") + endif() endforeach() - - list(REMOVE_DUPLICATES OpenCV_CUDA_LIBS_RELPATH) - link_directories(${OpenCV_CUDA_LIBS_RELPATH}) endif() - # ============================================================== # Android camera helper macro # ==============================================================