From c9f3f4d1d32ec065eb6431c7fbe78466e08a2fbe Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 26 Apr 2019 15:13:16 +0300 Subject: [PATCH] cmake: fix pkg-config handling Avoid non-intentional call of "include(FindPkgConfig)" It should be handled in the root CMakeLists.txt (safely for cross-compiling) --- CMakeLists.txt | 2 +- cmake/OpenCVUtils.cmake | 2 +- modules/videoio/cmake/init.cmake | 2 - modules/videoio/cmake/plugin.cmake | 72 ------------------ modules/videoio/cmake/plugin_standalone.cmake | 73 +++++++++++++++++++ .../videoio/misc/plugin_ffmpeg/CMakeLists.txt | 2 +- .../misc/plugin_gstreamer/CMakeLists.txt | 2 +- 7 files changed, 77 insertions(+), 78 deletions(-) create mode 100644 modules/videoio/cmake/plugin_standalone.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f324dd3d8..e19e32d62c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -698,7 +698,7 @@ ocv_cmake_hook(POST_COMPILER_OPTIONS) # ---------------------------------------------------------------------------- if(UNIX) if(NOT APPLE_FRAMEWORK OR OPENCV_ENABLE_PKG_CONFIG) - if(CMAKE_CROSSCOMPILING AND NOT DEFINED ENV{PKG_CONFIG_LIBDIR} + if(CMAKE_CROSSCOMPILING AND NOT DEFINED ENV{PKG_CONFIG_LIBDIR} AND NOT DEFINED ENV{PKG_CONFIG_SYSROOT_DIR} AND NOT OPENCV_ENABLE_PKG_CONFIG ) if(NOT PkgConfig_FOUND) diff --git a/cmake/OpenCVUtils.cmake b/cmake/OpenCVUtils.cmake index 18aabd150e..9cba5d9a62 100644 --- a/cmake/OpenCVUtils.cmake +++ b/cmake/OpenCVUtils.cmake @@ -743,7 +743,7 @@ macro(ocv_check_modules define) endif() unset(${define}_${__modname}_FOUND) endforeach() - if(COMMAND pkg_check_modules) + if(PKG_CONFIG_FOUND OR PkgConfig_FOUND) pkg_check_modules(${define} ${ARGN}) endif() if(${define}_FOUND) diff --git a/modules/videoio/cmake/init.cmake b/modules/videoio/cmake/init.cmake index bbf57bc3b0..1efef12c5e 100644 --- a/modules/videoio/cmake/init.cmake +++ b/modules/videoio/cmake/init.cmake @@ -19,8 +19,6 @@ function(ocv_add_external_target name inc link def) endif() endfunction() -include(FindPkgConfig) - add_backend("ffmpeg" WITH_FFMPEG) add_backend("gstreamer" WITH_GSTREAMER) add_backend("v4l" WITH_V4L) diff --git a/modules/videoio/cmake/plugin.cmake b/modules/videoio/cmake/plugin.cmake index adc52f89f5..8e27401910 100644 --- a/modules/videoio/cmake/plugin.cmake +++ b/modules/videoio/cmake/plugin.cmake @@ -1,7 +1,3 @@ -#============================================= -# build with OpenCV -include("${OpenCV_SOURCE_DIR}/cmake/OpenCVUtils.cmake") - function(ocv_create_builtin_videoio_plugin name target videoio_src_file) ocv_debug_message("ocv_create_builtin_videoio_plugin(${ARGV})") @@ -36,71 +32,3 @@ function(ocv_create_builtin_videoio_plugin name target videoio_src_file) add_dependencies(opencv_videoio_plugins ${name}) endfunction() - -#============================================= -# standalone build - -function(ocv_create_videoio_plugin default_name target target_desc videoio_src_file) - - set(OPENCV_PLUGIN_NAME ${default_name} CACHE STRING "") - set(OPENCV_PLUGIN_DESTINATION "" CACHE PATH "") - project(${OPENCV_PLUGIN_NAME} LANGUAGES CXX) - - set(BUILD_SHARED_LIBS ON CACHE BOOL "") - if(NOT BUILD_SHARED_LIBS) - message(FATAL_ERROR "Static plugin build does not make sense") - endif() - - if(NOT OpenCV_SOURCE_DIR) - message(FATAL_ERROR "OpenCV_SOURCE_DIR must be set to build the plugin!") - endif() - - include("${OpenCV_SOURCE_DIR}/modules/videoio/cmake/init.cmake") - - if(NOT TARGET ${target}) - message(FATAL_ERROR "${target_desc} was not found!") - endif() - - set(modules_ROOT "${CMAKE_CURRENT_LIST_DIR}/../../..") - set(videoio_ROOT "${modules_ROOT}/videoio") - set(core_ROOT "${modules_ROOT}/core") - set(imgproc_ROOT "${modules_ROOT}/imgproc") - set(imgcodecs_ROOT "${modules_ROOT}/imgcodecs") - - add_library(${OPENCV_PLUGIN_NAME} MODULE "${videoio_ROOT}/src/${videoio_src_file}") - target_include_directories(${OPENCV_PLUGIN_NAME} PRIVATE - "${CMAKE_CURRENT_BINARY_DIR}" - "${videoio_ROOT}/src" - "${videoio_ROOT}/include" - "${core_ROOT}/include" - "${imgproc_ROOT}/include" - "${imgcodecs_ROOT}/include" - ) - target_compile_definitions(${OPENCV_PLUGIN_NAME} PRIVATE BUILD_PLUGIN) - - # Fixes for build - target_compile_definitions(${OPENCV_PLUGIN_NAME} PRIVATE __OPENCV_BUILD) - file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/cvconfig.h" "#pragma once") - file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/cv_cpu_config.h" "#pragma once") - file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/opencv2/opencv_modules.hpp" "#pragma once") - - target_link_libraries(${OPENCV_PLUGIN_NAME} PRIVATE ${target}) - set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES - CXX_STANDARD 11 - CXX_VISIBILITY_PRESET hidden - ) - - # Hack for Windows - if(WIN32) - find_package(OpenCV REQUIRED core imgproc videoio) - target_link_libraries(${OPENCV_PLUGIN_NAME} ${OpenCV_LIBS}) - endif() - - if(OPENCV_PLUGIN_DESTINATION) - set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${OPENCV_PLUGIN_DESTINATION}") - message(STATUS "Output destination: ${OPENCV_PLUGIN_DESTINATION}") - endif() - - message(STATUS "Library name: ${OPENCV_PLUGIN_NAME}") - -endfunction() diff --git a/modules/videoio/cmake/plugin_standalone.cmake b/modules/videoio/cmake/plugin_standalone.cmake new file mode 100644 index 0000000000..045d029b7e --- /dev/null +++ b/modules/videoio/cmake/plugin_standalone.cmake @@ -0,0 +1,73 @@ +#============================================= +# standalone build + +include(FindPkgConfig) + +#============================================= +# build with OpenCV +include("${OpenCV_SOURCE_DIR}/cmake/OpenCVUtils.cmake") + +function(ocv_create_videoio_plugin default_name target target_desc videoio_src_file) + + set(OPENCV_PLUGIN_NAME ${default_name} CACHE STRING "") + set(OPENCV_PLUGIN_DESTINATION "" CACHE PATH "") + project(${OPENCV_PLUGIN_NAME} LANGUAGES CXX) + + set(BUILD_SHARED_LIBS ON CACHE BOOL "") + if(NOT BUILD_SHARED_LIBS) + message(FATAL_ERROR "Static plugin build does not make sense") + endif() + + if(NOT OpenCV_SOURCE_DIR) + message(FATAL_ERROR "OpenCV_SOURCE_DIR must be set to build the plugin!") + endif() + + include("${OpenCV_SOURCE_DIR}/modules/videoio/cmake/init.cmake") + + if(NOT TARGET ${target}) + message(FATAL_ERROR "${target_desc} was not found!") + endif() + + set(modules_ROOT "${CMAKE_CURRENT_LIST_DIR}/../../..") + set(videoio_ROOT "${modules_ROOT}/videoio") + set(core_ROOT "${modules_ROOT}/core") + set(imgproc_ROOT "${modules_ROOT}/imgproc") + set(imgcodecs_ROOT "${modules_ROOT}/imgcodecs") + + add_library(${OPENCV_PLUGIN_NAME} MODULE "${videoio_ROOT}/src/${videoio_src_file}") + target_include_directories(${OPENCV_PLUGIN_NAME} PRIVATE + "${CMAKE_CURRENT_BINARY_DIR}" + "${videoio_ROOT}/src" + "${videoio_ROOT}/include" + "${core_ROOT}/include" + "${imgproc_ROOT}/include" + "${imgcodecs_ROOT}/include" + ) + target_compile_definitions(${OPENCV_PLUGIN_NAME} PRIVATE BUILD_PLUGIN) + + # Fixes for build + target_compile_definitions(${OPENCV_PLUGIN_NAME} PRIVATE __OPENCV_BUILD) + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/cvconfig.h" "#pragma once") + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/cv_cpu_config.h" "#pragma once") + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/opencv2/opencv_modules.hpp" "#pragma once") + + target_link_libraries(${OPENCV_PLUGIN_NAME} PRIVATE ${target}) + set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES + CXX_STANDARD 11 + CXX_VISIBILITY_PRESET hidden + ) + + # Hack for Windows + if(WIN32) + find_package(OpenCV REQUIRED core imgproc videoio) + target_link_libraries(${OPENCV_PLUGIN_NAME} ${OpenCV_LIBS}) + endif() + + if(OPENCV_PLUGIN_DESTINATION) + set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${OPENCV_PLUGIN_DESTINATION}") + message(STATUS "Output destination: ${OPENCV_PLUGIN_DESTINATION}") + endif() + + message(STATUS "Library name: ${OPENCV_PLUGIN_NAME}") + +endfunction() diff --git a/modules/videoio/misc/plugin_ffmpeg/CMakeLists.txt b/modules/videoio/misc/plugin_ffmpeg/CMakeLists.txt index 347b4da28c..55f3c2945f 100644 --- a/modules/videoio/misc/plugin_ffmpeg/CMakeLists.txt +++ b/modules/videoio/misc/plugin_ffmpeg/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.5) set(OpenCV_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../..") set(WITH_FFMPEG ON) set(OPENCV_FFMPEG_SKIP_BUILD_CHECK ON) -include("${OpenCV_SOURCE_DIR}/modules/videoio/cmake/plugin.cmake") +include("${OpenCV_SOURCE_DIR}/modules/videoio/cmake/plugin_standalone.cmake") ocv_create_videoio_plugin("opencv_videoio_ffmpeg" "ocv.3rdparty.ffmpeg" "FFmpeg" "cap_ffmpeg.cpp") message(STATUS "FFMPEG_libavcodec_VERSION=${FFMPEG_libavcodec_VERSION}") diff --git a/modules/videoio/misc/plugin_gstreamer/CMakeLists.txt b/modules/videoio/misc/plugin_gstreamer/CMakeLists.txt index b95424c33c..1361487e9b 100644 --- a/modules/videoio/misc/plugin_gstreamer/CMakeLists.txt +++ b/modules/videoio/misc/plugin_gstreamer/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.5) set(OpenCV_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../..") set(WITH_GSTREAMER ON) -include("${OpenCV_SOURCE_DIR}/modules/videoio/cmake/plugin.cmake") +include("${OpenCV_SOURCE_DIR}/modules/videoio/cmake/plugin_standalone.cmake") ocv_create_videoio_plugin("opencv_videoio_gstreamer" "ocv.3rdparty.gstreamer" "GStreamer" "cap_gstreamer.cpp") message(STATUS "Using GStreamer: ${GSTREAMER_VERSION}")