Fix doc bugs in LibIberty
[dyninst.git] / cmake / Modules / FindTBB.cmake
1 #======================================================================================================
2 # FindTBB.cmake
3 #
4 # Find TBB include directories and libraries.
5 #
6 #               ----------------------------------------
7 #
8 # Use this module by invoking find_package with the form::
9 #
10 #  find_package(TBB
11 #    [major[.minor]] [EXACT] [QUIET]     # Minimum or EXACT version e.g. 2018.6
12 #    [REQUIRED]                          # Fail with error if TBB is not found
13 #    [[COMPONENTS] [components...]]      # Required components
14 #    [OPTIONAL_COMPONENTS components...] # Optional components
15 #   )
16 #
17 # This module reads hints about search locations from variables::
18 #
19 #   TBB_ROOT_DIR          - The base directory the of TBB installation.
20 #   TBB_INCLUDE_DIR       - The directory that contains the TBB headers files.
21 #   TBB_LIBRARY           - The directory that contains the TBB library files.
22 #   TBB_<library>_LIBRARY - The path of the TBB the corresponding TBB library.
23 #                           These libraries override the corresponding library search results.
24 #   TBB_USE_DEBUG_BUILD   - Use the debug version of tbb libraries
25 #
26 # Environment variable aliases for TBB_ROOT_DIR:
27 #
28 #   TBB_INSTALL_DIR
29 #   TBBROOT
30 #   LIBRARY_PATH
31 #
32 # This module will set the following variables:
33 #
34 #   TBB_FOUND                     - If false, or undefined, TBB not found, or don’t want to use TBB.
35 #   TBB_<component>_FOUND         - If False, optional <component> part of TBB sytem is not available.
36 #   TBB_VERSION                   - The full version string
37 #   TBB_VERSION_MAJOR             - The major version
38 #   TBB_VERSION_MINOR             - The minor version
39 #   TBB_INTERFACE_VERSION         - The interface version number defined in tbb/tbb_stddef.h.
40 #   TBB_<library>_LIBRARY_RELEASE - The path of the TBB release version of <library>.
41 #   TBB_<library>_LIBRARY_DEBUG   - The path of the TBB debug version of <library>.
42 #
43 # The following varibles should be used to build and link with TBB:
44 #
45 #   TBB_INCLUDE_DIRS        - The include directory for TBB.
46 #   TBB_LIBRARY_DIRS        - The library directory for TBB.
47 #   TBB_LIBRARIES           - The libraries to link against to use TBB.
48 #   TBB_LIBRARIES_RELEASE   - The release libraries to link against to use TBB.
49 #   TBB_LIBRARIES_DEBUG     - The debug libraries to link against to use TBB.
50 #   TBB_DEFINITIONS         - Definitions to use when compiling code that uses TBB.
51 #   TBB_DEFINITIONS_RELEASE - Definitions to use when compiling release code that uses TBB.
52 #   TBB_DEFINITIONS_DEBUG   - Definitions to use when compiling debug code that uses TBB.
53 #
54 # This module will also create the "TBB" target that may be used when building
55 # executables and libraries.
56 #
57 # Based on the version by Justus Calvin - Copyright (c) 2015
58 #
59 #======================================================================================================
60
61 if(TBB_FOUND)
62   return()
63 endif()
64
65 include(FindPackageHandleStandardArgs)
66
67 #
68 # Check the build type
69 #
70 if(NOT DEFINED TBB_USE_DEBUG_BUILD)
71   if(CMAKE_BUILD_TYPE MATCHES "(Debug|DEBUG|debug)")
72     set(TBB_BUILD_TYPE DEBUG)
73   else()
74     set(TBB_BUILD_TYPE RELEASE)
75   endif()
76 elseif(TBB_USE_DEBUG_BUILD)
77   set(TBB_BUILD_TYPE DEBUG)
78 else()
79   set(TBB_BUILD_TYPE RELEASE)
80 endif()
81
82 #
83 # Set the TBB search directories
84 #
85
86 # Define search paths based on user input and environment variables
87 set(TBB_SEARCH_DIR ${TBB_ROOT_DIR} $ENV{TBB_INSTALL_DIR} $ENV{TBBROOT})
88
89 # Define the search directories based on the current platform
90 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
91   set(TBB_DEFAULT_SEARCH_DIR "C:/Program Files/Intel/TBB"
92       "C:/Program Files (x86)/Intel/TBB")
93
94   # Set the target architecture
95   if(CMAKE_SIZEOF_VOID_P EQUAL 8)
96     set(TBB_ARCHITECTURE "intel64")
97   else()
98     set(TBB_ARCHITECTURE "ia32")
99   endif()
100
101   # Set the TBB search library path search suffix based on the version of VC
102   if(WINDOWS_STORE)
103     set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc11_ui")
104   elseif(MSVC14)
105     set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc14")
106   elseif(MSVC12)
107     set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc12")
108   elseif(MSVC11)
109     set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc11")
110   elseif(MSVC10)
111     set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc10")
112   endif()
113
114   # Add the library path search suffix for the VC independent version of TBB
115   list(APPEND TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc_mt")
116
117 elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
118   # OS X
119   set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
120
121   # TODO: Check to see which C++ library is being used by the compiler.
122   if(NOT ${CMAKE_SYSTEM_VERSION} VERSION_LESS 13.0)
123     # The default C++ library on OS X 10.9 and later is libc++
124     set(TBB_LIB_PATH_SUFFIX "lib/libc++" "lib")
125   else()
126     set(TBB_LIB_PATH_SUFFIX "lib")
127   endif()
128 elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
129   # Linux
130   set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
131
132   # TODO: Check compiler version to see the suffix should be <arch>/gcc4.1 or
133   # <arch>/gcc4.1. For now, assume that the compiler is more recent than gcc
134   # 4.4.x or later.
135   if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
136     set(TBB_LIB_PATH_SUFFIX "lib/intel64/gcc4.4")
137   elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
138     set(TBB_LIB_PATH_SUFFIX "lib/ia32/gcc4.4")
139   endif()
140 endif()
141
142 #
143 # Find the TBB include dir
144 #
145 find_path(TBB_INCLUDE_DIRS tbb/tbb.h
146           HINTS ${TBB_INCLUDE_DIRS} ${TBB_SEARCH_DIR}
147           PATHS ${TBB_DEFAULT_SEARCH_DIR}
148           PATH_SUFFIXES include)
149
150 #
151 # Set version strings
152 #
153 if(TBB_INCLUDE_DIRS)
154   file(READ "${TBB_INCLUDE_DIRS}/tbb/tbb_stddef.h" _tbb_version_file)
155   string(REGEX
156          REPLACE ".*#define TBB_VERSION_MAJOR ([0-9]+).*"
157                  "\\1"
158                  TBB_VERSION_MAJOR
159                  "${_tbb_version_file}")
160   string(REGEX
161          REPLACE ".*#define TBB_VERSION_MINOR ([0-9]+).*"
162                  "\\1"
163                  TBB_VERSION_MINOR
164                  "${_tbb_version_file}")
165   string(REGEX
166          REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*"
167                  "\\1"
168                  TBB_INTERFACE_VERSION
169                  "${_tbb_version_file}")
170
171   # The TBB_VERSION_MINOR isn't necessarily changed for minor releases
172   # Hence, we need to read the engineering versioning in TBB_INTERFACE_VERSION
173   # to get the minor version correct
174   if("${TBB_VERSION_MINOR}" STREQUAL "0")
175           math(EXPR _tbb_iface_major_ver "${TBB_INTERFACE_VERSION} / 100")
176           math(EXPR TBB_VERSION_MINOR "${TBB_INTERFACE_VERSION} - ${_tbb_iface_major_ver} * 100")
177   endif()
178   set(TBB_VERSION "${TBB_VERSION_MAJOR}.${TBB_VERSION_MINOR}")
179 endif()
180
181 #
182 # Find TBB components
183 #
184 if(TBB_VERSION VERSION_LESS 4.3)
185   set(TBB_SEARCH_COMPOMPONENTS tbb_preview tbbmalloc tbb)
186 else()
187   set(TBB_SEARCH_COMPOMPONENTS tbb_preview tbbmalloc_proxy tbbmalloc tbb)
188 endif()
189
190 set(TBB_LIBRARY_DIRS)
191
192 # Find each component
193 foreach(_comp ${TBB_SEARCH_COMPOMPONENTS})
194   message(STATUS "Searching for ${_comp}...")
195   message(STATUS "Hints: ${TBB_LIBRARY} ${TBB_SEARCH_DIR}")
196   if(";${TBB_FIND_COMPONENTS};tbb;" MATCHES ";${_comp};")
197
198     # Search for the libraries
199     find_library(TBB_${_comp}_LIBRARY_RELEASE ${_comp}
200                  HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
201                  PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
202                  PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX} lib_release)
203
204     find_library(TBB_${_comp}_LIBRARY_DEBUG ${_comp}_debug
205                  HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
206                  PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
207                  PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX} lib_debug)
208
209     if(TBB_${_comp}_LIBRARY_DEBUG)
210       list(APPEND TBB_LIBRARIES_DEBUG "${TBB_${_comp}_LIBRARY_DEBUG}")
211       message(STATUS "Found ${TBB_${_comp}_LIBRARY_DEBUG}")
212     endif()
213     if(TBB_${_comp}_LIBRARY_RELEASE)
214       list(APPEND TBB_LIBRARIES_RELEASE "${TBB_${_comp}_LIBRARY_RELEASE}")
215       message(STATUS "Found ${TBB_${_comp}_LIBRARY_RELEASE}")
216     endif()
217     if(TBB_${_comp}_LIBRARY_${TBB_BUILD_TYPE} AND NOT TBB_${_comp}_LIBRARY)
218       set(TBB_${_comp}_LIBRARY "${TBB_${_comp}_LIBRARY_${TBB_BUILD_TYPE}}")
219     endif()
220
221     if(TBB_${_comp}_LIBRARY AND EXISTS "${TBB_${_comp}_LIBRARY}")
222       set(TBB_${_comp}_FOUND TRUE)
223     else()
224       set(TBB_${_comp}_FOUND FALSE)
225     endif()
226
227     # Mark internal variables as advanced
228     mark_as_advanced(TBB_${_comp}_LIBRARY_RELEASE)
229     mark_as_advanced(TBB_${_comp}_LIBRARY_DEBUG)
230     mark_as_advanced(TBB_${_comp}_LIBRARY)
231
232     # Save the directory names for each library component
233     if(TBB_USE_DEBUG_BUILD)
234       get_filename_component(_dir ${TBB_${_comp}_LIBRARY_DEBUG} DIRECTORY)
235     else()
236       get_filename_component(_dir ${TBB_${_comp}_LIBRARY_RELEASE} DIRECTORY)
237     endif()
238     list(APPEND TBB_LIBRARY_DIRS ${_dir})
239   endif()
240 endforeach()
241
242 #
243 # Set compile flags and libraries
244 #
245 set(TBB_DEFINITIONS_RELEASE "")
246 set(TBB_DEFINITIONS_DEBUG "-DTBB_USE_DEBUG=1")
247
248 if(TBB_LIBRARIES_${TBB_BUILD_TYPE})
249   set(TBB_DEFINITIONS "${TBB_DEFINITIONS_${TBB_BUILD_TYPE}}")
250   set(TBB_LIBRARIES "${TBB_LIBRARIES_${TBB_BUILD_TYPE}}")
251 elseif(TBB_LIBRARIES_RELEASE)
252   set(TBB_DEFINITIONS "${TBB_DEFINITIONS_RELEASE}")
253   set(TBB_LIBRARIES "${TBB_LIBRARIES_RELEASE}")
254 elseif(TBB_LIBRARIES_DEBUG)
255   set(TBB_DEFINITIONS "${TBB_DEFINITIONS_DEBUG}")
256   set(TBB_LIBRARIES "${TBB_LIBRARIES_DEBUG}")
257 endif()
258
259 find_package_handle_standard_args(TBB
260                                   REQUIRED_VARS
261                                   TBB_INCLUDE_DIRS
262                                   TBB_LIBRARIES
263                                   HANDLE_COMPONENTS
264                                   VERSION_VAR
265                                   TBB_VERSION)
266
267 #
268 # Create targets
269 #
270 if(NOT CMAKE_VERSION VERSION_LESS 3.0 AND TBB_FOUND)
271   add_library(TBB SHARED IMPORTED)
272   set_target_properties(TBB
273                         PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
274                                    ${TBB_INCLUDE_DIRS}
275                                    IMPORTED_LOCATION
276                                    ${TBB_LIBRARIES})
277   if(TBB_LIBRARIES_RELEASE AND TBB_LIBRARIES_DEBUG)
278     set_target_properties(TBB
279                           PROPERTIES INTERFACE_COMPILE_DEFINITIONS
280                                      "$<$<CONFIG:Debug>:TBB_USE_DEBUG=1>"
281                                      IMPORTED_LOCATION_DEBUG
282                                      ${TBB_LIBRARIES_DEBUG}
283                                      IMPORTED_LOCATION_RELWITHDEBINFO
284                                      ${TBB_LIBRARIES_DEBUG}
285                                      IMPORTED_LOCATION_RELEASE
286                                      ${TBB_LIBRARIES_RELEASE}
287                                      IMPORTED_LOCATION_MINSIZEREL
288                                      ${TBB_LIBRARIES_RELEASE})
289   elseif(TBB_LIBRARIES_RELEASE)
290     set_target_properties(TBB
291                           PROPERTIES IMPORTED_LOCATION ${TBB_LIBRARIES_RELEASE})
292   else()
293     set_target_properties(TBB
294                           PROPERTIES IMPORTED_LOCATION ${TBB_LIBRARIES_DEBUG})
295   endif()
296 endif()
297
298 mark_as_advanced(TBB_INCLUDE_DIRS TBB_LIBRARIES TBB_LIBRARY_DIRS)
299
300 unset(TBB_ARCHITECTURE)
301 unset(TBB_BUILD_TYPE)
302 unset(TBB_LIB_PATH_SUFFIX)
303 unset(TBB_DEFAULT_SEARCH_DIR)