1 #Cross compile TBB from source 2 project(tbb) 3 4 if (WIN32 AND NOT ARM) 5 message(FATAL_ERROR "BUILD_TBB option supports Windows on ARM only!\nUse regular official TBB build instead of the BUILD_TBB option!") 6 endif() 7 8 if (WIN32 AND ARM) 9 # 4.1 update 4 - The first release that supports Windows RT. Hangs on some Android devices 10 set(tbb_ver "tbb41_20130613oss") 11 set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb41_20130613oss_src.tgz") 12 set(tbb_md5 "108c8c1e481b0aaea61878289eb28b6a") 13 set(tbb_version_file "version_string.ver") 14 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4702) 15 else() 16 # 4.1 update 2 - works fine 17 set(tbb_ver "tbb41_20130116oss") 18 set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb41_20130116oss_src.tgz") 19 set(tbb_md5 "3809790e1001a1b32d59c9fee590ee85") 20 set(tbb_version_file "version_string.ver") 21 ocv_warnings_disable(CMAKE_CXX_FLAGS -Wshadow) 22 endif() 23 24 # 4.1 update 3 dev - Hangs on some Android devices 25 #set(tbb_ver "tbb41_20130401oss") 26 #set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb41_20130401oss_src.tgz") 27 #set(tbb_md5 "f2f591a0d2ca8f801e221ce7d9ea84bb") 28 #set(tbb_version_file "version_string.ver") 29 #ocv_warnings_disable(CMAKE_CXX_FLAGS -Wshadow) 30 31 # 4.1 update 2 - works fine 32 #set(tbb_ver "tbb41_20130116oss") 33 #set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb41_20130116oss_src.tgz") 34 #set(tbb_md5 "3809790e1001a1b32d59c9fee590ee85") 35 #set(tbb_version_file "version_string.ver") 36 #ocv_warnings_disable(CMAKE_CXX_FLAGS -Wshadow) 37 38 # 4.1 update 1 - works fine 39 #set(tbb_ver "tbb41_20121003oss") 40 #set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb41_20121003oss_src.tgz") 41 #set(tbb_md5 "2a684fefb855d2d0318d1ef09afa75ff") 42 #set(tbb_version_file "version_string.ver") 43 44 # 4.1 - works fine 45 #set(tbb_ver "tbb41_20120718oss") 46 #set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb41_20120718oss_src.tgz") 47 #set(tbb_md5 "31b9ec300f3d09da2504d5d882788dd4") 48 #set(tbb_version_file "version_string.ver") 49 50 # 4.0 update 5 - works fine 51 #set(tbb_ver "tbb40_20120613oss") 52 #set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb40_20120613oss_src.tgz") 53 #set(tbb_md5 "da01ed74944ec5950cfae3476901a172") 54 #set(tbb_version_file "version_string.ver") 55 56 # 4.0 update 4 - works fine 57 #set(tbb_ver "tbb40_20120408oss") 58 #set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb40_20120408oss_src.tgz") 59 #set(tbb_md5 "734b356da7fe0ed308741f3e6018251e") 60 #set(tbb_version_file "version_string.ver") 61 62 # 4.0 update 3 - build broken 63 #set(tbb_ver "tbb40_20120201oss") 64 #set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb40_20120201oss_src.tgz") 65 #set(tbb_md5 "4669e7d4adee018de7a7b8b972987218") 66 #set(tbb_version_file "version_string.tmp") 67 68 # 4.0 update 2 - works fine 69 #set(tbb_ver "tbb40_20111130oss") 70 #set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb40_20111130oss_src.tgz") 71 #set(tbb_md5 "1e6926b21e865e79772119cd44fc3ad8") 72 #set(tbb_version_file "version_string.tmp") 73 #set(tbb_need_GENERIC_DWORD_LOAD_STORE TRUE) 74 75 # 4.0 update 1 - works fine 76 #set(tbb_ver "tbb40_20111003oss") 77 #set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb40_20111003oss_src.tgz") 78 #set(tbb_md5 "7b5d94eb35a563b29ef402e0fd8f15c9") 79 #set(tbb_version_file "version_string.tmp") 80 #set(tbb_need_GENERIC_DWORD_LOAD_STORE TRUE) 81 82 set(tbb_tarball "${CMAKE_CURRENT_SOURCE_DIR}/${tbb_ver}_src.tgz") 83 set(tbb_src_dir "${CMAKE_CURRENT_BINARY_DIR}/${tbb_ver}") 84 85 if(EXISTS "${tbb_tarball}") 86 file(MD5 "${tbb_tarball}" tbb_local_md5) 87 if(NOT tbb_local_md5 STREQUAL tbb_md5) 88 message(WARNING "Local copy of TBB source tarball has invalid MD5 hash: ${tbb_local_md5} (expected: ${tbb_md5})") 89 file(REMOVE "${tbb_tarball}") 90 if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${tbb_ver}") 91 file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/${tbb_ver}") 92 endif() 93 endif() 94 endif() 95 96 if(NOT EXISTS "${tbb_tarball}") 97 message(STATUS "Downloading ${tbb_ver}_src.tgz") 98 file(DOWNLOAD "${tbb_url}" "${tbb_tarball}" TIMEOUT 600 STATUS __statvar) 99 if(NOT __statvar EQUAL 0) 100 message(FATAL_ERROR "Failed to download TBB sources: ${tbb_url}") 101 endif() 102 file(MD5 "${tbb_tarball}" tbb_local_md5) 103 if(NOT tbb_local_md5 STREQUAL tbb_md5) 104 file(REMOVE "${tbb_tarball}") 105 message(FATAL_ERROR "Downloaded TBB source tarball has invalid MD5 hash: ${tbb_local_md5} (expected: ${tbb_md5})") 106 endif() 107 108 if(EXISTS "${tbb_src_dir}") 109 file(REMOVE_RECURSE "${tbb_src_dir}") 110 endif() 111 endif() 112 113 # untar TBB sources 114 if(NOT EXISTS "${tbb_src_dir}") 115 message(STATUS "Unpacking ${tbb_ver}_src.tgz to ${tbb_src_dir}") 116 execute_process(COMMAND ${CMAKE_COMMAND} -E tar xz "${tbb_tarball}" 117 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" 118 RESULT_VARIABLE tbb_untar_RESULT) 119 120 if(NOT tbb_untar_RESULT EQUAL 0 OR NOT EXISTS "${tbb_src_dir}") 121 message(FATAL_ERROR "Failed to unpack TBB sources from ${tbb_tarball} to ${tbb_src_dir} with error ${tbb_untar_RESULT}") 122 endif() 123 endif() 124 125 set(TBB_INCLUDE_DIRS "${tbb_src_dir}/include" PARENT_SCOPE) 126 127 ocv_include_directories("${tbb_src_dir}/include" 128 "${tbb_src_dir}/src/" 129 "${tbb_src_dir}/src/rml/include" 130 "${CMAKE_CURRENT_SOURCE_DIR}") 131 132 file(GLOB lib_srcs "${tbb_src_dir}/src/tbb/*.cpp") 133 file(GLOB lib_hdrs "${tbb_src_dir}/src/tbb/*.h") 134 list(APPEND lib_srcs "${tbb_src_dir}/src/rml/client/rml_tbb.cpp") 135 136 if (WIN32) 137 add_definitions(/D__TBB_DYNAMIC_LOAD_ENABLED=0 138 /D__TBB_BUILD=1 139 /DTBB_NO_LEGACY=1 140 /D_UNICODE 141 /DUNICODE 142 /DWINAPI_FAMILY=WINAPI_FAMILY_APP 143 /DDO_ITT_NOTIFY=0 144 /DUSE_WINTHREAD 145 ) # defines were copied from windows.cl.inc 146 147 if (ARM) 148 add_definitions(/D_WIN32_WINNT=0x0602 149 /D__TBB_WIN32_USE_CL_BUILTINS 150 ) 151 endif() 152 153 set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} /APPCONTAINER") 154 else() 155 add_definitions(-D__TBB_DYNAMIC_LOAD_ENABLED=0 #required 156 -D__TBB_BUILD=1 #required 157 -D__TBB_SURVIVE_THREAD_SWITCH=0 #no cilk support 158 -DTBB_USE_DEBUG=0 #just to be sure 159 -DTBB_NO_LEGACY=1 #don't need backward compatibility 160 -DDO_ITT_NOTIFY=0 #it seems that we don't need these notifications 161 ) 162 endif() 163 164 if (HAVE_LIBPTHREAD) 165 add_definitions(-DUSE_PTHREAD) #required for Unix 166 endif() 167 168 if (CMAKE_COMPILER_IS_GNUCXX) 169 add_definitions(-DTBB_USE_GCC_BUILTINS=1) #required for ARM GCC 170 endif() 171 172 if(ANDROID_COMPILER_IS_CLANG) 173 add_definitions(-D__TBB_GCC_BUILTIN_ATOMICS_PRESENT=1) 174 ocv_warnings_disable(CMAKE_CXX_FLAGS -Wmissing-prototypes) 175 endif() 176 177 if(tbb_need_GENERIC_DWORD_LOAD_STORE) 178 #needed by TBB 4.0 update 1,2; fixed in TBB 4.0 update 3 but it has 2 new problems 179 add_definitions(-D__TBB_USE_GENERIC_DWORD_LOAD_STORE=1) 180 set(tbb_need_GENERIC_DWORD_LOAD_STORE ON PARENT_SCOPE) 181 endif() 182 183 set(TBB_SOURCE_FILES ${lib_srcs} ${lib_hdrs}) 184 185 if (ARM AND NOT WIN32) 186 if (NOT ANDROID) 187 set(TBB_SOURCE_FILES ${TBB_SOURCE_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/arm_linux_stub.cpp") 188 endif() 189 set(TBB_SOURCE_FILES ${TBB_SOURCE_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/android_additional.h") 190 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -include \"${CMAKE_CURRENT_SOURCE_DIR}/android_additional.h\"") 191 endif() 192 193 set(TBB_SOURCE_FILES ${TBB_SOURCE_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/${tbb_version_file}") 194 195 add_library(tbb ${TBB_SOURCE_FILES}) 196 197 if (WIN32) 198 if (ARM) 199 set(platform_macro /D_M_ARM=1) 200 endif() 201 202 add_custom_command(TARGET tbb 203 PRE_BUILD 204 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" 205 WORKING_DIRECTORY ${tbb_src_dir}\\src\\tbb 206 COMMENT "Generating tbb.def file" VERBATIM 207 ) 208 209 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEF:${tbb_src_dir}/src/tbb/tbb.def /DLL /MAP /fixed:no /INCREMENTAL:NO") 210 else() 211 target_link_libraries(tbb c m dl) 212 endif() 213 214 ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef -Wmissing-declarations) 215 string(REPLACE "-Werror=non-virtual-dtor" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 216 217 if (WIN32) 218 set(tbb_debug_postfix "_debug") # to fit pragmas in _windef.h inside TBB 219 else() 220 set(tbb_debug_postfix ${OPENCV_DEBUG_POSTFIX}) 221 endif() 222 223 set_target_properties(tbb 224 PROPERTIES OUTPUT_NAME tbb 225 DEBUG_POSTFIX "${tbb_debug_postfix}" 226 ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH} 227 RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} 228 ) 229 230 if(ENABLE_SOLUTION_FOLDERS) 231 set_target_properties(tbb PROPERTIES FOLDER "3rdparty") 232 endif() 233 234 ocv_install_target(tbb EXPORT OpenCVModules 235 RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs 236 LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs 237 ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev 238 ) 239 240 # get TBB version 241 ocv_parse_header("${tbb_src_dir}/include/tbb/tbb_stddef.h" TBB_VERSION_LINES TBB_VERSION_MAJOR TBB_VERSION_MINOR TBB_INTERFACE_VERSION CACHE) 242