Update LibIberty to new build system (#601)
[dyninst.git] / cmake / Modules / FindTBB.cmake
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2015 Justus Calvin
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in all
13 # copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22
23 #
24 # FindTBB
25 # -------
26 #
27 # Find TBB include directories and libraries.
28 #
29 # Usage:
30 #
31 # find_package(TBB [major[.minor]] [EXACT] [QUIET] [REQUIRED] [[COMPONENTS]
32 # [components...]] [OPTIONAL_COMPONENTS components...])
33 #
34 # where the allowed components are tbbmalloc and tbb_preview. Users may modify
35 # the behavior of this module with the following variables:
36 #
37 # * TBB_ROOT_DIR          - The base directory the of TBB installation.
38 # * TBB_INCLUDE_DIR       - The directory that contains the TBB headers files.
39 # * TBB_LIBRARY           - The directory that contains the TBB library files.
40 # * TBB_<library>_LIBRARY - The path of the TBB the corresponding TBB library.
41 #   These libraries, if specified, override the corresponding library search
42 #   results, where <library> may be tbb, tbb_debug, tbbmalloc, tbbmalloc_debug,
43 #   tbb_preview, or tbb_preview_debug.
44 # * TBB_USE_DEBUG_BUILD   - The debug version of tbb libraries, if present, will
45 #   be used instead of the release version.
46 #
47 # Users may modify the behavior of this module with the following environment
48 # variables:
49 #
50 # * TBB_INSTALL_DIR
51 # * TBBROOT
52 # * LIBRARY_PATH
53 #
54 # This module will set the following variables:
55 #
56 # * TBB_FOUND             - Set to false, or undefined, if we haven’t found, or
57 #   don’t want to use TBB.
58 # * TBB_<component>_FOUND - If False, optional <component> part of TBB sytem is
59 #   not available.
60 # * TBB_VERSION           - The full version string
61 # * TBB_VERSION_MAJOR     - The major version
62 # * TBB_VERSION_MINOR     - The minor version
63 # * TBB_INTERFACE_VERSION - The interface version number defined in
64 #   tbb/tbb_stddef.h.
65 # * TBB_<library>_LIBRARY_RELEASE - The path of the TBB release version of
66 #   <library>, where <library> may be tbb, tbb_debug, tbbmalloc,
67 #   tbbmalloc_debug, tbb_preview, or tbb_preview_debug.
68 # * TBB_<library>_LIBRARY_DEGUG - The path of the TBB release version of
69 #   <library>, where <library> may be tbb, tbb_debug, tbbmalloc,
70 #   tbbmalloc_debug, tbb_preview, or tbb_preview_debug.
71 #
72 # The following varibles should be used to build and link with TBB:
73 #
74 # * TBB_INCLUDE_DIRS        - The include directory for TBB.
75 # * TBB_LIBRARY_DIRS        - The library directory for TBB.
76 # * TBB_LIBRARIES           - The libraries to link against to use TBB.
77 # * TBB_LIBRARIES_RELEASE   - The release libraries to link against to use TBB.
78 # * TBB_LIBRARIES_DEBUG     - The debug libraries to link against to use TBB.
79 # * TBB_DEFINITIONS         - Definitions to use when compiling code that uses
80 #   TBB.
81 # * TBB_DEFINITIONS_RELEASE - Definitions to use when compiling release code
82 #   that uses TBB.
83 # * TBB_DEFINITIONS_DEBUG   - Definitions to use when compiling debug code that
84 #   uses TBB.
85 #
86 # This module will also create the "tbb" target that may be used when building
87 # executables and libraries.
88
89 if(TBB_FOUND)
90   return()
91 endif()
92
93 include(FindPackageHandleStandardArgs)
94
95 #
96 # Check the build type
97 #
98 if(NOT DEFINED TBB_USE_DEBUG_BUILD)
99   if(CMAKE_BUILD_TYPE MATCHES "(Debug|DEBUG|debug)")
100     set(TBB_BUILD_TYPE DEBUG)
101   else()
102     set(TBB_BUILD_TYPE RELEASE)
103   endif()
104 elseif(TBB_USE_DEBUG_BUILD)
105   set(TBB_BUILD_TYPE DEBUG)
106 else()
107   set(TBB_BUILD_TYPE RELEASE)
108 endif()
109
110 #
111 # Set the TBB search directories
112 #
113
114 # Define search paths based on user input and environment variables
115 set(TBB_SEARCH_DIR ${TBB_ROOT_DIR} $ENV{TBB_INSTALL_DIR} $ENV{TBBROOT})
116
117 # Define the search directories based on the current platform
118 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
119   set(TBB_DEFAULT_SEARCH_DIR "C:/Program Files/Intel/TBB"
120       "C:/Program Files (x86)/Intel/TBB")
121
122   # Set the target architecture
123   if(CMAKE_SIZEOF_VOID_P EQUAL 8)
124     set(TBB_ARCHITECTURE "intel64")
125   else()
126     set(TBB_ARCHITECTURE "ia32")
127   endif()
128
129   # Set the TBB search library path search suffix based on the version of VC
130   if(WINDOWS_STORE)
131     set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc11_ui")
132   elseif(MSVC14)
133     set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc14")
134   elseif(MSVC12)
135     set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc12")
136   elseif(MSVC11)
137     set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc11")
138   elseif(MSVC10)
139     set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc10")
140   endif()
141
142   # Add the library path search suffix for the VC independent version of TBB
143   list(APPEND TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc_mt")
144
145 elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
146   # OS X
147   set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
148
149   # TODO: Check to see which C++ library is being used by the compiler.
150   if(NOT ${CMAKE_SYSTEM_VERSION} VERSION_LESS 13.0)
151     # The default C++ library on OS X 10.9 and later is libc++
152     set(TBB_LIB_PATH_SUFFIX "lib/libc++" "lib")
153   else()
154     set(TBB_LIB_PATH_SUFFIX "lib")
155   endif()
156 elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
157   # Linux
158   set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
159
160   # TODO: Check compiler version to see the suffix should be <arch>/gcc4.1 or
161   # <arch>/gcc4.1. For now, assume that the compiler is more recent than gcc
162   # 4.4.x or later.
163   if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
164     set(TBB_LIB_PATH_SUFFIX "lib/intel64/gcc4.4")
165   elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
166     set(TBB_LIB_PATH_SUFFIX "lib/ia32/gcc4.4")
167   endif()
168 endif()
169
170 #
171 # Find the TBB include dir
172 #
173 find_path(TBB_INCLUDE_DIRS tbb/tbb.h
174           HINTS ${TBB_INCLUDE_DIRS} ${TBB_SEARCH_DIR}
175           PATHS ${TBB_DEFAULT_SEARCH_DIR}
176           PATH_SUFFIXES include)
177
178 #
179 # Set version strings
180 #
181 if(TBB_INCLUDE_DIRS)
182   file(READ "${TBB_INCLUDE_DIRS}/tbb/tbb_stddef.h" _tbb_version_file)
183   string(REGEX
184          REPLACE ".*#define TBB_VERSION_MAJOR ([0-9]+).*"
185                  "\\1"
186                  TBB_VERSION_MAJOR
187                  "${_tbb_version_file}")
188   string(REGEX
189          REPLACE ".*#define TBB_VERSION_MINOR ([0-9]+).*"
190                  "\\1"
191                  TBB_VERSION_MINOR
192                  "${_tbb_version_file}")
193   string(REGEX
194          REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*"
195                  "\\1"
196                  TBB_INTERFACE_VERSION
197                  "${_tbb_version_file}")
198   set(TBB_VERSION "${TBB_VERSION_MAJOR}.${TBB_VERSION_MINOR}")
199 endif()
200
201 #
202 # Find TBB components
203 #
204 if(TBB_VERSION VERSION_LESS 4.3)
205   set(TBB_SEARCH_COMPOMPONENTS tbb_preview tbbmalloc tbb)
206 else()
207   set(TBB_SEARCH_COMPOMPONENTS tbb_preview tbbmalloc_proxy tbbmalloc tbb)
208 endif()
209
210 set(TBB_LIBRARY_DIRS)
211
212 # Find each component
213 foreach(_comp ${TBB_SEARCH_COMPOMPONENTS})
214   message(STATUS "Searching for ${_comp}...")
215   message(STATUS "Hints: ${TBB_LIBRARY} ${TBB_SEARCH_DIR}")
216   if(";${TBB_FIND_COMPONENTS};tbb;" MATCHES ";${_comp};")
217
218     # Search for the libraries
219     find_library(TBB_${_comp}_LIBRARY_RELEASE ${_comp}
220                  HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
221                  PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
222                  PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX} lib_release)
223
224     find_library(TBB_${_comp}_LIBRARY_DEBUG ${_comp}_debug
225                  HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
226                  PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
227                  PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX} lib_debug)
228
229     if(TBB_${_comp}_LIBRARY_DEBUG)
230       list(APPEND TBB_LIBRARIES_DEBUG "${TBB_${_comp}_LIBRARY_DEBUG}")
231       message(STATUS "Found ${TBB_${_comp}_LIBRARY_DEBUG}")
232     endif()
233     if(TBB_${_comp}_LIBRARY_RELEASE)
234       list(APPEND TBB_LIBRARIES_RELEASE "${TBB_${_comp}_LIBRARY_RELEASE}")
235       message(STATUS "Found ${TBB_${_comp}_LIBRARY_RELEASE}")
236     endif()
237     if(TBB_${_comp}_LIBRARY_${TBB_BUILD_TYPE} AND NOT TBB_${_comp}_LIBRARY)
238       set(TBB_${_comp}_LIBRARY "${TBB_${_comp}_LIBRARY_${TBB_BUILD_TYPE}}")
239     endif()
240
241     if(TBB_${_comp}_LIBRARY AND EXISTS "${TBB_${_comp}_LIBRARY}")
242       set(TBB_${_comp}_FOUND TRUE)
243     else()
244       set(TBB_${_comp}_FOUND FALSE)
245     endif()
246
247     # Mark internal variables as advanced
248     mark_as_advanced(TBB_${_comp}_LIBRARY_RELEASE)
249     mark_as_advanced(TBB_${_comp}_LIBRARY_DEBUG)
250     mark_as_advanced(TBB_${_comp}_LIBRARY)
251
252     # Save the directory names for each library component
253     if(TBB_USE_DEBUG_BUILD)
254       get_filename_component(_dir ${TBB_${_comp}_LIBRARY_DEBUG} DIRECTORY)
255     else()
256       get_filename_component(_dir ${TBB_${_comp}_LIBRARY_RELEASE} DIRECTORY)
257     endif()
258     list(APPEND TBB_LIBRARY_DIRS ${_dir})
259   endif()
260 endforeach()
261
262 #
263 # Set compile flags and libraries
264 #
265 set(TBB_DEFINITIONS_RELEASE "")
266 set(TBB_DEFINITIONS_DEBUG "-DTBB_USE_DEBUG=1")
267
268 if(TBB_LIBRARIES_${TBB_BUILD_TYPE})
269   set(TBB_DEFINITIONS "${TBB_DEFINITIONS_${TBB_BUILD_TYPE}}")
270   set(TBB_LIBRARIES "${TBB_LIBRARIES_${TBB_BUILD_TYPE}}")
271 elseif(TBB_LIBRARIES_RELEASE)
272   set(TBB_DEFINITIONS "${TBB_DEFINITIONS_RELEASE}")
273   set(TBB_LIBRARIES "${TBB_LIBRARIES_RELEASE}")
274 elseif(TBB_LIBRARIES_DEBUG)
275   set(TBB_DEFINITIONS "${TBB_DEFINITIONS_DEBUG}")
276   set(TBB_LIBRARIES "${TBB_LIBRARIES_DEBUG}")
277 endif()
278
279 find_package_handle_standard_args(TBB
280                                   REQUIRED_VARS
281                                   TBB_INCLUDE_DIRS
282                                   TBB_LIBRARIES
283                                   HANDLE_COMPONENTS
284                                   VERSION_VAR
285                                   TBB_VERSION)
286
287 #
288 # Create targets
289 #
290 if(NOT CMAKE_VERSION VERSION_LESS 3.0 AND TBB_FOUND)
291   add_library(TBB SHARED IMPORTED)
292   set_target_properties(TBB
293                         PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
294                                    ${TBB_INCLUDE_DIRS}
295                                    IMPORTED_LOCATION
296                                    ${TBB_LIBRARIES})
297   if(TBB_LIBRARIES_RELEASE AND TBB_LIBRARIES_DEBUG)
298     set_target_properties(TBB
299                           PROPERTIES INTERFACE_COMPILE_DEFINITIONS
300                                      "$<$<CONFIG:Debug>:TBB_USE_DEBUG=1>"
301                                      IMPORTED_LOCATION_DEBUG
302                                      ${TBB_LIBRARIES_DEBUG}
303                                      IMPORTED_LOCATION_RELWITHDEBINFO
304                                      ${TBB_LIBRARIES_DEBUG}
305                                      IMPORTED_LOCATION_RELEASE
306                                      ${TBB_LIBRARIES_RELEASE}
307                                      IMPORTED_LOCATION_MINSIZEREL
308                                      ${TBB_LIBRARIES_RELEASE})
309   elseif(TBB_LIBRARIES_RELEASE)
310     set_target_properties(TBB
311                           PROPERTIES IMPORTED_LOCATION ${TBB_LIBRARIES_RELEASE})
312   else()
313     set_target_properties(TBB
314                           PROPERTIES IMPORTED_LOCATION ${TBB_LIBRARIES_DEBUG})
315   endif()
316 endif()
317
318 mark_as_advanced(TBB_INCLUDE_DIRS TBB_LIBRARIES TBB_LIBRARY_DIRS)
319
320 unset(TBB_ARCHITECTURE)
321 unset(TBB_BUILD_TYPE)
322 unset(TBB_LIB_PATH_SUFFIX)
323 unset(TBB_DEFAULT_SEARCH_DIR)