|
| 1 | +# Copyright (c) 2015 Amanieu d'Antras |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +cmake_minimum_required(VERSION 3.1) |
| 22 | +project(Async++ C CXX) |
| 23 | + |
| 24 | +option(BUILD_SHARED_LIBS "Build Async++ as a shared library" ON) |
| 25 | +option(USE_CXX_EXCEPTIONS "Enable C++ exception support" ON) |
| 26 | +if (APPLE) |
| 27 | + option(BUILD_FRAMEWORK "Build a Mac OS X framework instead of a library" OFF) |
| 28 | + if (BUILD_FRAMEWORK AND NOT BUILD_SHARED_LIBS) |
| 29 | + message(FATAL_ERROR "Can't build a framework with static libraries") |
| 30 | + endif() |
| 31 | +endif() |
| 32 | + |
| 33 | +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
| 34 | + |
| 35 | +# Add all source and header files so IDEs can see them |
| 36 | +set(ASYNCXX_INCLUDE |
| 37 | + ${PROJECT_SOURCE_DIR}/include/async++/aligned_alloc.h |
| 38 | + ${PROJECT_SOURCE_DIR}/include/async++/cancel.h |
| 39 | + ${PROJECT_SOURCE_DIR}/include/async++/continuation_vector.h |
| 40 | + ${PROJECT_SOURCE_DIR}/include/async++/parallel_for.h |
| 41 | + ${PROJECT_SOURCE_DIR}/include/async++/parallel_invoke.h |
| 42 | + ${PROJECT_SOURCE_DIR}/include/async++/parallel_reduce.h |
| 43 | + ${PROJECT_SOURCE_DIR}/include/async++/partitioner.h |
| 44 | + ${PROJECT_SOURCE_DIR}/include/async++/range.h |
| 45 | + ${PROJECT_SOURCE_DIR}/include/async++/ref_count.h |
| 46 | + ${PROJECT_SOURCE_DIR}/include/async++/scheduler.h |
| 47 | + ${PROJECT_SOURCE_DIR}/include/async++/scheduler_fwd.h |
| 48 | + ${PROJECT_SOURCE_DIR}/include/async++/task.h |
| 49 | + ${PROJECT_SOURCE_DIR}/include/async++/task_base.h |
| 50 | + ${PROJECT_SOURCE_DIR}/include/async++/traits.h |
| 51 | + ${PROJECT_SOURCE_DIR}/include/async++/when_all_any.h |
| 52 | +) |
| 53 | +set(ASYNCXX_SRC |
| 54 | + ${PROJECT_SOURCE_DIR}/src/internal.h |
| 55 | + ${PROJECT_SOURCE_DIR}/src/fifo_queue.h |
| 56 | + ${PROJECT_SOURCE_DIR}/src/scheduler.cpp |
| 57 | + ${PROJECT_SOURCE_DIR}/src/singleton.h |
| 58 | + ${PROJECT_SOURCE_DIR}/src/task_wait_event.h |
| 59 | + ${PROJECT_SOURCE_DIR}/src/threadpool_scheduler.cpp |
| 60 | + ${PROJECT_SOURCE_DIR}/src/work_steal_queue.h |
| 61 | +) |
| 62 | +source_group(include FILES ${PROJECT_SOURCE_DIR}/include/async++.h ${ASYNCXX_INCLUDE}) |
| 63 | +source_group(src FILES ${ASYNCXX_SRC}) |
| 64 | +add_library(Async++ ${PROJECT_SOURCE_DIR}/include/async++.h ${ASYNCXX_INCLUDE} ${ASYNCXX_SRC}) |
| 65 | + |
| 66 | +# Async++ only depends on the C++11 standard libraries, but some implementations |
| 67 | +# require the -pthread compiler flag to enable threading functionality. |
| 68 | +if (NOT MSVC) |
| 69 | + target_compile_options(Async++ PRIVATE -std=c++11) |
| 70 | +endif() |
| 71 | +if (APPLE) |
| 72 | + # Use libc++ on Mac because the shipped libstdc++ version is ancient |
| 73 | + target_compile_options(Async++ PRIVATE -stdlib=libc++) |
| 74 | + set_target_properties(Async++ PROPERTIES LINK_FLAGS -stdlib=libc++) |
| 75 | +endif() |
| 76 | +set(THREADS_PREFER_PTHREAD_FLAG ON) |
| 77 | +find_package(Threads REQUIRED) |
| 78 | +target_link_libraries(Async++ PUBLIC Threads::Threads) |
| 79 | + |
| 80 | +# Set up preprocessor definitions |
| 81 | +target_include_directories(Async++ PRIVATE ${PROJECT_SOURCE_DIR}/include) |
| 82 | +set_target_properties(Async++ PROPERTIES DEFINE_SYMBOL LIBASYNC_BUILD) |
| 83 | +if (BUILD_SHARED_LIBS) |
| 84 | + # Minimize the set of symbols exported by libraries |
| 85 | + set_target_properties(Async++ PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON) |
| 86 | +else() |
| 87 | + target_compile_definitions(Async++ PUBLIC LIBASYNC_STATIC) |
| 88 | +endif() |
| 89 | + |
| 90 | +# Enable warnings for strict C++ standard conformance |
| 91 | +if (NOT MSVC) |
| 92 | + target_compile_options(Async++ PRIVATE -Wall -Wextra -pedantic) |
| 93 | +endif() |
| 94 | + |
| 95 | +# Async++ doesn't make use of RTTI information, so don't generate it. |
| 96 | +# There are issues on Apple platforms with exceptions and -fno-rtti, so keep it |
| 97 | +# enabled there. |
| 98 | +# See https://stackoverflow.com/questions/21737201/problems-throwing-and-catching-exceptions-on-os-x-with-fno-rtti |
| 99 | +if (MSVC) |
| 100 | + target_compile_options(Async++ PRIVATE /GR-) |
| 101 | +elseif(NOT APPLE) |
| 102 | + target_compile_options(Async++ PRIVATE -fno-rtti) |
| 103 | +endif() |
| 104 | + |
| 105 | +# Allow disabling exceptions, but warn the user about the consequences |
| 106 | +if (NOT USE_CXX_EXCEPTIONS) |
| 107 | + message(WARNING "Exceptions have been disabled. Any operation that would " |
| 108 | + "throw an exception will result in a call to std::abort() instead.") |
| 109 | + target_compile_definitions(Async++ PUBLIC LIBASYNC_NO_EXCEPTIONS) |
| 110 | + if (MSVC) |
| 111 | + target_compile_options(Async++ PUBLIC /EHs-c-) |
| 112 | + else() |
| 113 | + target_compile_options(Async++ PUBLIC -fno-exceptions) |
| 114 | + endif() |
| 115 | +endif() |
| 116 | + |
| 117 | +include(CMakePackageConfigHelpers) |
| 118 | +configure_package_config_file("${CMAKE_CURRENT_LIST_DIR}/Async++Config.cmake.in" |
| 119 | + "${PROJECT_BINARY_DIR}/Async++Config.cmake" |
| 120 | + INSTALL_DESTINATION cmake |
| 121 | +) |
| 122 | + |
| 123 | +install(FILES "${PROJECT_BINARY_DIR}/Async++Config.cmake" |
| 124 | + DESTINATION cmake |
| 125 | +) |
| 126 | + |
| 127 | +# Install the library and produce a CMake export script |
| 128 | +include(GNUInstallDirs) |
| 129 | +install(TARGETS Async++ |
| 130 | + EXPORT Async++ |
| 131 | + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} |
| 132 | + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 133 | + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 134 | + FRAMEWORK DESTINATION Frameworks |
| 135 | +) |
| 136 | +export(EXPORT Async++) |
| 137 | +install(EXPORT Async++ DESTINATION cmake) |
| 138 | +if (APPLE AND BUILD_FRAMEWORK) |
| 139 | + set_target_properties(Async++ PROPERTIES OUTPUT_NAME Async++ FRAMEWORK ON) |
| 140 | + set_source_files_properties(${ASYNCXX_INCLUDE} PROPERTIES MACOSX_PACKAGE_LOCATION Headers/async++) |
| 141 | + set_source_files_properties(${PROJECT_SOURCE_DIR}/include/async++.h PROPERTIES MACOSX_PACKAGE_LOCATION Headers) |
| 142 | +else() |
| 143 | + set_target_properties(Async++ PROPERTIES OUTPUT_NAME async++) |
| 144 | + target_include_directories(Async++ INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>) |
| 145 | + install(FILES ${PROJECT_SOURCE_DIR}/include/async++.h DESTINATION include) |
| 146 | + install(FILES ${ASYNCXX_INCLUDE} DESTINATION include/async++) |
| 147 | +endif() |
| 148 | + |
| 149 | +SET(CPACK_GENERATOR "DEB") |
| 150 | +SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "none") #required |
| 151 | + |
| 152 | +INCLUDE(CPack) |
0 commit comments