OpenCVDetectTBB.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Search TBB library: 4.1 - 4.4, 2017-2020, 2021+ (oneTBB)
  2. #
  3. # Own TBB (3rdparty/tbb):
  4. # - set cmake option BUILD_TBB to ON
  5. #
  6. # External TBB (from system):
  7. # - Fedora: install 'tbb-devel' package
  8. # - Ubuntu: install 'libtbb-dev' package
  9. #
  10. # External TBB (from official site):
  11. # - Linux/OSX:
  12. # - in tbbvars.sh replace 'SUBSTITUTE_INSTALL_DIR_HERE' with absolute path to TBB dir
  13. # - in terminal run 'source tbbvars.sh intel64 linux' ('source tbbvars.sh' in OSX)
  14. # - Windows:
  15. # - in terminal run 'tbbvars.bat intel64 vs2015'
  16. #
  17. # Return:
  18. # - HAVE_TBB set to TRUE
  19. # - "tbb" target exists and added to OPENCV_LINKER_LIBS
  20. function(ocv_tbb_cmake_guess _found)
  21. find_package(TBB QUIET COMPONENTS tbb PATHS "$ENV{TBBROOT}/cmake" "$ENV{TBBROOT}/lib/cmake/tbb")
  22. if(TBB_FOUND)
  23. if(NOT TARGET TBB::tbb)
  24. message(WARNING "No TBB::tbb target found!")
  25. return()
  26. endif()
  27. get_target_property(_lib TBB::tbb IMPORTED_LOCATION_RELEASE)
  28. message(STATUS "Found TBB (cmake): ${_lib}")
  29. get_target_property(_inc TBB::tbb INTERFACE_INCLUDE_DIRECTORIES)
  30. add_library(tbb INTERFACE IMPORTED)
  31. set_target_properties(tbb PROPERTIES
  32. INTERFACE_LINK_LIBRARIES TBB::tbb
  33. )
  34. ocv_tbb_read_version("${_inc}" tbb)
  35. set(${_found} TRUE PARENT_SCOPE)
  36. endif()
  37. endfunction()
  38. function(ocv_tbb_env_verify)
  39. if (NOT "$ENV{TBBROOT}" STREQUAL "")
  40. # check that library and include dir are inside TBBROOT location
  41. get_filename_component(_root "$ENV{TBBROOT}" ABSOLUTE)
  42. get_filename_component(_lib "${TBB_ENV_LIB}" ABSOLUTE)
  43. get_filename_component(_inc "${TBB_ENV_INCLUDE}" ABSOLUTE)
  44. string(FIND "${_lib}" "${_root}" _lib_pos)
  45. string(FIND "${_inc}" "${_root}" _inc_pos)
  46. if (NOT (_lib_pos EQUAL 0 AND _inc_pos EQUAL 0))
  47. message(SEND_ERROR
  48. "Possible issue with TBB detection - TBBROOT is set, "
  49. "but library/include path is not inside it:\n "
  50. "TBBROOT: $ENV{TBBROOT}\n "
  51. "(absolute): ${_root}\n "
  52. "INCLUDE: ${_inc}\n "
  53. "LIBRARY: ${_lib}\n")
  54. endif()
  55. endif()
  56. endfunction()
  57. function(ocv_tbb_env_guess _found)
  58. find_path(TBB_ENV_INCLUDE NAMES "tbb/tbb.h" PATHS ENV CPATH NO_DEFAULT_PATH)
  59. find_path(TBB_ENV_INCLUDE NAMES "tbb/tbb.h")
  60. find_library(TBB_ENV_LIB NAMES "tbb" PATHS ENV LIBRARY_PATH NO_DEFAULT_PATH)
  61. find_library(TBB_ENV_LIB NAMES "tbb")
  62. find_library(TBB_ENV_LIB_DEBUG NAMES "tbb_debug" PATHS ENV LIBRARY_PATH NO_DEFAULT_PATH)
  63. find_library(TBB_ENV_LIB_DEBUG NAMES "tbb_debug")
  64. if (TBB_ENV_INCLUDE AND (TBB_ENV_LIB OR TBB_ENV_LIB_DEBUG))
  65. ocv_tbb_env_verify()
  66. add_library(tbb UNKNOWN IMPORTED)
  67. set_target_properties(tbb PROPERTIES
  68. IMPORTED_LOCATION "${TBB_ENV_LIB}"
  69. INTERFACE_INCLUDE_DIRECTORIES "${TBB_ENV_INCLUDE}"
  70. )
  71. if (TBB_ENV_LIB_DEBUG)
  72. set_target_properties(tbb PROPERTIES
  73. IMPORTED_LOCATION_DEBUG "${TBB_ENV_LIB_DEBUG}"
  74. )
  75. endif()
  76. # workaround: system TBB library is used for linking instead of provided
  77. if(CV_GCC)
  78. get_filename_component(_dir "${TBB_ENV_LIB}" DIRECTORY)
  79. set_target_properties(tbb PROPERTIES INTERFACE_LINK_LIBRARIES "-L${_dir}")
  80. endif()
  81. ocv_tbb_read_version("${TBB_ENV_INCLUDE}" tbb)
  82. if(NOT (TBB_INTERFACE_VERSION LESS 12000)) # >= 12000, oneTBB 2021+
  83. # avoid "defaultlib" requirement of tbb12.lib (we are using absolute path to 'tbb.lib' only)
  84. # https://github.com/oneapi-src/oneTBB/blame/2dba2072869a189b9fdab3ffa431d3ea49059a19/include/oneapi/tbb/detail/_config.h#L334
  85. if(NOT (CMAKE_VERSION VERSION_LESS "3.16.0")) # https://gitlab.kitware.com/cmake/cmake/-/issues/19434
  86. target_compile_definitions(tbb INTERFACE "__TBB_NO_IMPLICIT_LINKAGE=1")
  87. else()
  88. set_target_properties(tbb PROPERTIES INTERFACE_COMPILE_DEFINITIONS "__TBB_NO_IMPLICIT_LINKAGE=1")
  89. endif()
  90. endif()
  91. message(STATUS "Found TBB (env): ${TBB_ENV_LIB}")
  92. set(${_found} TRUE PARENT_SCOPE)
  93. endif()
  94. endfunction()
  95. function(ocv_tbb_read_version _path _tgt)
  96. find_file(TBB_VER_FILE oneapi/tbb/version.h "${_path}" NO_DEFAULT_PATH CMAKE_FIND_ROOT_PATH_BOTH)
  97. find_file(TBB_VER_FILE tbb/tbb_stddef.h "${_path}" NO_DEFAULT_PATH CMAKE_FIND_ROOT_PATH_BOTH)
  98. ocv_parse_header("${TBB_VER_FILE}" TBB_VERSION_LINES TBB_VERSION_MAJOR TBB_VERSION_MINOR TBB_INTERFACE_VERSION CACHE)
  99. endfunction()
  100. #=====================================================================
  101. if(BUILD_TBB)
  102. add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/tbb")
  103. if(NOT TARGET tbb)
  104. return()
  105. endif()
  106. set(HAVE_TBB TRUE)
  107. endif()
  108. if(NOT HAVE_TBB)
  109. ocv_tbb_cmake_guess(HAVE_TBB)
  110. endif()
  111. if(NOT HAVE_TBB)
  112. ocv_tbb_env_guess(HAVE_TBB)
  113. endif()
  114. if(TBB_INTERFACE_VERSION LESS 6000) # drop support of versions < 4.0
  115. set(HAVE_TBB FALSE)
  116. endif()