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 install config bugfix #127

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
76 changes: 75 additions & 1 deletion cmake/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,80 @@ 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
=========================================== ===========================================================
``INSTALL_DIR <directory>`` Directory to install CMake configuration files
``SYSTEM_NAME Linux|Darwin`` OS name to generate config files for
``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 -DSYSTEM_NAME=Linux -DTBB_VERSION_FILE=/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 -DSYSTEM_NAME=Linux -DLIB_PATH=/usr/lib -DINC_PATH=/usr/include -P tbb_config_installer.cmake``.

TBBGet
^^^^^^

Expand All @@ -234,7 +308,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
98 changes: 98 additions & 0 deletions cmake/TBBInstallConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# 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)
file(MAKE_DIRECTORY ${config_install_dir})

# --- 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
130 changes: 130 additions & 0 deletions cmake/build_tbb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/bin/bash
# \author Hans J. Johnson
#
# This is a wrapper around the various components
# tbb build environment needed to build and configure
# a typical unix file system layout with cmake packing
# support
#
# It requires make, python, and cmake
# to get a standard build and install
# layout.
#
# Usage: buld_tbb.sh -b build_directory -p install_prefix
# EG: ./build_tbb.sh -b /tmp -p /home/myaccout/local
#
# A test case then attempts to build an external application
# against the newly installed version of tbb by referencing
# the cmake packaging files. The external application use
# of find_package(TBB) is purposefully more complicated
# than this test requires in order to simulate support
# for usecases where multiple modules may require
# different components of tbb, thus requiring multiple
# calls to find_package(TBB)
#
set -ev

while getopts ":b:p:" opt; do
case ${opt} in
b ) # process option for build CACHE
BUILD_CACHE=$OPTARG
;;
p ) # process option
INSTALL_PREFIX=$OPTARG
;;
\? ) echo "Usage: cmd [-h] [-t]"
;;
esac
done

if [ -z ${BUILD_CACHE+x} ]; then
echo "Usage was wrong, -b is a required option"
exit -1
fi
if [ -z ${INSTALL_PREFIX+x} ]; then
INSTALL_PREFIX=${BUILD_CACHE}/tbb-install
fi

SRC_DIR=${BUILD_CACHE}/tbb

SYSTEM_NAME=Darwin
TBB_VERSION_FILE=${SRC_DIR}/include/tbb/tbb_stddef.h

mkdir -p ${BUILD_CACHE}
mkdir -p ${INSTALL_PREFIX}

pushd ${BUILD_CACHE}
if [[ ! -d tbb ]]; then
git clone https://github.com/AlexVeprev/tbb.git
fi
GIT_TAG=cmake-install-config
pushd tbb
git checkout ${GIT_TAG}
popd


python ${SRC_DIR}/build/build.py \
--tbbroot ${SRC_DIR} \
--prefix ${INSTALL_PREFIX} \
--install-libs --install-devel --install-docs

cmake -DINSTALL_DIR=${INSTALL_PREFIX}/lib/cmake/tbb \
-DLIB_REL_PATH=../../../lib \
-DINC_REL_PATH=../../../include \
-DSYSTEM_NAME=$(uname) \
-DTBB_VERSION_FILE=${TBB_VERSION_FILE} \
-P ${SRC_DIR}/cmake/tbb_config_installer.cmake


DO_TESTING=1
if [[ ${DO_TESTING} -eq 1 ]]; then

TEST_DIR=${BUILD_CACHE}/tbb_test
mkdir -p ${TEST_DIR}

cat > ${TEST_DIR}/CMakeLists.txt << EOF
# Put this cmake file in a directory, run "cmake ." finding TBB results in a FATAL_ERROR from cmake
cmake_minimum_required(VERSION 3.0)
# tbb;tbbmalloc;tbbmalloc_proxy
find_package(TBB REQUIRED COMPONENTS tbbmalloc CONFIG) #Find one component - test
message(STATUS "COMPONENTS FOUND: TBB_IMPORTED_TARGETS=:\${TBB_IMPORTED_TARGETS}: TBB_tbb_FOUND=:\${TBB_tbb_FOUND}: TBB_tbbmalloc_FOUND=:\${TBB_tbbmalloc_FOUND}: TBB_tbbmalloc_proxy_FOUND=:\${TBB_tbbmalloc_proxy_FOUND}:")
find_package(TBB REQUIRED COMPONENTS tbbmalloc_proxy CONFIG) #Find one component - test
message(STATUS "COMPONENTS FOUND: TBB_IMPORTED_TARGETS=:\${TBB_IMPORTED_TARGETS}: TBB_tbb_FOUND=:\${TBB_tbb_FOUND}: TBB_tbbmalloc_FOUND=:\${TBB_tbbmalloc_FOUND}: TBB_tbbmalloc_proxy_FOUND=:\${TBB_tbbmalloc_proxy_FOUND}:")
find_package(TBB REQUIRED COMPONENTS tbb CONFIG) #Find one component - test
message(STATUS "COMPONENTS FOUND: TBB_IMPORTED_TARGETS=:\${TBB_IMPORTED_TARGETS}: TBB_tbb_FOUND=:\${TBB_tbb_FOUND}: TBB_tbbmalloc_FOUND=:\${TBB_tbbmalloc_FOUND}: TBB_tbbmalloc_proxy_FOUND=:\${TBB_tbbmalloc_proxy_FOUND}:")
find_package(TBB REQUIRED CONFIG) # Should be able to find_package many times
message(STATUS "COMPONENTS FOUND: TBB_IMPORTED_TARGETS=:\${TBB_IMPORTED_TARGETS}: TBB_tbb_FOUND=:\${TBB_tbb_FOUND}: TBB_tbbmalloc_FOUND=:\${TBB_tbbmalloc_FOUND}: TBB_tbbmalloc_proxy_FOUND=:\${TBB_tbbmalloc_proxy_FOUND}:")
if(NOT TBB_FOUND)
message(FATAL_ERROR "NO TBB")
else()
message(STATUS "FOUND TBB")
endif()
add_executable(tbb_test test.cpp)
target_link_libraries(tbb_test TBB::tbb)
EOF

cat > ${TEST_DIR}/test.cpp << EOF
#include <tbb/task_scheduler_init.h>
#include <iostream>
int main()
{
std::cout << "Default number of threads: " << tbb::task_scheduler_init::default_num_threads() << std::endl;;
return 0;
}
EOF

mkdir -p ${TEST_DIR}-bld
pushd ${TEST_DIR}-bld
cmake -DCMAKE_BUILD_TYPE:STRING=Release -DTBB_DIR:PATH=${INSTALL_PREFIX}/lib/cmake/tbb ${TEST_DIR}
make
./tbb_test

if [[ $? -eq 0 ]]; then
echo "SUCCESSFUL TESTING"
exit 0
else
echo "FAILED TESTING"
exit -1
fi

fi
Loading