-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
460 lines (398 loc) · 16.6 KB
/
Copy pathCMakeLists.txt
File metadata and controls
460 lines (398 loc) · 16.6 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# @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 | DB module (core anti-ORM)
cmake_minimum_required(VERSION 3.20)
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
project(vix_db VERSION 0.1.0 LANGUAGES CXX)
endif()
include(GNUInstallDirs)
# ------------------------------------------------------------------------------
# Options
# ------------------------------------------------------------------------------
option(VIX_DB_BUILD_TESTS "Build unit tests for Vix DB" OFF)
option(VIX_DB_BUILD_EXAMPLES "Build examples for Vix DB" OFF)
if (DEFINED VIX_UMBRELLA_BUILD)
set(VIX_DB_BUILD_TOOLS ON CACHE BOOL "Build DB CLI tools (migrator)" FORCE)
endif()
# SQL engines (drivers)
option(VIX_DB_USE_MYSQL "Enable MySQL Connector/C++ driver" ON)
option(VIX_DB_REQUIRE_MYSQL "Fail if MySQL requested but not found" OFF)
option(VIX_DB_USE_SQLITE "Enable SQLite3 driver" OFF)
option(VIX_DB_REQUIRE_SQLITE "Fail if SQLite requested but not found" OFF)
# Future (not enabled by default)
option(VIX_DB_USE_POSTGRES "Enable PostgreSQL driver (future)" OFF)
option(VIX_DB_REQUIRE_POSTGRES "Fail if Postgres requested but not found" OFF)
# Redis is not SQL, but useful for caching / sessions / queue (future)
option(VIX_DB_USE_REDIS "Enable Redis client (future)" OFF)
option(VIX_DB_REQUIRE_REDIS "Fail if Redis requested but not found" OFF)
option(VIX_BUILD_EXAMPLES "Build examples for Vix modules" OFF)
# ------------------------------------------------------------------------------
# Language / PIC
# ------------------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# ------------------------------------------------------------------------------
# Warnings / Sanitizers
# ------------------------------------------------------------------------------
set(_WARNINGS_GNU
-Wall -Wextra -Wshadow -Wconversion
-Wnon-virtual-dtor -Wold-style-cast -Woverloaded-virtual -Wpedantic
)
set(_WARNINGS_MSVC /W4 /permissive-)
# ------------------------------------------------------------------------------
# Optional deps
# ------------------------------------------------------------------------------
find_package(spdlog QUIET CONFIG)
# ------------------------------------------------------------------------------
# Feature flags (computed)
# ------------------------------------------------------------------------------
set(VIX_DB_HAS_MYSQL OFF)
set(VIX_DB_HAS_SQLITE OFF)
set(VIX_DB_HAS_POSTGRES OFF)
set(VIX_DB_HAS_REDIS OFF)
# ------------------------------------------------------------------------------
# MySQL detection (via alias)
# - Source of truth: cmake/MySQLCppConnAlias.cmake defines MySQLCppConn::MySQLCppConn
# ------------------------------------------------------------------------------
if (VIX_DB_USE_MYSQL)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/MySQLCppConnAlias.cmake)
if (TARGET MySQLCppConn::MySQLCppConn)
set(VIX_DB_HAS_MYSQL ON)
message(STATUS "[vix_db] mysql: enabled (alias)")
get_target_property(_vix_mysql_loc MySQLCppConn::MySQLCppConn IMPORTED_LOCATION)
if (NOT _vix_mysql_loc)
message(WARNING "[vix_db] mysql: alias target exists but has no IMPORTED_LOCATION. Disabling.")
set(VIX_DB_HAS_MYSQL OFF)
set(VIX_DB_USE_MYSQL OFF)
endif()
else()
if (VIX_DB_REQUIRE_MYSQL)
message(FATAL_ERROR "[vix_db] mysql requested but Connector/C++ not found (VIX_DB_REQUIRE_MYSQL=ON).")
endif()
message(STATUS "[vix_db] mysql: disabled (connector not found)")
set(VIX_DB_USE_MYSQL OFF)
endif()
endif()
# ------------------------------------------------------------------------------
# SQLite detection (soft)
# ------------------------------------------------------------------------------
set(_VIX_SQLITE_TARGET "")
if (VIX_DB_USE_SQLITE)
find_package(SQLite3 QUIET)
if (TARGET SQLite::SQLite3)
set(_VIX_SQLITE_TARGET SQLite::SQLite3)
elseif (TARGET SQLite3::SQLite3)
add_library(SQLite::SQLite3 ALIAS SQLite3::SQLite3)
set(_VIX_SQLITE_TARGET SQLite::SQLite3)
endif()
if (SQLite3_FOUND OR _VIX_SQLITE_TARGET)
set(VIX_DB_HAS_SQLITE ON)
message(STATUS "[vix_db] sqlite: enabled")
else()
if (VIX_DB_REQUIRE_SQLITE)
message(FATAL_ERROR "[vix_db] sqlite requested but not found (VIX_DB_REQUIRE_SQLITE=ON).")
endif()
message(STATUS "[vix_db] sqlite: disabled (not found)")
set(VIX_DB_USE_SQLITE OFF)
endif()
endif()
# ------------------------------------------------------------------------------
# PostgreSQL detection (future placeholder)
# ------------------------------------------------------------------------------
set(_VIX_PQ_TARGET "")
if (VIX_DB_USE_POSTGRES)
# Try CMake module first (may define PostgreSQL::PostgreSQL in some setups)
find_package(PostgreSQL QUIET)
# Generic fallback to libpq
if (PostgreSQL_FOUND)
# PostgreSQL CMake module typically exports variables, not a target. Create an alias target.
if (NOT TARGET PostgreSQL::libpq)
add_library(PostgreSQL::libpq INTERFACE IMPORTED)
set_target_properties(PostgreSQL::libpq PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${PostgreSQL_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${PostgreSQL_LIBRARIES}"
)
endif()
set(_VIX_PQ_TARGET PostgreSQL::libpq)
else()
# fallback manual find (libpq)
find_library(_VIX_PQ_LIB NAMES pq libpq)
find_path(_VIX_PQ_INC NAMES libpq-fe.h)
if (_VIX_PQ_LIB AND _VIX_PQ_INC)
add_library(PostgreSQL::libpq INTERFACE IMPORTED)
set_target_properties(PostgreSQL::libpq PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_VIX_PQ_INC}"
INTERFACE_LINK_LIBRARIES "${_VIX_PQ_LIB}"
)
set(_VIX_PQ_TARGET PostgreSQL::libpq)
endif()
endif()
if (_VIX_PQ_TARGET)
set(VIX_DB_HAS_POSTGRES ON)
message(STATUS "[vix_db] postgres: enabled (placeholder target=${_VIX_PQ_TARGET})")
else()
if (VIX_DB_REQUIRE_POSTGRES)
message(FATAL_ERROR "[vix_db] postgres requested but libpq not found (VIX_DB_REQUIRE_POSTGRES=ON).")
endif()
message(STATUS "[vix_db] postgres: disabled (not found)")
set(VIX_DB_USE_POSTGRES OFF)
endif()
endif()
# Redis detection (future placeholder)
set(_VIX_REDIS_TARGET "")
if (VIX_DB_USE_REDIS)
# Try redis-plus-plus first (CMake package commonly provides redis++::redis++)
find_package(redis++ QUIET CONFIG)
if (TARGET redis++::redis++)
set(_VIX_REDIS_TARGET redis++::redis++)
else()
# fallback hiredis (often exports hiredis::hiredis via config, but not always)
find_package(hiredis QUIET CONFIG)
if (TARGET hiredis::hiredis)
set(_VIX_REDIS_TARGET hiredis::hiredis)
else()
find_library(_VIX_HIREDIS_LIB NAMES hiredis)
find_path(_VIX_HIREDIS_INC NAMES hiredis/hiredis.h)
if (_VIX_HIREDIS_LIB AND _VIX_HIREDIS_INC)
add_library(hiredis::hiredis INTERFACE IMPORTED)
set_target_properties(hiredis::hiredis PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_VIX_HIREDIS_INC}"
INTERFACE_LINK_LIBRARIES "${_VIX_HIREDIS_LIB}"
)
set(_VIX_REDIS_TARGET hiredis::hiredis)
endif()
endif()
endif()
if (_VIX_REDIS_TARGET)
set(VIX_DB_HAS_REDIS ON)
message(STATUS "[vix_db] redis: enabled (placeholder target=${_VIX_REDIS_TARGET})")
else()
if (VIX_DB_REQUIRE_REDIS)
message(FATAL_ERROR "[vix_db] redis requested but client not found (VIX_DB_REQUIRE_REDIS=ON).")
endif()
message(STATUS "[vix_db] redis: disabled (not found)")
set(VIX_DB_USE_REDIS OFF)
endif()
endif()
# ------------------------------------------------------------------------------
# Sources / Headers
# ------------------------------------------------------------------------------
set(VIX_DB_PUBLIC_HEADERS
include/vix/db/db.hpp
include/vix/db/Database.hpp
include/vix/db/Transaction.hpp
include/vix/db/Sha256.hpp
include/vix/db/core/Errors.hpp
include/vix/db/core/Value.hpp
include/vix/db/core/Drivers.hpp
include/vix/db/core/Result.hpp
include/vix/db/pool/ConnectionPool.hpp
include/vix/db/mig/Migration.hpp
include/vix/db/mig/MigrationsRunner.hpp
include/vix/db/mig/FileMigrationsRunner.hpp
)
set(VIX_DB_SOURCES
src/pool/ConnectionPool.cpp
src/Database.cpp
src/mig/MigrationsRunner.cpp
src/mig/FileMigrationsRunner.cpp
src/Sha256.cpp
src/schema/Json.cpp
src/mig/diff/Diff.cpp
src/mig/sql/MySqlGenerator.cpp
)
# MySQL driver sources
if (VIX_DB_HAS_MYSQL)
list(APPEND VIX_DB_PUBLIC_HEADERS include/vix/db/drivers/mysql/MySQLDriver.hpp)
list(APPEND VIX_DB_SOURCES src/mysql/MySQLDriver.cpp)
endif()
# SQLite driver sources
if (VIX_DB_HAS_SQLITE)
list(APPEND VIX_DB_PUBLIC_HEADERS include/vix/db/drivers/sqlite/SQLiteDriver.hpp)
list(APPEND VIX_DB_SOURCES src/sqlite/SQLiteDriver.cpp)
endif()
# Postgres driver sources (future placeholders)
if (VIX_DB_HAS_POSTGRES)
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/include/vix/db/postgres/PostgresDriver.hpp"
AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/postgres/PostgresDriver.cpp")
list(APPEND VIX_DB_PUBLIC_HEADERS include/vix/db/postgres/PostgresDriver.hpp)
list(APPEND VIX_DB_SOURCES src/postgres/PostgresDriver.cpp)
else()
message(WARNING "[vix_db] postgres enabled but driver sources not found; disabling.")
set(VIX_DB_HAS_POSTGRES OFF)
set(VIX_DB_USE_POSTGRES OFF)
endif()
endif()
# Redis client sources (future placeholders)
if (VIX_DB_HAS_REDIS)
list(APPEND VIX_DB_PUBLIC_HEADERS include/vix/db/redis/RedisClient.hpp)
list(APPEND VIX_DB_SOURCES src/redis/RedisClient.cpp)
endif()
if (VIX_DB_BUILD_TOOLS)
add_executable(vix_db_migrator)
target_sources(vix_db_migrator PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/tools/migrator/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tools/migrator/MigratorCLI.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tools/migrator/MakeMigrations.cpp
)
target_link_libraries(vix_db_migrator PRIVATE vix::db)
target_compile_features(vix_db_migrator PRIVATE cxx_std_20)
if (COMMAND vix_enable_sanitizers)
vix_enable_sanitizers(vix_db_migrator)
endif()
if (MSVC)
target_compile_options(vix_db_migrator PRIVATE ${_WARNINGS_MSVC})
else()
target_compile_options(vix_db_migrator PRIVATE ${_WARNINGS_GNU})
endif()
install(TARGETS vix_db_migrator
RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/vix
)
endif()
# ------------------------------------------------------------------------------
# Core preload dependencies
# ------------------------------------------------------------------------------
# Core needs these targets to be available before it is configured.
# Otherwise it may disable JSON/template/env and fail later while compiling
# headers that still include those modules.
if (NOT TARGET vix::json AND NOT TARGET vix_json)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../json/CMakeLists.txt")
message(STATUS "[vix_db] Adding json before core: ../json")
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../json" json_build)
endif()
endif()
if (NOT TARGET vix::template AND NOT TARGET template)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../template/CMakeLists.txt")
message(STATUS "[vix_db] Adding template before core: ../template")
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../template" template_build)
endif()
endif()
if (NOT TARGET vix::env AND NOT TARGET vix_env)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../env/CMakeLists.txt")
message(STATUS "[vix_db] Adding env before core: ../env")
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../env" env_build)
endif()
endif()
# ------------------------------------------------------------------------------
# If building DB standalone (not from umbrella), bring core as subdir
# ------------------------------------------------------------------------------
if (NOT TARGET vix::core)
add_subdirectory(../core core_build)
endif()
# ------------------------------------------------------------------------------
# Library target
# ------------------------------------------------------------------------------
add_library(vix_db STATIC ${VIX_DB_SOURCES} ${VIX_DB_PUBLIC_HEADERS})
add_library(vix::db ALIAS vix_db)
if (COMMAND vix_enable_sanitizers)
vix_enable_sanitizers(vix_db)
endif()
# Keep feature macros consistent everywhere (build + consumers)
target_compile_definitions(vix_db PUBLIC
VIX_DB_HAS_MYSQL=$<BOOL:${VIX_DB_HAS_MYSQL}>
VIX_DB_HAS_SQLITE=$<BOOL:${VIX_DB_HAS_SQLITE}>
VIX_DB_HAS_POSTGRES=$<BOOL:${VIX_DB_HAS_POSTGRES}>
VIX_DB_HAS_REDIS=$<BOOL:${VIX_DB_HAS_REDIS}>
)
# export name => installed imported target is vix::db
set_target_properties(vix_db PROPERTIES EXPORT_NAME db)
target_include_directories(vix_db PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
# DB depends on Vix Core (Config.hpp, util, etc.)
target_link_libraries(vix_db PUBLIC vix::core)
if (MSVC)
target_compile_options(vix_db PRIVATE ${_WARNINGS_MSVC})
else()
target_compile_options(vix_db PRIVATE ${_WARNINGS_GNU})
endif()
# deps / defines
if (spdlog_FOUND)
if (TARGET spdlog::spdlog_header_only)
target_link_libraries(vix_db PRIVATE spdlog::spdlog_header_only)
elseif (TARGET spdlog::spdlog)
target_link_libraries(vix_db PRIVATE spdlog::spdlog)
endif()
target_compile_definitions(vix_db PUBLIC VIX_DB_HAS_SPDLOG=1)
endif()
# ------------------------------------------------------------------------------
# Link drivers (single source of truth)
# ------------------------------------------------------------------------------
if (VIX_DB_HAS_MYSQL)
# Flatten the imported target into real link flags so consumers
# always link the connector (important for static vix_db).
get_target_property(_vix_mysql_lib MySQLCppConn::MySQLCppConn IMPORTED_LOCATION)
get_target_property(_vix_mysql_inc MySQLCppConn::MySQLCppConn INTERFACE_INCLUDE_DIRECTORIES)
if (NOT _vix_mysql_lib)
message(FATAL_ERROR "[vix_db] mysql enabled but MySQLCppConn::MySQLCppConn has no IMPORTED_LOCATION")
endif()
target_link_libraries(vix_db PUBLIC "${_vix_mysql_lib}")
if (_vix_mysql_inc)
target_include_directories(vix_db PUBLIC "${_vix_mysql_inc}")
endif()
endif()
if (VIX_DB_HAS_SQLITE)
if (_VIX_SQLITE_TARGET)
target_link_libraries(vix_db PUBLIC ${_VIX_SQLITE_TARGET})
else()
message(WARNING "[vix_db] sqlite enabled but no target found; linkage skipped.")
endif()
endif()
if (VIX_DB_HAS_POSTGRES)
if (_VIX_PQ_TARGET)
target_link_libraries(vix_db PUBLIC ${_VIX_PQ_TARGET})
else()
message(WARNING "[vix_db] postgres enabled but no libpq target found; linkage skipped.")
endif()
endif()
if (VIX_DB_HAS_REDIS)
if (_VIX_REDIS_TARGET)
target_link_libraries(vix_db PUBLIC ${_VIX_REDIS_TARGET})
else()
message(WARNING "[vix_db] redis enabled but no client target found; linkage skipped.")
endif()
endif()
# ------------------------------------------------------------------------------
# Examples
# ------------------------------------------------------------------------------
if (VIX_DB_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
# ------------------------------------------------------------------------------
# Install / export via umbrella export-set "VixTargets"
# ------------------------------------------------------------------------------
if (DEFINED VIX_UMBRELLA_BUILD)
install(TARGETS vix_db
EXPORT VixTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
else()
install(TARGETS vix_db
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
endif()
install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h"
)
message(STATUS "------------------------------------------------------")
message(STATUS "[vix_db] configured (${PROJECT_VERSION})")
message(STATUS "[vix_db] mysql: requested=${VIX_DB_USE_MYSQL} available=${VIX_DB_HAS_MYSQL}")
message(STATUS "[vix_db] sqlite: requested=${VIX_DB_USE_SQLITE} available=${VIX_DB_HAS_SQLITE}")
message(STATUS "[vix_db] postgres: requested=${VIX_DB_USE_POSTGRES} available=${VIX_DB_HAS_POSTGRES}")
message(STATUS "[vix_db] redis: requested=${VIX_DB_USE_REDIS} available=${VIX_DB_HAS_REDIS}")
message(STATUS "------------------------------------------------------")