OpenCVFindMKL.cmake 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #
  2. # The script to detect Intel(R) Math Kernel Library (MKL)
  3. # installation/package
  4. #
  5. # Parameters:
  6. # MKL_ROOT_DIR / ENV{MKLROOT}
  7. # MKL_INCLUDE_DIR
  8. # MKL_LIBRARIES
  9. # MKL_USE_SINGLE_DYNAMIC_LIBRARY - use single dynamic library mkl_rt.lib / libmkl_rt.so
  10. # MKL_WITH_TBB / MKL_WITH_OPENMP
  11. #
  12. # Extra:
  13. # MKL_LIB_FIND_PATHS
  14. #
  15. # On return this will define:
  16. #
  17. # HAVE_MKL - True if Intel MKL found
  18. # MKL_ROOT_DIR - root of MKL installation
  19. # MKL_INCLUDE_DIRS - MKL include folder
  20. # MKL_LIBRARIES - MKL libraries that are used by OpenCV
  21. #
  22. macro(mkl_fail)
  23. set(HAVE_MKL OFF)
  24. set(MKL_ROOT_DIR "${MKL_ROOT_DIR}" CACHE PATH "Path to MKL directory")
  25. return()
  26. endmacro()
  27. macro(get_mkl_version VERSION_FILE)
  28. # read MKL version info from file
  29. file(STRINGS ${VERSION_FILE} STR1 REGEX "__INTEL_MKL__")
  30. file(STRINGS ${VERSION_FILE} STR2 REGEX "__INTEL_MKL_MINOR__")
  31. file(STRINGS ${VERSION_FILE} STR3 REGEX "__INTEL_MKL_UPDATE__")
  32. #file(STRINGS ${VERSION_FILE} STR4 REGEX "INTEL_MKL_VERSION")
  33. # extract info and assign to variables
  34. string(REGEX MATCHALL "[0-9]+" MKL_VERSION_MAJOR ${STR1})
  35. string(REGEX MATCHALL "[0-9]+" MKL_VERSION_MINOR ${STR2})
  36. string(REGEX MATCHALL "[0-9]+" MKL_VERSION_UPDATE ${STR3})
  37. set(MKL_VERSION_STR "${MKL_VERSION_MAJOR}.${MKL_VERSION_MINOR}.${MKL_VERSION_UPDATE}" CACHE STRING "MKL version" FORCE)
  38. endmacro()
  39. OCV_OPTION(MKL_USE_SINGLE_DYNAMIC_LIBRARY "Use MKL Single Dynamic Library thorugh mkl_rt.lib / libmkl_rt.so" OFF)
  40. OCV_OPTION(MKL_WITH_TBB "Use MKL with TBB multithreading" OFF)#ON IF WITH_TBB)
  41. OCV_OPTION(MKL_WITH_OPENMP "Use MKL with OpenMP multithreading" OFF)#ON IF WITH_OPENMP)
  42. if(NOT MKL_ROOT_DIR AND DEFINED MKL_INCLUDE_DIR AND EXISTS "${MKL_INCLUDE_DIR}/mkl.h")
  43. file(TO_CMAKE_PATH "${MKL_INCLUDE_DIR}" MKL_INCLUDE_DIR)
  44. get_filename_component(MKL_ROOT_DIR "${MKL_INCLUDE_DIR}/.." ABSOLUTE)
  45. endif()
  46. if(NOT MKL_ROOT_DIR)
  47. file(TO_CMAKE_PATH "${MKL_ROOT_DIR}" mkl_root_paths)
  48. if(DEFINED ENV{MKLROOT})
  49. file(TO_CMAKE_PATH "$ENV{MKLROOT}" path)
  50. list(APPEND mkl_root_paths "${path}")
  51. endif()
  52. if(WITH_MKL AND NOT mkl_root_paths)
  53. if(WIN32)
  54. set(ProgramFilesx86 "ProgramFiles(x86)")
  55. file(TO_CMAKE_PATH "$ENV{${ProgramFilesx86}}" path)
  56. list(APPEND mkl_root_paths ${path}/IntelSWTools/compilers_and_libraries/windows/mkl)
  57. endif()
  58. if(UNIX)
  59. list(APPEND mkl_root_paths "/opt/intel/mkl")
  60. endif()
  61. endif()
  62. find_path(MKL_ROOT_DIR include/mkl.h PATHS ${mkl_root_paths})
  63. endif()
  64. if(NOT MKL_ROOT_DIR OR NOT EXISTS "${MKL_ROOT_DIR}/include/mkl.h")
  65. mkl_fail()
  66. endif()
  67. set(MKL_INCLUDE_DIR "${MKL_ROOT_DIR}/include" CACHE PATH "Path to MKL include directory")
  68. if(NOT MKL_ROOT_DIR
  69. OR NOT EXISTS "${MKL_ROOT_DIR}"
  70. OR NOT EXISTS "${MKL_INCLUDE_DIR}"
  71. OR NOT EXISTS "${MKL_INCLUDE_DIR}/mkl_version.h"
  72. )
  73. mkl_fail()
  74. endif()
  75. get_mkl_version(${MKL_INCLUDE_DIR}/mkl_version.h)
  76. #determine arch
  77. if(CMAKE_CXX_SIZEOF_DATA_PTR EQUAL 8)
  78. set(MKL_ARCH_LIST "intel64")
  79. if(MSVC)
  80. list(APPEND MKL_ARCH_LIST "win-x64")
  81. endif()
  82. include(CheckTypeSize)
  83. CHECK_TYPE_SIZE(int _sizeof_int)
  84. if (_sizeof_int EQUAL 4)
  85. set(MKL_ARCH_SUFFIX "lp64")
  86. else()
  87. set(MKL_ARCH_SUFFIX "ilp64")
  88. endif()
  89. else()
  90. set(MKL_ARCH_LIST "ia32")
  91. set(MKL_ARCH_SUFFIX "c")
  92. endif()
  93. set(mkl_lib_find_paths ${MKL_LIB_FIND_PATHS} ${MKL_ROOT_DIR}/lib)
  94. foreach(MKL_ARCH ${MKL_ARCH_LIST})
  95. list(APPEND mkl_lib_find_paths
  96. ${MKL_ROOT_DIR}/lib/${MKL_ARCH}
  97. ${MKL_ROOT_DIR}/${MKL_ARCH}
  98. )
  99. endforeach()
  100. if(DEFINED OPENCV_MKL_LIBRARIES)
  101. # custom list, user specified
  102. set(mkl_lib_list ${OPENCV_MKL_LIBRARIES})
  103. elseif(MKL_USE_SINGLE_DYNAMIC_LIBRARY AND NOT (MKL_VERSION_STR VERSION_LESS "10.3.0"))
  104. # https://software.intel.com/content/www/us/en/develop/articles/a-new-linking-model-single-dynamic-library-mkl_rt-since-intel-mkl-103.html
  105. set(mkl_lib_list "mkl_rt")
  106. elseif(NOT (MKL_VERSION_STR VERSION_LESS "11.3.0"))
  107. set(mkl_lib_list "mkl_intel_${MKL_ARCH_SUFFIX}")
  108. if(MKL_WITH_TBB)
  109. list(APPEND mkl_lib_list mkl_tbb_thread)
  110. elseif(MKL_WITH_OPENMP)
  111. if(MSVC)
  112. list(APPEND mkl_lib_list mkl_intel_thread libiomp5md)
  113. else()
  114. list(APPEND mkl_lib_list mkl_gnu_thread)
  115. endif()
  116. else()
  117. list(APPEND mkl_lib_list mkl_sequential)
  118. endif()
  119. list(APPEND mkl_lib_list mkl_core)
  120. else()
  121. message(STATUS "MKL version ${MKL_VERSION_STR} is not supported")
  122. mkl_fail()
  123. endif()
  124. if(NOT MKL_LIBRARIES)
  125. set(MKL_LIBRARIES "")
  126. foreach(lib ${mkl_lib_list})
  127. set(lib_var_name MKL_LIBRARY_${lib})
  128. find_library(${lib_var_name} NAMES ${lib} ${lib}_dll HINTS ${mkl_lib_find_paths})
  129. mark_as_advanced(${lib_var_name})
  130. if(NOT ${lib_var_name})
  131. mkl_fail()
  132. endif()
  133. list(APPEND MKL_LIBRARIES ${${lib_var_name}})
  134. endforeach()
  135. list(APPEND MKL_LIBRARIES ${OPENCV_EXTRA_MKL_LIBRARIES})
  136. endif()
  137. if(MKL_WITH_TBB)
  138. if(BUILD_TBB)
  139. message(STATUS "MKL: reusing builtin TBB binaries is not supported. Consider disabling MKL_WITH_TBB flag to prevent build/runtime errors")
  140. else()
  141. list(APPEND MKL_LIBRARIES tbb) # tbb target is expected
  142. endif()
  143. endif()
  144. message(STATUS "Found MKL ${MKL_VERSION_STR} at: ${MKL_ROOT_DIR}")
  145. set(HAVE_MKL ON)
  146. set(MKL_ROOT_DIR "${MKL_ROOT_DIR}" CACHE PATH "Path to MKL directory")
  147. set(MKL_INCLUDE_DIRS "${MKL_INCLUDE_DIR}")
  148. set(MKL_LIBRARIES "${MKL_LIBRARIES}")
  149. if(UNIX AND NOT MKL_USE_SINGLE_DYNAMIC_LIBRARY AND NOT MKL_LIBRARIES_DONT_HACK)
  150. #it's ugly but helps to avoid cyclic lib problem
  151. set(MKL_LIBRARIES ${MKL_LIBRARIES} ${MKL_LIBRARIES} ${MKL_LIBRARIES} "-lpthread" "-lm" "-ldl")
  152. endif()