Skip to content

Commit

Permalink
CMake: Add Apple framework support (#1319)
Browse files Browse the repository at this point in the history
Add cmake option TBB_BUILD_APPLE_FRAMEWORKS to build oneTBB as Apple Frameworks.
This is needed to be able to create iOS compatible app bundles which need to embed TBB as framework, as using dylibs is prohibited by Apple.

Co-authored-by: Ilya Isaev <[email protected]>
  • Loading branch information
geertbleyen and isaevil authored Apr 24, 2024
1 parent a5c8624 commit 39b4ff4
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ option(TBB_DISABLE_HWLOC_AUTOMATIC_SEARCH "Disable HWLOC automatic search by pkg
option(TBB_ENABLE_IPO "Enable Interprocedural Optimization (IPO) during the compilation" ON)
option(TBB_FUZZ_TESTING "Enable fuzz testing" OFF)
option(TBB_INSTALL "Enable installation" ON)
if(APPLE)
option(TBB_BUILD_APPLE_FRAMEWORKS "Build as Apple Frameworks" OFF)
endif()

if (NOT DEFINED BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS ON)
Expand Down
1 change: 1 addition & 0 deletions cmake/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ TBB_INSTALL_VARS:BOOL - Enable auto-generated vars installation(packages generat
TBB_VALGRIND_MEMCHECK:BOOL - Enable scan for memory leaks using Valgrind (OFF by default)
TBB_DISABLE_HWLOC_AUTOMATIC_SEARCH - Disable HWLOC automatic search by pkg-config tool (OFF by default)
TBB_ENABLE_IPO - Enable Interprocedural Optimization (IPO) during the compilation (ON by default)
TBB_BUILD_APPLE_FRAMEWORKS - Enable the Apple* frameworks instead of dylibs, only available on the Apple platform. (OFF by default)
```

## Configure, Build, and Test
Expand Down
8 changes: 6 additions & 2 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2023 Intel Corporation
# Copyright (c) 2020-2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,7 +35,11 @@ macro(tbb_install_target target)
COMPONENT runtime
ARCHIVE
DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT devel)
COMPONENT devel
FRAMEWORK
DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT runtime
OPTIONAL)

if (BUILD_SHARED_LIBS)
install(TARGETS ${target}
Expand Down
12 changes: 10 additions & 2 deletions cmake/vars_utils.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2021 Intel Corporation
# Copyright (c) 2020-2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -26,12 +26,20 @@ get_filename_component(TBB_VARS_TEMPLATE_NAME ${PROJECT_SOURCE_DIR}/integration/
string(REPLACE ".in" "" TBB_VARS_NAME ${TBB_VARS_TEMPLATE_NAME})

macro(tbb_gen_vars target)
if (NOT TBB_BUILD_APPLE_FRAMEWORKS)
set(BIN_PATH $<TARGET_FILE_DIR:${target}>)
else()
# For Apple* frameworks, the binaries are placed in a framework bundle.
# When using an Apple* framework, you refer to the bundle, not the binary inside, so we take the bundle's path and go up one level.
# This path will then be used to generate the vars file, and the contents of the vars file will use the bundle's parent directory.
set(BIN_PATH $<TARGET_BUNDLE_DIR:${target}>/..)
endif()
if (${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})
add_custom_command(TARGET ${target} POST_BUILD COMMAND
${CMAKE_COMMAND}
-DBINARY_DIR=${CMAKE_BINARY_DIR}
-DSOURCE_DIR=${PROJECT_SOURCE_DIR}
-DBIN_PATH=$<TARGET_FILE_DIR:${target}>
-DBIN_PATH=${BIN_PATH}
-DVARS_TEMPLATE=${TBB_VARS_TEMPLATE}
-DVARS_NAME=${TBB_VARS_NAME}
-DTBB_INSTALL_VARS=${TBB_INSTALL_VARS}
Expand Down
12 changes: 11 additions & 1 deletion src/tbb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2023 Intel Corporation
# Copyright (c) 2020-2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -126,6 +126,16 @@ target_link_libraries(tbb
${TBB_COMMON_LINK_LIBS}
)

if(TBB_BUILD_APPLE_FRAMEWORKS)
set_target_properties(tbb PROPERTIES
FRAMEWORK TRUE
FRAMEWORK_VERSION ${TBB_BINARY_VERSION}.${TBB_BINARY_MINOR_VERSION}
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER com.intel.tbb
MACOSX_FRAMEWORK_IDENTIFIER com.intel.tbb
MACOSX_FRAMEWORK_BUNDLE_VERSION ${TBB_BINARY_VERSION}.${TBB_BINARY_MINOR_VERSION}
MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${TBB_BINARY_VERSION})
endif()

tbb_install_target(tbb)

if (TBB_INSTALL)
Expand Down
14 changes: 12 additions & 2 deletions src/tbbmalloc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2023 Intel Corporation
# Copyright (c) 2020-2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -109,5 +109,15 @@ target_link_libraries(tbbmalloc
${TBB_COMMON_LINK_LIBS}
)

tbb_install_target(tbbmalloc)
if(TBB_BUILD_APPLE_FRAMEWORKS)
set_target_properties(tbbmalloc PROPERTIES
FRAMEWORK TRUE
FRAMEWORK_VERSION ${TBBMALLOC_BINARY_VERSION}.${TBB_BINARY_MINOR_VERSION}
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER com.intel.tbbmalloc
MACOSX_FRAMEWORK_IDENTIFIER com.intel.tbbmalloc
MACOSX_FRAMEWORK_BUNDLE_VERSION ${TBBMALLOC_BINARY_VERSION}.${TBB_BINARY_MINOR_VERSION}
MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${TBBMALLOC_BINARY_VERSION}
)
endif()

tbb_install_target(tbbmalloc)
12 changes: 11 additions & 1 deletion src/tbbmalloc_proxy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2022 Intel Corporation
# Copyright (c) 2020-2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -90,4 +90,14 @@ target_link_libraries(tbbmalloc_proxy
${TBB_COMMON_LINK_LIBS}
)

if(TBB_BUILD_APPLE_FRAMEWORKS)
set_target_properties(tbbmalloc_proxy PROPERTIES
FRAMEWORK TRUE
FRAMEWORK_VERSION ${TBBMALLOC_BINARY_VERSION}.${TBB_BINARY_MINOR_VERSION}
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER com.intel.tbbmalloc-proxy
MACOSX_FRAMEWORK_IDENTIFIER com.intel.tbbmalloc-proxy
MACOSX_FRAMEWORK_BUNDLE_VERSION ${TBBMALLOC_BINARY_VERSION}.${TBB_BINARY_MINOR_VERSION}
MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${TBBMALLOC_BINARY_VERSION})
endif()

tbb_install_target(tbbmalloc_proxy)
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ function(tbb_add_test)
${TBB_COMMON_COMPILE_FLAGS}
)

if (TBB_BUILD_APPLE_FRAMEWORKS)
add_compile_definitions(TBB_USE_APPLE_FRAMEWORKS)
endif()

if (ANDROID_PLATFORM)
# Expand the linker rpath by the CMAKE_LIBRARY_OUTPUT_DIRECTORY path since clang compiler from Android SDK
# doesn't respect the -L flag.
Expand Down
19 changes: 16 additions & 3 deletions test/common/utils_dynamic_libs.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2005-2021 Intel Corporation
Copyright (c) 2005-2024 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,9 +46,17 @@ namespace utils {
#endif
#define EXT ".dll"
#else
#if TBB_USE_APPLE_FRAMEWORKS
#define PREFIX // When built as Apple* Framework, the binary has no lib prefix
#else
#define PREFIX "lib"
#endif
#if __APPLE__
#if TBB_USE_APPLE_FRAMEWORKS
#define EXT // When built as Apple* Framework, the binary has no extension
#else
#define EXT ".dylib"
#endif
// Android SDK build system does not support .so file name versioning
#elif __FreeBSD__ || __NetBSD__ || __sun || _AIX || __ANDROID__
#define EXT ".so"
Expand All @@ -58,10 +66,15 @@ namespace utils {
#error Unknown OS
#endif
#endif
#if TBB_USE_APPLE_FRAMEWORKS
#define MALLOCFRAMEWORK "tbbmalloc.framework/"
#else
#define MALLOCFRAMEWORK
#endif

// Form the names of the TBB memory allocator binaries.
#define MALLOCLIB_NAME1 PREFIX "tbbmalloc" SUFFIX1 EXT
#define MALLOCLIB_NAME2 PREFIX "tbbmalloc" SUFFIX2 EXT
#define MALLOCLIB_NAME1 MALLOCFRAMEWORK PREFIX "tbbmalloc" SUFFIX1 EXT
#define MALLOCLIB_NAME2 MALLOCFRAMEWORK PREFIX "tbbmalloc" SUFFIX2 EXT

#if _WIN32 || _WIN64
using LIBRARY_HANDLE = HMODULE;
Expand Down

0 comments on commit 39b4ff4

Please sign in to comment.