Initial PSTL commit
[lldb.git] / pstl / CMakeLists.txt
1 #===-- CMakeLists.txt ----------------------------------------------------===##
2 #
3 #                     The LLVM Compiler Infrastructure
4 #
5 # This file is dual licensed under the MIT and the University of Illinois Open
6 # Source Licenses. See LICENSE.TXT for details.
7 #
8 #===----------------------------------------------------------------------===##
9
10 cmake_minimum_required(VERSION 3.1)
11
12 set(PARALLELSTL_VERSION_FILE "include/pstl/internal/pstl_config.h")
13 file(STRINGS "${PARALLELSTL_VERSION_FILE}" PARALLELSTL_VERSION_SOURCE REGEX "#define PSTL_VERSION .*$")
14 string(REGEX MATCH "#define PSTL_VERSION (.*)$" PARALLELSTL_VERSION_SOURCE "${PARALLELSTL_VERSION_SOURCE}")
15 math(EXPR VERSION_MAJOR "${PARALLELSTL_VERSION_SOURCE} / 100")
16 math(EXPR VERSION_MINOR "${PARALLELSTL_VERSION_SOURCE} % 100")
17
18 project(ParallelSTL VERSION ${VERSION_MAJOR}.${VERSION_MINOR} LANGUAGES CXX)
19
20 option(PARALLELSTL_USE_PARALLEL_POLICIES "Enable parallel policies" ON)
21 set(PARALLELSTL_BACKEND "tbb" CACHE STRING "Threading backend; defaults to TBB")
22
23 include(CMakePackageConfigHelpers)
24
25 if (NOT TBB_DIR)
26     get_filename_component(PSTL_DIR_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
27     string(REPLACE pstl tbb TBB_DIR_NAME ${PSTL_DIR_NAME})
28     if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake")
29         get_filename_component(TBB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake" ABSOLUTE)
30     endif()
31 endif()
32
33 set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
34
35 add_library(ParallelSTL INTERFACE)
36 add_library(pstl::ParallelSTL ALIAS ParallelSTL)
37
38 if (PARALLELSTL_USE_PARALLEL_POLICIES)
39     if (PARALLELSTL_BACKEND STREQUAL "tbb")
40         find_package(TBB 2018 REQUIRED tbb OPTIONAL_COMPONENTS tbbmalloc)
41         message(STATUS "Parallel STL uses TBB ${TBB_VERSION} (interface version: ${TBB_INTERFACE_VERSION})")
42         target_link_libraries(ParallelSTL INTERFACE TBB::tbb)
43     else()
44         if (TARGET ${PARALLELSTL_BACKEND})
45             target_link_libraries(ParallelSTL INTERFACE ${PARALLELSTL_BACKEND})
46         else()
47             find_package(${PARALLELSTL_BACKEND} REQUIRED)
48             target_link_libraries(ParallelSTL INTERFACE ${${PARALLELSTL_BACKEND}_IMPORTED_TARGETS})
49         endif()
50     endif()
51 else()
52     target_add_definitions(ParallelSTL INTERFACE PSTL_USE_PARALLEL_POLICIES=0)
53 endif()
54
55 target_include_directories(ParallelSTL
56     INTERFACE
57     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
58     $<INSTALL_INTERFACE:include>)
59
60 write_basic_package_version_file(
61     ${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfigVersion.cmake
62     VERSION ${PROJECT_VERSION}
63     COMPATIBILITY AnyNewerVersion)
64
65 configure_file(
66     ParallelSTLConfig.cmake.in
67     ${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfig.cmake
68     @ONLY)
69
70 export(TARGETS ParallelSTL NAMESPACE pstl:: FILE ParallelSTLTargets.cmake)
71 export(PACKAGE ParallelSTL)