-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
180 lines (149 loc) · 4.9 KB
/
Copy pathCMakeLists.txt
File metadata and controls
180 lines (149 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# ====================================================================
# Rix - IO Module
# ====================================================================
# Purpose:
# Build configuration for the io module.
#
# This module provides basic I/O utilities for Rix:
# - simple file abstraction
# - text/binary read & write helpers
# - stream helpers
#
# Design:
# - Header-only by default (INTERFACE library)
# - If any .cpp files exist under src/, we switch to a STATIC lib
#
# Targets:
# - rix_io : The actual library target (STATIC or INTERFACE)
# - rix::io : Namespaced alias for consumers
#
# Installation:
# - Installs headers under <prefix>/include/rix/...
# - Contributes to the umbrella export-set `RixTargets`
# ====================================================================
cmake_minimum_required(VERSION 3.20)
project(rix_io VERSION 0.1.0 LANGUAGES CXX)
include(GNUInstallDirs)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
file(GLOB_RECURSE RIX_IO_SOURCES CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
)
option(RIX_IO_ENABLE_SANITIZERS "Enable address/UB sanitizers for io" OFF)
option(RIX_IO_BUILD_TESTS "Build io tests" ON)
option(RIX_IO_BUILD_EXAMPLES "Build io examples" ON)
function(rix_io_apply_warnings tgt scope)
if (NOT TARGET ${tgt})
return()
endif()
if (MSVC)
target_compile_options(${tgt} ${scope} /W4 /permissive-)
else()
target_compile_options(${tgt} ${scope} -Wall -Wextra -Wshadow -Wpedantic)
endif()
endfunction()
function(rix_io_apply_sanitizers tgt scope)
if (NOT RIX_IO_ENABLE_SANITIZERS OR NOT TARGET ${tgt})
return()
endif()
if (MSVC)
message(WARNING "[io] Sanitizers are not enabled on MSVC in this module.")
return()
endif()
target_compile_options(${tgt} ${scope}
-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined
)
target_link_options(${tgt} ${scope}
-fsanitize=address,undefined
)
endfunction()
if (RIX_IO_SOURCES)
message(STATUS "[io] Building STATIC library (sources found).")
add_library(rix_io STATIC ${RIX_IO_SOURCES})
add_library(rix::io ALIAS rix_io)
target_compile_features(rix_io PUBLIC cxx_std_20)
target_include_directories(rix_io
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
rix_io_apply_warnings(rix_io PUBLIC)
rix_io_apply_sanitizers(rix_io PRIVATE)
set_target_properties(rix_io PROPERTIES
OUTPUT_NAME rix_io
VERSION ${PROJECT_VERSION}
SOVERSION 0
EXPORT_NAME io
)
install(TARGETS rix_io
EXPORT RixTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
else()
message(STATUS "[io] Building HEADER-ONLY library (no sources).")
add_library(rix_io INTERFACE)
add_library(rix::io ALIAS rix_io)
target_compile_features(rix_io INTERFACE cxx_std_20)
target_include_directories(rix_io INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
rix_io_apply_warnings(rix_io INTERFACE)
rix_io_apply_sanitizers(rix_io INTERFACE)
install(TARGETS rix_io
EXPORT RixTargets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
endif()
install(
DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h"
)
if (RIX_IO_BUILD_TESTS)
enable_testing()
add_executable(rix_io_tests
${CMAKE_CURRENT_SOURCE_DIR}/tests/test_basic.cpp
)
target_link_libraries(rix_io_tests PRIVATE rix_io)
rix_io_apply_warnings(rix_io_tests PRIVATE)
rix_io_apply_sanitizers(rix_io_tests PRIVATE)
add_test(NAME rix_io_tests COMMAND rix_io_tests)
endif()
if (RIX_IO_BUILD_EXAMPLES)
function(rix_io_add_example exe_name src_file)
add_executable(${exe_name} ${src_file})
target_link_libraries(${exe_name} PRIVATE rix_io)
rix_io_apply_warnings(${exe_name} PRIVATE)
rix_io_apply_sanitizers(${exe_name} PRIVATE)
endfunction()
set(RIX_IO_EXAMPLES
read_write
binary_read_write
buffer_text_roundtrip
append_text
try_read_missing
util_exists_and_size
file_copy
)
foreach(ex ${RIX_IO_EXAMPLES})
rix_io_add_example(rix_io_${ex}
${CMAKE_CURRENT_SOURCE_DIR}/examples/${ex}.cpp
)
endforeach()
endif()
message(STATUS "------------------------------------------------------")
message(STATUS "rix::io configured (${PROJECT_VERSION})")
if (RIX_IO_SOURCES)
message(STATUS "Mode: STATIC / sources found")
else()
message(STATUS "Mode: HEADER-ONLY / no sources")
endif()
message(STATUS "Sanitizers enabled: ${RIX_IO_ENABLE_SANITIZERS}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Binary dir: ${CMAKE_BINARY_DIR}")
message(STATUS "------------------------------------------------------")