1# Copyright (c) 2019-2020 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(CMakeParseArguments)
16
17# Save the location of Intel TBB CMake modules here, as it will not be possible to do inside functions,
18# see for details: https://cmake.org/cmake/help/latest/variable/CMAKE_CURRENT_LIST_DIR.html
19set(_tbb_cmake_module_path ${CMAKE_CURRENT_LIST_DIR})
20
21function(tbb_install_config)
22    set(oneValueArgs INSTALL_DIR
23                     SYSTEM_NAME
24                     LIB_REL_PATH INC_REL_PATH BIN_REL_PATH TBB_VERSION TBB_VERSION_FILE
25                     LIB_PATH BIN_PATH INC_PATH)                                      # If TBB is installed on the system
26
27    cmake_parse_arguments(tbb_IC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
28
29    get_filename_component(config_install_dir ${tbb_IC_INSTALL_DIR} ABSOLUTE)
30    file(MAKE_DIRECTORY ${config_install_dir})
31
32    # --- TBB_LIB_REL_PATH handling ---
33    set(TBB_LIB_REL_PATH "../../../lib")
34
35    if (tbb_IC_LIB_REL_PATH)
36        file(TO_CMAKE_PATH ${tbb_IC_LIB_REL_PATH} TBB_LIB_REL_PATH)
37    endif()
38
39    if (tbb_IC_LIB_PATH)
40        get_filename_component(lib_abs_path ${tbb_IC_LIB_PATH} ABSOLUTE)
41        file(RELATIVE_PATH TBB_LIB_REL_PATH ${config_install_dir} ${lib_abs_path})
42        unset(lib_abs_path)
43    endif()
44    # ------
45
46    # --- TBB_BIN_REL_PATH handling ---
47    set(TBB_BIN_REL_PATH "../../../bin")
48
49    if (tbb_IC_BIN_REL_PATH)
50        file(TO_CMAKE_PATH ${tbb_IC_BIN_REL_PATH} TBB_BIN_REL_PATH)
51    endif()
52
53    if (tbb_IC_BIN_PATH)
54        get_filename_component(bin_abs_path ${tbb_IC_BIN_PATH} ABSOLUTE)
55        file(RELATIVE_PATH TBB_BIN_REL_PATH ${config_install_dir} ${bin_abs_path})
56        unset(bin_abs_path)
57    endif()
58    # ------
59
60    # --- TBB_INC_REL_PATH handling ---
61    set(TBB_INC_REL_PATH "../../../include")
62
63    if (tbb_IC_INC_REL_PATH)
64        file(TO_CMAKE_PATH ${tbb_IC_INC_REL_PATH} TBB_INC_REL_PATH)
65    endif()
66
67    if (tbb_IC_INC_PATH)
68        get_filename_component(inc_abs_path ${tbb_IC_INC_PATH} ABSOLUTE)
69        file(RELATIVE_PATH TBB_INC_REL_PATH ${config_install_dir} ${inc_abs_path})
70        unset(inc_abs_path)
71    endif()
72    # ------
73
74    # --- TBB_VERSION handling ---
75    if (tbb_IC_TBB_VERSION)
76        set(TBB_VERSION ${tbb_IC_TBB_VERSION})
77    else()
78        set(tbb_version_file "${config_install_dir}/${TBB_INC_REL_PATH}/tbb/tbb_stddef.h")
79        if (tbb_IC_TBB_VERSION_FILE)
80            set(tbb_version_file ${tbb_IC_TBB_VERSION_FILE})
81        endif()
82
83        file(READ ${tbb_version_file} _tbb_stddef)
84        string(REGEX REPLACE ".*#define TBB_VERSION_MAJOR ([0-9]+).*" "\\1" _tbb_ver_major "${_tbb_stddef}")
85        string(REGEX REPLACE ".*#define TBB_VERSION_MINOR ([0-9]+).*" "\\1" _tbb_ver_minor "${_tbb_stddef}")
86        string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" TBB_INTERFACE_VERSION "${_tbb_stddef}")
87        set(TBB_VERSION "${_tbb_ver_major}.${_tbb_ver_minor}")
88    endif()
89    # ------
90
91    set(tbb_system_name ${CMAKE_SYSTEM_NAME})
92    if (tbb_IC_SYSTEM_NAME)
93        set(tbb_system_name ${tbb_IC_SYSTEM_NAME})
94    endif()
95
96    if (tbb_system_name STREQUAL "Linux")
97        set(TBB_LIB_PREFIX "lib")
98        set(TBB_LIB_EXT "so.2")
99        set(TBB_IMPLIB_RELEASE "")
100        set(TBB_IMPLIB_DEBUG "")
101    elseif (tbb_system_name STREQUAL "Darwin")
102        set(TBB_LIB_PREFIX "lib")
103        set(TBB_LIB_EXT "dylib")
104        set(TBB_IMPLIB_RELEASE "")
105        set(TBB_IMPLIB_DEBUG "")
106    elseif (tbb_system_name STREQUAL "Windows")
107        set(TBB_LIB_PREFIX "")
108        set(TBB_LIB_EXT "dll")
109        # .lib files installed to TBB_LIB_REL_PATH (e.g. <prefix>/lib);
110        # .dll files installed to TBB_BIN_REL_PATH (e.g. <prefix>/bin);
111        # Expand TBB_LIB_REL_PATH here in IMPORTED_IMPLIB property and
112        # redefine it with TBB_BIN_REL_PATH value to properly fill IMPORTED_LOCATION property in TBBConfig.cmake.in template.
113        set(TBB_IMPLIB_RELEASE "
114                                      IMPORTED_IMPLIB_RELEASE \"\${CMAKE_CURRENT_LIST_DIR}/${TBB_LIB_REL_PATH}/\${_tbb_component}.lib\"")
115        set(TBB_IMPLIB_DEBUG "
116                                      IMPORTED_IMPLIB_DEBUG \"\${CMAKE_CURRENT_LIST_DIR}/${TBB_LIB_REL_PATH}/\${_tbb_component}_debug.lib\"")
117        set(TBB_LIB_REL_PATH ${TBB_BIN_REL_PATH})
118    else()
119        message(FATAL_ERROR "Unsupported OS name: ${tbb_system_name}")
120    endif()
121
122    configure_file(${_tbb_cmake_module_path}/templates/TBBConfig.cmake.in ${config_install_dir}/TBBConfig.cmake @ONLY)
123    configure_file(${_tbb_cmake_module_path}/templates/TBBConfigVersion.cmake.in ${config_install_dir}/TBBConfigVersion.cmake @ONLY)
124endfunction()
125