[pstl] By default, disable the parallel policies
[platform/upstream/llvm.git] / pstl / CMakeLists.txt
1 #===-- CMakeLists.txt ----------------------------------------------------===##
2 #
3 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 # See https://llvm.org/LICENSE.txt for license information.
5 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 #
7 #===----------------------------------------------------------------------===##
8 cmake_minimum_required(VERSION 3.4.3)
9 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
10
11 set(PARALLELSTL_VERSION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/include/pstl/internal/pstl_config.h")
12 file(STRINGS "${PARALLELSTL_VERSION_FILE}" PARALLELSTL_VERSION_SOURCE REGEX "#define PSTL_VERSION .*$")
13 string(REGEX REPLACE "#define PSTL_VERSION (.*)$" "\\1" PARALLELSTL_VERSION_SOURCE "${PARALLELSTL_VERSION_SOURCE}")
14 math(EXPR VERSION_MAJOR "${PARALLELSTL_VERSION_SOURCE} / 100")
15 math(EXPR VERSION_MINOR "${PARALLELSTL_VERSION_SOURCE} % 100")
16
17 project(ParallelSTL VERSION ${VERSION_MAJOR}.${VERSION_MINOR} LANGUAGES CXX)
18
19 option(PARALLELSTL_USE_PARALLEL_POLICIES "Enable parallel policies" OFF)
20 set(PARALLELSTL_BACKEND "tbb" CACHE STRING "Threading backend; defaults to TBB")
21
22 include(CMakePackageConfigHelpers)
23
24 if (NOT TBB_DIR)
25     get_filename_component(PSTL_DIR_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
26     string(REPLACE pstl tbb TBB_DIR_NAME ${PSTL_DIR_NAME})
27     if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake")
28         get_filename_component(TBB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake" ABSOLUTE)
29     endif()
30 endif()
31
32 add_library(ParallelSTL INTERFACE)
33 add_library(pstl::ParallelSTL ALIAS ParallelSTL)
34
35 if (PARALLELSTL_USE_PARALLEL_POLICIES)
36     if (PARALLELSTL_BACKEND STREQUAL "tbb")
37         find_package(TBB 2018 REQUIRED tbb OPTIONAL_COMPONENTS tbbmalloc)
38         message(STATUS "Parallel STL uses TBB ${TBB_VERSION} (interface version: ${TBB_INTERFACE_VERSION})")
39         target_link_libraries(ParallelSTL INTERFACE TBB::tbb)
40     else()
41         if (TARGET ${PARALLELSTL_BACKEND})
42             target_link_libraries(ParallelSTL INTERFACE ${PARALLELSTL_BACKEND})
43         else()
44             find_package(${PARALLELSTL_BACKEND} REQUIRED)
45             target_link_libraries(ParallelSTL INTERFACE ${${PARALLELSTL_BACKEND}_IMPORTED_TARGETS})
46         endif()
47     endif()
48 else()
49     target_compile_definitions(ParallelSTL INTERFACE PSTL_USE_PARALLEL_POLICIES=0)
50 endif()
51
52 target_include_directories(ParallelSTL
53     INTERFACE
54     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
55     $<INSTALL_INTERFACE:include>)
56
57 write_basic_package_version_file(
58     ${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfigVersion.cmake
59     VERSION ${PROJECT_VERSION}
60     COMPATIBILITY AnyNewerVersion)
61
62 configure_file(
63     ParallelSTLConfig.cmake.in
64     ${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfig.cmake
65     @ONLY)
66
67 export(TARGETS ParallelSTL NAMESPACE pstl:: FILE ParallelSTLTargets.cmake)
68 export(PACKAGE ParallelSTL)
69
70 enable_testing()
71 add_subdirectory(test)