1# Copyright (c) 2020-2021 Intel Corporation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15include(FindPackageHandleStandardArgs)
16
17# Firstly search for TBB in config mode (i.e. search for TBBConfig.cmake).
18find_package(TBB QUIET CONFIG)
19if (TBB_FOUND)
20    find_package_handle_standard_args(TBB CONFIG_MODE)
21    return()
22endif()
23
24if (NOT TBB_FIND_COMPONENTS)
25    set(TBB_FIND_COMPONENTS tbb tbbmalloc)
26    foreach (_tbb_component ${TBB_FIND_COMPONENTS})
27        set(TBB_FIND_REQUIRED_${_tbb_component} 1)
28    endforeach()
29endif()
30
31if (WIN32)
32    list(APPEND ADDITIONAL_LIB_DIRS ENV PATH ENV LIB)
33    list(APPEND ADDITIONAL_INCLUDE_DIRS ENV INCLUDE ENV CPATH)
34else()
35    list(APPEND ADDITIONAL_LIB_DIRS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH ENV DYLD_LIBRARY_PATH)
36    list(APPEND ADDITIONAL_INCLUDE_DIRS ENV CPATH ENV C_INCLUDE_PATH ENV CPLUS_INCLUDE_PATH ENV INCLUDE_PATH)
37endif()
38
39find_path(_tbb_include_dir NAMES tbb/tbb.h PATHS ${ADDITIONAL_INCLUDE_DIRS})
40
41if (_tbb_include_dir)
42    # TODO: consider TBB_VERSION handling
43    set(_TBB_BUILD_MODES RELEASE DEBUG)
44    set(_TBB_DEBUG_SUFFIX _debug)
45
46    foreach (_tbb_component ${TBB_FIND_COMPONENTS})
47        if (NOT TARGET TBB::${_tbb_component})
48            add_library(TBB::${_tbb_component} SHARED IMPORTED)
49            set_property(TARGET TBB::${_tbb_component} APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${_tbb_include_dir})
50
51            foreach(_TBB_BUILD_MODE ${_TBB_BUILD_MODES})
52                set(_tbb_component_lib_name ${_tbb_component}${_TBB_${_TBB_BUILD_MODE}_SUFFIX})
53                if (WIN32)
54                    find_library(${_tbb_component_lib_name}_lib ${_tbb_component_lib_name} PATHS ${ADDITIONAL_LIB_DIRS})
55                    find_file(${_tbb_component_lib_name}_dll ${_tbb_component_lib_name}.dll PATHS ${ADDITIONAL_LIB_DIRS})
56
57                    set_target_properties(TBB::${_tbb_component} PROPERTIES
58                                          IMPORTED_LOCATION_${_TBB_BUILD_MODE} "${${_tbb_component_lib_name}_dll}"
59                                          IMPORTED_IMPLIB_${_TBB_BUILD_MODE}   "${${_tbb_component_lib_name}_lib}"
60                                          )
61                else()
62                    find_library(${_tbb_component_lib_name}_so ${_tbb_component_lib_name} PATHS ${ADDITIONAL_LIB_DIRS})
63
64                    set_target_properties(TBB::${_tbb_component} PROPERTIES
65                                          IMPORTED_LOCATION_${_TBB_BUILD_MODE} "${${_tbb_component_lib_name}_so}"
66                                          )
67                endif()
68                if (${_tbb_component_lib_name}_lib AND ${_tbb_component_lib_name}_dll OR ${_tbb_component_lib_name}_so)
69                    set_property(TARGET TBB::${_tbb_component} APPEND PROPERTY IMPORTED_CONFIGURATIONS ${_TBB_BUILD_MODE})
70                    list(APPEND TBB_IMPORTED_TARGETS TBB::${_tbb_component})
71                    set(TBB_${_tbb_component}_FOUND 1)
72                endif()
73                unset(${_tbb_component_lib_name}_lib CACHE)
74                unset(${_tbb_component_lib_name}_dll CACHE)
75                unset(${_tbb_component_lib_name}_so CACHE)
76                unset(_tbb_component_lib_name)
77            endforeach()
78        endif()
79    endforeach()
80    unset(_TBB_BUILD_MODESS)
81    unset(_TBB_DEBUG_SUFFIX)
82endif()
83unset(_tbb_include_dir CACHE)
84
85list(REMOVE_DUPLICATES TBB_IMPORTED_TARGETS)
86
87find_package_handle_standard_args(TBB
88                                  REQUIRED_VARS TBB_IMPORTED_TARGETS
89                                  HANDLE_COMPONENTS)
90