CMakeLists.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. src/storage/lmdb_txn.cpp
  57. src/storage/lmdb_dbi.cpp
  58. src/storage/document_store_lmdb.cpp
  59. src/storage/migrate_v1_to_v2.cpp
  60. src/storage/project_store.cpp
  61. src/tls/cert_generator.cpp
  62. )
  63. # Create executable
  64. add_executable(smartbotic-database ${DATABASE_SERVICE_SOURCES})
  65. target_include_directories(smartbotic-database PRIVATE
  66. ${CMAKE_CURRENT_SOURCE_DIR}/src
  67. )
  68. # Link dependencies
  69. if(BUILD_SHARED_LIBS)
  70. target_link_libraries(smartbotic-database PRIVATE smartbotic-db-client)
  71. else()
  72. target_link_libraries(smartbotic-database PRIVATE smartbotic_db_proto)
  73. endif()
  74. target_link_libraries(smartbotic-database PRIVATE
  75. OpenSSL::SSL
  76. OpenSSL::Crypto
  77. )
  78. if(TARGET nlohmann_json::nlohmann_json)
  79. target_link_libraries(smartbotic-database PRIVATE nlohmann_json::nlohmann_json)
  80. else()
  81. target_include_directories(smartbotic-database PRIVATE ${NLOHMANN_JSON_INCLUDE_DIRS})
  82. endif()
  83. if(TARGET spdlog::spdlog)
  84. target_link_libraries(smartbotic-database PRIVATE spdlog::spdlog)
  85. else()
  86. target_link_libraries(smartbotic-database PRIVATE ${SPDLOG_LIBRARIES})
  87. target_include_directories(smartbotic-database PRIVATE ${SPDLOG_INCLUDE_DIRS})
  88. endif()
  89. # LZ4 compression support
  90. if(LZ4_FOUND)
  91. target_compile_definitions(smartbotic-database PRIVATE HAVE_LZ4)
  92. target_link_libraries(smartbotic-database PRIVATE ${LZ4_LIBRARIES})
  93. target_include_directories(smartbotic-database PRIVATE ${LZ4_INCLUDE_DIRS})
  94. endif()
  95. # yyjson — phase A v1.10 hot-path JSON parser
  96. target_link_libraries(smartbotic-database PRIVATE ${yyjson_LIBRARIES})
  97. target_include_directories(smartbotic-database PRIVATE ${yyjson_INCLUDE_DIRS})
  98. # LMDB — v2.0 storage substrate
  99. target_link_libraries(smartbotic-database PRIVATE ${LMDB_LIBRARY})
  100. target_include_directories(smartbotic-database PRIVATE ${LMDB_INCLUDE_DIR})
  101. # v1.9.4 — jemalloc allocator (overrides glibc malloc/free)
  102. if(JEMALLOC_FOUND)
  103. target_compile_definitions(smartbotic-database PRIVATE HAVE_JEMALLOC)
  104. target_link_libraries(smartbotic-database PRIVATE ${JEMALLOC_LIBRARIES})
  105. target_include_directories(smartbotic-database PRIVATE ${JEMALLOC_INCLUDE_DIRS})
  106. # Position-sensitive: jemalloc must be linked early so its symbols win
  107. # over libc's. -Wl,-no-as-needed forces the linker to record the
  108. # dependency even though we don't reference any jemalloc symbol
  109. # directly (we just want its malloc to override).
  110. target_link_options(smartbotic-database PRIVATE -Wl,-no-as-needed)
  111. message(STATUS "Linking with jemalloc (${JEMALLOC_VERSION})")
  112. else()
  113. message(STATUS "jemalloc not found — falling back to glibc malloc (degraded RSS reclaim under load)")
  114. endif()
  115. # Systemd support
  116. if(SYSTEMD_FOUND)
  117. target_compile_definitions(smartbotic-database PRIVATE HAVE_SYSTEMD)
  118. target_link_libraries(smartbotic-database PRIVATE ${SYSTEMD_LIBRARIES})
  119. target_include_directories(smartbotic-database PRIVATE ${SYSTEMD_INCLUDE_DIRS})
  120. endif()
  121. # Installation
  122. install(TARGETS smartbotic-database
  123. RUNTIME DESTINATION bin
  124. )