|
| 1 | +cmake_minimum_required(VERSION 3.14) |
| 2 | + |
| 3 | +project(slick_shm |
| 4 | + VERSION 0.1.0 |
| 5 | + LANGUAGES CXX |
| 6 | + DESCRIPTION "C++ header-only cross-platform shared memory library" |
| 7 | +) |
| 8 | + |
| 9 | +# Options |
| 10 | +option(SLICK_SHM_BUILD_EXAMPLES "Build example programs" ON) |
| 11 | +option(SLICK_SHM_BUILD_TESTS "Build unit tests" ON) |
| 12 | +option(SLICK_SHM_INSTALL "Generate install target" ON) |
| 13 | + |
| 14 | +# Interface library (header-only) |
| 15 | +add_library(slick_shm INTERFACE) |
| 16 | +add_library(slick::shm ALIAS slick_shm) |
| 17 | + |
| 18 | +target_include_directories(slick_shm INTERFACE |
| 19 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 20 | + $<INSTALL_INTERFACE:include> |
| 21 | +) |
| 22 | + |
| 23 | +target_compile_features(slick_shm INTERFACE cxx_std_17) |
| 24 | + |
| 25 | +# Platform-specific linking |
| 26 | +if(WIN32) |
| 27 | + # No special libraries needed on Windows |
| 28 | +elseif(UNIX) |
| 29 | + # Link with rt (realtime) for shm_open/shm_unlink on POSIX systems |
| 30 | + # pthread may be needed for some implementations |
| 31 | + target_link_libraries(slick_shm INTERFACE rt) |
| 32 | + |
| 33 | + # On some systems (older Linux), pthread is also needed |
| 34 | + find_package(Threads) |
| 35 | + if(Threads_FOUND) |
| 36 | + target_link_libraries(slick_shm INTERFACE Threads::Threads) |
| 37 | + endif() |
| 38 | +endif() |
| 39 | + |
| 40 | +# Examples |
| 41 | +if(SLICK_SHM_BUILD_EXAMPLES) |
| 42 | + add_subdirectory(examples) |
| 43 | +endif() |
| 44 | + |
| 45 | +# Tests |
| 46 | +if(SLICK_SHM_BUILD_TESTS) |
| 47 | + enable_testing() |
| 48 | + add_subdirectory(tests) |
| 49 | +endif() |
| 50 | + |
| 51 | +# Install |
| 52 | +if(SLICK_SHM_INSTALL) |
| 53 | + include(GNUInstallDirs) |
| 54 | + include(CMakePackageConfigHelpers) |
| 55 | + |
| 56 | + # Install headers |
| 57 | + install(DIRECTORY include/slick |
| 58 | + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
| 59 | + ) |
| 60 | + |
| 61 | + # Install targets |
| 62 | + install(TARGETS slick_shm |
| 63 | + EXPORT slick_shm-targets |
| 64 | + ) |
| 65 | + |
| 66 | + # Install export set |
| 67 | + install(EXPORT slick_shm-targets |
| 68 | + FILE slick_shm-targets.cmake |
| 69 | + NAMESPACE slick:: |
| 70 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/slick_shm |
| 71 | + ) |
| 72 | + |
| 73 | + # Generate and install package config file |
| 74 | + configure_package_config_file( |
| 75 | + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/slick_shm-config.cmake.in |
| 76 | + ${CMAKE_CURRENT_BINARY_DIR}/slick_shm-config.cmake |
| 77 | + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/slick_shm |
| 78 | + ) |
| 79 | + |
| 80 | + # Generate and install package version file |
| 81 | + write_basic_package_version_file( |
| 82 | + ${CMAKE_CURRENT_BINARY_DIR}/slick_shm-config-version.cmake |
| 83 | + VERSION ${PROJECT_VERSION} |
| 84 | + COMPATIBILITY SameMajorVersion |
| 85 | + ) |
| 86 | + |
| 87 | + # Install config files |
| 88 | + install(FILES |
| 89 | + ${CMAKE_CURRENT_BINARY_DIR}/slick_shm-config.cmake |
| 90 | + ${CMAKE_CURRENT_BINARY_DIR}/slick_shm-config-version.cmake |
| 91 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/slick_shm |
| 92 | + ) |
| 93 | +endif() |
0 commit comments