6ccd5b0296c42805fc399fcedb5ce1f2c877ff86
[profile/ivi/opencv.git] / 3rdparty / tbb / CMakeLists.txt
1 #build TBB for Android from source
2 if(NOT ANDROID)
3   message(FATAL_ERROR "The script is designed for Android only!")
4 endif()
5
6 project(tbb)
7
8 # 4.0 update 4 - works fine
9 set(tbb_ver "tbb40_20120408oss")
10 set(tbb_url "http://threadingbuildingblocks.org/uploads/77/185/4.0%20update%204/tbb40_20120408oss_src.tgz")
11 set(tbb_md5 "734b356da7fe0ed308741f3e6018251e")
12 set(tbb_version_file "version_string.ver")
13
14 # 4.0 update 3 - build broken
15 #set(tbb_ver "tbb40_20120201oss")
16 #set(tbb_url "http://threadingbuildingblocks.org/uploads/77/182/4.0%20update%203/tbb40_20120201oss_src.tgz")
17 #set(tbb_md5 "4669e7d4adee018de7a7b8b972987218")
18 #set(tbb_version_file "version_string.tmp")
19
20 # 4.0 update 2 - works fine
21 #set(tbb_ver "tbb40_20111130oss")
22 #set(tbb_url "http://threadingbuildingblocks.org/uploads/77/180/4.0%20update%202/tbb40_20111130oss_src.tgz")
23 #set(tbb_md5 "1e6926b21e865e79772119cd44fc3ad8")
24 #set(tbb_version_file "version_string.tmp")
25 #set(tbb_need_GENERIC_DWORD_LOAD_STORE TRUE)
26
27 # 4.0 update 1 - works fine
28 #set(tbb_ver "tbb40_20111003oss")
29 #set(tbb_url "http://threadingbuildingblocks.org/uploads/77/177/4.0%20update%201/tbb40_20111003oss_src.tgz")
30 #set(tbb_md5 "7b5d94eb35a563b29ef402e0fd8f15c9")
31 #set(tbb_version_file "version_string.tmp")
32 #set(tbb_need_GENERIC_DWORD_LOAD_STORE TRUE)
33
34 set(tbb_tarball "${CMAKE_CURRENT_SOURCE_DIR}/${tbb_ver}_src.tgz")
35 set(tbb_src_dir "${CMAKE_CURRENT_BINARY_DIR}/${tbb_ver}")
36
37 macro(getMD5 filename varname)
38   if(CMAKE_VERSION VERSION_GREATER 2.8.6)
39     file(MD5 "${filename}" ${varname})
40   else()
41     execute_process(COMMAND ${CMAKE_COMMAND} -E md5sum "${filename}"
42               RESULT_VARIABLE getMD5_RESULT
43               OUTPUT_VARIABLE getMD5_OUTPUT
44               OUTPUT_STRIP_TRAILING_WHITESPACE)
45     if(getMD5_RESULT EQUAL 0)
46       string(REGEX MATCH "^[a-z0-9]+" ${varname} "${getMD5_OUTPUT}")
47     else()
48       set(${varname} "invalid_md5")
49     endif()
50   endif()
51 endmacro()
52
53 if(EXISTS "${tbb_tarball}")
54   getMD5("${tbb_tarball}" tbb_local_md5)
55   if(NOT tbb_local_md5 STREQUAL tbb_md5)
56     message(WARNING "Local copy of TBB source tarball has invalid MD5 hash: ${tbb_local_md5} (expected: ${tbb_md5})")
57     file(REMOVE "${tbb_tarball}")
58     if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${tbb_ver}")
59       file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/${tbb_ver}")
60     endif()
61   endif()
62 endif()
63
64 if(NOT EXISTS "${tbb_tarball}")
65   message(STATUS "Downloading ${tbb_ver}_src.tgz")
66   file(DOWNLOAD "${tbb_url}" "${tbb_tarball}" TIMEOUT 600 STATUS __statvar)
67   if(NOT __statvar EQUAL 0)
68     message(FATAL_ERROR "Failed to download TBB sources: ${tbb_url}")
69   endif()
70   getMD5("${tbb_tarball}" tbb_local_md5)
71   if(NOT tbb_local_md5 STREQUAL tbb_md5)
72     file(REMOVE "${tbb_tarball}")
73     message(FATAL_ERROR "Downloaded TBB source tarball has invalid MD5 hash: ${tbb_local_md5} (expected: ${tbb_md5})")
74   endif()
75
76   if(EXISTS "${tbb_src_dir}")
77     file(REMOVE_RECURSE "${tbb_src_dir}")
78   endif()
79 endif()
80
81 # untar TBB sources
82 if(NOT EXISTS "${tbb_src_dir}")
83   message(STATUS "Unpacking ${tbb_ver}_src.tgz to ${tbb_src_dir}")
84   execute_process(COMMAND ${CMAKE_COMMAND} -E tar xz "${tbb_tarball}"
85               WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
86               RESULT_VARIABLE tbb_untar_RESULT)
87
88   if(NOT tbb_untar_RESULT EQUAL 0 OR NOT EXISTS "${tbb_src_dir}")
89     message(FATAL_ERROR "Failed to unpack TBB sources")
90   endif()
91 endif()
92
93 set(TBB_INCLUDE_DIRS "${tbb_src_dir}/include" PARENT_SCOPE)
94
95 ocv_include_directories("${tbb_src_dir}/include"
96                         "${tbb_src_dir}/src/"
97                         "${tbb_src_dir}/src/rml/include"
98                         "${CMAKE_CURRENT_SOURCE_DIR}")
99
100 file(GLOB lib_srcs "${tbb_src_dir}/src/tbb/*.cpp")
101 file(GLOB lib_hdrs "${tbb_src_dir}/src/tbb/*.h")
102 list(APPEND lib_srcs "${tbb_src_dir}/src/rml/client/rml_tbb.cpp")
103
104 add_definitions(-D__TBB_DYNAMIC_LOAD_ENABLED=0         #required
105                 -D__TBB_BUILD=1                        #required
106                 -D__TBB_SURVIVE_THREAD_SWITCH=0        #no cilk on Android ?
107                 -DUSE_PTHREAD                          #required
108                 -DTBB_USE_GCC_BUILTINS=1               #required
109                 -DTBB_USE_DEBUG=0                      #just to be sure
110                 -DTBB_NO_LEGACY=1                      #don't need backward compatibility
111                 -DDO_ITT_NOTIFY=0                      #it seems that we don't need these notifications
112                )
113
114 if(tbb_need_GENERIC_DWORD_LOAD_STORE)
115   #needed by TBB 4.0 update 1,2; fixed in TBB 4.0 update 3 but it has 2 new problems
116   add_definitions(-D__TBB_USE_GENERIC_DWORD_LOAD_STORE=1)
117 endif()
118
119 add_library(tbb STATIC ${lib_srcs} ${lib_hdrs} "${CMAKE_CURRENT_SOURCE_DIR}/android_additional.h" "${CMAKE_CURRENT_SOURCE_DIR}/${tbb_version_file}")
120 target_link_libraries(tbb c m dl)
121
122 ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef -Wmissing-declarations)
123 string(REPLACE "-Werror=non-virtual-dtor" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
124
125 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -include \"${CMAKE_CURRENT_SOURCE_DIR}/android_additional.h\"")
126
127 set_target_properties(tbb
128   PROPERTIES OUTPUT_NAME tbb
129   DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
130   ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/3rdparty/${OPENCV_LIB_INSTALL_PATH}"
131   )
132
133 if(ENABLE_SOLUTION_FOLDERS)
134   set_target_properties(tbb PROPERTIES FOLDER "3rdparty")
135 endif()
136
137 if(NOT BUILD_SHARED_LIBS)
138   install(TARGETS tbb ARCHIVE DESTINATION share/OpenCV/3rdparty/${OPENCV_LIB_INSTALL_PATH} COMPONENT main)
139 endif()
140
141 # get TBB version
142 ocv_parse_header("${tbb_src_dir}/include/tbb/tbb_stddef.h" TBB_VERSION_LINES TBB_VERSION_MAJOR TBB_VERSION_MINOR TBB_INTERFACE_VERSION CACHE)
143