|
| 1 | +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) |
| 2 | + |
| 3 | +# ---- Project ---- |
| 4 | + |
| 5 | +# Note: update this to your new project's name and version |
| 6 | +project( |
| 7 | + Threadpool |
| 8 | + VERSION 0.1 |
| 9 | + LANGUAGES CXX |
| 10 | +) |
| 11 | + |
| 12 | +# ---- Include guards ---- |
| 13 | + |
| 14 | +if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR) |
| 15 | + message( |
| 16 | + FATAL_ERROR |
| 17 | + "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there." |
| 18 | + ) |
| 19 | +endif() |
| 20 | + |
| 21 | +# ---- Add dependencies ---- |
| 22 | + |
| 23 | +find_package( Threads ) |
| 24 | + |
| 25 | +include(cmake/CPM.cmake) |
| 26 | + |
| 27 | +CPMAddPackage("gh:TheLartians/PackageProject.cmake@1.4.1") |
| 28 | +CPMAddPackage("gh:Naios/function2#4.1.0") |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +# ---- Create library ---- |
| 34 | + |
| 35 | +set(sources "include/singlequeue.cpp") |
| 36 | + |
| 37 | +# Note: for header-only libraries change all PUBLIC flags to INTERFACE and create an interface |
| 38 | +# target: add_library(Threadpool INTERFACE) |
| 39 | +add_library(Threadpool ${sources}) |
| 40 | + |
| 41 | +target_compile_features(Threadpool PUBLIC cxx_std_20) |
| 42 | + |
| 43 | +target_compile_options(Threadpool PRIVATE -Wall -Wextra -Wpedantic) |
| 44 | + |
| 45 | +# Being a cross-platform target, we enforce standards conformance on MSVC |
| 46 | +target_compile_options(Threadpool PUBLIC "$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:/permissive>") |
| 47 | + |
| 48 | +# Link dependencies |
| 49 | +target_link_libraries(Threadpool PUBLIC function2::function2 ${CMAKE_THREAD_LIBS_INIT}) |
| 50 | + |
| 51 | +target_include_directories( |
| 52 | + Threadpool PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> |
| 53 | + $<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}> |
| 54 | +) |
| 55 | + |
| 56 | +# ---- Create an installable target ---- |
| 57 | +# this allows users to install and find the library via `find_package()`. |
| 58 | + |
| 59 | +# the location where the project's version header will be placed should match the project's regular |
| 60 | +# header paths |
| 61 | +string(TOLOWER ${PROJECT_NAME}/version.h VERSION_HEADER_LOCATION) |
| 62 | + |
| 63 | +packageProject( |
| 64 | + NAME ${PROJECT_NAME} |
| 65 | + VERSION ${PROJECT_VERSION} |
| 66 | + NAMESPACE ${PROJECT_NAME} |
| 67 | + BINARY_DIR ${PROJECT_BINARY_DIR} |
| 68 | + INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include |
| 69 | + INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION} |
| 70 | + VERSION_HEADER "${VERSION_HEADER_LOCATION}" |
| 71 | + COMPATIBILITY SameMajorVersion |
| 72 | + DEPENDENCIES "" |
| 73 | +) |
| 74 | + |
| 75 | + |
| 76 | +# ---- Conditionally build examples ---- |
| 77 | + |
| 78 | +option(BUILD_EXAMPLES "Build the examples" OFF) |
| 79 | + |
| 80 | +if(BUILD_EXAMPLES) |
| 81 | + add_executable(example "include/main.cpp") |
| 82 | + target_link_libraries(example PUBLIC Threadpool::Threadpool) |
| 83 | +endif() |
0 commit comments