0029589: Configuration - allow MFC samples to be built when OCCT is linked statically
[occt.git] / adm / cmake / tbb.cmake
1 # tbb
2
3 if (NOT DEFINED INSTALL_TBB AND BUILD_SHARED_LIBS)
4   set (INSTALL_TBB OFF CACHE BOOL "${INSTALL_TBB_DESCR}")
5 endif()
6
7 # tbb directory
8 if (NOT DEFINED 3RDPARTY_TBB_DIR)
9   set (3RDPARTY_TBB_DIR "" CACHE PATH "The directory containing tbb")
10 endif()
11
12 if (MSVC AND BUILD_SHARED_LIBS)
13   add_definitions (-D__TBB_NO_IMPLICIT_LINKAGE)
14   add_definitions (-D__TBBMALLOC_NO_IMPLICIT_LINKAGE)
15 endif()
16
17 # include occt macros. compiler_bitness, os_wiht_bit, compiler
18 OCCT_INCLUDE_CMAKE_FILE ("adm/cmake/occt_macros")
19
20 # specify TBB folder in connectin with 3RDPARTY_DIR
21 if (3RDPARTY_DIR AND EXISTS "${3RDPARTY_DIR}")
22   #CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_DIR 3RDPARTY_TBB_DIR PATH "The directory containing tbb")
23
24   if (NOT 3RDPARTY_TBB_DIR OR NOT EXISTS "${3RDPARTY_TBB_DIR}")
25     FIND_PRODUCT_DIR ("${3RDPARTY_DIR}" TBB TBB_DIR_NAME)
26     if (TBB_DIR_NAME)
27       set (3RDPARTY_TBB_DIR "${3RDPARTY_DIR}/${TBB_DIR_NAME}" CACHE PATH "The directory containing tbb" FORCE)
28     endif()
29   endif()
30 else()
31   #set (3RDPARTY_TBB_DIR "" CACHE PATH "The directory containing TBB" FORCE)
32 endif()
33
34 if (NOT DEFINED 3RDPARTY_TBB_INCLUDE_DIR)
35   set (3RDPARTY_TBB_INCLUDE_DIR "" CACHE PATH "The directory containing headers of the TBB")
36 endif()
37
38 if (3RDPARTY_TBB_DIR AND EXISTS "${3RDPARTY_TBB_DIR}")
39     # check 3RDPARTY_TBB_INCLUDE_DIR for consictency with specified 3RDPARTY_TBB_DIR
40     CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_TBB_DIR 3RDPARTY_TBB_INCLUDE_DIR PATH "The directory containing headers of the TBB")
41 endif()
42
43 # tbb.h
44 if (NOT 3RDPARTY_TBB_INCLUDE_DIR OR NOT EXISTS "${3RDPARTY_TBB_INCLUDE_DIR}")
45
46   set (HEADER_NAMES tbb.h tbb/tbb.h)
47
48   # set 3RDPARTY_TBB_INCLUDE_DIR as notfound, otherwise find_library can't assign a new value to 3RDPARTY_TBB_INCLUDE_DIR
49   set (3RDPARTY_TBB_INCLUDE_DIR "3RDPARTY_TBB_INCLUDE_DIR-NOTFOUND" CACHE PATH "the path to tbb.h" FORCE)
50
51   if (3RDPARTY_TBB_DIR AND EXISTS "${3RDPARTY_TBB_DIR}")
52     find_path (3RDPARTY_TBB_INCLUDE_DIR NAMES ${HEADER_NAMES}
53                                               PATHS ${3RDPARTY_TBB_DIR}
54                                               PATH_SUFFIXES include
55                                               CMAKE_FIND_ROOT_PATH_BOTH
56                                               NO_DEFAULT_PATH)
57   else()
58     find_path (3RDPARTY_TBB_INCLUDE_DIR NAMES ${HEADER_NAMES}
59                                               PATH_SUFFIXES include
60                                               CMAKE_FIND_ROOT_PATH_BOTH)
61   endif()
62 endif()
63
64 if (3RDPARTY_TBB_INCLUDE_DIR AND EXISTS "${3RDPARTY_TBB_INCLUDE_DIR}")
65   list (APPEND 3RDPARTY_INCLUDE_DIRS "${3RDPARTY_TBB_INCLUDE_DIR}")
66 else()
67   list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_TBB_INCLUDE_DIR)
68
69   set (3RDPARTY_TBB_INCLUDE_DIR "" CACHE PATH "the path to tbb.h" FORCE)
70 endif()
71
72 # common steps for tbb and tbbmalloc
73 macro (TBB_PRODUCT_SEARCH PRODUCT_LIBRARY_NAME)
74
75   string (TOUPPER ${PRODUCT_LIBRARY_NAME} upper_PRODUCT_LIBRARY_NAME)
76
77   # define required tbb/tbbmalloc variables
78   if (NOT DEFINED 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY OR NOT 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR OR NOT EXISTS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR}")
79     set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY "" CACHE FILEPATH "${upper_PRODUCT_LIBRARY_NAME} library" FORCE)
80   endif()
81
82   if (NOT DEFINED 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR)
83     set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR "" CACHE PATH "The directory containing ${upper_PRODUCT_LIBRARY_NAME} library")
84   endif()
85
86   if (WIN32)
87     if (NOT DEFINED 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL OR NOT 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR OR NOT EXISTS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR}")
88       set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL "" CACHE FILEPATH "${upper_PRODUCT_LIBRARY_NAME} shared library" FORCE)
89     endif()
90   endif()
91
92   if (WIN32 AND NOT DEFINED 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR)
93     set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR "" CACHE PATH "The directory containing ${upper_PRODUCT_LIBRARY_NAME} shared library")
94   endif()
95
96   # check 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_ paths for consistency with specified 3RDPARTY_TBB_DIR
97   if (3RDPARTY_TBB_DIR AND EXISTS "${3RDPARTY_TBB_DIR}")
98     CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_TBB_DIR 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY FILEPATH "the path to ${upper_PRODUCT_LIBRARY_NAME} library")
99
100     if (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY AND EXISTS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY}")
101       get_filename_component (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY}" PATH)
102       set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR}" CACHE PATH "The directory containing ${upper_PRODUCT_LIBRARY_NAME} library" FORCE)
103     else()
104       CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_TBB_DIR 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR PATH "The directory containing ${upper_PRODUCT_LIBRARY_NAME} library")
105     endif()
106
107     if (WIN32)
108       CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_TBB_DIR 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL FILEPATH "the path to ${upper_PRODUCT_LIBRARY_NAME} shared library")
109
110       if (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL AND EXISTS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL}")
111         get_filename_component (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL}" PATH)
112         set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR}" CACHE PATH "The directory containing ${upper_PRODUCT_LIBRARY_NAME} shared library" FORCE)
113       else()
114
115       CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_TBB_DIR 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR PATH "The directory containing ${upper_PRODUCT_LIBRARY_NAME} shared library")
116       endif()
117     endif()
118   endif()
119
120   OCCT_MAKE_COMPILER_SHORT_NAME()
121   OCCT_MAKE_COMPILER_BITNESS()
122
123   if (${COMPILER_BITNESS} EQUAL 32)
124     set (${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME ia32)
125   else()
126     set (${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME intel64)
127   endif()
128
129   # tbb/tbbmalloc library
130   if (NOT 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY OR NOT EXISTS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY}")
131     
132     set (CMAKE_FIND_LIBRARY_SUFFIXES .lib .so .dylib .a)
133     set (PRODUCT_PATH_SUFFIXES lib ${PRODUCT_LIBRARY_NAME})
134
135     # set 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY as notfound, otherwise find_library can't assign a new value to 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY
136     set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY "3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY-NOTFOUND" CACHE FILEPATH "The path to ${upper_PRODUCT_LIBRARY_NAME} library" FORCE)
137
138     if (3RDPARTY_TBB_DIR AND EXISTS "${3RDPARTY_TBB_DIR}")
139       if (NOT EXISTS "${3RDPARTY_TBB_DIR}/lib/${${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME}/${COMPILER}")
140         if (EXISTS "${3RDPARTY_TBB_DIR}/lib/${${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME}")
141           file (GLOB ${upper_PRODUCT_LIBRARY_NAME}_COMPILER_LIST "${3RDPARTY_TBB_DIR}/lib/${${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME}/*")
142           if (${upper_PRODUCT_LIBRARY_NAME}_COMPILER_LIST)
143             list (GET ${upper_PRODUCT_LIBRARY_NAME}_COMPILER_LIST -1 THE_MOST_FRESH_COMPILER_VERSION)
144             if (THE_MOST_FRESH_COMPILER_VERSION)
145               get_filename_component (THE_MOST_FRESH_COMPILER_VERSION_NAME "${THE_MOST_FRESH_COMPILER_VERSION}" NAME)
146               set (PRODUCT_PATH_SUFFIXES lib ${PRODUCT_LIBRARY_NAME} lib/${${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME}/${THE_MOST_FRESH_COMPILER_VERSION_NAME})
147             endif()
148           endif()
149         endif()
150       else()
151         set (PRODUCT_PATH_SUFFIXES lib ${PRODUCT_LIBRARY_NAME} lib/${${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME}/${COMPILER})
152       endif()
153
154       find_library (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY ${PRODUCT_LIBRARY_NAME}
155                                          PATHS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR}" "${3RDPARTY_TBB_DIR}"
156                                          PATH_SUFFIXES ${PRODUCT_PATH_SUFFIXES}
157                                          CMAKE_FIND_ROOT_PATH_BOTH
158                                          NO_DEFAULT_PATH)
159     else()
160       find_library (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY ${PRODUCT_LIBRARY_NAME}
161                                          PATH_SUFFIXES ${PRODUCT_PATH_SUFFIXES}
162                                          CMAKE_FIND_ROOT_PATH_BOTH)
163     endif()
164
165     if (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY AND EXISTS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY}")
166       get_filename_component (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY}" PATH)
167       set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR}" CACHE PATH "The directory containing ${upper_PRODUCT_LIBRARY_NAME} library" FORCE)
168     else()
169       set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR "" CACHE PATH "The directory containing ${upper_PRODUCT_LIBRARY_NAME} library" FORCE)
170     endif()
171   endif()
172
173   if (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR AND EXISTS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR}")
174     list (APPEND 3RDPARTY_LIBRARY_DIRS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR}")
175   else()
176     list (APPEND 3RDPARTY_NO_LIBS 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY_DIR)
177
178     set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY "" CACHE FILEPATH "The path to ${upper_PRODUCT_LIBRARY_NAME} library" FORCE)
179   endif()
180
181   # tbb/tbbmalloc shared library
182   if (WIN32)
183     if (NOT 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL OR NOT EXISTS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL}")
184       set (CMAKE_FIND_LIBRARY_SUFFIXES .dll)
185       set (PRODUCT_PATH_SUFFIXES bin)
186
187       # set 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL as notfound, otherwise find_library can't assign a new value to 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL
188       set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL "3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL-NOTFOUND" CACHE FILEPATH "${upper_PRODUCT_LIBRARY_NAME} shared library" FORCE)
189
190       if (3RDPARTY_TBB_DIR AND EXISTS "${3RDPARTY_TBB_DIR}")
191         if (NOT EXISTS "${3RDPARTY_TBB_DIR}/bin/${${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME}/${COMPILER}")
192           if (EXISTS "${3RDPARTY_TBB_DIR}/bin/${${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME}")
193             file (GLOB ${upper_PRODUCT_LIBRARY_NAME}_COMPILER_LIST "${3RDPARTY_TBB_DIR}/bin/${${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME}/*")
194             if (${upper_PRODUCT_LIBRARY_NAME}_COMPILER_LIST)
195               list (GET ${upper_PRODUCT_LIBRARY_NAME}_COMPILER_LIST -1 THE_MOST_FRESH_COMPILER_VERSION)
196               if (THE_MOST_FRESH_COMPILER_VERSION)
197                 get_filename_component (THE_MOST_FRESH_COMPILER_VERSION_NAME "${THE_MOST_FRESH_COMPILER_VERSION}" NAME)
198                 set (PRODUCT_PATH_SUFFIXES bin bin/${${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME}/${THE_MOST_FRESH_COMPILER_VERSION_NAME})
199               endif()
200             endif()
201           endif()
202         else()
203           set (PRODUCT_PATH_SUFFIXES bin bin/${${upper_PRODUCT_LIBRARY_NAME}_ARCH_NAME}/${COMPILER})
204         endif()
205
206         if (3RDPARTY_TBB_DIR AND EXISTS "${3RDPARTY_TBB_DIR}")
207           find_library (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL ${PRODUCT_LIBRARY_NAME}
208                                          PATHS "${3RDPARTY_TBB_DIR}"
209                                          PATH_SUFFIXES ${PRODUCT_PATH_SUFFIXES}
210                                          NO_DEFAULT_PATH)
211         else()
212           find_library (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL ${PRODUCT_LIBRARY_NAME} PATH_SUFFIXES ${PRODUCT_PATH_SUFFIXES})
213         endif()
214
215         if (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL AND EXISTS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL}")
216           get_filename_component (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL}" PATH)
217           set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR}" CACHE PATH "The directory containing ${upper_PRODUCT_LIBRARY_NAME} library" FORCE)
218         else()
219           set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR "" CACHE PATH "The directory containing ${upper_PRODUCT_LIBRARY_NAME} shared library" FORCE)
220
221           set (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL "" CACHE FILEPATH "${upper_PRODUCT_LIBRARY_NAME} shared library" FORCE)
222         endif()
223       endif()
224     endif()
225
226     if (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR OR EXISTS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR}")
227       list (APPEND 3RDPARTY_DLL_DIRS "${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR}")
228     else()
229       list (APPEND 3RDPARTY_NO_DLLS 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL_DIR)
230     endif()
231   endif()
232
233   # install tbb/tbbmalloc
234   if (INSTALL_TBB)
235     OCCT_MAKE_OS_WITH_BITNESS()
236     OCCT_MAKE_COMPILER_SHORT_NAME()
237
238     if (WIN32)
239       if (SINGLE_GENERATOR)
240         install (FILES ${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL} DESTINATION "${INSTALL_DIR_BIN}")
241       else()
242         install (FILES ${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL}
243                  CONFIGURATIONS Release
244                  DESTINATION "${INSTALL_DIR_BIN}")
245         install (FILES ${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL}
246                  CONFIGURATIONS RelWithDebInfo
247                  DESTINATION "${INSTALL_DIR_BIN}i")
248         install (FILES ${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL}
249                  CONFIGURATIONS Debug
250                  DESTINATION "${INSTALL_DIR_BIN}d")
251       endif()
252     else()
253       get_filename_component (PRODUCT_LIBRARY_NAME ${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY} NAME)
254
255       if (SINGLE_GENERATOR)
256         install (FILES ${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY}.2
257                  DESTINATION "${INSTALL_DIR_LIB}"
258                  RENAME ${PRODUCT_LIBRARY_NAME}.2)
259       else()
260         install (FILES ${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY}.2
261                  CONFIGURATIONS Release
262                  DESTINATION "${INSTALL_DIR_LIB}"
263                  RENAME ${PRODUCT_LIBRARY_NAME}.2)
264         install (FILES ${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY}.2
265                  CONFIGURATIONS RelWithDebInfo
266                  DESTINATION "${INSTALL_DIR_LIB}i"
267                  RENAME ${PRODUCT_LIBRARY_NAME}.2)
268         install (FILES ${3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY}.2
269                  CONFIGURATIONS Debug
270                  DESTINATION "${INSTALL_DIR_LIB}d"
271                  RENAME ${PRODUCT_LIBRARY_NAME}.2)
272       endif()
273     endif()
274   endif()
275   mark_as_advanced (3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_LIBRARY 3RDPARTY_${upper_PRODUCT_LIBRARY_NAME}_DLL)
276 endmacro()
277
278 #if (BUILD_SHARED_LIBS)
279   separate_arguments (CSF_TBB)
280   foreach (LIB IN LISTS CSF_TBB)
281     TBB_PRODUCT_SEARCH (${LIB})
282   endforeach()
283
284   if (INSTALL_TBB)
285     set (USED_3RDPARTY_TBB_DIR "")
286   else()
287     # the library directory for using by the executable
288     if (WIN32)
289       set (USED_3RDPARTY_TBB_DIR ${3RDPARTY_TBB_DLL_DIR})
290     else()
291       set (USED_3RDPARTY_TBB_DIR ${3RDPARTY_TBB_LIBRARY_DIR})
292     endif()
293   endif()
294 #endif()