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