-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
317 lines (270 loc) · 9.73 KB
/
CMakeLists.txt
File metadata and controls
317 lines (270 loc) · 9.73 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
# @file CMakeLists.cpp
# @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
# Modules - websocket
# Purpose:
# Build configuration for the Vix WebSocket module:
# - Upgrades HTTP connections from vix::core to WebSocket
# - Provides real-time streaming primitives (channels, broadcasts,
# chat-style messaging, notifications, dashboards, IoT streams, ...)
#
# Public Targets:
# - vix_websocket : The actual library target (STATIC or INTERFACE)
# - vix::websocket : Namespaced alias for consumers
#
# Public Dependencies:
# - vix::core (HTTP server, routing, config)
# - vix::utils (logging, small helpers)
# - optional JSON backend (vix::json / vix_json)
#
# Options:
# - VIX_WEBSOCKET_WITH_JSON : AUTO|ON|OFF (default: AUTO)
# - VIX_WEBSOCKET_FETCH_CORE : Auto-fetch vix::core if missing (default ON)
# - VIX_WEBSOCKET_FETCH_UTILS: Auto-fetch vix::utils if missing (default ON)
#
# Installation/Export:
# - Contributes to the umbrella export-set `VixTargets`
# - Headers installed under <prefix>/include/vix/websocket/...
# ====================================================================
cmake_minimum_required(VERSION 3.20)
project(vix_websocket VERSION 1.1.0 LANGUAGES CXX)
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 WEBSOCKET_SOURCES CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
)
# Core dependency
option(VIX_WEBSOCKET_FETCH_CORE "Auto-fetch vix::core if missing" ON)
option(VIX_WEBSOCKET_FETCH_UTILS "Auto-fetch vix::utils if missing" ON)
if (NOT TARGET vix::core AND NOT TARGET vix_core)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../core/CMakeLists.txt")
message(STATUS "[websocket] Adding core from umbrella: ../core")
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../core" "${CMAKE_CURRENT_BINARY_DIR}/core")
elseif (VIX_WEBSOCKET_FETCH_CORE)
include(FetchContent)
message(STATUS "[websocket] Fetching vix::core via FetchContent")
FetchContent_Declare(vix_core_ext
GIT_REPOSITORY https://github.com/vixcpp/core.git
GIT_TAG dev
)
FetchContent_MakeAvailable(vix_core_ext)
else()
message(FATAL_ERROR
"vix::core not found. Provide vix::core before websocket "
"or enable VIX_WEBSOCKET_FETCH_CORE=ON.")
endif()
endif()
# Utils dependency
if (NOT TARGET vix::utils AND NOT TARGET vix_utils)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../utils/CMakeLists.txt")
message(STATUS "[websocket] Adding utils from umbrella: ../utils")
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../utils" "${CMAKE_CURRENT_BINARY_DIR}/utils")
elseif (VIX_WEBSOCKET_FETCH_UTILS)
include(FetchContent)
message(STATUS "[websocket] Fetching vix::utils via FetchContent")
FetchContent_Declare(vix_utils_ext
GIT_REPOSITORY https://github.com/vixcpp/utils.git
GIT_TAG dev
)
FetchContent_MakeAvailable(vix_utils_ext)
else()
message(FATAL_ERROR
"vix::utils not found. Provide vix::utils before websocket "
"or enable VIX_WEBSOCKET_FETCH_UTILS=ON.")
endif()
endif()
if (TARGET vix::core)
set(VIX_CORE_TARGET vix::core)
elseif (TARGET vix_core)
set(VIX_CORE_TARGET vix_core)
else()
message(FATAL_ERROR "[websocket] Core dependency resolution finished but no usable core target was found.")
endif()
if (TARGET vix::utils)
set(VIX_UTILS_TARGET vix::utils)
elseif (TARGET vix_utils)
set(VIX_UTILS_TARGET vix_utils)
else()
message(FATAL_ERROR "[websocket] Utils dependency resolution finished but no usable utils target was found.")
endif()
# JSON linkage policy
set(VIX_WEBSOCKET_WITH_JSON "AUTO" CACHE STRING "Link JSON backend (AUTO|ON|OFF)")
set_property(CACHE VIX_WEBSOCKET_WITH_JSON PROPERTY STRINGS AUTO ON OFF)
function(vix_websocket_try_link_json onto link_scope)
if (VIX_WEBSOCKET_WITH_JSON STREQUAL "OFF")
message(STATUS "[websocket] JSON disabled (OFF)")
target_compile_definitions(${onto} ${link_scope} VIX_WEBSOCKET_NO_JSON=1)
return()
endif()
set(_json_candidates vix::json vix_json)
set(_json_found "")
foreach(t IN LISTS _json_candidates)
if (TARGET ${t})
set(_json_found ${t})
break()
endif()
endforeach()
if (_json_found)
message(STATUS "[websocket] JSON backend: ${_json_found}")
target_link_libraries(${onto} ${link_scope} ${_json_found})
else()
if (VIX_WEBSOCKET_WITH_JSON STREQUAL "ON")
message(FATAL_ERROR
"JSON required but not found (expected: vix::json or vix_json).")
else()
message(STATUS "[websocket] No JSON backend detected; building without JSON.")
target_compile_definitions(${onto} ${link_scope} VIX_WEBSOCKET_NO_JSON=1)
endif()
endif()
endfunction()
# STATIC
if (WEBSOCKET_SOURCES)
message(STATUS "[websocket] Building STATIC library with detected sources.")
add_library(vix_websocket STATIC ${WEBSOCKET_SOURCES})
if (NOT TARGET vix::websocket)
add_library(vix::websocket ALIAS vix_websocket)
endif()
if (NOT TARGET vix::ws)
add_library(vix::ws ALIAS vix_websocket)
endif()
target_compile_features(vix_websocket PUBLIC cxx_std_20)
find_package(SQLite3 QUIET)
# SQLite (required for websocket storage)
# vcpkg exposes: unofficial-sqlite3::sqlite3
# other platforms may use FindSQLite3 (preferred: SQLite3::SQLite3)
set(_vix_sqlite_target "")
find_package(unofficial-sqlite3 CONFIG QUIET)
if (unofficial-sqlite3_FOUND AND TARGET unofficial-sqlite3::sqlite3)
message(STATUS "[websocket] SQLite3 found (vcpkg): unofficial-sqlite3::sqlite3")
set(_vix_sqlite_target unofficial-sqlite3::sqlite3)
else()
find_package(SQLite3 QUIET)
if (SQLite3_FOUND)
if (TARGET SQLite3::SQLite3)
set(_vix_sqlite_target SQLite3::SQLite3)
elseif (TARGET SQLite::SQLite3)
# upgrade deprecated target → modern alias
add_library(SQLite3::SQLite3 ALIAS SQLite::SQLite3)
set(_vix_sqlite_target SQLite3::SQLite3)
elseif (TARGET sqlite3)
add_library(SQLite3::SQLite3 ALIAS sqlite3)
set(_vix_sqlite_target SQLite3::SQLite3)
endif()
message(STATUS "[websocket] SQLite3 found (FindSQLite3): ${_vix_sqlite_target}")
endif()
endif()
if (NOT _vix_sqlite_target)
message(FATAL_ERROR
"[websocket] SQLite3 not found.\n"
"hint(vcpkg): vcpkg install sqlite3\n"
"hint: or provide SQLite3_INCLUDE_DIR / SQLite3_LIBRARY.\n")
endif()
target_include_directories(vix_websocket
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_libraries(vix_websocket
PUBLIC
${VIX_CORE_TARGET}
${VIX_UTILS_TARGET}
${_vix_sqlite_target}
)
if (NOT MSVC)
target_compile_options(vix_websocket PRIVATE
-Wall
-Wextra
-Wpedantic
)
endif()
if (VIX_ENABLE_SANITIZERS AND NOT MSVC)
target_compile_options(vix_websocket PUBLIC
-fno-omit-frame-pointer
-fsanitize=address,undefined
)
target_link_options(vix_websocket INTERFACE
-fsanitize=address,undefined
)
endif()
vix_websocket_try_link_json(vix_websocket PUBLIC)
set_target_properties(vix_websocket PROPERTIES
OUTPUT_NAME vix_websocket
VERSION ${PROJECT_VERSION}
SOVERSION 0
EXPORT_NAME websocket
)
install(TARGETS vix_websocket
EXPORT VixTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING
PATTERN "*.hpp"
PATTERN "*.h"
PATTERN "*.inc"
)
# HEADER-ONLY
else()
message(STATUS "[websocket] Building HEADER-ONLY library (no sources).")
add_library(vix_websocket INTERFACE)
if (NOT TARGET vix::websocket)
add_library(vix::websocket ALIAS vix_websocket)
endif()
if (NOT TARGET vix::ws)
add_library(vix::ws ALIAS vix_websocket)
endif()
target_compile_features(vix_websocket INTERFACE cxx_std_20)
target_include_directories(vix_websocket INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_libraries(vix_websocket INTERFACE
${VIX_CORE_TARGET}
${VIX_UTILS_TARGET}
)
vix_websocket_try_link_json(vix_websocket INTERFACE)
install(TARGETS vix_websocket
EXPORT VixTargets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING
PATTERN "*.hpp"
PATTERN "*.h"
)
endif()
# Examples (optional)
option(VIX_WEBSOCKET_BUILD_EXAMPLES
"Build WebSocket examples (server + HTML chat demo)" ON)
if (VIX_WEBSOCKET_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
# Summary
message(STATUS "------------------------------------------------------")
message(STATUS "vix::websocket configured (${PROJECT_VERSION})")
if (WEBSOCKET_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 "Utils target: ${VIX_UTILS_TARGET}")
message(STATUS "JSON policy: ${VIX_WEBSOCKET_WITH_JSON}")
message(STATUS "Include dir: ${CMAKE_CURRENT_SOURCE_DIR}/include")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Fetch core enabled: ${VIX_WEBSOCKET_FETCH_CORE}")
message(STATUS "Fetch utils enabled: ${VIX_WEBSOCKET_FETCH_UTILS}")
message(STATUS "------------------------------------------------------")