1 #===-- CMakeLists.txt ----------------------------------------------------===##
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
7 #===----------------------------------------------------------------------===##
8 cmake_minimum_required(VERSION 3.20.0)
9 set(LLVM_SUBPROJECT_TITLE "Parallel STL")
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} / 1000)")
15 math(EXPR VERSION_MINOR "((${PARALLELSTL_VERSION_SOURCE} % 1000) / 10)")
16 math(EXPR VERSION_PATCH "(${PARALLELSTL_VERSION_SOURCE} % 10)")
18 project(ParallelSTL VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH} LANGUAGES CXX)
20 # Must go below project(..)
21 include(GNUInstallDirs)
23 set(PSTL_PARALLEL_BACKEND "serial" CACHE STRING "Threading backend to use. Valid choices are 'serial', 'omp', and 'tbb'. The default is 'serial'.")
24 set(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).")
25 set(_PSTL_HIDE_FROM_ABI_PER_TU ${PSTL_HIDE_FROM_ABI_PER_TU}) # For __pstl_config_site
28 get_filename_component(PSTL_DIR_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
29 string(REPLACE pstl tbb TBB_DIR_NAME ${PSTL_DIR_NAME})
30 if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake")
31 get_filename_component(TBB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake" ABSOLUTE)
35 ###############################################################################
36 # Setup the ParallelSTL library target
37 ###############################################################################
38 add_library(ParallelSTL INTERFACE)
39 add_library(pstl::ParallelSTL ALIAS ParallelSTL)
40 target_compile_features(ParallelSTL INTERFACE cxx_std_17)
42 if (PSTL_PARALLEL_BACKEND STREQUAL "serial")
43 message(STATUS "Parallel STL uses the serial backend")
44 set(_PSTL_PAR_BACKEND_SERIAL ON)
45 elseif (PSTL_PARALLEL_BACKEND STREQUAL "tbb")
46 find_package(TBB 2018 REQUIRED tbb OPTIONAL_COMPONENTS tbbmalloc)
47 message(STATUS "Parallel STL uses TBB ${TBB_VERSION} (interface version: ${TBB_INTERFACE_VERSION})")
48 target_link_libraries(ParallelSTL INTERFACE TBB::tbb)
49 set(_PSTL_PAR_BACKEND_TBB ON)
50 elseif (PSTL_PARALLEL_BACKEND STREQUAL "omp")
51 message(STATUS "Parallel STL uses the omp backend")
52 target_compile_options(ParallelSTL INTERFACE "-fopenmp=libomp")
53 set(_PSTL_PAR_BACKEND_OPENMP ON)
55 message(FATAL_ERROR "Requested unknown Parallel STL backend '${PSTL_PARALLEL_BACKEND}'.")
58 set(PSTL_GENERATED_HEADERS_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated_headers")
59 set(PSTL_CONFIG_SITE_PATH "${PSTL_GENERATED_HEADERS_DIR}/__pstl_config_site")
60 configure_file("include/__pstl_config_site.in"
61 "${PSTL_CONFIG_SITE_PATH}"
64 target_include_directories(ParallelSTL
66 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
67 $<BUILD_INTERFACE:${PSTL_GENERATED_HEADERS_DIR}>
68 $<INSTALL_INTERFACE:include>)
70 ###############################################################################
72 ###############################################################################
74 add_subdirectory(test)
76 ###############################################################################
77 # Install the target and the associated CMake files
78 ###############################################################################
79 include(CMakePackageConfigHelpers)
80 write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfigVersion.cmake"
81 COMPATIBILITY ExactVersion)
83 configure_file(cmake/ParallelSTLConfig.cmake.in
84 "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfig.cmake"
87 install(TARGETS ParallelSTL
88 EXPORT ParallelSTLTargets)
89 install(EXPORT ParallelSTLTargets
90 FILE ParallelSTLTargets.cmake
92 DESTINATION lib/cmake/ParallelSTL)
93 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfig.cmake"
94 "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfigVersion.cmake"
95 DESTINATION lib/cmake/ParallelSTL)
96 install(DIRECTORY include/
97 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
98 PATTERN "*.in" EXCLUDE)
99 install(FILES "${PSTL_CONFIG_SITE_PATH}"
100 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
102 add_custom_target(install-pstl
103 COMMAND "${CMAKE_COMMAND}" -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" -DCOMPONENT=ParallelSTL)