xref: /oneTBB/src/tbbbind/CMakeLists.txt (revision a1f53c8f)
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
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        ${TBB_IPO_COMPILE_FLAGS}
50    )
51
52    # Avoid use of target_link_libraries here as it changes /DEF option to \DEF on Windows.
53    set_target_properties(${TBBBIND_NAME} PROPERTIES
54        DEFINE_SYMBOL ""
55        VERSION ${TBBBIND_BINARY_VERSION}.${TBB_BINARY_MINOR_VERSION}
56        SOVERSION ${TBBBIND_BINARY_VERSION}
57    )
58    if (TBB_DEF_FILE_PREFIX) # If there's no prefix, assume we're using export directives
59        set_target_properties(${TBBBIND_NAME} PROPERTIES
60            LINK_FLAGS ${TBB_LINK_DEF_FILE_FLAG}${CMAKE_CURRENT_SOURCE_DIR}/def/${TBB_DEF_FILE_PREFIX}-tbbbind.def
61            LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/def/${TBB_DEF_FILE_PREFIX}-tbbbind.def
62        )
63    endif()
64
65    # Prefer using target_link_options instead of target_link_libraries to specify link options because
66    # target_link_libraries may incorrectly handle some options (on Windows, for example).
67    if (COMMAND target_link_options)
68        target_link_options(${TBBBIND_NAME}
69            PRIVATE
70            ${TBB_LIB_LINK_FLAGS}
71            ${TBB_COMMON_LINK_FLAGS}
72            ${TBB_IPO_LINK_FLAGS}
73        )
74    else()
75        target_link_libraries(${TBBBIND_NAME}
76            PRIVATE
77            ${TBB_LIB_LINK_FLAGS}
78            ${TBB_COMMON_LINK_FLAGS}
79            ${TBB_IPO_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
93    if (COMMAND tbb_gen_vars)
94        tbb_gen_vars(${TBBBIND_NAME})
95    endif()
96endfunction()
97
98if (NOT DEFINED HWLOC_TARGET_EXPLICITLY_DEFINED AND TARGET PkgConfig::HWLOC)
99    message(STATUS "The ${TBBBIND_LIBRARY_NAME} target will be configured using the HWLOC ${HWLOC_VERSION}")
100    tbbbind_build(${TBBBIND_LIBRARY_NAME} PkgConfig::HWLOC)
101else()
102    tbbbind_build(tbbbind     HWLOC::hwloc_1_11)
103    tbbbind_build(tbbbind_2_0 HWLOC::hwloc_2   )
104    tbbbind_build(tbbbind_2_5 HWLOC::hwloc_2_5 )
105endif()
106
107
108if (DEFINED CMAKE_SKIP_BUILD_RPATH_OLD_VALUE)
109    set(CMAKE_SKIP_BUILD_RPATH ${CMAKE_SKIP_BUILD_RPATH_OLD_VALUE})
110    unset(CMAKE_SKIP_BUILD_RPATH_OLD_VALUE)
111else()
112    unset(CMAKE_SKIP_BUILD_RPATH)
113endif()
114