1# Copyright (c) 2017-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
15#
16# Usage:
17#   include(TBBMakeConfig.cmake)
18#   tbb_make_config(TBB_ROOT <tbb_root> SYSTEM_NAME <system_name> CONFIG_DIR <var_to_store_config_dir> [SAVE_TO] [CONFIG_FOR_SOURCE TBB_RELEASE_DIR <tbb_release_dir> TBB_DEBUG_DIR <tbb_debug_dir>])
19#
20
21include(CMakeParseArguments)
22
23# Save the location of Intel TBB CMake modules here, as it will not be possible to do inside functions,
24# see for details: https://cmake.org/cmake/help/latest/variable/CMAKE_CURRENT_LIST_DIR.html
25set(_tbb_cmake_module_path ${CMAKE_CURRENT_LIST_DIR})
26
27function(tbb_make_config)
28    set(oneValueArgs TBB_ROOT SYSTEM_NAME CONFIG_DIR SAVE_TO TBB_RELEASE_DIR TBB_DEBUG_DIR)
29    set(options CONFIG_FOR_SOURCE)
30    cmake_parse_arguments(tbb_MK "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
31
32    set(tbb_system_name ${CMAKE_SYSTEM_NAME})
33    if (tbb_MK_SYSTEM_NAME)
34        set(tbb_system_name ${tbb_MK_SYSTEM_NAME})
35    endif()
36
37    set(tbb_config_dir ${tbb_MK_TBB_ROOT}/cmake)
38    if (tbb_MK_SAVE_TO)
39        set(tbb_config_dir ${tbb_MK_SAVE_TO})
40    endif()
41
42    file(MAKE_DIRECTORY ${tbb_config_dir})
43
44    set(TBB_DEFAULT_COMPONENTS tbb tbbmalloc tbbmalloc_proxy)
45
46    if (tbb_MK_CONFIG_FOR_SOURCE)
47        set(TBB_RELEASE_DIR ${tbb_MK_TBB_RELEASE_DIR})
48        set(TBB_DEBUG_DIR ${tbb_MK_TBB_DEBUG_DIR})
49    endif()
50
51    if (tbb_system_name STREQUAL "Linux")
52        set(TBB_SHARED_LIB_DIR "lib")
53        set(TBB_X32_SUBDIR "ia32")
54        set(TBB_X64_SUBDIR "intel64")
55        set(TBB_LIB_PREFIX "lib")
56        set(TBB_LIB_EXT "so.2")
57
58        # Note: multiline variable
59        set(TBB_CHOOSE_COMPILER_SUBDIR "set(_tbb_compiler_subdir gcc4.8)
60
61# For non-GCC compilers try to find version of system GCC to choose right compiler subdirectory.
62if (NOT CMAKE_CXX_COMPILER_ID STREQUAL \"GNU\" AND NOT CMAKE_C_COMPILER_ID STREQUAL \"GNU\")
63    find_program(_gcc_executable gcc)
64    if (NOT _gcc_executable)
65        message(FATAL_ERROR \"This Intel TBB package is intended to be used only in environment with available 'gcc'\")
66    endif()
67    unset(_gcc_executable)
68endif()")
69
70    elseif (tbb_system_name STREQUAL "Windows")
71        set(TBB_SHARED_LIB_DIR "bin")
72        set(TBB_X32_SUBDIR "ia32")
73        set(TBB_X64_SUBDIR "intel64")
74        set(TBB_LIB_PREFIX "")
75        set(TBB_LIB_EXT "dll")
76
77        # Note: multiline variable
78        set(TBB_CHOOSE_COMPILER_SUBDIR "if (NOT MSVC)
79    message(FATAL_ERROR \"This Intel TBB package is intended to be used only in the project with MSVC\")
80endif()
81
82if (MSVC_VERSION VERSION_LESS 1900)
83    message(FATAL_ERROR \"This Intel TBB package is intended to be used only in the project with MSVC version 1900 (vc14) or higher\")
84endif()
85
86set(_tbb_compiler_subdir vc14)
87
88if (WINDOWS_STORE)
89    set(_tbb_compiler_subdir \${_tbb_compiler_subdir}_uwp)
90endif()")
91
92        if (tbb_MK_CONFIG_FOR_SOURCE)
93            set(TBB_IMPLIB_RELEASE "
94                                  IMPORTED_IMPLIB_RELEASE \"${tbb_MK_TBB_RELEASE_DIR}/\${_tbb_component}.lib\"")
95            set(TBB_IMPLIB_DEBUG "
96                                  IMPORTED_IMPLIB_DEBUG \"${tbb_MK_TBB_DEBUG_DIR}/\${_tbb_component}_debug.lib\"")
97        else()
98            set(TBB_IMPLIB_RELEASE "
99                                  IMPORTED_IMPLIB_RELEASE \"\${_tbb_root}/lib/\${_tbb_arch_subdir}/\${_tbb_compiler_subdir}/\${_tbb_component}.lib\"")
100            set(TBB_IMPLIB_DEBUG "
101                                  IMPORTED_IMPLIB_DEBUG \"\${_tbb_root}/lib/\${_tbb_arch_subdir}/\${_tbb_compiler_subdir}/\${_tbb_component}_debug.lib\"")
102        endif()
103
104        # Note: multiline variable
105        # tbb/internal/_tbb_windef.h (included via tbb/tbb_stddef.h) does implicit linkage of some .lib files, use a special define to avoid it
106        set(TBB_COMPILE_DEFINITIONS "
107                              INTERFACE_COMPILE_DEFINITIONS \"__TBB_NO_IMPLICIT_LINKAGE=1\"")
108    elseif (tbb_system_name STREQUAL "Darwin")
109        set(TBB_SHARED_LIB_DIR "lib")
110        set(TBB_X32_SUBDIR ".")
111        set(TBB_X64_SUBDIR ".")
112        set(TBB_LIB_PREFIX "lib")
113        set(TBB_LIB_EXT "dylib")
114        set(TBB_CHOOSE_COMPILER_SUBDIR "set(_tbb_compiler_subdir .)")
115    elseif (tbb_system_name STREQUAL "Android")
116        set(TBB_SHARED_LIB_DIR "lib")
117        set(TBB_X32_SUBDIR ".")
118        set(TBB_X64_SUBDIR "x86_64")
119        set(TBB_LIB_PREFIX "lib")
120        set(TBB_LIB_EXT "so")
121        set(TBB_CHOOSE_COMPILER_SUBDIR "set(_tbb_compiler_subdir .)")
122    else()
123        message(FATAL_ERROR "Unsupported OS name: ${tbb_system_name}")
124    endif()
125
126    file(READ "${tbb_MK_TBB_ROOT}/include/tbb/tbb_stddef.h" _tbb_stddef)
127    string(REGEX REPLACE ".*#define TBB_VERSION_MAJOR ([0-9]+).*" "\\1" _tbb_ver_major "${_tbb_stddef}")
128    string(REGEX REPLACE ".*#define TBB_VERSION_MINOR ([0-9]+).*" "\\1" _tbb_ver_minor "${_tbb_stddef}")
129    string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" TBB_INTERFACE_VERSION "${_tbb_stddef}")
130    set(TBB_VERSION "${_tbb_ver_major}.${_tbb_ver_minor}")
131
132    if (tbb_MK_CONFIG_FOR_SOURCE)
133        set(TBB_CHOOSE_ARCH_AND_COMPILER "")
134        set(TBB_RELEASE_LIB_PATH "${TBB_RELEASE_DIR}")
135        set(TBB_DEBUG_LIB_PATH "${TBB_DEBUG_DIR}")
136        set(TBB_UNSET_ADDITIONAL_VARIABLES "")
137    else()
138        # Note: multiline variable
139        set(TBB_CHOOSE_ARCH_AND_COMPILER "
140if (CMAKE_SIZEOF_VOID_P EQUAL 8)
141    set(_tbb_arch_subdir ${TBB_X64_SUBDIR})
142else()
143    set(_tbb_arch_subdir ${TBB_X32_SUBDIR})
144endif()
145
146${TBB_CHOOSE_COMPILER_SUBDIR}
147
148get_filename_component(_tbb_lib_path \"\${_tbb_root}/${TBB_SHARED_LIB_DIR}/\${_tbb_arch_subdir}/\${_tbb_compiler_subdir}\" ABSOLUTE)
149")
150
151    set(TBB_RELEASE_LIB_PATH "\${_tbb_lib_path}")
152    set(TBB_DEBUG_LIB_PATH "\${_tbb_lib_path}")
153
154    # Note: multiline variable
155    set(TBB_UNSET_ADDITIONAL_VARIABLES "
156unset(_tbb_arch_subdir)
157unset(_tbb_compiler_subdir)")
158    endif()
159
160    configure_file(${_tbb_cmake_module_path}/templates/TBBConfigInternal.cmake.in ${tbb_config_dir}/TBBConfig.cmake @ONLY)
161    configure_file(${_tbb_cmake_module_path}/templates/TBBConfigVersion.cmake.in ${tbb_config_dir}/TBBConfigVersion.cmake @ONLY)
162
163    set(${tbb_MK_CONFIG_DIR} ${tbb_config_dir} PARENT_SCOPE)
164endfunction()
165