Skip to content

Commit db71e18

Browse files
committed
TBB download support.
1 parent f952ee0 commit db71e18

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@
3434
# Remove build
3535
build
3636

37+
# tbb
38+
externals/tbb-*
39+
3740
# Ignore vscode
3841
.vscode

CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ project(essentials
1919
)
2020

2121
# Dependencies directory.
22+
include(${PROJECT_SOURCE_DIR}/cmake/TBB.cmake)
23+
set(TBB_DIR ${TBB_SOURCE_DIR}/lib/cmake/tbb)
24+
find_package(TBB)
2225
set(PROJECT_DEPS_DIR externals)
2326

2427
# Add executables.
@@ -31,11 +34,14 @@ add_library(${PROJECT_NAME} SHARED
3134
target_include_directories(${PROJECT_NAME}
3235
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
3336
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
37+
# PUBLIC ${TBB_INCLUDE_DIR}
3438
# PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_DEPS_DIR}
3539
)
3640

3741
# Link with the libraries.
38-
# target_link_libraries(${PROJECT_NAME})
42+
target_link_libraries(${PROJECT_NAME}
43+
PUBLIC TBB::tbb
44+
)
3945

4046
# Set target properties.
4147
# https://github.com/microsoft/STL/issues/1814

cmake/FindTBB.cmake

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
15+
include(FindPackageHandleStandardArgs)
16+
17+
# Firstly search for TBB in config mode (i.e. search for TBBConfig.cmake).
18+
find_package(TBB QUIET CONFIG)
19+
if (TBB_FOUND)
20+
find_package_handle_standard_args(TBB CONFIG_MODE)
21+
return()
22+
endif()
23+
24+
if (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()
29+
endif()
30+
31+
if (WIN32)
32+
list(APPEND ADDITIONAL_LIB_DIRS ENV PATH ENV LIB)
33+
list(APPEND ADDITIONAL_INCLUDE_DIRS ENV INCLUDE ENV CPATH)
34+
else()
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)
37+
endif()
38+
39+
find_path(_tbb_include_dir NAMES tbb/tbb.h PATHS ${ADDITIONAL_INCLUDE_DIRS})
40+
41+
if (_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)
82+
endif()
83+
unset(_tbb_include_dir CACHE)
84+
85+
list(REMOVE_DUPLICATES TBB_IMPORTED_TARGETS)
86+
87+
find_package_handle_standard_args(TBB
88+
REQUIRED_VARS TBB_IMPORTED_TARGETS
89+
HANDLE_COMPONENTS)

cmake/TBB.cmake

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
include(FetchContent)
2+
set(FETCHCONTENT_QUIET ON)
3+
4+
message(STATUS "Cloning External Project: TBB")
5+
get_filename_component(FC_BASE "../externals"
6+
REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
7+
set(FETCHCONTENT_BASE_DIR ${FC_BASE})
8+
9+
# Manually set the URLs from https://github.com/oneapi-src/oneTBB/releases/
10+
if (MSVC)
11+
set(DOWNLOAD_URL https://github.com/oneapi-src/oneTBB/releases/download/v2021.5.0/oneapi-tbb-2021.5.0-win.zip)
12+
set(DOWNLOAD_HASH 096c004c7079af89fe990bb259d58983b0ee272afa3a7ef0733875bfe09fcd8e)
13+
else()
14+
set(DOWNLOAD_URL https://github.com/oneapi-src/oneTBB/releases/download/v2021.5.0/oneapi-tbb-2021.5.0-lin.tgz)
15+
set(DOWNLOAD_HASH 74861b1586d6936b620cdab6775175de46ad8b0b36fa6438135ecfb8fb5bdf98)
16+
endif()
17+
18+
FetchContent_Declare(
19+
TBB
20+
URL ${DOWNLOAD_URL}
21+
URL_HASH SHA256=${DOWNLOAD_HASH}
22+
)
23+
24+
FetchContent_MakeAvailable(TBB)
25+
set(TBB_SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/tbb-src)
26+
set(TBB_INCLUDE_DIR ${FETCHCONTENT_BASE_DIR}/tbb-src/include)

0 commit comments

Comments
 (0)