0026600: CMake should disable auto-link for TBB
[occt.git] / adm / cmake / tbb.cmake
1 # tbb
2
3 if (NOT DEFINED INSTALL_TBB)
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 # tbb include directory
13 if (NOT DEFINED 3RDPARTY_TBB_INCLUDE_DIR)
14   set (3RDPARTY_TBB_INCLUDE_DIR "" CACHE FILEPATH "The directory containing headers of the tbb")
15 endif()
16
17 # tbb library file (with absolute path)
18 if (NOT DEFINED 3RDPARTY_TBB_LIBRARY OR NOT 3RDPARTY_TBB_LIBRARY_DIR)
19   set (3RDPARTY_TBB_LIBRARY "" CACHE FILEPATH "tbb library" FORCE)
20 endif()
21
22 # tbb library directory
23 if (NOT DEFINED 3RDPARTY_TBB_LIBRARY_DIR)
24   set (3RDPARTY_TBB_LIBRARY_DIR "" CACHE FILEPATH "The directory containing tbb library")
25 endif()
26
27 # tbb malloc library file (with absolute path)
28 if (NOT DEFINED 3RDPARTY_TBBMALLOC_LIBRARY OR NOT 3RDPARTY_TBBMALLOC_LIBRARY_DIR)
29   set (3RDPARTY_TBBMALLOC_LIBRARY "" CACHE FILEPATH "tbb malloc library" FORCE)
30 endif()
31
32 # tbb malloc library directory
33 if (NOT DEFINED 3RDPARTY_TBBMALLOC_LIBRARY_DIR)
34   set (3RDPARTY_TBBMALLOC_LIBRARY_DIR "" CACHE FILEPATH "The directory containing tbb malloc library")
35 endif()
36
37 # tbb shared library (with absolute path)
38 if (WIN32)
39   if (NOT DEFINED 3RDPARTY_TBB_DLL OR NOT 3RDPARTY_TBB_DLL_DIR)
40     set (3RDPARTY_TBB_DLL "" CACHE FILEPATH "tbb shared library" FORCE)
41   endif()
42 endif()
43
44 # tbb shared library directory
45 if (WIN32 AND NOT DEFINED 3RDPARTY_TBB_DLL_DIR)
46   set (3RDPARTY_TBB_DLL_DIR "" CACHE FILEPATH "The directory containing tbb shared library")
47 endif()
48
49 # tbb malloc shared library (with absolute path)
50 if (WIN32)
51   if (NOT DEFINED 3RDPARTY_TBBMALLOC_DLL OR NOT 3RDPARTY_TBBMALLOC_DLL_DIR)
52     set (3RDPARTY_TBBMALLOC_DLL "" CACHE FILEPATH "tbb malloc shared library" FORCE)
53   endif()
54 endif()
55
56 # tbb malloc shared library directory
57 if (WIN32 AND NOT DEFINED 3RDPARTY_TBBMALLOC_DLL_DIR)
58   set (3RDPARTY_TBBMALLOC_DLL_DIR "" CACHE FILEPATH "The directory containing tbb malloc shared library")
59 endif()
60
61 if (MSVC)
62   add_definitions (-D__TBB_NO_IMPLICIT_LINKAGE)
63   add_definitions (-D__TBBMALLOC_NO_IMPLICIT_LINKAGE)
64 endif()
65
66 # include occt macros. compiler_bitness, os_wiht_bit, compiler
67 OCCT_INCLUDE_CMAKE_FILE ("adm/cmake/occt_macros")
68
69 # search for product directory inside 3RDPARTY_DIR directory
70 if (NOT 3RDPARTY_TBB_DIR AND 3RDPARTY_DIR)
71   FIND_PRODUCT_DIR ("${3RDPARTY_DIR}" "TBB" TBB_DIR_NAME)
72   if (TBB_DIR_NAME)
73     message (STATUS "Info: TBB: ${TBB_DIR_NAME} folder is used")
74     set (3RDPARTY_TBB_DIR "${3RDPARTY_DIR}/${TBB_DIR_NAME}" CACHE PATH "The directory containing tbb" FORCE)
75   endif()
76 endif()
77
78 OCCT_MAKE_COMPILER_BITNESS()
79
80 if (${COMPILER_BITNESS} STREQUAL 32)
81   set (TBB_ARCH_NAME ia32)
82 else()
83   set (TBB_ARCH_NAME intel64)
84 endif()
85
86 # search for include directory in defined 3rdparty directory
87 if (NOT 3RDPARTY_TBB_INCLUDE_DIR OR NOT EXISTS "${3RDPARTY_TBB_INCLUDE_DIR}")
88   set (3RDPARTY_TBB_INCLUDE_DIR "3RDPARTY_TBB_INCLUDE_DIR-NOTFOUND" CACHE FILEPATH "The directory containing the headers of tbb" FORCE)
89   find_path (3RDPARTY_TBB_INCLUDE_DIR tbb/tbb.h PATHS "${3RDPARTY_TBB_DIR}/include" NO_DEFAULT_PATH)
90   find_path (3RDPARTY_TBB_INCLUDE_DIR tbb/tbb.h PATHS "${3RDPARTY_TBB_DIR}/include")
91 endif()
92
93 if (NOT 3RDPARTY_TBB_INCLUDE_DIR OR NOT EXISTS "${3RDPARTY_TBB_INCLUDE_DIR}")
94   set (3RDPARTY_TBB_INCLUDE_DIR "" CACHE FILEPATH "The directory containing the headers of tbb" FORCE)
95 endif()
96
97 OCCT_MAKE_COMPILER_SHORT_NAME()
98
99 # TBB_COMPILER_FOLER
100 #if (WIN32)
101   set (TBB_COMPILER_FOLER ${COMPILER})
102 #else()
103 #  set (TBB_COMPILER_FOLER ${COMPILER})
104 #endif()
105
106 # search for tbb and tbb malloc library in defined 3rdparty directory
107 foreach (LIBRARY_NAME TBB TBBMALLOC)
108   if (NOT WIN32)
109     file (GLOB TBB_SUBDIRS RELATIVE ${3RDPARTY_TBB_DIR}/lib/${TBB_ARCH_NAME}/ ${3RDPARTY_TBB_DIR}/lib/${TBB_ARCH_NAME}/*)
110     list (SORT TBB_SUBDIRS)
111     list (REVERSE TBB_SUBDIRS)
112     list (LENGTH TBB_SUBDIRS TBB_SUBDIRS_LENGTH)
113     set (TBB_SUBDIR_FIRST "")
114     if (${TBB_SUBDIRS_LENGTH})
115       list (GET TBB_SUBDIRS 0 TBB_SUBDIR_FIRST)
116       set (3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR "${3RDPARTY_TBB_DIR}/lib/${TBB_ARCH_NAME}/${TBB_SUBDIR_FIRST}")
117     endif()
118   endif()
119   if (NOT 3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR)
120     set (3RDPARTY_${LIBRARY_NAME}_LIBRARY "" CACHE FILEPATH "${LIBRARY_NAME} library" FORCE)
121   elseif (3RDPARTY_${LIBRARY_NAME}_LIBRARY AND EXISTS "${3RDPARTY_${LIBRARY_NAME}_LIBRARY}")
122     get_filename_component(3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR_TMP "${3RDPARTY_${LIBRARY_NAME}_LIBRARY}" PATH)
123     if (NOT "${3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR}" STREQUAL "${3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR_TMP}")
124       set (3RDPARTY_${LIBRARY_NAME}_LIBRARY "" CACHE FILEPATH "${LIBRARY_NAME} library" FORCE)
125     endif()
126   endif()
127
128   if (NOT 3RDPARTY_${LIBRARY_NAME}_LIBRARY OR NOT EXISTS "${3RDPARTY_${LIBRARY_NAME}_LIBRARY}")
129     set (3RDPARTY_${LIBRARY_NAME}_LIBRARY "3RDPARTY_${LIBRARY_NAME}_LIBRARY-NOTFOUND" CACHE FILEPATH "Path to library of ${LIBRARY_NAME}" FORCE)
130
131     string (TOLOWER "${LIBRARY_NAME}" lower_LIBRARY_NAME)
132     find_library (3RDPARTY_${LIBRARY_NAME}_LIBRARY ${lower_LIBRARY_NAME} PATHS
133                                                                          "${3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR}"
134                                                                          "${3RDPARTY_TBB_DIR}/lib/${TBB_ARCH_NAME}/${TBB_COMPILER_FOLER}"
135                                                                          NO_DEFAULT_PATH)
136
137       # second search if previous one do not find anything
138       find_library (3RDPARTY_${LIBRARY_NAME}_LIBRARY ${LIBRARY_NAME})
139   endif()
140
141   if (NOT 3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR OR NOT EXISTS "${3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR}")
142     get_filename_component(3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR "${3RDPARTY_${LIBRARY_NAME}_LIBRARY}" PATH)
143   endif()
144   set (3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR "${3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR}" CACHE FILEPATH "The directory containing ${LIBRARY_NAME} library" FORCE)
145
146   # search for dll in defined 3rdparty directory (just for win case)
147   if (WIN32)
148     set (CMAKE_FIND_LIBRARY_SUFFIXES ".lib" ".dll")
149
150     if (NOT 3RDPARTY_${LIBRARY_NAME}_DLL_DIR)
151       set (3RDPARTY_${LIBRARY_NAME}_DLL "" CACHE FILEPATH "${LIBRARY_NAME} shared library" FORCE)
152     elseif (3RDPARTY_${LIBRARY_NAME}_DLL AND EXISTS "${3RDPARTY_${LIBRARY_NAME}_DLL}")
153       get_filename_component(3RDPARTY_${LIBRARY_NAME}_DLL_DIR_TMP "${3RDPARTY_${LIBRARY_NAME}_DLL}" PATH)
154       if (NOT "${3RDPARTY_${LIBRARY_NAME}_DLL_DIR}" STREQUAL "${3RDPARTY_${LIBRARY_NAME}_DLL_DIR_TMP}")
155         set (3RDPARTY_${LIBRARY_NAME}_DLL "" CACHE FILEPATH "${LIBRARY_NAME} shared library" FORCE)
156       endif()
157     endif()
158
159     if (NOT 3RDPARTY_${LIBRARY_NAME}_DLL OR NOT EXISTS "${3RDPARTY_${LIBRARY_NAME}_DLL}")
160       set (3RDPARTY_${LIBRARY_NAME}_DLL "3RDPARTY_${LIBRARY_NAME}_DLL-NOTFOUND" CACHE FILEPATH "Path to shared library of ${LIBRARY_NAME}" FORCE)
161
162       find_library (3RDPARTY_${LIBRARY_NAME}_DLL ${LIBRARY_NAME} PATHS
163                                                                  "${3RDPARTY_${LIBRARY_NAME}_DLL_DIR}"
164                                                                  "${3RDPARTY_TBB_DIR}/bin/${TBB_ARCH_NAME}/${TBB_COMPILER_FOLER}"
165                                                                  NO_DEFAULT_PATH)
166
167         # second search if previous one do not find anything
168         find_library (3RDPARTY_${LIBRARY_NAME}_DLL ${LIBRARY_NAME})
169     endif()
170
171     if (NOT 3RDPARTY_${LIBRARY_NAME}_DLL_DIR OR NOT EXISTS "${3RDPARTY_${LIBRARY_NAME}_DLL_DIR}")
172       get_filename_component(3RDPARTY_${LIBRARY_NAME}_DLL_DIR "${3RDPARTY_${LIBRARY_NAME}_DLL}" PATH)
173       set (3RDPARTY_${LIBRARY_NAME}_DLL_DIR "${3RDPARTY_${LIBRARY_NAME}_DLL_DIR}" CACHE FILEPATH "The directory containing ${LIBRARY_NAME} shared library" FORCE)
174     endif()
175   endif() # end dll search
176 endforeach() # end tbb / tbbmalloc
177
178 # include found paths to common variables
179 if (3RDPARTY_TBB_INCLUDE_DIR AND EXISTS "${3RDPARTY_TBB_INCLUDE_DIR}")
180   list (APPEND 3RDPARTY_INCLUDE_DIRS "${3RDPARTY_TBB_INCLUDE_DIR}")
181 else()
182   list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_TBB_INCLUDE_DIR)
183 endif()
184
185 foreach (LIBRARY_NAME TBB TBBMALLOC)
186   if (3RDPARTY_${LIBRARY_NAME}_LIBRARY AND EXISTS "${3RDPARTY_${LIBRARY_NAME}_LIBRARY}")
187     list (APPEND 3RDPARTY_LIBRARY_DIRS "${3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR}")
188   else()
189     list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_${LIBRARY_NAME}_LIBRARY_DIR)
190   endif()
191
192   if (WIN32)
193     if (3RDPARTY_${LIBRARY_NAME}_DLL OR EXISTS "${3RDPARTY_${LIBRARY_NAME}_DLL}")
194       list (APPEND 3RDPARTY_DLL_DIRS "${3RDPARTY_${LIBRARY_NAME}_DLL_DIR}")
195     else()
196       list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_${LIBRARY_NAME}_DLL_DIR)
197     endif()
198   endif()
199 endforeach()
200
201 # install tbb
202 if (INSTALL_TBB)
203   OCCT_MAKE_OS_WITH_BITNESS()
204   OCCT_MAKE_COMPILER_SHORT_NAME()
205
206   if (WIN32)
207     install (FILES ${3RDPARTY_TBB_DLL} ${3RDPARTY_TBBMALLOC_DLL}
208              CONFIGURATIONS Release
209              DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/bin")
210     install (FILES ${3RDPARTY_TBB_DLL} ${3RDPARTY_TBBMALLOC_DLL}
211              CONFIGURATIONS RelWithDebInfo
212              DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/bini")
213     install (FILES ${3RDPARTY_TBB_DLL} ${3RDPARTY_TBBMALLOC_DLL}
214              CONFIGURATIONS Debug
215              DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/bind")
216   else()
217     get_filename_component(TBBLIB ${3RDPARTY_TBB_LIBRARY} NAME)
218     get_filename_component(TBBMALLOCLIB ${3RDPARTY_TBBMALLOC_LIBRARY} NAME)
219
220     install (FILES ${3RDPARTY_TBB_LIBRARY}.2
221              CONFIGURATIONS Release
222              DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/lib"
223              RENAME ${TBBLIB}.2)
224     install (FILES ${3RDPARTY_TBB_LIBRARY}.2
225              CONFIGURATIONS RelWithDebInfo
226              DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/libi"
227              RENAME ${TBBLIB}.2)
228     install (FILES ${3RDPARTY_TBB_LIBRARY}.2
229              CONFIGURATIONS Debug
230              DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/libd"
231              RENAME ${TBBLIB}.2)
232
233     install (FILES ${3RDPARTY_TBBMALLOC_LIBRARY}.2
234              CONFIGURATIONS Release
235              DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/lib"
236              RENAME ${TBBMALLOCLIB}.2)
237     install (FILES ${3RDPARTY_TBBMALLOC_LIBRARY}.2
238              CONFIGURATIONS RelWithDebInfo
239              DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/libi"
240              RENAME ${TBBMALLOCLIB}.2)
241     install (FILES ${3RDPARTY_TBBMALLOC_LIBRARY}.2
242              CONFIGURATIONS Debug
243              DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/libd"
244              RENAME ${TBBMALLOCLIB}.2)
245   endif()
246
247   set (USED_3RDPARTY_TBB_DIR "")
248 else()
249   # the library directory for using by the executable
250   if (WIN32)
251     set (USED_3RDPARTY_TBB_DIR ${3RDPARTY_TBB_DLL_DIR})
252   else()
253     set (USED_3RDPARTY_TBB_DIR ${3RDPARTY_TBB_LIBRARY_DIR})
254   endif()
255 endif()
256
257 mark_as_advanced (3RDPARTY_TBB_LIBRARY 3RDPARTY_TBBMALLOC_LIBRARY 3RDPARTY_TBB_DLL 3RDPARTY_TBBMALLOC_DLL)