From d1e76a34a07e2e8e68c7bf582aebe94665605980 Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Sat, 15 Jan 2022 00:53:19 +0300 Subject: [PATCH] 3.4: Use modern OpenVINO package interface original commit: 437af37b1341e5cef90aa1a4ce60210feacc6c8a --- CMakeLists.txt | 112 ++++++++++++------------ cmake/OpenCVDetectInferenceEngine.cmake | 43 +++++---- cmake/OpenCVUtils.cmake | 41 +++++++++ modules/dnn/CMakeLists.txt | 22 +---- 4 files changed, 127 insertions(+), 91 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 617d9dd468..8f9a2d17d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -291,12 +291,10 @@ OCV_OPTION(WITH_IPP "Include Intel IPP support" (NOT MINGW AND NOT CV_DISABLE_OP OCV_OPTION(WITH_HALIDE "Include Halide support" OFF VISIBLE_IF TRUE VERIFY HAVE_HALIDE) -OCV_OPTION(WITH_INF_ENGINE "Include Intel Inference Engine support" OFF +# replacement for deprecated options: WITH_INF_ENGINE, WITH_NGRAPH +OCV_OPTION(WITH_OPENVINO "Include Intel OpenVINO toolkit support" (WITH_INF_ENGINE) VISIBLE_IF TRUE - VERIFY INF_ENGINE_TARGET) -OCV_OPTION(WITH_NGRAPH "Include nGraph support" WITH_INF_ENGINE - VISIBLE_IF TRUE - VERIFY TARGET ngraph::ngraph) + VERIFY TARGET ocv.3rdparty.openvino) OCV_OPTION(WITH_JASPER "Include JPEG2K support" ON VISIBLE_IF NOT IOS VERIFY HAVE_JASPER) @@ -764,7 +762,7 @@ if(WITH_HALIDE) endif() # --- Inference Engine --- -if(WITH_INF_ENGINE) +if(WITH_INF_ENGINE OR WITH_OPENVINO) include(cmake/OpenCVDetectInferenceEngine.cmake) endif() @@ -1524,55 +1522,61 @@ if(WITH_HALIDE OR HAVE_HALIDE) status(" Halide:" HAVE_HALIDE THEN "YES (${HALIDE_LIBRARIES} ${HALIDE_INCLUDE_DIRS})" ELSE NO) endif() -if(WITH_INF_ENGINE OR INF_ENGINE_TARGET) - if(INF_ENGINE_TARGET) - list(GET INF_ENGINE_TARGET 0 ie_target) - set(__msg "YES (${INF_ENGINE_RELEASE} / ${INF_ENGINE_VERSION})") - ocv_get_imported_target(ie_target "${ie_target}") - get_target_property(_lib ${ie_target} IMPORTED_LOCATION) - get_target_property(_lib_imp_rel ${ie_target} IMPORTED_IMPLIB_RELEASE) - get_target_property(_lib_imp_dbg ${ie_target} IMPORTED_IMPLIB_DEBUG) - get_target_property(_lib_rel ${ie_target} IMPORTED_LOCATION_RELEASE) - get_target_property(_lib_dbg ${ie_target} IMPORTED_LOCATION_DEBUG) - ocv_build_features_string(_lib - IF _lib THEN "${_lib}" - IF _lib_imp_rel AND _lib_imp_dbg THEN "${_lib_imp_rel} / ${_lib_imp_dbg}" - IF _lib_rel AND _lib_dbg THEN "${_lib_rel} / ${_lib_dbg}" - IF _lib_rel THEN "${_lib_rel}" - IF _lib_dbg THEN "${_lib_dbg}" - ELSE "unknown" - ) - get_target_property(_inc ${ie_target} INTERFACE_INCLUDE_DIRECTORIES) - status(" Inference Engine:" "${__msg}") - status(" * libs:" "${_lib}") - status(" * includes:" "${_inc}") - else() - status(" Inference Engine:" "NO") +if(HAVE_OPENVINO + OR (WITH_OPENVINO AND NOT WITH_INF_ENGINE AND NOT INF_ENGINE_TARGET) +) + status(" OpenVINO:" TARGET openvino::runtime THEN "YES (${OpenVINO_VERSION})" ELSE "NO") +else() + if(WITH_INF_ENGINE OR INF_ENGINE_TARGET) + if(INF_ENGINE_TARGET) + list(GET INF_ENGINE_TARGET 0 ie_target) + set(__msg "YES (${INF_ENGINE_RELEASE} / ${INF_ENGINE_VERSION})") + ocv_get_imported_target(ie_target "${ie_target}") + get_target_property(_lib ${ie_target} IMPORTED_LOCATION) + get_target_property(_lib_imp_rel ${ie_target} IMPORTED_IMPLIB_RELEASE) + get_target_property(_lib_imp_dbg ${ie_target} IMPORTED_IMPLIB_DEBUG) + get_target_property(_lib_rel ${ie_target} IMPORTED_LOCATION_RELEASE) + get_target_property(_lib_dbg ${ie_target} IMPORTED_LOCATION_DEBUG) + ocv_build_features_string(_lib + IF _lib THEN "${_lib}" + IF _lib_imp_rel AND _lib_imp_dbg THEN "${_lib_imp_rel} / ${_lib_imp_dbg}" + IF _lib_rel AND _lib_dbg THEN "${_lib_rel} / ${_lib_dbg}" + IF _lib_rel THEN "${_lib_rel}" + IF _lib_dbg THEN "${_lib_dbg}" + ELSE "unknown" + ) + get_target_property(_inc ${ie_target} INTERFACE_INCLUDE_DIRECTORIES) + status(" Inference Engine:" "${__msg}") + status(" * libs:" "${_lib}") + status(" * includes:" "${_inc}") + else() + status(" Inference Engine:" "NO") + endif() endif() -endif() -if(WITH_NGRAPH OR HAVE_NGRAPH) - if(HAVE_NGRAPH) - ocv_get_imported_target(__target ngraph::ngraph) - set(__msg "YES (${ngraph_VERSION})") - get_target_property(_lib ${__target} IMPORTED_LOCATION) - get_target_property(_lib_imp_rel ${__target} IMPORTED_IMPLIB_RELEASE) - get_target_property(_lib_imp_dbg ${__target} IMPORTED_IMPLIB_DEBUG) - get_target_property(_lib_rel ${__target} IMPORTED_LOCATION_RELEASE) - get_target_property(_lib_dbg ${__target} IMPORTED_LOCATION_DEBUG) - ocv_build_features_string(_lib - IF _lib THEN "${_lib}" - IF _lib_imp_rel AND _lib_imp_dbg THEN "${_lib_imp_rel} / ${_lib_imp_dbg}" - IF _lib_rel AND _lib_dbg THEN "${_lib_rel} / ${_lib_dbg}" - IF _lib_rel THEN "${_lib_rel}" - IF _lib_dbg THEN "${_lib_dbg}" - ELSE "unknown" - ) - get_target_property(_inc ${__target} INTERFACE_INCLUDE_DIRECTORIES) - status(" nGraph:" "${__msg}") - status(" * libs:" "${_lib}") - status(" * includes:" "${_inc}") - else() - status(" nGraph:" "NO") + if(WITH_NGRAPH OR HAVE_NGRAPH) + if(HAVE_NGRAPH) + ocv_get_imported_target(__target ngraph::ngraph) + set(__msg "YES (${ngraph_VERSION})") + get_target_property(_lib ${__target} IMPORTED_LOCATION) + get_target_property(_lib_imp_rel ${__target} IMPORTED_IMPLIB_RELEASE) + get_target_property(_lib_imp_dbg ${__target} IMPORTED_IMPLIB_DEBUG) + get_target_property(_lib_rel ${__target} IMPORTED_LOCATION_RELEASE) + get_target_property(_lib_dbg ${__target} IMPORTED_LOCATION_DEBUG) + ocv_build_features_string(_lib + IF _lib THEN "${_lib}" + IF _lib_imp_rel AND _lib_imp_dbg THEN "${_lib_imp_rel} / ${_lib_imp_dbg}" + IF _lib_rel AND _lib_dbg THEN "${_lib_rel} / ${_lib_dbg}" + IF _lib_rel THEN "${_lib_rel}" + IF _lib_dbg THEN "${_lib_dbg}" + ELSE "unknown" + ) + get_target_property(_inc ${__target} INTERFACE_INCLUDE_DIRECTORIES) + status(" nGraph:" "${__msg}") + status(" * libs:" "${_lib}") + status(" * includes:" "${_inc}") + else() + status(" nGraph:" "NO") + endif() endif() endif() diff --git a/cmake/OpenCVDetectInferenceEngine.cmake b/cmake/OpenCVDetectInferenceEngine.cmake index 128883b128..58a16f6d54 100644 --- a/cmake/OpenCVDetectInferenceEngine.cmake +++ b/cmake/OpenCVDetectInferenceEngine.cmake @@ -1,21 +1,7 @@ -# The script detects Intel(R) Inference Engine installation -# -# Cache variables: -# INF_ENGINE_RELEASE - a number reflecting IE source interface (linked with OpenVINO release) -# -# Detect parameters: -# 1. Native cmake IE package: -# - environment variable InferenceEngine_DIR is set to location of cmake module -# 2. Custom location: -# - INF_ENGINE_INCLUDE_DIRS - headers search location -# - INF_ENGINE_LIB_DIRS - library search location -# 3. OpenVINO location: -# - environment variable INTEL_OPENVINO_DIR is set to location of OpenVINO installation dir -# - INF_ENGINE_PLATFORM - part of name of library directory representing its platform +# The script detects Intel(R) OpenVINO(TM) runtime installation # # Result: -# INF_ENGINE_TARGET - set to name of imported library target representing InferenceEngine -# +# - target ocv.3rdparty.openvino if(NOT HAVE_CXX11) message(WARNING "DL Inference engine requires C++11. You can turn it on via ENABLE_CXX11=ON CMake flag.") @@ -24,6 +10,19 @@ endif() # ======================= +if(WITH_OPENVINO) + find_package(OpenVINO QUIET) + if(OpenVINO_FOUND) + message(STATUS "OpenVINO FOUND: ${OpenVINO_VERSION}") + math(EXPR ver "${OpenVINO_VERSION_MAJOR} * 1000000 + ${OpenVINO_VERSION_MINOR} * 10000 + ${OpenVINO_VERSION_PATCH} * 100") + ocv_add_external_target(openvino "" "openvino::runtime" "INF_ENGINE_RELEASE=${ver};HAVE_NGRAPH;HAVE_DNN_NGRAPH;HAVE_INF_ENGINE") + set(HAVE_OPENVINO 1) + return() + endif() +endif() + +# ====================== + macro(ocv_ie_find_extra_libraries find_prefix find_suffix) file(GLOB libraries "${INF_ENGINE_LIB_DIRS}/${find_prefix}inference_engine*${find_suffix}") foreach(full_path IN LISTS libraries) @@ -151,15 +150,19 @@ if(NOT INF_ENGINE_TARGET AND _loc) add_custom_ie_build("${ie_custom_env_inc}" "${ie_custom_env_lib}" "${ie_custom_env_lib_rel}" "${ie_custom_env_lib_dbg}" "OpenVINO (${_loc})") endif() -# Add more features to the target +set(tgts) +set(defs) +# Add more features to the target if(INF_ENGINE_TARGET) set_target_properties(${INF_ENGINE_TARGET} PROPERTIES INTERFACE_COMPILE_DEFINITIONS "HAVE_INF_ENGINE=1;INF_ENGINE_RELEASE=${INF_ENGINE_RELEASE}" ) + list(APPEND tgts ${INF_ENGINE_TARGET}) + list(APPEND defs "INF_ENGINE_RELEASE=${INF_ENGINE_RELEASE}" "HAVE_INF_ENGINE") endif() -if(WITH_NGRAPH) +if(WITH_NGRAPH OR NOT DEFINED WITH_NGRAPH) find_package(ngraph QUIET) if(ngraph_FOUND) ocv_assert(TARGET ngraph::ngraph) @@ -168,5 +171,9 @@ if(WITH_NGRAPH) endif() message(STATUS "Detected ngraph: cmake package (${ngraph_VERSION})") set(HAVE_NGRAPH ON) + list(APPEND tgts ngraph::ngraph) + list(APPEND defs "HAVE_NGRAPH" "HAVE_DNN_NGRAPH") endif() endif() + +ocv_add_external_target(openvino "" "${tgts}" "${defs}") diff --git a/cmake/OpenCVUtils.cmake b/cmake/OpenCVUtils.cmake index 9f23abe9f7..938c4e1f82 100644 --- a/cmake/OpenCVUtils.cmake +++ b/cmake/OpenCVUtils.cmake @@ -1580,6 +1580,47 @@ function(ocv_add_library target) endfunction() +function(ocv_add_external_target name inc link def) + if(BUILD_SHARED_LIBS AND link) + set(imp IMPORTED) + endif() + add_library(ocv.3rdparty.${name} INTERFACE ${imp}) + if(def) + if(NOT (CMAKE_VERSION VERSION_LESS "3.11.0")) # https://gitlab.kitware.com/cmake/cmake/-/merge_requests/1264 : eliminates "Cannot specify compile definitions for imported target" error message + target_compile_definitions(ocv.3rdparty.${name} INTERFACE "${def}") + else() + set_target_properties(ocv.3rdparty.${name} PROPERTIES INTERFACE_COMPILE_DEFINITIONS "${def}") + endif() + endif() + if(inc) + if(NOT (CMAKE_VERSION VERSION_LESS "3.11.0")) # https://gitlab.kitware.com/cmake/cmake/-/merge_requests/1264 : eliminates "Cannot specify compile definitions for imported target" error message + target_include_directories(ocv.3rdparty.${name} SYSTEM INTERFACE "$") + else() + set_target_properties(ocv.3rdparty.${name} PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "$" + INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "$" + ) + endif() + endif() + if(link) + # When cmake version is greater than or equal to 3.11, INTERFACE_LINK_LIBRARIES no longer applies to interface library + # See https://github.com/opencv/opencv/pull/18658 + if(CMAKE_VERSION VERSION_LESS 3.11) + set_target_properties(ocv.3rdparty.${name} PROPERTIES + INTERFACE_LINK_LIBRARIES "${link}") + else() + target_link_libraries(ocv.3rdparty.${name} INTERFACE ${link}) + endif() + endif() + # to install used target only upgrade CMake + if(NOT BUILD_SHARED_LIBS + AND CMAKE_VERSION VERSION_LESS "3.13.0" # https://gitlab.kitware.com/cmake/cmake/-/merge_requests/2152 + ) + install(TARGETS ocv.3rdparty.${name} EXPORT OpenCVModules) + endif() +endfunction() + + # Returns the first non-interface target function(ocv_get_imported_target imported interface) set(__result "${interface}") diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt index 9dc048f5b6..8b117a952d 100644 --- a/modules/dnn/CMakeLists.txt +++ b/modules/dnn/CMakeLists.txt @@ -115,24 +115,8 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") endif() set(dnn_runtime_libs "") -if(INF_ENGINE_TARGET) - set(use_nn_builder OFF) - if(TARGET inference_engine_nn_builder OR # custom imported target - TARGET IE::inference_engine_nn_builder OR # default imported target via InferenceEngineConfig.cmake - INF_ENGINE_RELEASE VERSION_LESS "2020000000") # compatibility with older versions on IE - set(use_nn_builder ON) - endif() - ocv_option(OPENCV_DNN_IE_NN_BUILDER_2019 "Build with Inference Engine NN Builder API support" ${use_nn_builder}) # future: NOT HAVE_NGRAPH - if(OPENCV_DNN_IE_NN_BUILDER_2019) - message(STATUS "DNN: Enabling Inference Engine NN Builder API support") - add_definitions(-DHAVE_DNN_IE_NN_BUILDER_2019=1) - endif() - list(APPEND dnn_runtime_libs ${INF_ENGINE_TARGET}) -endif() -if(HAVE_NGRAPH) - message(STATUS "DNN: Enabling Inference Engine nGraph API support") - add_definitions(-DHAVE_DNN_NGRAPH) - list(APPEND dnn_runtime_libs ngraph::ngraph) +if(TARGET ocv.3rdparty.openvino) + list(APPEND dnn_runtime_libs ocv.3rdparty.openvino) endif() ocv_glob_module_sources(${sources_options} SOURCES ${fw_srcs}) @@ -143,7 +127,7 @@ ocv_add_accuracy_tests(${dnn_runtime_libs}) set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf") file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp") file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h") -ocv_add_perf_tests(${INF_ENGINE_TARGET} +ocv_add_perf_tests(${dnn_runtime_libs} FILES test_common "${CMAKE_CURRENT_LIST_DIR}/test/test_common.hpp" "${CMAKE_CURRENT_LIST_DIR}/test/test_common.impl.hpp" FILES Src ${perf_srcs} FILES Include ${perf_hdrs}