OpenCVDetectInferenceEngine.cmake 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # The script detects Intel(R) OpenVINO(TM) runtime installation
  2. #
  3. # Result:
  4. # - target ocv.3rdparty.openvino
  5. if(WITH_OPENVINO)
  6. find_package(OpenVINO QUIET)
  7. if(OpenVINO_FOUND)
  8. message(STATUS "OpenVINO FOUND: ${OpenVINO_VERSION}")
  9. math(EXPR ver "${OpenVINO_VERSION_MAJOR} * 1000000 + ${OpenVINO_VERSION_MINOR} * 10000 + ${OpenVINO_VERSION_PATCH} * 100")
  10. ocv_add_external_target(openvino "" "openvino::runtime" "INF_ENGINE_RELEASE=${ver};HAVE_NGRAPH;HAVE_DNN_NGRAPH;HAVE_INF_ENGINE")
  11. set(HAVE_OPENVINO 1)
  12. return()
  13. endif()
  14. endif()
  15. # ======================
  16. if(WITH_OPENVINO)
  17. find_package(OpenVINO QUIET)
  18. if(OpenVINO_FOUND)
  19. message(STATUS "OpenVINO FOUND: ${OpenVINO_VERSION}")
  20. math(EXPR ver "${OpenVINO_VERSION_MAJOR} * 1000000 + ${OpenVINO_VERSION_MINOR} * 10000 + ${OpenVINO_VERSION_PATCH} * 100")
  21. ocv_add_external_target(openvino "" "openvino::runtime" "INF_ENGINE_RELEASE=${ver};HAVE_NGRAPH;HAVE_DNN_NGRAPH;HAVE_INF_ENGINE")
  22. set(HAVE_OPENVINO 1)
  23. return()
  24. endif()
  25. endif()
  26. # ======================
  27. macro(ocv_ie_find_extra_libraries find_prefix find_suffix)
  28. file(GLOB libraries "${INF_ENGINE_LIB_DIRS}/${find_prefix}inference_engine*${find_suffix}")
  29. foreach(full_path IN LISTS libraries)
  30. get_filename_component(library "${full_path}" NAME_WE)
  31. string(REPLACE "${find_prefix}" "" library "${library}")
  32. if(library STREQUAL "inference_engine" OR library STREQUAL "inference_engined")
  33. # skip
  34. else()
  35. add_library(${library} UNKNOWN IMPORTED)
  36. set_target_properties(${library} PROPERTIES
  37. IMPORTED_LOCATION "${full_path}")
  38. list(APPEND custom_libraries ${library})
  39. endif()
  40. endforeach()
  41. endmacro()
  42. function(add_custom_ie_build _inc _lib _lib_rel _lib_dbg _msg)
  43. if(NOT _inc OR NOT (_lib OR _lib_rel OR _lib_dbg))
  44. return()
  45. endif()
  46. if(NOT _lib)
  47. if(_lib_rel)
  48. set(_lib "${_lib_rel}")
  49. else()
  50. set(_lib "${_lib_dbg}")
  51. endif()
  52. endif()
  53. add_library(inference_engine UNKNOWN IMPORTED)
  54. set_target_properties(inference_engine PROPERTIES
  55. IMPORTED_LOCATION "${_lib}"
  56. IMPORTED_IMPLIB_RELEASE "${_lib_rel}"
  57. IMPORTED_IMPLIB_DEBUG "${_lib_dbg}"
  58. INTERFACE_INCLUDE_DIRECTORIES "${_inc}"
  59. )
  60. set(custom_libraries "")
  61. set(__prefixes "${CMAKE_FIND_LIBRARY_PREFIXES}")
  62. if(NOT __prefixes)
  63. set(__prefixes "_empty_")
  64. endif()
  65. foreach(find_prefix ${__prefixes})
  66. if(find_prefix STREQUAL "_empty_") # foreach doesn't iterate over empty elements
  67. set(find_prefix "")
  68. endif()
  69. if(NOT DEFINED INFERENCE_ENGINE_FIND_LIBRARY_SUFFIXES) # allow custom override
  70. set(INFERENCE_ENGINE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  71. if(APPLE)
  72. ocv_list_filterout(INFERENCE_ENGINE_FIND_LIBRARY_SUFFIXES "^.so$") # skip plugins (can't be linked)
  73. endif()
  74. endif()
  75. foreach(find_suffix ${INFERENCE_ENGINE_FIND_LIBRARY_SUFFIXES})
  76. ocv_ie_find_extra_libraries("${find_prefix}" "${find_suffix}")
  77. endforeach()
  78. if(NOT CMAKE_FIND_LIBRARY_SUFFIXES)
  79. ocv_ie_find_extra_libraries("${find_prefix}" "")
  80. endif()
  81. endforeach()
  82. if(NOT INF_ENGINE_RELEASE VERSION_GREATER "2018050000")
  83. find_library(INF_ENGINE_OMP_LIBRARY iomp5 PATHS "${INF_ENGINE_OMP_DIR}" NO_DEFAULT_PATH)
  84. if(NOT INF_ENGINE_OMP_LIBRARY)
  85. message(WARNING "OpenMP for IE have not been found. Set INF_ENGINE_OMP_DIR variable if you experience build errors.")
  86. endif()
  87. endif()
  88. if(EXISTS "${INF_ENGINE_OMP_LIBRARY}")
  89. set_target_properties(inference_engine PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES "${INF_ENGINE_OMP_LIBRARY}")
  90. endif()
  91. set(INF_ENGINE_VERSION "Unknown" CACHE STRING "")
  92. set(INF_ENGINE_TARGET "inference_engine;${custom_libraries}" PARENT_SCOPE)
  93. message(STATUS "Detected InferenceEngine: ${_msg}")
  94. endfunction()
  95. # ======================
  96. find_package(InferenceEngine QUIET)
  97. if(InferenceEngine_FOUND)
  98. set(INF_ENGINE_TARGET ${InferenceEngine_LIBRARIES})
  99. set(INF_ENGINE_VERSION "${InferenceEngine_VERSION}" CACHE STRING "")
  100. message(STATUS "Detected InferenceEngine: cmake package (${InferenceEngine_VERSION})")
  101. endif()
  102. if(DEFINED InferenceEngine_VERSION)
  103. message(STATUS "InferenceEngine: ${InferenceEngine_VERSION}")
  104. if(NOT INF_ENGINE_RELEASE AND NOT (InferenceEngine_VERSION VERSION_LESS "2021.4"))
  105. math(EXPR INF_ENGINE_RELEASE_INIT "${InferenceEngine_VERSION_MAJOR} * 1000000 + ${InferenceEngine_VERSION_MINOR} * 10000 + ${InferenceEngine_VERSION_PATCH} * 100")
  106. endif()
  107. endif()
  108. if(NOT INF_ENGINE_RELEASE AND NOT INF_ENGINE_RELEASE_INIT)
  109. message(STATUS "WARNING: InferenceEngine version has not been set, 2021.4.2 will be used by default. Set INF_ENGINE_RELEASE variable if you experience build errors.")
  110. set(INF_ENGINE_RELEASE_INIT "2021040200")
  111. elseif(DEFINED INF_ENGINE_RELEASE)
  112. set(INF_ENGINE_RELEASE_INIT "${INF_ENGINE_RELEASE}")
  113. endif()
  114. set(INF_ENGINE_RELEASE "${INF_ENGINE_RELEASE_INIT}" CACHE STRING "Force IE version, should be in form YYYYAABBCC (e.g. 2020.1.0.2 -> 2020010002)")
  115. if(NOT INF_ENGINE_TARGET AND INF_ENGINE_LIB_DIRS AND INF_ENGINE_INCLUDE_DIRS)
  116. find_path(ie_custom_inc "inference_engine.hpp" PATHS "${INF_ENGINE_INCLUDE_DIRS}" NO_DEFAULT_PATH)
  117. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  118. find_library(ie_custom_lib_dbg "inference_engined" PATHS "${INF_ENGINE_LIB_DIRS}" NO_DEFAULT_PATH) # Win32 and MacOSX
  119. endif()
  120. find_library(ie_custom_lib "inference_engine" PATHS "${INF_ENGINE_LIB_DIRS}" NO_DEFAULT_PATH)
  121. find_library(ie_custom_lib_rel "inference_engine" PATHS "${INF_ENGINE_LIB_DIRS}/Release" NO_DEFAULT_PATH)
  122. find_library(ie_custom_lib_dbg "inference_engine" PATHS "${INF_ENGINE_LIB_DIRS}/Debug" NO_DEFAULT_PATH)
  123. add_custom_ie_build("${ie_custom_inc}" "${ie_custom_lib}" "${ie_custom_lib_rel}" "${ie_custom_lib_dbg}" "INF_ENGINE_{INCLUDE,LIB}_DIRS")
  124. endif()
  125. set(_loc "$ENV{INTEL_OPENVINO_DIR}")
  126. if(NOT _loc AND DEFINED ENV{INTEL_CVSDK_DIR})
  127. set(_loc "$ENV{INTEL_CVSDK_DIR}") # OpenVINO 2018.x
  128. endif()
  129. if(NOT INF_ENGINE_TARGET AND _loc)
  130. if(NOT INF_ENGINE_RELEASE VERSION_GREATER "2018050000")
  131. set(INF_ENGINE_PLATFORM_DEFAULT "ubuntu_16.04")
  132. else()
  133. set(INF_ENGINE_PLATFORM_DEFAULT "")
  134. endif()
  135. set(INF_ENGINE_PLATFORM "${INF_ENGINE_PLATFORM_DEFAULT}" CACHE STRING "InferenceEngine platform (library dir)")
  136. find_path(ie_custom_env_inc "inference_engine.hpp" PATHS "${_loc}/deployment_tools/inference_engine/include" NO_DEFAULT_PATH)
  137. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  138. find_library(ie_custom_env_lib_dbg "inference_engined" PATHS "${_loc}/deployment_tools/inference_engine/lib/${INF_ENGINE_PLATFORM}/intel64" NO_DEFAULT_PATH)
  139. endif()
  140. find_library(ie_custom_env_lib "inference_engine" PATHS "${_loc}/deployment_tools/inference_engine/lib/${INF_ENGINE_PLATFORM}/intel64" NO_DEFAULT_PATH)
  141. find_library(ie_custom_env_lib_rel "inference_engine" PATHS "${_loc}/deployment_tools/inference_engine/lib/intel64/Release" NO_DEFAULT_PATH)
  142. find_library(ie_custom_env_lib_dbg "inference_engine" PATHS "${_loc}/deployment_tools/inference_engine/lib/intel64/Debug" NO_DEFAULT_PATH)
  143. add_custom_ie_build("${ie_custom_env_inc}" "${ie_custom_env_lib}" "${ie_custom_env_lib_rel}" "${ie_custom_env_lib_dbg}" "OpenVINO (${_loc})")
  144. endif()
  145. set(tgts)
  146. set(defs)
  147. # Add more features to the target
  148. if(INF_ENGINE_TARGET)
  149. set_target_properties(${INF_ENGINE_TARGET} PROPERTIES
  150. INTERFACE_COMPILE_DEFINITIONS "HAVE_INF_ENGINE=1;INF_ENGINE_RELEASE=${INF_ENGINE_RELEASE}"
  151. )
  152. list(APPEND tgts ${INF_ENGINE_TARGET})
  153. list(APPEND defs "INF_ENGINE_RELEASE=${INF_ENGINE_RELEASE}" "HAVE_INF_ENGINE")
  154. endif()
  155. if(WITH_NGRAPH OR NOT DEFINED WITH_NGRAPH)
  156. find_package(ngraph QUIET)
  157. if(ngraph_FOUND)
  158. ocv_assert(TARGET ngraph::ngraph)
  159. if(INF_ENGINE_RELEASE VERSION_LESS "2019039999")
  160. message(WARNING "nGraph is not tested with current InferenceEngine version: INF_ENGINE_RELEASE=${INF_ENGINE_RELEASE}")
  161. endif()
  162. message(STATUS "Detected ngraph: cmake package (${ngraph_VERSION})")
  163. set(HAVE_NGRAPH ON)
  164. list(APPEND tgts ngraph::ngraph)
  165. list(APPEND defs "HAVE_NGRAPH" "HAVE_DNN_NGRAPH")
  166. endif()
  167. endif()
  168. ocv_add_external_target(openvino "" "${tgts}" "${defs}")