xref: /oneTBB/src/tbbbind/CMakeLists.txt (revision 3e352b48)
1# Copyright (c) 2020-2022 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
15if (DEFINED CMAKE_SKIP_BUILD_RPATH)
16    set(CMAKE_SKIP_BUILD_RPATH_OLD_VALUE ${CMAKE_SKIP_BUILD_RPATH})
17endif()
18set(CMAKE_SKIP_BUILD_RPATH TRUE)
19
20function(tbbbind_build TBBBIND_NAME REQUIRED_HWLOC_TARGET)
21    if (NOT TARGET ${REQUIRED_HWLOC_TARGET})
22        message(STATUS "HWLOC target ${REQUIRED_HWLOC_TARGET} doesn't exist."
23                       " The ${TBBBIND_NAME} target cannot be created")
24        return()
25    endif()
26    add_library(${TBBBIND_NAME} tbb_bind.cpp)
27    add_library(TBB::${TBBBIND_NAME} ALIAS ${TBBBIND_NAME})
28
29    target_compile_definitions(${TBBBIND_NAME}
30                               PUBLIC
31                               $<$<CONFIG:DEBUG>:TBB_USE_DEBUG>
32                               PRIVATE
33                               __TBBBIND_BUILD)
34    target_include_directories(${TBBBIND_NAME}
35        PUBLIC
36        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../include>
37        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
38        ${HWLOC_INCLUDE_DIRS} # pkg-config defined
39    )
40
41    target_compile_options(${TBBBIND_NAME}
42        PRIVATE
43        ${TBB_CXX_STD_FLAG} # TODO: consider making it PUBLIC.
44        ${TBB_MMD_FLAG}
45        ${TBB_DSE_FLAG}
46        ${TBB_WARNING_LEVEL}
47        ${TBB_LIB_COMPILE_FLAGS}
48        ${TBB_COMMON_COMPILE_FLAGS}
49    )
50
51    # Avoid use of target_link_libraries here as it changes /DEF option to \DEF on Windows.
52    set_target_properties(${TBBBIND_NAME} PROPERTIES
53        DEFINE_SYMBOL ""
54        VERSION ${TBBBIND_BINARY_VERSION}.${TBB_BINARY_MINOR_VERSION}
55        SOVERSION ${TBBBIND_BINARY_VERSION}
56    )
57
58    tbb_handle_ipo(${TBBBIND_NAME})
59
60    if (TBB_DEF_FILE_PREFIX) # If there's no prefix, assume we're using export directives
61        set_target_properties(${TBBBIND_NAME} PROPERTIES
62            LINK_FLAGS ${TBB_LINK_DEF_FILE_FLAG}${CMAKE_CURRENT_SOURCE_DIR}/def/${TBB_DEF_FILE_PREFIX}-tbbbind.def
63            LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/def/${TBB_DEF_FILE_PREFIX}-tbbbind.def
64        )
65    endif()
66
67    # Prefer using target_link_options instead of target_link_libraries to specify link options because
68    # target_link_libraries may incorrectly handle some options (on Windows, for example).
69    if (COMMAND target_link_options)
70        target_link_options(${TBBBIND_NAME}
71            PRIVATE
72            ${TBB_LIB_LINK_FLAGS}
73            ${TBB_COMMON_LINK_FLAGS}
74        )
75    else()
76        target_link_libraries(${TBBBIND_NAME}
77            PRIVATE
78            ${TBB_LIB_LINK_FLAGS}
79            ${TBB_COMMON_LINK_FLAGS}
80        )
81    endif()
82
83    target_link_libraries(${TBBBIND_NAME}
84        PUBLIC
85        ${REQUIRED_HWLOC_TARGET}
86        PRIVATE
87        ${TBB_LIB_LINK_LIBS}
88        ${TBB_COMMON_LINK_LIBS}
89    )
90
91    tbb_install_target(${TBBBIND_NAME})
92
93endfunction()
94
95if (NOT DEFINED HWLOC_TARGET_EXPLICITLY_DEFINED AND TARGET PkgConfig::HWLOC)
96    message(STATUS "The ${TBBBIND_LIBRARY_NAME} target will be configured using the HWLOC ${HWLOC_VERSION}")
97    tbbbind_build(${TBBBIND_LIBRARY_NAME} PkgConfig::HWLOC)
98else()
99    tbbbind_build(tbbbind     HWLOC::hwloc_1_11)
100    tbbbind_build(tbbbind_2_0 HWLOC::hwloc_2   )
101    tbbbind_build(tbbbind_2_5 HWLOC::hwloc_2_5 )
102endif()
103
104
105if (DEFINED CMAKE_SKIP_BUILD_RPATH_OLD_VALUE)
106    set(CMAKE_SKIP_BUILD_RPATH ${CMAKE_SKIP_BUILD_RPATH_OLD_VALUE})
107    unset(CMAKE_SKIP_BUILD_RPATH_OLD_VALUE)
108else()
109    unset(CMAKE_SKIP_BUILD_RPATH)
110endif()
111