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#===----------------------------------------------------------------------===##
8cmake_minimum_required(VERSION 3.13.4)
9
10set(PARALLELSTL_VERSION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/include/pstl/internal/pstl_config.h")
11file(STRINGS "${PARALLELSTL_VERSION_FILE}" PARALLELSTL_VERSION_SOURCE REGEX "#define _PSTL_VERSION .*$")
12string(REGEX REPLACE "#define _PSTL_VERSION (.*)$" "\\1" PARALLELSTL_VERSION_SOURCE "${PARALLELSTL_VERSION_SOURCE}")
13math(EXPR VERSION_MAJOR "(${PARALLELSTL_VERSION_SOURCE} / 1000)")
14math(EXPR VERSION_MINOR "((${PARALLELSTL_VERSION_SOURCE} % 1000) / 10)")
15math(EXPR VERSION_PATCH "(${PARALLELSTL_VERSION_SOURCE} % 10)")
16
17project(ParallelSTL VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH} LANGUAGES CXX)
18
19set(PSTL_PARALLEL_BACKEND "serial" CACHE STRING "Threading backend to use. Valid choices are 'serial' and 'tbb'. The default is 'serial'.")
20set(PSTL_HIDE_FROM_ABI_PER_TU OFF CACHE BOOL "Whether to constrain ABI-unstable symbols to each translation unit (basically, mark them with C's static keyword).")
21set(_PSTL_HIDE_FROM_ABI_PER_TU ${PSTL_HIDE_FROM_ABI_PER_TU}) # For __pstl_config_site
22
23if (NOT TBB_DIR)
24    get_filename_component(PSTL_DIR_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
25    string(REPLACE pstl tbb TBB_DIR_NAME ${PSTL_DIR_NAME})
26    if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake")
27        get_filename_component(TBB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake" ABSOLUTE)
28    endif()
29endif()
30
31###############################################################################
32# Setup the ParallelSTL library target
33###############################################################################
34add_library(ParallelSTL INTERFACE)
35add_library(pstl::ParallelSTL ALIAS ParallelSTL)
36target_compile_features(ParallelSTL INTERFACE cxx_std_17)
37
38if (PSTL_PARALLEL_BACKEND STREQUAL "serial")
39    message(STATUS "Parallel STL uses the serial backend")
40    set(_PSTL_PAR_BACKEND_SERIAL ON)
41elseif (PSTL_PARALLEL_BACKEND STREQUAL "tbb")
42    find_package(TBB 2018 REQUIRED tbb OPTIONAL_COMPONENTS tbbmalloc)
43    message(STATUS "Parallel STL uses TBB ${TBB_VERSION} (interface version: ${TBB_INTERFACE_VERSION})")
44    target_link_libraries(ParallelSTL INTERFACE TBB::tbb)
45    set(_PSTL_PAR_BACKEND_TBB ON)
46else()
47    message(FATAL_ERROR "Requested unknown Parallel STL backend '${PSTL_PARALLEL_BACKEND}'.")
48endif()
49
50set(PSTL_GENERATED_HEADERS_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated_headers")
51set(PSTL_CONFIG_SITE_PATH "${PSTL_GENERATED_HEADERS_DIR}/__pstl_config_site")
52configure_file("include/__pstl_config_site.in"
53               "${PSTL_CONFIG_SITE_PATH}"
54               @ONLY)
55
56target_include_directories(ParallelSTL
57    INTERFACE
58    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
59    $<BUILD_INTERFACE:${PSTL_GENERATED_HEADERS_DIR}>
60    $<INSTALL_INTERFACE:include>)
61
62###############################################################################
63# Setup tests
64###############################################################################
65enable_testing()
66add_subdirectory(test)
67
68###############################################################################
69# Install the target and the associated CMake files
70###############################################################################
71include(CMakePackageConfigHelpers)
72write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfigVersion.cmake"
73                                 COMPATIBILITY ExactVersion)
74
75configure_file(cmake/ParallelSTLConfig.cmake.in
76               "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfig.cmake"
77               @ONLY)
78
79install(TARGETS ParallelSTL
80        EXPORT ParallelSTLTargets)
81install(EXPORT ParallelSTLTargets
82        FILE ParallelSTLTargets.cmake
83        NAMESPACE pstl::
84        DESTINATION lib/cmake/ParallelSTL)
85install(FILES "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfig.cmake"
86              "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfigVersion.cmake"
87        DESTINATION lib/cmake/ParallelSTL)
88install(DIRECTORY include/
89        DESTINATION include
90        PATTERN "*.in" EXCLUDE)
91install(FILES "${PSTL_CONFIG_SITE_PATH}"
92        DESTINATION include)
93
94add_custom_target(install-pstl
95                  COMMAND "${CMAKE_COMMAND}" -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" -DCOMPONENT=ParallelSTL)
96