|
| 1 | +# ==================================================================== |
| 2 | +# Rix — IO Module |
| 3 | +# ==================================================================== |
| 4 | +# Purpose: |
| 5 | +# Build configuration for the rix-io module. |
| 6 | +# |
| 7 | +# This module provides basic I/O utilities for Rix: |
| 8 | +# - simple file abstraction |
| 9 | +# - text/binary read & write helpers |
| 10 | +# - stream helpers |
| 11 | +# |
| 12 | +# Design: |
| 13 | +# - Header-only by default (INTERFACE library) |
| 14 | +# - If any .cpp files exist under src/, we switch to a STATIC lib |
| 15 | +# |
| 16 | +# Targets: |
| 17 | +# - rix_io : The actual library target (STATIC or INTERFACE) |
| 18 | +# - rix::io : Namespaced alias for consumers |
| 19 | +# |
| 20 | +# Installation: |
| 21 | +# - Installs headers under <prefix>/include/rix/... |
| 22 | +# - Contributes to the umbrella export-set `RixTargets` |
| 23 | +# ==================================================================== |
| 24 | + |
| 25 | +cmake_minimum_required(VERSION 3.20) |
| 26 | +project(rix_io VERSION 0.1.0 LANGUAGES CXX) |
| 27 | + |
| 28 | +include(GNUInstallDirs) |
| 29 | + |
| 30 | +# ------------------------ Global settings ---------------------------- |
| 31 | +set(CMAKE_CXX_STANDARD 20) |
| 32 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 33 | +set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 34 | + |
| 35 | +# Friendly warnings (can be tuned later if needed) |
| 36 | +set(RIX_IO_GLOBAL_CXX_FLAGS "-Wall -Wextra -Wshadow") |
| 37 | +set(CMAKE_CXX_FLAGS_RELEASE "${RIX_IO_GLOBAL_CXX_FLAGS} -O2 -DNDEBUG") |
| 38 | +set(CMAKE_CXX_FLAGS_DEBUG "${RIX_IO_GLOBAL_CXX_FLAGS} -g") |
| 39 | + |
| 40 | +# ------------------------ Sources discovery -------------------------- |
| 41 | +# If any .cpp exists under src/, we switch to STATIC build mode. |
| 42 | +file(GLOB_RECURSE RIX_IO_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp") |
| 43 | + |
| 44 | +# ------------------------ Helper: Sanitizers (optional) -------------- |
| 45 | +# This is opt-in and controlled by a top-level option if needed. |
| 46 | +option(RIX_IO_ENABLE_SANITIZERS "Enable address/UB sanitizers for rix-io" OFF) |
| 47 | + |
| 48 | +function(rix_io_apply_sanitizers tgt scope) |
| 49 | + if (RIX_IO_ENABLE_SANITIZERS AND TARGET ${tgt}) |
| 50 | + message(STATUS "[rix-io] enabling sanitizers on ${tgt} (${scope})") |
| 51 | + if (${scope} STREQUAL "PRIVATE") |
| 52 | + target_compile_options(${tgt} PRIVATE |
| 53 | + -O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined) |
| 54 | + target_link_options(${tgt} PRIVATE |
| 55 | + -fsanitize=address,undefined) |
| 56 | + else() |
| 57 | + target_compile_options(${tgt} INTERFACE |
| 58 | + -O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined) |
| 59 | + target_link_options(${tgt} INTERFACE |
| 60 | + -fsanitize=address,undefined) |
| 61 | + endif() |
| 62 | + endif() |
| 63 | +endfunction() |
| 64 | + |
| 65 | +# ============================== STATIC =============================== |
| 66 | +if (RIX_IO_SOURCES) |
| 67 | + message(STATUS "[rix-io] Building STATIC library with detected sources.") |
| 68 | + |
| 69 | + add_library(rix_io STATIC ${RIX_IO_SOURCES}) |
| 70 | + add_library(rix::io ALIAS rix_io) |
| 71 | + target_compile_features(rix_io PUBLIC cxx_std_20) |
| 72 | + |
| 73 | + target_include_directories(rix_io |
| 74 | + PUBLIC |
| 75 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 76 | + $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> |
| 77 | + ) |
| 78 | + |
| 79 | + # Sanitizers (opt-in) |
| 80 | + rix_io_apply_sanitizers(rix_io PRIVATE) |
| 81 | + |
| 82 | + set_target_properties(rix_io PROPERTIES |
| 83 | + OUTPUT_NAME rix_io |
| 84 | + VERSION ${PROJECT_VERSION} |
| 85 | + SOVERSION 0 |
| 86 | + EXPORT_NAME io |
| 87 | + ) |
| 88 | + |
| 89 | + install(TARGETS rix_io |
| 90 | + EXPORT RixTargets |
| 91 | + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 92 | + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 93 | + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} |
| 94 | + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
| 95 | + ) |
| 96 | + |
| 97 | +# ============================ HEADER-ONLY ============================ |
| 98 | +else() |
| 99 | + message(STATUS "[rix-io] Building HEADER-ONLY library (no sources).") |
| 100 | + |
| 101 | + add_library(rix_io INTERFACE) |
| 102 | + add_library(rix::io ALIAS rix_io) |
| 103 | + target_compile_features(rix_io INTERFACE cxx_std_20) |
| 104 | + |
| 105 | + target_include_directories(rix_io INTERFACE |
| 106 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 107 | + $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> |
| 108 | + ) |
| 109 | + |
| 110 | + # Propagate sanitizers to dependents if requested |
| 111 | + rix_io_apply_sanitizers(rix_io INTERFACE) |
| 112 | + |
| 113 | + install(TARGETS rix_io |
| 114 | + EXPORT RixTargets |
| 115 | + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
| 116 | + ) |
| 117 | +endif() |
| 118 | + |
| 119 | +# ------------------------ Options local build ------------------------ |
| 120 | +option(RIX_IO_BUILD_TESTS "Build rix-io tests" ON) |
| 121 | +option(RIX_IO_BUILD_EXAMPLES "Build rix-io examples" ON) |
| 122 | + |
| 123 | +# ----------------------------- Tests -------------------------------- |
| 124 | +if (RIX_IO_BUILD_TESTS) |
| 125 | + enable_testing() |
| 126 | + |
| 127 | + add_executable(rix_io_tests |
| 128 | + ${CMAKE_CURRENT_SOURCE_DIR}/tests/file_test.cpp |
| 129 | + ) |
| 130 | + |
| 131 | + target_link_libraries(rix_io_tests PRIVATE rix_io) |
| 132 | + |
| 133 | + # Include dirs arrivent via rix_io (PUBLIC / INTERFACE), donc pas besoin |
| 134 | + # d'en rajouter ici. |
| 135 | + |
| 136 | + # Optionnel : activer les sanitizers aussi pour les tests |
| 137 | + rix_io_apply_sanitizers(rix_io_tests PRIVATE) |
| 138 | + |
| 139 | + add_test(NAME rix_io_file_test COMMAND rix_io_tests) |
| 140 | +endif() |
| 141 | + |
| 142 | +# ---------------------------- Examples ------------------------------- |
| 143 | +if (RIX_IO_BUILD_EXAMPLES) |
| 144 | + add_executable(rix_io_read_write |
| 145 | + ${CMAKE_CURRENT_SOURCE_DIR}/examples/read_write.cpp |
| 146 | + ) |
| 147 | + |
| 148 | + target_link_libraries(rix_io_read_write PRIVATE rix_io) |
| 149 | + |
| 150 | + # Optionnel : sanitizers sur l'exemple aussi |
| 151 | + rix_io_apply_sanitizers(rix_io_read_write PRIVATE) |
| 152 | +endif() |
| 153 | + |
| 154 | + |
| 155 | +# ------------------------ Headers install ---------------------------- |
| 156 | +install(DIRECTORY include/ |
| 157 | + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
| 158 | + FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h") |
| 159 | + |
| 160 | +# ----------------------------- Summary ------------------------------- |
| 161 | +message(STATUS "------------------------------------------------------") |
| 162 | +message(STATUS "rix::io configured (${PROJECT_VERSION})") |
| 163 | +if (RIX_IO_SOURCES) |
| 164 | + message(STATUS "Mode: STATIC / sources found") |
| 165 | +else() |
| 166 | + message(STATUS "Mode: HEADER-ONLY / no sources") |
| 167 | +endif() |
| 168 | +message(STATUS "Include dir: ${CMAKE_CURRENT_SOURCE_DIR}/include (for <rix/...>)") |
| 169 | +message(STATUS "Sanitizers enabled: ${RIX_IO_ENABLE_SANITIZERS}") |
| 170 | +message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") |
| 171 | +message(STATUS "------------------------------------------------------") |
0 commit comments