CMakeLists.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Smartbotic Database Service
  2. # Find OpenSSL for encryption
  3. find_package(OpenSSL REQUIRED)
  4. # PkgConfig is REQUIRED because yyjson (v1.10+) is a hard build dependency.
  5. # LZ4 and jemalloc remain optional, but if pkg-config itself is missing we
  6. # fail at configure time rather than silently producing a yyjson-free binary
  7. # via empty ${yyjson_LIBRARIES} expansion.
  8. find_package(PkgConfig REQUIRED)
  9. # yyjson — phase A v1.10 hot-path JSON parser. Hard dependency.
  10. pkg_check_modules(yyjson REQUIRED yyjson)
  11. # Optional LZ4 for compression
  12. pkg_check_modules(LZ4 QUIET liblz4)
  13. # v1.9.4 — jemalloc replaces glibc malloc for the smartbotic-database
  14. # binary. The fragmentation pattern from per-RPC nlohmann::json node
  15. # churn (107k 33-byte allocations observed on Zoe) defeats glibc's
  16. # malloc_trim because the trailing region of each arena always has a
  17. # live chunk; jemalloc's background-thread dirty-page decay sidesteps
  18. # the contiguity requirement and reclaims unused pages on a wallclock
  19. # timer regardless of allocation activity. Linked as a library so
  20. # the binary picks up jemalloc's malloc/free symbols at link time;
  21. # no LD_PRELOAD needed.
  22. pkg_check_modules(JEMALLOC QUIET jemalloc)
  23. # LMDB — v2.0 storage substrate. liblmdb does not ship a pkg-config file on
  24. # Debian 13 (`pkg-config --modversion lmdb` fails), so resolve via find_path /
  25. # find_library. Hard dependency.
  26. find_path(LMDB_INCLUDE_DIR lmdb.h)
  27. find_library(LMDB_LIBRARY NAMES lmdb)
  28. if(NOT LMDB_INCLUDE_DIR OR NOT LMDB_LIBRARY)
  29. message(FATAL_ERROR "LMDB not found. Install liblmdb-dev.")
  30. endif()
  31. # Source files
  32. set(DATABASE_SERVICE_SOURCES
  33. src/main.cpp
  34. src/database_service.cpp
  35. src/database_grpc_impl.cpp
  36. src/doc_binary.cpp
  37. src/json_parse.cpp
  38. src/memory_store.cpp
  39. src/persistence/persistence_manager.cpp
  40. src/persistence/wal.cpp
  41. src/persistence/snapshot.cpp
  42. src/persistence/history_store.cpp
  43. src/encryption/encryption_manager.cpp
  44. src/events/event_manager.cpp
  45. src/files/file_manager.cpp
  46. src/files/file_store.cpp
  47. src/replication/replication_manager.cpp
  48. src/replication/sync_protocol.cpp
  49. src/replication/conflict_resolver.cpp
  50. src/migrations/migration_runner.cpp
  51. src/views/projection.cpp
  52. src/views/view_manager.cpp
  53. src/config/collection_config_manager.cpp
  54. src/config/config_loader.cpp
  55. src/storage/lmdb_env.cpp
  56. )
  57. # Create executable
  58. add_executable(smartbotic-database ${DATABASE_SERVICE_SOURCES})
  59. target_include_directories(smartbotic-database PRIVATE
  60. ${CMAKE_CURRENT_SOURCE_DIR}/src
  61. )
  62. # Link dependencies
  63. if(BUILD_SHARED_LIBS)
  64. target_link_libraries(smartbotic-database PRIVATE smartbotic-db-client)
  65. else()
  66. target_link_libraries(smartbotic-database PRIVATE smartbotic_db_proto)
  67. endif()
  68. target_link_libraries(smartbotic-database PRIVATE
  69. OpenSSL::SSL
  70. OpenSSL::Crypto
  71. )
  72. if(TARGET nlohmann_json::nlohmann_json)
  73. target_link_libraries(smartbotic-database PRIVATE nlohmann_json::nlohmann_json)
  74. else()
  75. target_include_directories(smartbotic-database PRIVATE ${NLOHMANN_JSON_INCLUDE_DIRS})
  76. endif()
  77. if(TARGET spdlog::spdlog)
  78. target_link_libraries(smartbotic-database PRIVATE spdlog::spdlog)
  79. else()
  80. target_link_libraries(smartbotic-database PRIVATE ${SPDLOG_LIBRARIES})
  81. target_include_directories(smartbotic-database PRIVATE ${SPDLOG_INCLUDE_DIRS})
  82. endif()
  83. # LZ4 compression support
  84. if(LZ4_FOUND)
  85. target_compile_definitions(smartbotic-database PRIVATE HAVE_LZ4)
  86. target_link_libraries(smartbotic-database PRIVATE ${LZ4_LIBRARIES})
  87. target_include_directories(smartbotic-database PRIVATE ${LZ4_INCLUDE_DIRS})
  88. endif()
  89. # yyjson — phase A v1.10 hot-path JSON parser
  90. target_link_libraries(smartbotic-database PRIVATE ${yyjson_LIBRARIES})
  91. target_include_directories(smartbotic-database PRIVATE ${yyjson_INCLUDE_DIRS})
  92. # LMDB — v2.0 storage substrate
  93. target_link_libraries(smartbotic-database PRIVATE ${LMDB_LIBRARY})
  94. target_include_directories(smartbotic-database PRIVATE ${LMDB_INCLUDE_DIR})
  95. # v1.9.4 — jemalloc allocator (overrides glibc malloc/free)
  96. if(JEMALLOC_FOUND)
  97. target_compile_definitions(smartbotic-database PRIVATE HAVE_JEMALLOC)
  98. target_link_libraries(smartbotic-database PRIVATE ${JEMALLOC_LIBRARIES})
  99. target_include_directories(smartbotic-database PRIVATE ${JEMALLOC_INCLUDE_DIRS})
  100. # Position-sensitive: jemalloc must be linked early so its symbols win
  101. # over libc's. -Wl,-no-as-needed forces the linker to record the
  102. # dependency even though we don't reference any jemalloc symbol
  103. # directly (we just want its malloc to override).
  104. target_link_options(smartbotic-database PRIVATE -Wl,-no-as-needed)
  105. message(STATUS "Linking with jemalloc (${JEMALLOC_VERSION})")
  106. else()
  107. message(STATUS "jemalloc not found — falling back to glibc malloc (degraded RSS reclaim under load)")
  108. endif()
  109. # Systemd support
  110. if(SYSTEMD_FOUND)
  111. target_compile_definitions(smartbotic-database PRIVATE HAVE_SYSTEMD)
  112. target_link_libraries(smartbotic-database PRIVATE ${SYSTEMD_LIBRARIES})
  113. target_include_directories(smartbotic-database PRIVATE ${SYSTEMD_INCLUDE_DIRS})
  114. endif()
  115. # Installation
  116. install(TARGETS smartbotic-database
  117. RUNTIME DESTINATION bin
  118. )