-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
408 lines (358 loc) · 12.7 KB
/
CMakeLists.txt
File metadata and controls
408 lines (358 loc) · 12.7 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# ====================================================================
# @file CMakeLists.txt
# @author Gaspard Kirira
#
# Copyright 2025, Gaspard Kirira. All rights reserved.
# https://github.com/vixcpp/vix
# Use of this source code is governed by a MIT license
# that can be found in the License file.
#
# Vix.cpp
#
# Purpose:
# Build configuration for the Vix middleware module.
# This module provides HTTP middleware building blocks on top of the
# Vix module ecosystem.
#
# Public Targets:
# - vix_middleware : The actual library target (STATIC or INTERFACE)
# - vix::middleware : Namespaced alias for consumers
# - vix::mw : Short alias for consumers
#
# Required Dependencies:
# - vix::core
# - vix::cache
#
# Optional Dependencies:
# - vix::utils
# - vix::json / vix_json
#
# Options:
# - VIX_MW_FETCH_CORE : Auto-fetch core if missing
# - VIX_MW_FETCH_CACHE : Auto-fetch cache if missing
# - VIX_MW_FETCH_UTILS : Auto-fetch utils if missing
# - VIX_MW_WITH_UTILS : AUTO|ON|OFF
# - VIX_MW_WITH_JSON : AUTO|ON|OFF
# - VIX_MW_BUILD_TESTS : Build middleware tests
# - VIX_ENABLE_SANITIZERS : Inherit sanitizers from umbrella project
#
# Installation/Export:
# This module installs its target and headers and contributes to the
# top-level export-set `VixTargets`. Consumers can then use:
#
# find_package(Vix CONFIG REQUIRED)
# target_link_libraries(app PRIVATE vix::middleware)
#
# Notes:
# - This file is designed to be included from a superproject (preferred).
# - Avoids per-module export-sets; relies on the umbrella's `VixTargets`.
# - This module must not depend on Boost.
# ====================================================================
cmake_minimum_required(VERSION 3.20)
project(vix_middleware VERSION 1.0.0 LANGUAGES CXX)
set(Boost_FOUND OFF)
set(Boost_NO_BOOST_CMAKE ON)
set(Boost_NO_SYSTEM_PATHS ON)
set(CMAKE_DISABLE_FIND_PACKAGE_VixOrm ON)
include(GNUInstallDirs)
# --------------------------------------------------------------------
# Global settings
# --------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# --------------------------------------------------------------------
# Sources discovery
# --------------------------------------------------------------------
file(GLOB_RECURSE MIDDLEWARE_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
)
# --------------------------------------------------------------------
# Helper: pick the first available target from a candidate list
# --------------------------------------------------------------------
function(vix_pick_first_target out_var)
set(_picked "")
foreach(_cand IN LISTS ARGN)
if (TARGET ${_cand})
set(_picked ${_cand})
break()
endif()
endforeach()
set(${out_var} "${_picked}" PARENT_SCOPE)
endfunction()
# --------------------------------------------------------------------
# Required: Core
# --------------------------------------------------------------------
option(VIX_MW_FETCH_CORE "Auto-fetch vix::core if missing" ON)
vix_pick_first_target(VIX_CORE_TARGET
vix::core
vix_core
core
)
if (NOT VIX_CORE_TARGET)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../core/CMakeLists.txt")
message(STATUS "[middleware] Adding core from umbrella: ../core")
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../core" "core")
elseif (VIX_MW_FETCH_CORE)
include(FetchContent)
message(STATUS "[middleware] Fetching vix::core via FetchContent")
FetchContent_Declare(vix_core
GIT_REPOSITORY https://github.com/vixcpp/core.git
GIT_TAG dev
)
FetchContent_MakeAvailable(vix_core)
else()
message(FATAL_ERROR
"vix::core not found. Provide ../core or enable VIX_MW_FETCH_CORE=ON."
)
endif()
vix_pick_first_target(VIX_CORE_TARGET
vix::core
vix_core
core
)
endif()
if (NOT VIX_CORE_TARGET)
message(FATAL_ERROR
"Core target not found after dependency resolution. "
"Expected one of: vix::core, vix_core, core."
)
endif()
message(STATUS "[middleware] Core target: ${VIX_CORE_TARGET}")
# --------------------------------------------------------------------
# Required: Cache
# --------------------------------------------------------------------
option(VIX_MW_FETCH_CACHE "Auto-fetch vix::cache if missing" ON)
vix_pick_first_target(VIX_CACHE_TARGET
vix::cache
vix_cache
cache
)
if (NOT VIX_CACHE_TARGET)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../cache/CMakeLists.txt")
message(STATUS "[middleware] Adding cache from umbrella: ../cache")
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../cache" "cache")
elseif (VIX_MW_FETCH_CACHE)
include(FetchContent)
message(STATUS "[middleware] Fetching vix::cache via FetchContent")
FetchContent_Declare(vix_cache
GIT_REPOSITORY https://github.com/vixcpp/cache.git
GIT_TAG v0.1.0
)
FetchContent_MakeAvailable(vix_cache)
else()
message(FATAL_ERROR
"vix::cache not found. Provide ../cache or enable VIX_MW_FETCH_CACHE=ON."
)
endif()
vix_pick_first_target(VIX_CACHE_TARGET
vix::cache
vix_cache
cache
)
endif()
if (NOT VIX_CACHE_TARGET)
message(FATAL_ERROR
"Cache target not found after dependency resolution. "
"Expected one of: vix::cache, vix_cache, cache."
)
endif()
message(STATUS "[middleware] Cache target: ${VIX_CACHE_TARGET}")
# --------------------------------------------------------------------
# Optional: Utils
# --------------------------------------------------------------------
set(VIX_MW_WITH_UTILS "AUTO" CACHE STRING "Link utils module (AUTO|ON|OFF)")
set_property(CACHE VIX_MW_WITH_UTILS PROPERTY STRINGS AUTO ON OFF)
option(VIX_MW_FETCH_UTILS "Auto-fetch vix::utils if missing (when needed)" ON)
function(vix_mw_try_link_utils onto link_scope)
if (VIX_MW_WITH_UTILS STREQUAL "OFF")
message(STATUS "[middleware] Utils disabled (OFF)")
target_compile_definitions(${onto} ${link_scope} VIX_MW_NO_UTILS=1)
return()
endif()
set(_utils_target "")
vix_pick_first_target(_utils_target
vix::utils
vix_utils
utils
)
if (NOT _utils_target)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../utils/CMakeLists.txt")
message(STATUS "[middleware] Adding utils from umbrella: ../utils")
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../utils" "utils")
elseif (VIX_MW_FETCH_UTILS)
include(FetchContent)
message(STATUS "[middleware] Fetching vix::utils via FetchContent")
FetchContent_Declare(vix_utils
GIT_REPOSITORY https://github.com/vixcpp/utils.git
GIT_TAG dev
)
FetchContent_MakeAvailable(vix_utils)
endif()
vix_pick_first_target(_utils_target
vix::utils
vix_utils
utils
)
endif()
if (_utils_target)
message(STATUS "[middleware] Utils backend: ${_utils_target}")
target_link_libraries(${onto} ${link_scope} ${_utils_target})
target_compile_definitions(${onto} ${link_scope} VIX_MW_WITH_UTILS=1)
else()
if (VIX_MW_WITH_UTILS STREQUAL "ON")
message(FATAL_ERROR
"Utils required but not found. Expected one of: vix::utils, vix_utils, utils."
)
else()
message(STATUS "[middleware] Utils not detected; building without utils.")
target_compile_definitions(${onto} ${link_scope} VIX_MW_NO_UTILS=1)
endif()
endif()
endfunction()
# --------------------------------------------------------------------
# Optional: JSON backend
# --------------------------------------------------------------------
set(VIX_MW_WITH_JSON "AUTO" CACHE STRING "Link JSON backend (AUTO|ON|OFF)")
set_property(CACHE VIX_MW_WITH_JSON PROPERTY STRINGS AUTO ON OFF)
function(vix_mw_try_link_json onto link_scope)
if (VIX_MW_WITH_JSON STREQUAL "OFF")
message(STATUS "[middleware] JSON disabled (OFF)")
target_compile_definitions(${onto} ${link_scope} VIX_MW_NO_JSON=1)
return()
endif()
set(_json_target "")
vix_pick_first_target(_json_target
vix::json
vix_json
json
)
if (_json_target)
message(STATUS "[middleware] JSON backend: ${_json_target}")
target_link_libraries(${onto} ${link_scope} ${_json_target})
target_compile_definitions(${onto} ${link_scope} VIX_MW_WITH_JSON=1)
else()
if (VIX_MW_WITH_JSON STREQUAL "ON")
message(FATAL_ERROR
"JSON required but not found. Expected one of: vix::json, vix_json, json."
)
else()
message(STATUS "[middleware] No JSON backend detected; building without JSON.")
target_compile_definitions(${onto} ${link_scope} VIX_MW_NO_JSON=1)
endif()
endif()
endfunction()
# --------------------------------------------------------------------
# Hard fail if Boost sneaks in
# --------------------------------------------------------------------
if (TARGET Boost::boost OR TARGET Boost::system)
message(FATAL_ERROR "Boost is not allowed in vix::middleware")
endif()
# --------------------------------------------------------------------
# Library target
# --------------------------------------------------------------------
if (MIDDLEWARE_SOURCES)
message(STATUS "[middleware] Building STATIC library with detected sources.")
add_library(vix_middleware STATIC ${MIDDLEWARE_SOURCES})
add_library(vix::middleware ALIAS vix_middleware)
add_library(vix::mw ALIAS vix_middleware)
target_compile_features(vix_middleware PUBLIC cxx_std_20)
target_include_directories(vix_middleware
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_libraries(vix_middleware
PUBLIC
${VIX_CORE_TARGET}
${VIX_CACHE_TARGET}
)
if (NOT MSVC)
target_compile_options(vix_middleware PRIVATE
-Wall
-Wextra
-Wpedantic
)
endif()
if (VIX_ENABLE_SANITIZERS AND NOT MSVC)
target_compile_options(vix_middleware PRIVATE
-fno-omit-frame-pointer
-fsanitize=address,undefined
)
target_link_options(vix_middleware PRIVATE
-fsanitize=address,undefined
)
endif()
vix_mw_try_link_utils(vix_middleware PUBLIC)
vix_mw_try_link_json(vix_middleware PUBLIC)
set_target_properties(vix_middleware PROPERTIES
OUTPUT_NAME vix_middleware
VERSION ${PROJECT_VERSION}
SOVERSION 0
EXPORT_NAME middleware
)
install(TARGETS vix_middleware
EXPORT VixTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
else()
message(STATUS "[middleware] Building HEADER-ONLY library (no sources).")
add_library(vix_middleware INTERFACE)
add_library(vix::middleware ALIAS vix_middleware)
add_library(vix::mw ALIAS vix_middleware)
target_compile_features(vix_middleware INTERFACE cxx_std_20)
target_include_directories(vix_middleware
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_libraries(vix_middleware
INTERFACE
${VIX_CORE_TARGET}
${VIX_CACHE_TARGET}
)
vix_mw_try_link_utils(vix_middleware INTERFACE)
vix_mw_try_link_json(vix_middleware INTERFACE)
install(TARGETS vix_middleware
EXPORT VixTargets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
endif()
# --------------------------------------------------------------------
# Headers installation
# --------------------------------------------------------------------
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING
PATTERN "*.hpp"
PATTERN "*.h"
)
# --------------------------------------------------------------------
# Tests
# --------------------------------------------------------------------
option(VIX_MW_BUILD_TESTS "Build middleware tests" OFF)
if (VIX_MW_BUILD_TESTS AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeLists.txt")
include(CTest)
enable_testing()
add_subdirectory(tests)
endif()
# --------------------------------------------------------------------
# Summary
# --------------------------------------------------------------------
message(STATUS "------------------------------------------------------")
message(STATUS "vix::middleware configured (${PROJECT_VERSION})")
if (MIDDLEWARE_SOURCES)
message(STATUS "Mode: STATIC / sources found")
else()
message(STATUS "Mode: HEADER-ONLY / no sources")
endif()
message(STATUS "Core target: ${VIX_CORE_TARGET}")
message(STATUS "Cache target: ${VIX_CACHE_TARGET}")
message(STATUS "Utils mode: ${VIX_MW_WITH_UTILS}")
message(STATUS "JSON mode: ${VIX_MW_WITH_JSON}")
message(STATUS "Tests: ${VIX_MW_BUILD_TESTS}")
message(STATUS "Include dir: ${CMAKE_CURRENT_SOURCE_DIR}/include")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "------------------------------------------------------")