Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake: add module TBBInstallConfig #126

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion cmake/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,78 @@ Variables set during Intel TBB configuration:
``TBB_INTERFACE_VERSION`` Intel TBB interface version
========================= ================================================

TBBInstallConfig
^^^^^^^^^^^^^^^^

Module for generation and installation of TBB CMake configuration files (TBBConfig.cmake and TBBConfigVersion.cmake files) on Linux and macOS.

Provides the following functions:

.. code:: cmake

tbb_install_config(INSTALL_DIR <install_dir> SYSTEM_NAME Linux|Darwin
[TBB_VERSION <major>.<minor>.<interface>|TBB_VERSION_FILE <version_file>]
[LIB_REL_PATH <lib_rel_path> INC_REL_PATH <inc_rel_path>]
[LIB_PATH <lib_path> INC_PATH <inc_path>])``

**Note: the module overwrites existing TBBConfig.cmake and TBBConfigVersion.cmake files in <install_dir>.**

``tbb_config_installer.cmake`` allows to run ``TBBInstallConfig.cmake`` from command line.
It accepts the same parameters as ``tbb_install_config`` function, run ``cmake -P tbb_config_installer.cmake`` to get help.

Use cases
"""""""""
**Prepare TBB CMake configuration files for custom TBB package.**

The use case is applicable for package maintainers who create own TBB packages and want to create TBBConfig.cmake and TBBConfigVersion.cmake for these packages.

=========================================== ===========================================================
Parameter Description
=========================================== ===========================================================
``TBB_VERSION_FILE <version_file>`` Path to ``tbb_stddef.h`` to parse version from and
write it to TBBConfigVersion.cmake
``TBB_VERSION <major>.<minor>.<interface>`` Directly specified TBB version;
alternative to ``TBB_VERSION_FILE`` parameter
``LIB_REL_PATH <lib_rel_path>`` Relative path to TBB binaries, default: ``../..``
``INC_REL_PATH <inc_rel_path>`` Relative path to TBB headers, default: ``../../../include``
=========================================== ===========================================================

*Example*

Assume your package is installed to the following structure:

* Binaries go to ``<prefix>/lib``
* Headers go to ``<prefix>/include``
* CMake configuration files go to ``<prefix>/lib/cmake/<package>``

The package is packed from ``/my/package/content`` directory.

``cmake -DINSTALL_DIR=/my/package/content/lib/cmake/TBB -DTBB_VERSION=/my/package/content/include/tbb/tbb_stddef.h -P tbb_config_installer.cmake`` (default relative paths will be used)

**Install TBB CMake configuration files for installed TBB.**

The use case is applicable for users who have installed TBB, but do not have (or have incorrect) CMake configuration files for this TBB.

============================ ==============================================
Parameter Description
============================ ==============================================
``INSTALL_DIR <directory>`` Directory to install CMake configuration files
``SYSTEM_NAME Linux|Darwin`` OS name to generate config files for
``LIB_PATH <lib_path>`` Path to installed TBB binaries
``INC_PATH <inc_path>`` Path to installed TBB headers
============================ ==============================================

``LIB_PATH`` and ``INC_PATH`` will be converted to relative paths based on ``INSTALL_DIR``.
By default TBB version will be parsed from ``<inc_path>/tbb/tbb_stddef.h``,
but it can be overridden by optional parameters ``TBB_VERSION_FILE`` or ``TBB_VERSION``.

*Example*

TBB is installed to ``/usr`` directory.
In order to create TBBConfig.cmake and TBBConfigVersion.cmake in ``/usr/lib/cmake/TBB`` run

``cmake -DINSTALL_DIR=/usr/lib/cmake/TBB -DLIB_PATH=/usr/lib -DINC_PATH=/usr/include -P tbb_config_installer.cmake``.

TBBGet
^^^^^^

Expand All @@ -234,7 +306,7 @@ Provides the following functions:
TBBMakeConfig
^^^^^^^^^^^^^

Module for making TBBConfig in ``Intel(R) Threading Building Blocks (Intel(R) TBB)`` binary package.
Module for making TBBConfig in `official TBB binary packages published on GitHub <https://github.com/01org/tbb/releases>`_.

This module is to be used for packages that do not have TBBConfig.

Expand Down
97 changes: 97 additions & 0 deletions cmake/TBBInstallConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright (c) 2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#
#

include(CMakeParseArguments)

# Save the location of Intel TBB CMake modules here, as it will not be possible to do inside functions,
# see for details: https://cmake.org/cmake/help/latest/variable/CMAKE_CURRENT_LIST_DIR.html
set(_tbb_cmake_module_path ${CMAKE_CURRENT_LIST_DIR})

function(tbb_install_config)
set(oneValueArgs INSTALL_DIR
SYSTEM_NAME
LIB_REL_PATH INC_REL_PATH TBB_VERSION TBB_VERSION_FILE
LIB_PATH INC_PATH) # If TBB is installed on the system

cmake_parse_arguments(tbb_IC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

get_filename_component(config_install_dir ${tbb_IC_INSTALL_DIR} ABSOLUTE)

# --- TBB_LIB_REL_PATH handling ---
set(TBB_LIB_REL_PATH "../..")

if (tbb_IC_LIB_REL_PATH)
set(TBB_LIB_REL_PATH ${tbb_IC_LIB_REL_PATH})
endif()

if (tbb_IC_LIB_PATH)
get_filename_component(lib_abs_path ${tbb_IC_LIB_PATH} ABSOLUTE)
file(RELATIVE_PATH TBB_LIB_REL_PATH ${config_install_dir} ${lib_abs_path})
unset(lib_abs_path)
endif()
# ------

# --- TBB_INC_REL_PATH handling ---
set(TBB_INC_REL_PATH "../../../include")

if (tbb_IC_INC_REL_PATH)
set(TBB_INC_REL_PATH ${tbb_IC_INC_REL_PATH})
endif()

if (tbb_IC_INC_PATH)
get_filename_component(inc_abs_path ${tbb_IC_INC_PATH} ABSOLUTE)
file(RELATIVE_PATH TBB_INC_REL_PATH ${config_install_dir} ${inc_abs_path})
unset(inc_abs_path)
endif()
# ------

# --- TBB_VERSION handling ---
if (tbb_IC_TBB_VERSION)
set(TBB_VERSION ${tbb_IC_TBB_VERSION})
else()
set(tbb_version_file "${config_install_dir}/${TBB_INC_REL_PATH}/tbb/tbb_stddef.h")
if (tbb_IC_TBB_VERSION_FILE)
set(tbb_version_file ${tbb_IC_TBB_VERSION_FILE})
endif()

file(READ ${tbb_version_file} _tbb_stddef)
string(REGEX REPLACE ".*#define TBB_VERSION_MAJOR ([0-9]+).*" "\\1" _tbb_ver_major "${_tbb_stddef}")
string(REGEX REPLACE ".*#define TBB_VERSION_MINOR ([0-9]+).*" "\\1" _tbb_ver_minor "${_tbb_stddef}")
string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" _tbb_ver_interface "${_tbb_stddef}")
set(TBB_VERSION "${_tbb_ver_major}.${_tbb_ver_minor}.${_tbb_ver_interface}")
endif()
# ------

set(tbb_system_name ${CMAKE_SYSTEM_NAME})
if (tbb_IC_SYSTEM_NAME)
set(tbb_system_name ${tbb_IC_SYSTEM_NAME})
endif()

if (tbb_system_name STREQUAL "Linux")
set(TBB_LIB_PREFIX "lib")
set(TBB_LIB_EXT "so.2")
elseif (tbb_system_name STREQUAL "Darwin")
set(TBB_LIB_PREFIX "lib")
set(TBB_LIB_EXT "dylib")
else()
message(FATAL_ERROR "Unsupported OS name: ${tbb_system_name}")
endif()

configure_file(${_tbb_cmake_module_path}/templates/TBBConfig.cmake.in ${config_install_dir}/TBBConfig.cmake @ONLY)
configure_file(${_tbb_cmake_module_path}/templates/TBBConfigVersion.cmake.in ${config_install_dir}/TBBConfigVersion.cmake @ONLY)
endfunction()
2 changes: 1 addition & 1 deletion cmake/TBBMakeConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ endif()")
else()
set(_tbb_config_template TBBConfig.cmake.in)
endif()
configure_file(${_tbb_cmake_module_path}/templates/${_tbb_config_template} ${tbb_config_dir}/TBBConfig.cmake @ONLY)
configure_file(${_tbb_cmake_module_path}/templates/${_tbb_config_template} ${tbb_config_dir}/TBBConfigInternal.cmake @ONLY)
configure_file(${_tbb_cmake_module_path}/templates/TBBConfigVersion.cmake.in ${tbb_config_dir}/TBBConfigVersion.cmake @ONLY)

set(${tbb_MK_CONFIG_DIR} ${tbb_config_dir} PARENT_SCOPE)
Expand Down
50 changes: 50 additions & 0 deletions cmake/tbb_config_installer.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) 2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#
#

function(tbb_conf_gen_print_help)
message("Usage: cmake -DINSTALL_DIR=<config_install_dir> -DSYSTEM_NAME=Linux|Darwin <parameters> -P tbb_config_generator.cmake

Parameters:
For custom TBB package:
-DTBB_VERSION_FILE=<tbb_version_file>
-DTBB_VERSION=<major>.<minor>.<interface> (alternative to TBB_VERSION_FILE)
-DLIB_REL_PATH=<relative_path_to_tbb_binaries>
-DINC_REL_PATH=<relative_path_to_tbb_headers>
For installed TBB:
-DLIB_PATH=<path_to_installed_tbb_binaries>
-DINC_PATH=<path_to_installed_tbb_headers>
")
endfunction()

if (NOT DEFINED INSTALL_DIR)
tbb_conf_gen_print_help()
message(FATAL_ERROR "Required parameter INSTALL_DIR is not defined")
endif()

if (NOT DEFINED SYSTEM_NAME)
tbb_conf_gen_print_help()
message(FATAL_ERROR "Required parameter SYSTEM_NAME is not defined")
endif()

foreach (arg TBB_VERSION LIB_REL_PATH INC_REL_PATH TBB_VERSION_FILE LIB_PATH INC_PATH)
set(optional_args ${optional_args} ${arg} ${${arg}})
endforeach()

include(${CMAKE_CURRENT_LIST_DIR}/TBBInstallConfig.cmake)
tbb_install_config(INSTALL_DIR ${INSTALL_DIR} SYSTEM_NAME ${SYSTEM_NAME} ${optional_args})
message(STATUS "TBBConfig files were created in ${INSTALL_DIR}")
59 changes: 23 additions & 36 deletions cmake/templates/TBBConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017-2018 Intel Corporation
# Copyright (c) 2017-2019 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 All @@ -20,7 +20,7 @@
# Handling of TBB_VERSION is in TBBConfigVersion.cmake.

if (NOT TBB_FIND_COMPONENTS)
set(TBB_FIND_COMPONENTS "@TBB_DEFAULT_COMPONENTS@")
set(TBB_FIND_COMPONENTS "tbb tbbmalloc tbbmalloc_proxy")
foreach (_tbb_component ${TBB_FIND_COMPONENTS})
set(TBB_FIND_REQUIRED_${_tbb_component} 1)
endforeach()
Expand All @@ -34,37 +34,30 @@ if (NOT _tbbmalloc_proxy_ix EQUAL -1)
list(APPEND TBB_FIND_COMPONENTS tbbmalloc)
set(TBB_FIND_REQUIRED_tbbmalloc ${TBB_FIND_REQUIRED_tbbmalloc_proxy})
endif()
unset(_tbbmalloc_ix)
endif()

set(TBB_INTERFACE_VERSION @TBB_INTERFACE_VERSION@)

get_filename_component(_tbb_root "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_tbb_root "${_tbb_root}" PATH)

set(_tbb_x32_subdir @TBB_X32_SUBDIR@)
set(_tbb_x64_subdir @TBB_X64_SUBDIR@)

if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(_tbb_arch_subdir ${_tbb_x64_subdir})
else()
set(_tbb_arch_subdir ${_tbb_x32_subdir})
endif()

@TBB_CHOOSE_COMPILER_SUBDIR@

get_filename_component(_tbb_lib_path "${_tbb_root}/@TBB_SHARED_LIB_DIR@/${_tbb_arch_subdir}/${_tbb_compiler_subdir}" ABSOLUTE)
unset(_tbbmalloc_proxy_ix)

foreach (_tbb_component ${TBB_FIND_COMPONENTS})
set(_tbb_release_lib "${_tbb_lib_path}/@TBB_LIB_PREFIX@${_tbb_component}.@TBB_LIB_EXT@")
set(_tbb_debug_lib "${_tbb_lib_path}/@TBB_LIB_PREFIX@${_tbb_component}_debug.@TBB_LIB_EXT@")
set(_tbb_release_lib "${CMAKE_CURRENT_LIST_FILE}/@TBB_LIB_REL_PATH@/@TBB_LIB_PREFIX@${_tbb_component}.@TBB_LIB_EXT@")
AlexVeprev marked this conversation as resolved.
Show resolved Hide resolved
set(_tbb_debug_lib "${CMAKE_CURRENT_LIST_FILE}/@TBB_LIB_REL_PATH@/@TBB_LIB_PREFIX@${_tbb_component}_debug.@TBB_LIB_EXT@")

if (EXISTS "${_tbb_release_lib}" AND EXISTS "${_tbb_debug_lib}")
if (EXISTS "${_tbb_release_lib}" OR EXISTS "${_tbb_debug_lib}")
add_library(TBB::${_tbb_component} SHARED IMPORTED)
set_target_properties(TBB::${_tbb_component} PROPERTIES
IMPORTED_CONFIGURATIONS "RELEASE;DEBUG"
IMPORTED_LOCATION_RELEASE "${_tbb_release_lib}"
IMPORTED_LOCATION_DEBUG "${_tbb_debug_lib}"
INTERFACE_INCLUDE_DIRECTORIES "${_tbb_root}/include"@TBB_IMPLIB@@TBB_COMPILE_DEFINITIONS@)
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_FILE}/@TBB_INC_REL_PATH@")

if (EXISTS "${_tbb_release_lib}")
set_target_properties(TBB::${_tbb_component} PROPERTIES
IMPORTED_LOCATION_RELEASE "${_tbb_release_lib}")
set_property(TARGET TBB::${_tbb_component} APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
endif()

if (EXISTS "${_tbb_debug_lib}")
set_target_properties(TBB::${_tbb_component} PROPERTIES
IMPORTED_LOCATION_DEBUG "${_tbb_debug_lib}")
set_property(TARGET TBB::${_tbb_component} APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
endif()

# Add internal dependencies for imported targets: TBB::tbbmalloc_proxy -> TBB::tbbmalloc
if (_tbb_component STREQUAL tbbmalloc_proxy)
Expand All @@ -74,16 +67,10 @@ foreach (_tbb_component ${TBB_FIND_COMPONENTS})
list(APPEND TBB_IMPORTED_TARGETS TBB::${_tbb_component})
set(TBB_${_tbb_component}_FOUND 1)
elseif (TBB_FIND_REQUIRED AND TBB_FIND_REQUIRED_${_tbb_component})
message(FATAL_ERROR "Missed required Intel TBB component: ${_tbb_component}")
message(STATUS "Missed required Intel TBB component: ${_tbb_component}")
set(TBB_FOUND FALSE)
set(TBB_${_tbb_component}_FOUND 0)
endif()
endforeach()

unset(_tbb_x32_subdir)
unset(_tbb_x64_subdir)
unset(_tbb_arch_subdir)
unset(_tbb_compiler_subdir)
unset(_tbbmalloc_proxy_ix)
unset(_tbbmalloc_ix)
unset(_tbb_lib_path)
unset(_tbb_release_lib)
unset(_tbb_debug_lib)
Loading