CMakeLists.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #Cross compile TBB from source
  2. project(tbb CXX)
  3. if (WIN32 AND NOT ARM)
  4. message(FATAL_ERROR "BUILD_TBB option supports Windows on ARM only!\nUse regular official TBB build instead of the BUILD_TBB option!")
  5. endif()
  6. ocv_update(OPENCV_TBB_RELEASE "v2020.2")
  7. ocv_update(OPENCV_TBB_RELEASE_MD5 "5af6f6c2a24c2043e62e47205e273b1f")
  8. ocv_update(OPENCV_TBB_FILENAME "${OPENCV_TBB_RELEASE}.tar.gz")
  9. string(REGEX REPLACE "^v" "" OPENCV_TBB_RELEASE_ "${OPENCV_TBB_RELEASE}")
  10. #ocv_update(OPENCV_TBB_SUBDIR ...)
  11. set(tbb_src_dir "${OpenCV_BINARY_DIR}/3rdparty/tbb")
  12. ocv_download(FILENAME ${OPENCV_TBB_FILENAME}
  13. HASH ${OPENCV_TBB_RELEASE_MD5}
  14. URL
  15. "${OPENCV_TBB_URL}"
  16. "$ENV{OPENCV_TBB_URL}"
  17. "https://github.com/01org/tbb/archive/"
  18. DESTINATION_DIR ${tbb_src_dir}
  19. ID TBB
  20. STATUS res
  21. UNPACK RELATIVE_URL)
  22. if(NOT res)
  23. return()
  24. endif()
  25. if(OPENCV_TBB_SUBDIR)
  26. # use current value
  27. ocv_assert(EXISTS "${tbb_src_dir}/${OPENCV_TBB_SUBDIR}")
  28. elseif(EXISTS "${tbb_src_dir}/oneTBB-${OPENCV_TBB_RELEASE_}")
  29. set(OPENCV_TBB_SUBDIR "oneTBB-${OPENCV_TBB_RELEASE_}")
  30. elseif(EXISTS "${tbb_src_dir}/tbb-${OPENCV_TBB_RELEASE_}")
  31. set(OPENCV_TBB_SUBDIR "oneTBB-${OPENCV_TBB_RELEASE_}")
  32. else()
  33. message(FATAL_ERROR "TBB: Can't configure TBB. Specify OPENCV_TBB_SUBDIR through command-line.")
  34. endif()
  35. set(tbb_src_dir "${tbb_src_dir}/${OPENCV_TBB_SUBDIR}")
  36. ocv_include_directories("${tbb_src_dir}/include"
  37. "${tbb_src_dir}/src/"
  38. "${tbb_src_dir}/src/rml/include"
  39. "${CMAKE_CURRENT_SOURCE_DIR}")
  40. file(GLOB lib_srcs "${tbb_src_dir}/src/tbb/*.cpp")
  41. file(GLOB lib_hdrs "${tbb_src_dir}/src/tbb/*.h")
  42. list(APPEND lib_srcs "${tbb_src_dir}/src/rml/client/rml_tbb.cpp")
  43. ocv_list_filterout(lib_srcs "${tbb_src_dir}/src/tbb/tbbbind.cpp") # hwloc.h requirement
  44. ocv_list_filterout(lib_srcs "${tbb_src_dir}/src/tbb/tbb_bind.cpp") # hwloc.h requirement 2020.1+
  45. if (WIN32)
  46. add_definitions(/D__TBB_DYNAMIC_LOAD_ENABLED=0
  47. /D__TBB_BUILD=1
  48. /DTBB_NO_LEGACY=1
  49. /D_UNICODE
  50. /DUNICODE
  51. /DWINAPI_FAMILY=WINAPI_FAMILY_APP
  52. /DDO_ITT_NOTIFY=0
  53. /DUSE_WINTHREAD
  54. ) # defines were copied from windows.cl.inc
  55. if (ARM)
  56. add_definitions(/D_WIN32_WINNT=0x0602
  57. /D__TBB_WIN32_USE_CL_BUILTINS
  58. )
  59. endif()
  60. set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} /APPCONTAINER")
  61. else()
  62. add_definitions(-D__TBB_DYNAMIC_LOAD_ENABLED=0 #required
  63. -D__TBB_WEAK_SYMBOLS_PRESENT=0 #required for 4.3
  64. -D__TBB_BUILD=1 #required
  65. -D__TBB_SURVIVE_THREAD_SWITCH=0 #no cilk support
  66. -DTBB_USE_DEBUG=0 #just to be sure
  67. -DTBB_NO_LEGACY=1 #don't need backward compatibility
  68. -DDO_ITT_NOTIFY=0 #it seems that we don't need these notifications
  69. )
  70. endif()
  71. if(HAVE_PTHREAD)
  72. add_definitions(-DUSE_PTHREAD) #required for Unix
  73. endif()
  74. if(CV_GCC)
  75. add_definitions(-DTBB_USE_GCC_BUILTINS=1) #required for ARM GCC
  76. if(NOT CMAKE_CXX_COMPILER_VERSION LESS 6.0)
  77. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flifetime-dse=1") # workaround for GCC 6.x
  78. endif()
  79. endif()
  80. if(ANDROID_COMPILER_IS_CLANG)
  81. add_definitions(-D__TBB_GCC_BUILTIN_ATOMICS_PRESENT=1)
  82. endif()
  83. ocv_warnings_disable(CMAKE_CXX_FLAGS
  84. /wd4702
  85. -Wshadow
  86. -Wunused-parameter
  87. -Wclass-memaccess # TBB 2018 under GCC 8+
  88. -Wimplicit-fallthrough # TBB 2018 under GCC 7+
  89. -Wmissing-prototypes # MacOSX, Android/Clang
  90. -Wundef -Wmissing-declarations # TBB 2019
  91. )
  92. set(TBB_SOURCE_FILES ${lib_srcs} ${lib_hdrs})
  93. set(tbb_version_file "version_string.ver")
  94. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${tbb_version_file}.cmakein" "${CMAKE_CURRENT_BINARY_DIR}/${tbb_version_file}" @ONLY)
  95. list(APPEND TBB_SOURCE_FILES "${CMAKE_CURRENT_BINARY_DIR}/${tbb_version_file}")
  96. add_library(tbb ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} ${TBB_SOURCE_FILES})
  97. target_compile_definitions(tbb PUBLIC
  98. TBB_USE_GCC_BUILTINS=1
  99. __TBB_GCC_BUILTIN_ATOMICS_PRESENT=1
  100. TBB_SUPPRESS_DEPRECATED_MESSAGES=1
  101. )
  102. target_include_directories(tbb SYSTEM PUBLIC $<BUILD_INTERFACE:${tbb_src_dir}/include>
  103. PRIVATE "${CMAKE_CURRENT_BINARY_DIR}"
  104. )
  105. if (WIN32)
  106. if (ARM)
  107. set(platform_macro /D_M_ARM=1)
  108. endif()
  109. add_custom_command(TARGET tbb
  110. PRE_BUILD
  111. COMMAND ${CMAKE_C_COMPILER} /nologo /TC /EP ${tbb_src_dir}\\src\\tbb\\win32-tbb-export.def /DTBB_NO_LEGACY=1 /D_CRT_SECURE_NO_DEPRECATE /D__TBB_BUILD=1 ${platform_macro} /I${tbb_src_dir}\\src /I${tbb_src_dir}\\include > "${tbb_src_dir}\\src\\tbb\\tbb.def"
  112. WORKING_DIRECTORY ${tbb_src_dir}\\src\\tbb
  113. COMMENT "Generating tbb.def file" VERBATIM
  114. )
  115. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEF:${tbb_src_dir}/src/tbb/tbb.def /DLL /MAP /fixed:no /INCREMENTAL:NO")
  116. else()
  117. target_link_libraries(tbb c m dl)
  118. endif()
  119. # filter out flags that are not handled well by the TBB code
  120. foreach(var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
  121. string(REPLACE "-Werror=non-virtual-dtor" "" ${var} "${${var}}")
  122. string(REPLACE "-fvisibility=hidden" "" ${var} "${${var}}")
  123. string(REPLACE "-fvisibility-inlines-hidden" "" ${var} "${${var}}")
  124. endforeach()
  125. if (WIN32)
  126. set(tbb_debug_postfix "_debug") # to fit pragmas in _windef.h inside TBB
  127. else()
  128. set(tbb_debug_postfix ${OPENCV_DEBUG_POSTFIX})
  129. endif()
  130. set_target_properties(tbb
  131. PROPERTIES OUTPUT_NAME tbb
  132. DEBUG_POSTFIX "${tbb_debug_postfix}"
  133. COMPILE_PDB_NAME tbb
  134. COMPILE_PDB_NAME_DEBUG "tbb${OPENCV_DEBUG_POSTFIX}"
  135. ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH}
  136. RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
  137. )
  138. if(ENABLE_SOLUTION_FOLDERS)
  139. set_target_properties(tbb PROPERTIES FOLDER "3rdparty")
  140. endif()
  141. ocv_install_target(tbb EXPORT OpenCVModules
  142. RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
  143. LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs
  144. ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev
  145. OPTIONAL
  146. )
  147. ocv_install_3rdparty_licenses(tbb "${tbb_src_dir}/LICENSE" "${tbb_src_dir}/README")
  148. ocv_tbb_read_version("${tbb_src_dir}/include" tbb)