1# Locate Intel Threading Building Blocks include paths and libraries
2# FindTBB.cmake can be found at https://code.google.com/p/findtbb/
3# Written by Hannes Hofmann <hannes.hofmann _at_ informatik.uni-erlangen.de>
4# Improvements by Gino van den Bergen <gino _at_ dtecta.com>,
5#   Florian Uhlig <F.Uhlig _at_ gsi.de>,
6#   Jiri Marsik <jiri.marsik89 _at_ gmail.com>
7
8# The MIT License
9#
10# Copyright (c) 2011 Hannes Hofmann
11#
12# Permission is hereby granted, free of charge, to any person obtaining a copy
13# of this software and associated documentation files (the "Software"), to deal
14# in the Software without restriction, including without limitation the rights
15# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16# copies of the Software, and to permit persons to whom the Software is
17# furnished to do so, subject to the following conditions:
18#
19# The above copyright notice and this permission notice shall be included in
20# all copies or substantial portions of the Software.
21#
22# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28# THE SOFTWARE.
29
30# GvdB: This module uses the environment variable TBB_ARCH_PLATFORM which defines architecture and compiler.
31#   e.g. "ia32/vc8" or "em64t/cc4.1.0_libc2.4_kernel2.6.16.21"
32#   TBB_ARCH_PLATFORM is set by the build script tbbvars[.bat|.sh|.csh], which can be found
33#   in the TBB installation directory (TBB_INSTALL_DIR).
34#
35# GvdB: Mac OS X distribution places libraries directly in lib directory.
36#
37# For backwards compatibility, you may explicitely set the CMake variables TBB_ARCHITECTURE and TBB_COMPILER.
38# TBB_ARCHITECTURE [ ia32 | em64t | itanium ]
39#   which architecture to use
40# TBB_COMPILER e.g. vc9 or cc3.2.3_libc2.3.2_kernel2.4.21 or cc4.0.1_os10.4.9
41#   which compiler to use (detected automatically on Windows)
42
43# This module respects
44# TBB_INSTALL_DIR or $ENV{TBB21_INSTALL_DIR} or $ENV{TBB_INSTALL_DIR}
45
46# This module defines
47# TBB_INCLUDE_DIRS, where to find task_scheduler_init.h, etc.
48# TBB_LIBRARY_DIRS, where to find libtbb, libtbbmalloc
49# TBB_DEBUG_LIBRARY_DIRS, where to find libtbb_debug, libtbbmalloc_debug
50# TBB_INSTALL_DIR, the base TBB install directory
51# TBB_LIBRARIES, the libraries to link against to use TBB.
52# TBB_DEBUG_LIBRARIES, the libraries to link against to use TBB with debug symbols.
53# TBB_FOUND, If false, don't try to use TBB.
54# TBB_INTERFACE_VERSION, as defined in tbb/tbb_stddef.h
55
56
57if (WIN32)
58    # has em64t/vc8 em64t/vc9
59    # has ia32/vc7.1 ia32/vc8 ia32/vc9
60    set(_TBB_DEFAULT_INSTALL_DIR "C:/Program Files/Intel/TBB" "C:/Program Files (x86)/Intel/TBB")
61    set(_TBB_LIB_NAME "tbb")
62    set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc")
63    set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug")
64    set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug")
65    if (MSVC71)
66        set (_TBB_COMPILER "vc7.1")
67    endif()
68    if (MSVC80)
69        set(_TBB_COMPILER "vc8")
70    endif()
71    if (MSVC90)
72        set(_TBB_COMPILER "vc9")
73    endif()
74    if(MSVC10)
75        set(_TBB_COMPILER "vc10")
76    endif()
77    # Todo: add other Windows compilers such as ICL.
78    set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE})
79endif ()
80
81if (UNIX)
82    if (APPLE)
83        # MAC
84        set(_TBB_DEFAULT_INSTALL_DIR "/Library/Frameworks/Intel_TBB.framework/Versions")
85        # libs: libtbb.dylib, libtbbmalloc.dylib, *_debug
86        set(_TBB_LIB_NAME "tbb")
87        set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc")
88        set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug")
89        set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug")
90        # default flavor on apple: ia32/cc4.0.1_os10.4.9
91        # Jiri: There is no reason to presume there is only one flavor and
92        #       that user's setting of variables should be ignored.
93        if(NOT TBB_COMPILER)
94            set(_TBB_COMPILER "cc4.0.1_os10.4.9")
95        else ()
96            set(_TBB_COMPILER ${TBB_COMPILER})
97        endif()
98        if(NOT TBB_ARCHITECTURE)
99            set(_TBB_ARCHITECTURE "ia32")
100        else()
101            set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE})
102        endif()
103    else ()
104        # LINUX
105        set(_TBB_DEFAULT_INSTALL_DIR "/opt/intel/tbb" "/usr/local/include" "/usr/include")
106        set(_TBB_LIB_NAME "tbb")
107        set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc")
108        set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug")
109        set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug")
110        # has em64t/cc3.2.3_libc2.3.2_kernel2.4.21 em64t/cc3.3.3_libc2.3.3_kernel2.6.5 em64t/cc3.4.3_libc2.3.4_kernel2.6.9 em64t/cc4.1.0_libc2.4_kernel2.6.16.21
111        # has ia32/*
112        # has itanium/*
113        set(_TBB_COMPILER ${TBB_COMPILER})
114        set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE})
115    endif ()
116endif ()
117
118if (CMAKE_SYSTEM MATCHES "SunOS.*")
119# SUN
120# not yet supported
121# has em64t/cc3.4.3_kernel5.10
122# has ia32/*
123endif ()
124
125
126#-- Clear the public variables
127set (TBB_FOUND "NO")
128
129
130#-- Find TBB install dir and set ${_TBB_INSTALL_DIR} and cached ${TBB_INSTALL_DIR}
131# first: use CMake variable TBB_INSTALL_DIR
132if (TBB_INSTALL_DIR)
133    set (_TBB_INSTALL_DIR ${TBB_INSTALL_DIR})
134endif ()
135# second: use environment variable
136if (NOT _TBB_INSTALL_DIR)
137    if (NOT "$ENV{TBB_INSTALL_DIR}" STREQUAL "")
138        set (_TBB_INSTALL_DIR $ENV{TBB_INSTALL_DIR})
139    endif ()
140    # Intel recommends setting TBB21_INSTALL_DIR
141    if (NOT "$ENV{TBB21_INSTALL_DIR}" STREQUAL "")
142        set (_TBB_INSTALL_DIR $ENV{TBB21_INSTALL_DIR})
143    endif ()
144    if (NOT "$ENV{TBB22_INSTALL_DIR}" STREQUAL "")
145        set (_TBB_INSTALL_DIR $ENV{TBB22_INSTALL_DIR})
146    endif ()
147    if (NOT "$ENV{TBB30_INSTALL_DIR}" STREQUAL "")
148        set (_TBB_INSTALL_DIR $ENV{TBB30_INSTALL_DIR})
149    endif ()
150endif ()
151# third: try to find path automatically
152if (NOT _TBB_INSTALL_DIR)
153    if (_TBB_DEFAULT_INSTALL_DIR)
154        set (_TBB_INSTALL_DIR ${_TBB_DEFAULT_INSTALL_DIR})
155    endif ()
156endif ()
157# sanity check
158if (NOT _TBB_INSTALL_DIR)
159    message ("ERROR: Unable to find Intel TBB install directory. ${_TBB_INSTALL_DIR}")
160else ()
161# finally: set the cached CMake variable TBB_INSTALL_DIR
162if (NOT TBB_INSTALL_DIR)
163    set (TBB_INSTALL_DIR ${_TBB_INSTALL_DIR} CACHE PATH "Intel TBB install directory")
164    mark_as_advanced(TBB_INSTALL_DIR)
165endif ()
166
167
168#-- A macro to rewrite the paths of the library. This is necessary, because
169#   find_library() always found the em64t/vc9 version of the TBB libs
170macro(TBB_CORRECT_LIB_DIR var_name)
171#    if (NOT "${_TBB_ARCHITECTURE}" STREQUAL "em64t")
172        string(REPLACE em64t "${_TBB_ARCHITECTURE}" ${var_name} ${${var_name}})
173#    endif ()
174    string(REPLACE ia32 "${_TBB_ARCHITECTURE}" ${var_name} ${${var_name}})
175    string(REPLACE vc7.1 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
176    string(REPLACE vc8 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
177    string(REPLACE vc9 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
178    string(REPLACE vc10 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
179endmacro()
180
181
182#-- Look for include directory and set ${TBB_INCLUDE_DIR}
183set (TBB_INC_SEARCH_DIR ${_TBB_INSTALL_DIR}/include)
184# Jiri: tbbvars now sets the CPATH environment variable to the directory
185#       containing the headers.
186find_path(TBB_INCLUDE_DIR
187    tbb/task_scheduler_init.h
188    PATHS ${TBB_INC_SEARCH_DIR} ENV CPATH
189)
190mark_as_advanced(TBB_INCLUDE_DIR)
191
192
193#-- Look for libraries
194# GvdB: $ENV{TBB_ARCH_PLATFORM} is set by the build script tbbvars[.bat|.sh|.csh]
195if (NOT $ENV{TBB_ARCH_PLATFORM} STREQUAL "")
196    set (_TBB_LIBRARY_DIR
197         ${_TBB_INSTALL_DIR}/lib/$ENV{TBB_ARCH_PLATFORM}
198         ${_TBB_INSTALL_DIR}/$ENV{TBB_ARCH_PLATFORM}/lib
199        )
200endif ()
201# Jiri: This block isn't mutually exclusive with the previous one
202#       (hence no else), instead I test if the user really specified
203#       the variables in question.
204if ((NOT ${TBB_ARCHITECTURE} STREQUAL "") AND (NOT ${TBB_COMPILER} STREQUAL ""))
205    # HH: deprecated
206    message(STATUS "[Warning] FindTBB.cmake: The use of TBB_ARCHITECTURE and TBB_COMPILER is deprecated and may not be supported in future versions. Please set \$ENV{TBB_ARCH_PLATFORM} (using tbbvars.[bat|csh|sh]).")
207    # Jiri: It doesn't hurt to look in more places, so I store the hints from
208    #       ENV{TBB_ARCH_PLATFORM} and the TBB_ARCHITECTURE and TBB_COMPILER
209    #       variables and search them both.
210    set (_TBB_LIBRARY_DIR "${_TBB_INSTALL_DIR}/${_TBB_ARCHITECTURE}/${_TBB_COMPILER}/lib" ${_TBB_LIBRARY_DIR})
211endif ()
212
213# GvdB: Mac OS X distribution places libraries directly in lib directory.
214list(APPEND _TBB_LIBRARY_DIR ${_TBB_INSTALL_DIR}/lib)
215
216# Jiri: No reason not to check the default paths. From recent versions,
217#       tbbvars has started exporting the LIBRARY_PATH and LD_LIBRARY_PATH
218#       variables, which now point to the directories of the lib files.
219#       It all makes more sense to use the ${_TBB_LIBRARY_DIR} as a HINTS
220#       argument instead of the implicit PATHS as it isn't hard-coded
221#       but computed by system introspection. Searching the LIBRARY_PATH
222#       and LD_LIBRARY_PATH environment variables is now even more important
223#       that tbbvars doesn't export TBB_ARCH_PLATFORM and it facilitates
224#       the use of TBB built from sources.
225find_library(TBB_LIBRARY ${_TBB_LIB_NAME} HINTS ${_TBB_LIBRARY_DIR}
226        PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
227find_library(TBB_MALLOC_LIBRARY ${_TBB_LIB_MALLOC_NAME} HINTS ${_TBB_LIBRARY_DIR}
228        PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
229
230#Extract path from TBB_LIBRARY name
231get_filename_component(TBB_LIBRARY_DIR ${TBB_LIBRARY} PATH)
232
233#TBB_CORRECT_LIB_DIR(TBB_LIBRARY)
234#TBB_CORRECT_LIB_DIR(TBB_MALLOC_LIBRARY)
235mark_as_advanced(TBB_LIBRARY TBB_MALLOC_LIBRARY)
236
237#-- Look for debug libraries
238# Jiri: Changed the same way as for the release libraries.
239find_library(TBB_LIBRARY_DEBUG ${_TBB_LIB_DEBUG_NAME} HINTS ${_TBB_LIBRARY_DIR}
240        PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
241find_library(TBB_MALLOC_LIBRARY_DEBUG ${_TBB_LIB_MALLOC_DEBUG_NAME} HINTS ${_TBB_LIBRARY_DIR}
242        PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
243
244# Jiri: Self-built TBB stores the debug libraries in a separate directory.
245#       Extract path from TBB_LIBRARY_DEBUG name
246get_filename_component(TBB_LIBRARY_DEBUG_DIR ${TBB_LIBRARY_DEBUG} PATH)
247
248#TBB_CORRECT_LIB_DIR(TBB_LIBRARY_DEBUG)
249#TBB_CORRECT_LIB_DIR(TBB_MALLOC_LIBRARY_DEBUG)
250mark_as_advanced(TBB_LIBRARY_DEBUG TBB_MALLOC_LIBRARY_DEBUG)
251
252
253if (TBB_INCLUDE_DIR)
254    if (TBB_LIBRARY)
255        set (TBB_FOUND "YES")
256        set (TBB_LIBRARIES ${TBB_LIBRARY} ${TBB_MALLOC_LIBRARY} ${TBB_LIBRARIES})
257        set (TBB_DEBUG_LIBRARIES ${TBB_LIBRARY_DEBUG} ${TBB_MALLOC_LIBRARY_DEBUG} ${TBB_DEBUG_LIBRARIES})
258        set (TBB_INCLUDE_DIRS ${TBB_INCLUDE_DIR} CACHE PATH "TBB include directory" FORCE)
259        set (TBB_LIBRARY_DIRS ${TBB_LIBRARY_DIR} CACHE PATH "TBB library directory" FORCE)
260        # Jiri: Self-built TBB stores the debug libraries in a separate directory.
261        set (TBB_DEBUG_LIBRARY_DIRS ${TBB_LIBRARY_DEBUG_DIR} CACHE PATH "TBB debug library directory" FORCE)
262        mark_as_advanced(TBB_INCLUDE_DIRS TBB_LIBRARY_DIRS TBB_DEBUG_LIBRARY_DIRS TBB_LIBRARIES TBB_DEBUG_LIBRARIES)
263        message(STATUS "Found Intel TBB")
264    endif ()
265endif ()
266
267if (NOT TBB_FOUND)
268    message("ERROR: Intel TBB NOT found!")
269    message(STATUS "Looked for Threading Building Blocks in ${_TBB_INSTALL_DIR}")
270    # do only throw fatal, if this pkg is REQUIRED
271    if (TBB_FIND_REQUIRED)
272        message(FATAL_ERROR "Could NOT find TBB library.")
273    endif ()
274endif ()
275
276endif ()
277
278if (TBB_FOUND)
279        set(TBB_INTERFACE_VERSION 0)
280        FILE(READ "${TBB_INCLUDE_DIRS}/tbb/tbb_stddef.h" _TBB_VERSION_CONTENTS)
281        STRING(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" TBB_INTERFACE_VERSION "${_TBB_VERSION_CONTENTS}")
282        set(TBB_INTERFACE_VERSION "${TBB_INTERFACE_VERSION}")
283endif ()
284