527c81
# Locate Intel Threading Building Blocks include paths and libraries
527c81
# FindTBB.cmake can be found at https://code.google.com/p/findtbb/
527c81
# Written by Hannes Hofmann <hannes.hofmann _at_ informatik.uni-erlangen.de>
527c81
# Improvements by Gino van den Bergen <gino _at_ dtecta.com>,
527c81
#   Florian Uhlig <F.Uhlig _at_ gsi.de>,
527c81
#   Jiri Marsik <jiri.marsik89 _at_ gmail.com>
527c81

527c81
# The MIT License
527c81
#
527c81
# Copyright (c) 2011 Hannes Hofmann
527c81
#
527c81
# Permission is hereby granted, free of charge, to any person obtaining a copy
527c81
# of this software and associated documentation files (the "Software"), to deal
527c81
# in the Software without restriction, including without limitation the rights
527c81
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
527c81
# copies of the Software, and to permit persons to whom the Software is
527c81
# furnished to do so, subject to the following conditions:
527c81
#
527c81
# The above copyright notice and this permission notice shall be included in
527c81
# all copies or substantial portions of the Software.
527c81
#
527c81
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
527c81
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
527c81
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
527c81
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
527c81
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
527c81
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
527c81
# THE SOFTWARE.
527c81

527c81
# GvdB: This module uses the environment variable TBB_ARCH_PLATFORM which defines architecture and compiler.
527c81
#   e.g. "ia32/vc8" or "em64t/cc4.1.0_libc2.4_kernel2.6.16.21"
527c81
#   TBB_ARCH_PLATFORM is set by the build script tbbvars[.bat|.sh|.csh], which can be found
527c81
#   in the TBB installation directory (TBB_INSTALL_DIR).
527c81
#
527c81
# GvdB: Mac OS X distribution places libraries directly in lib directory.
527c81
#
527c81
# For backwards compatibility, you may explicitely set the CMake variables TBB_ARCHITECTURE and TBB_COMPILER.
527c81
# TBB_ARCHITECTURE [ ia32 | em64t | itanium ]
527c81
#   which architecture to use
527c81
# TBB_COMPILER e.g. vc9 or cc3.2.3_libc2.3.2_kernel2.4.21 or cc4.0.1_os10.4.9
527c81
#   which compiler to use (detected automatically on Windows)
527c81

527c81
# This module respects
527c81
# TBB_INSTALL_DIR or $ENV{TBB21_INSTALL_DIR} or $ENV{TBB_INSTALL_DIR}
527c81

527c81
# This module defines
527c81
# TBB_INCLUDE_DIRS, where to find task_scheduler_init.h, etc.
527c81
# TBB_LIBRARY_DIRS, where to find libtbb, libtbbmalloc
527c81
# TBB_DEBUG_LIBRARY_DIRS, where to find libtbb_debug, libtbbmalloc_debug
527c81
# TBB_INSTALL_DIR, the base TBB install directory
527c81
# TBB_LIBRARIES, the libraries to link against to use TBB.
527c81
# TBB_DEBUG_LIBRARIES, the libraries to link against to use TBB with debug symbols.
527c81
# TBB_FOUND, If false, don't try to use TBB.
527c81
# TBB_INTERFACE_VERSION, as defined in tbb/tbb_stddef.h
527c81

527c81

527c81
if (WIN32)
527c81
    # has em64t/vc8 em64t/vc9
527c81
    # has ia32/vc7.1 ia32/vc8 ia32/vc9
527c81
    set(_TBB_DEFAULT_INSTALL_DIR "C:/Program Files/Intel/TBB" "C:/Program Files (x86)/Intel/TBB")
527c81
    set(_TBB_LIB_NAME "tbb")
527c81
    set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc")
527c81
    set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug")
527c81
    set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug")
527c81
    if (MSVC71)
527c81
        set (_TBB_COMPILER "vc7.1")
527c81
    endif(MSVC71)
527c81
    if (MSVC80)
527c81
        set(_TBB_COMPILER "vc8")
527c81
    endif(MSVC80)
527c81
    if (MSVC90)
527c81
        set(_TBB_COMPILER "vc9")
527c81
    endif(MSVC90)
527c81
    if(MSVC10)
527c81
        set(_TBB_COMPILER "vc10")
527c81
    endif(MSVC10)
527c81
    # Todo: add other Windows compilers such as ICL.
527c81
    set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE})
527c81
endif (WIN32)
527c81

527c81
if (UNIX)
527c81
    if (APPLE)
527c81
        # MAC
527c81
        set(_TBB_DEFAULT_INSTALL_DIR "/Library/Frameworks/Intel_TBB.framework/Versions")
527c81
        # libs: libtbb.dylib, libtbbmalloc.dylib, *_debug
527c81
        set(_TBB_LIB_NAME "tbb")
527c81
        set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc")
527c81
        set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug")
527c81
        set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug")
527c81
        # default flavor on apple: ia32/cc4.0.1_os10.4.9
527c81
        # Jiri: There is no reason to presume there is only one flavor and
527c81
        #       that user's setting of variables should be ignored.
527c81
        if(NOT TBB_COMPILER)
527c81
            set(_TBB_COMPILER "cc4.0.1_os10.4.9")
527c81
        elseif (NOT TBB_COMPILER)
527c81
            set(_TBB_COMPILER ${TBB_COMPILER})
527c81
        endif(NOT TBB_COMPILER)
527c81
        if(NOT TBB_ARCHITECTURE)
527c81
            set(_TBB_ARCHITECTURE "ia32")
527c81
        elseif(NOT TBB_ARCHITECTURE)
527c81
            set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE})
527c81
        endif(NOT TBB_ARCHITECTURE)
527c81
    else (APPLE)
527c81
        # LINUX
527c81
        set(_TBB_DEFAULT_INSTALL_DIR "/opt/intel/tbb" "/usr/local/include" "/usr/include")
527c81
        set(_TBB_LIB_NAME "tbb")
527c81
        set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc")
527c81
        set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug")
527c81
        set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug")
527c81
        # 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
527c81
        # has ia32/*
527c81
        # has itanium/*
527c81
        set(_TBB_COMPILER ${TBB_COMPILER})
527c81
        set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE})
527c81
    endif (APPLE)
527c81
endif (UNIX)
527c81

527c81
if (CMAKE_SYSTEM MATCHES "SunOS.*")
527c81
# SUN
527c81
# not yet supported
527c81
# has em64t/cc3.4.3_kernel5.10
527c81
# has ia32/*
527c81
endif (CMAKE_SYSTEM MATCHES "SunOS.*")
527c81

527c81

527c81
#-- Clear the public variables
527c81
set (TBB_FOUND "NO")
527c81

527c81

527c81
#-- Find TBB install dir and set ${_TBB_INSTALL_DIR} and cached ${TBB_INSTALL_DIR}
527c81
# first: use CMake variable TBB_INSTALL_DIR
527c81
if (TBB_INSTALL_DIR)
527c81
    set (_TBB_INSTALL_DIR ${TBB_INSTALL_DIR})
527c81
endif (TBB_INSTALL_DIR)
527c81
# second: use environment variable
527c81
if (NOT _TBB_INSTALL_DIR)
527c81
    if (NOT "$ENV{TBB_INSTALL_DIR}" STREQUAL "")
527c81
        set (_TBB_INSTALL_DIR $ENV{TBB_INSTALL_DIR})
527c81
    endif (NOT "$ENV{TBB_INSTALL_DIR}" STREQUAL "")
527c81
    # Intel recommends setting TBB21_INSTALL_DIR
527c81
    if (NOT "$ENV{TBB21_INSTALL_DIR}" STREQUAL "")
527c81
        set (_TBB_INSTALL_DIR $ENV{TBB21_INSTALL_DIR})
527c81
    endif (NOT "$ENV{TBB21_INSTALL_DIR}" STREQUAL "")
527c81
    if (NOT "$ENV{TBB22_INSTALL_DIR}" STREQUAL "")
527c81
        set (_TBB_INSTALL_DIR $ENV{TBB22_INSTALL_DIR})
527c81
    endif (NOT "$ENV{TBB22_INSTALL_DIR}" STREQUAL "")
527c81
    if (NOT "$ENV{TBB30_INSTALL_DIR}" STREQUAL "")
527c81
        set (_TBB_INSTALL_DIR $ENV{TBB30_INSTALL_DIR})
527c81
    endif (NOT "$ENV{TBB30_INSTALL_DIR}" STREQUAL "")
527c81
endif (NOT _TBB_INSTALL_DIR)
527c81
# third: try to find path automatically
527c81
if (NOT _TBB_INSTALL_DIR)
527c81
    if (_TBB_DEFAULT_INSTALL_DIR)
527c81
        set (_TBB_INSTALL_DIR ${_TBB_DEFAULT_INSTALL_DIR})
527c81
    endif (_TBB_DEFAULT_INSTALL_DIR)
527c81
endif (NOT _TBB_INSTALL_DIR)
527c81
# sanity check
527c81
if (NOT _TBB_INSTALL_DIR)
527c81
    message ("ERROR: Unable to find Intel TBB install directory. ${_TBB_INSTALL_DIR}")
527c81
else (NOT _TBB_INSTALL_DIR)
527c81
# finally: set the cached CMake variable TBB_INSTALL_DIR
527c81
if (NOT TBB_INSTALL_DIR)
527c81
    set (TBB_INSTALL_DIR ${_TBB_INSTALL_DIR} CACHE PATH "Intel TBB install directory")
527c81
    mark_as_advanced(TBB_INSTALL_DIR)
527c81
endif (NOT TBB_INSTALL_DIR)
527c81

527c81

527c81
#-- A macro to rewrite the paths of the library. This is necessary, because
527c81
#   find_library() always found the em64t/vc9 version of the TBB libs
527c81
macro(TBB_CORRECT_LIB_DIR var_name)
527c81
#    if (NOT "${_TBB_ARCHITECTURE}" STREQUAL "em64t")
527c81
        string(REPLACE em64t "${_TBB_ARCHITECTURE}" ${var_name} ${${var_name}})
527c81
#    endif (NOT "${_TBB_ARCHITECTURE}" STREQUAL "em64t")
527c81
    string(REPLACE ia32 "${_TBB_ARCHITECTURE}" ${var_name} ${${var_name}})
527c81
    string(REPLACE vc7.1 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
527c81
    string(REPLACE vc8 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
527c81
    string(REPLACE vc9 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
527c81
    string(REPLACE vc10 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
527c81
endmacro(TBB_CORRECT_LIB_DIR var_content)
527c81

527c81

527c81
#-- Look for include directory and set ${TBB_INCLUDE_DIR}
527c81
set (TBB_INC_SEARCH_DIR ${_TBB_INSTALL_DIR}/include)
527c81
# Jiri: tbbvars now sets the CPATH environment variable to the directory
527c81
#       containing the headers.
527c81
find_path(TBB_INCLUDE_DIR
527c81
    tbb/task_scheduler_init.h
527c81
    PATHS ${TBB_INC_SEARCH_DIR} ENV CPATH
527c81
)
527c81
mark_as_advanced(TBB_INCLUDE_DIR)
527c81

527c81

527c81
#-- Look for libraries
527c81
# GvdB: $ENV{TBB_ARCH_PLATFORM} is set by the build script tbbvars[.bat|.sh|.csh]
527c81
if (NOT $ENV{TBB_ARCH_PLATFORM} STREQUAL "")
527c81
    set (_TBB_LIBRARY_DIR 
527c81
         ${_TBB_INSTALL_DIR}/lib/$ENV{TBB_ARCH_PLATFORM}
527c81
         ${_TBB_INSTALL_DIR}/$ENV{TBB_ARCH_PLATFORM}/lib
527c81
        )
527c81
endif (NOT $ENV{TBB_ARCH_PLATFORM} STREQUAL "")
527c81
# Jiri: This block isn't mutually exclusive with the previous one
527c81
#       (hence no else), instead I test if the user really specified
527c81
#       the variables in question.
527c81
if ((NOT ${TBB_ARCHITECTURE} STREQUAL "") AND (NOT ${TBB_COMPILER} STREQUAL ""))
527c81
    # HH: deprecated
527c81
    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]).")
527c81
    # Jiri: It doesn't hurt to look in more places, so I store the hints from
527c81
    #       ENV{TBB_ARCH_PLATFORM} and the TBB_ARCHITECTURE and TBB_COMPILER
527c81
    #       variables and search them both.
527c81
    set (_TBB_LIBRARY_DIR "${_TBB_INSTALL_DIR}/${_TBB_ARCHITECTURE}/${_TBB_COMPILER}/lib" ${_TBB_LIBRARY_DIR})
527c81
endif ((NOT ${TBB_ARCHITECTURE} STREQUAL "") AND (NOT ${TBB_COMPILER} STREQUAL ""))
527c81

527c81
# GvdB: Mac OS X distribution places libraries directly in lib directory.
527c81
list(APPEND _TBB_LIBRARY_DIR ${_TBB_INSTALL_DIR}/lib)
527c81

527c81
# Jiri: No reason not to check the default paths. From recent versions,
527c81
#       tbbvars has started exporting the LIBRARY_PATH and LD_LIBRARY_PATH
527c81
#       variables, which now point to the directories of the lib files.
527c81
#       It all makes more sense to use the ${_TBB_LIBRARY_DIR} as a HINTS
527c81
#       argument instead of the implicit PATHS as it isn't hard-coded
527c81
#       but computed by system introspection. Searching the LIBRARY_PATH
527c81
#       and LD_LIBRARY_PATH environment variables is now even more important
527c81
#       that tbbvars doesn't export TBB_ARCH_PLATFORM and it facilitates
527c81
#       the use of TBB built from sources.
527c81
find_library(TBB_LIBRARY ${_TBB_LIB_NAME} HINTS ${_TBB_LIBRARY_DIR}
527c81
        PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
527c81
find_library(TBB_MALLOC_LIBRARY ${_TBB_LIB_MALLOC_NAME} HINTS ${_TBB_LIBRARY_DIR}
527c81
        PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
527c81

527c81
#Extract path from TBB_LIBRARY name
527c81
get_filename_component(TBB_LIBRARY_DIR ${TBB_LIBRARY} PATH)
527c81

527c81
#TBB_CORRECT_LIB_DIR(TBB_LIBRARY)
527c81
#TBB_CORRECT_LIB_DIR(TBB_MALLOC_LIBRARY)
527c81
mark_as_advanced(TBB_LIBRARY TBB_MALLOC_LIBRARY)
527c81

527c81
#-- Look for debug libraries
527c81
# Jiri: Changed the same way as for the release libraries.
527c81
find_library(TBB_LIBRARY_DEBUG ${_TBB_LIB_DEBUG_NAME} HINTS ${_TBB_LIBRARY_DIR}
527c81
        PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
527c81
find_library(TBB_MALLOC_LIBRARY_DEBUG ${_TBB_LIB_MALLOC_DEBUG_NAME} HINTS ${_TBB_LIBRARY_DIR}
527c81
        PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
527c81

527c81
# Jiri: Self-built TBB stores the debug libraries in a separate directory.
527c81
#       Extract path from TBB_LIBRARY_DEBUG name
527c81
get_filename_component(TBB_LIBRARY_DEBUG_DIR ${TBB_LIBRARY_DEBUG} PATH)
527c81

527c81
#TBB_CORRECT_LIB_DIR(TBB_LIBRARY_DEBUG)
527c81
#TBB_CORRECT_LIB_DIR(TBB_MALLOC_LIBRARY_DEBUG)
527c81
mark_as_advanced(TBB_LIBRARY_DEBUG TBB_MALLOC_LIBRARY_DEBUG)
527c81

527c81

527c81
if (TBB_INCLUDE_DIR)
527c81
    if (TBB_LIBRARY)
527c81
        set (TBB_FOUND "YES")
527c81
        set (TBB_LIBRARIES ${TBB_LIBRARY} ${TBB_MALLOC_LIBRARY} ${TBB_LIBRARIES})
527c81
        set (TBB_DEBUG_LIBRARIES ${TBB_LIBRARY_DEBUG} ${TBB_MALLOC_LIBRARY_DEBUG} ${TBB_DEBUG_LIBRARIES})
527c81
        set (TBB_INCLUDE_DIRS ${TBB_INCLUDE_DIR} CACHE PATH "TBB include directory" FORCE)
527c81
        set (TBB_LIBRARY_DIRS ${TBB_LIBRARY_DIR} CACHE PATH "TBB library directory" FORCE)
527c81
        # Jiri: Self-built TBB stores the debug libraries in a separate directory.
527c81
        set (TBB_DEBUG_LIBRARY_DIRS ${TBB_LIBRARY_DEBUG_DIR} CACHE PATH "TBB debug library directory" FORCE)
527c81
        mark_as_advanced(TBB_INCLUDE_DIRS TBB_LIBRARY_DIRS TBB_DEBUG_LIBRARY_DIRS TBB_LIBRARIES TBB_DEBUG_LIBRARIES)
527c81
        message(STATUS "Found Intel TBB")
527c81
    endif (TBB_LIBRARY)
527c81
endif (TBB_INCLUDE_DIR)
527c81

527c81
if (NOT TBB_FOUND)
527c81
    message("ERROR: Intel TBB NOT found!")
527c81
    message(STATUS "Looked for Threading Building Blocks in ${_TBB_INSTALL_DIR}")
527c81
    # do only throw fatal, if this pkg is REQUIRED
527c81
    if (TBB_FIND_REQUIRED)
527c81
        message(FATAL_ERROR "Could NOT find TBB library.")
527c81
    endif (TBB_FIND_REQUIRED)
527c81
endif (NOT TBB_FOUND)
527c81

527c81
endif (NOT _TBB_INSTALL_DIR)
527c81

527c81
if (TBB_FOUND)
527c81
	set(TBB_INTERFACE_VERSION 0)
527c81
	FILE(READ "${TBB_INCLUDE_DIRS}/tbb/tbb_stddef.h" _TBB_VERSION_CONTENTS)
527c81
	STRING(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" TBB_INTERFACE_VERSION "${_TBB_VERSION_CONTENTS}")
527c81
	set(TBB_INTERFACE_VERSION "${TBB_INTERFACE_VERSION}")
527c81
endif (TBB_FOUND)