CMakeLists.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/relations/relation_manager.cpp
  54. src/relations/relation_index.cpp
  55. src/relations/relation_enforcement.cpp
  56. src/relations/relation_cascade.cpp
  57. src/config/collection_config_manager.cpp
  58. src/config/config_loader.cpp
  59. src/storage/lmdb_env.cpp
  60. src/storage/lmdb_txn.cpp
  61. src/storage/lmdb_dbi.cpp
  62. src/storage/document_store_lmdb.cpp
  63. src/storage/secondary_index.cpp
  64. src/storage/subdb_identity.cpp
  65. src/storage/subdb_placement.cpp
  66. src/storage/migrate_v1_to_v2.cpp
  67. src/storage/project_store.cpp
  68. src/tls/cert_generator.cpp
  69. src/auth/auth_interceptor.cpp
  70. src/auth/principal.cpp
  71. src/security/policy_manager.cpp
  72. )
  73. # Create executable
  74. add_executable(smartbotic-database ${DATABASE_SERVICE_SOURCES})
  75. target_include_directories(smartbotic-database PRIVATE
  76. ${CMAKE_CURRENT_SOURCE_DIR}/src
  77. )
  78. # Version string comes from the top-level VERSION file so --version can never
  79. # drift from the deb version again (it reported a hardcoded 1.0.0 for all of 2.x).
  80. target_compile_definitions(smartbotic-database PRIVATE
  81. SMARTBOTIC_DB_VERSION_STRING="${SMARTBOTIC_DB_VERSION}"
  82. SMARTBOTIC_DB_GIT_COMMIT_STRING="${SMARTBOTIC_DB_GIT_COMMIT}"
  83. )
  84. # Link dependencies
  85. if(BUILD_SHARED_LIBS)
  86. target_link_libraries(smartbotic-database PRIVATE smartbotic-db-client)
  87. else()
  88. target_link_libraries(smartbotic-database PRIVATE smartbotic_db_proto)
  89. endif()
  90. target_link_libraries(smartbotic-database PRIVATE
  91. OpenSSL::SSL
  92. OpenSSL::Crypto
  93. )
  94. if(TARGET nlohmann_json::nlohmann_json)
  95. target_link_libraries(smartbotic-database PRIVATE nlohmann_json::nlohmann_json)
  96. else()
  97. target_include_directories(smartbotic-database PRIVATE ${NLOHMANN_JSON_INCLUDE_DIRS})
  98. endif()
  99. if(TARGET spdlog::spdlog)
  100. target_link_libraries(smartbotic-database PRIVATE spdlog::spdlog)
  101. else()
  102. target_link_libraries(smartbotic-database PRIVATE ${SPDLOG_LIBRARIES})
  103. target_include_directories(smartbotic-database PRIVATE ${SPDLOG_INCLUDE_DIRS})
  104. endif()
  105. # LZ4 compression support
  106. if(LZ4_FOUND)
  107. target_compile_definitions(smartbotic-database PRIVATE HAVE_LZ4)
  108. target_link_libraries(smartbotic-database PRIVATE ${LZ4_LIBRARIES})
  109. target_include_directories(smartbotic-database PRIVATE ${LZ4_INCLUDE_DIRS})
  110. endif()
  111. # yyjson — phase A v1.10 hot-path JSON parser
  112. target_link_libraries(smartbotic-database PRIVATE ${yyjson_LIBRARIES})
  113. target_include_directories(smartbotic-database PRIVATE ${yyjson_INCLUDE_DIRS})
  114. # LMDB — v2.0 storage substrate
  115. target_link_libraries(smartbotic-database PRIVATE ${LMDB_LIBRARY})
  116. target_include_directories(smartbotic-database PRIVATE ${LMDB_INCLUDE_DIR})
  117. # v1.9.4 — jemalloc allocator (overrides glibc malloc/free)
  118. if(JEMALLOC_FOUND)
  119. target_compile_definitions(smartbotic-database PRIVATE HAVE_JEMALLOC)
  120. target_link_libraries(smartbotic-database PRIVATE ${JEMALLOC_LIBRARIES})
  121. target_include_directories(smartbotic-database PRIVATE ${JEMALLOC_INCLUDE_DIRS})
  122. # Position-sensitive: jemalloc must be linked early so its symbols win
  123. # over libc's. -Wl,-no-as-needed forces the linker to record the
  124. # dependency even though we don't reference any jemalloc symbol
  125. # directly (we just want its malloc to override).
  126. target_link_options(smartbotic-database PRIVATE -Wl,-no-as-needed)
  127. message(STATUS "Linking with jemalloc (${JEMALLOC_VERSION})")
  128. else()
  129. message(STATUS "jemalloc not found — falling back to glibc malloc (degraded RSS reclaim under load)")
  130. endif()
  131. # Systemd support
  132. if(SYSTEMD_FOUND)
  133. target_compile_definitions(smartbotic-database PRIVATE HAVE_SYSTEMD)
  134. target_link_libraries(smartbotic-database PRIVATE ${SYSTEMD_LIBRARIES})
  135. target_include_directories(smartbotic-database PRIVATE ${SYSTEMD_INCLUDE_DIRS})
  136. endif()
  137. # Installation
  138. install(TARGETS smartbotic-database
  139. RUNTIME DESTINATION bin
  140. )