CMakeLists.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. # Source files
  24. set(DATABASE_SERVICE_SOURCES
  25. src/main.cpp
  26. src/database_service.cpp
  27. src/database_grpc_impl.cpp
  28. src/doc_binary.cpp
  29. src/json_parse.cpp
  30. src/memory_store.cpp
  31. src/persistence/persistence_manager.cpp
  32. src/persistence/wal.cpp
  33. src/persistence/snapshot.cpp
  34. src/persistence/history_store.cpp
  35. src/encryption/encryption_manager.cpp
  36. src/events/event_manager.cpp
  37. src/files/file_manager.cpp
  38. src/files/file_store.cpp
  39. src/replication/replication_manager.cpp
  40. src/replication/sync_protocol.cpp
  41. src/replication/conflict_resolver.cpp
  42. src/migrations/migration_runner.cpp
  43. src/views/projection.cpp
  44. src/views/view_manager.cpp
  45. src/config/collection_config_manager.cpp
  46. src/config/config_loader.cpp
  47. )
  48. # Create executable
  49. add_executable(smartbotic-database ${DATABASE_SERVICE_SOURCES})
  50. target_include_directories(smartbotic-database PRIVATE
  51. ${CMAKE_CURRENT_SOURCE_DIR}/src
  52. )
  53. # Link dependencies
  54. if(BUILD_SHARED_LIBS)
  55. target_link_libraries(smartbotic-database PRIVATE smartbotic-db-client)
  56. else()
  57. target_link_libraries(smartbotic-database PRIVATE smartbotic_db_proto)
  58. endif()
  59. target_link_libraries(smartbotic-database PRIVATE
  60. OpenSSL::SSL
  61. OpenSSL::Crypto
  62. )
  63. if(TARGET nlohmann_json::nlohmann_json)
  64. target_link_libraries(smartbotic-database PRIVATE nlohmann_json::nlohmann_json)
  65. else()
  66. target_include_directories(smartbotic-database PRIVATE ${NLOHMANN_JSON_INCLUDE_DIRS})
  67. endif()
  68. if(TARGET spdlog::spdlog)
  69. target_link_libraries(smartbotic-database PRIVATE spdlog::spdlog)
  70. else()
  71. target_link_libraries(smartbotic-database PRIVATE ${SPDLOG_LIBRARIES})
  72. target_include_directories(smartbotic-database PRIVATE ${SPDLOG_INCLUDE_DIRS})
  73. endif()
  74. # LZ4 compression support
  75. if(LZ4_FOUND)
  76. target_compile_definitions(smartbotic-database PRIVATE HAVE_LZ4)
  77. target_link_libraries(smartbotic-database PRIVATE ${LZ4_LIBRARIES})
  78. target_include_directories(smartbotic-database PRIVATE ${LZ4_INCLUDE_DIRS})
  79. endif()
  80. # yyjson — phase A v1.10 hot-path JSON parser
  81. target_link_libraries(smartbotic-database PRIVATE ${yyjson_LIBRARIES})
  82. target_include_directories(smartbotic-database PRIVATE ${yyjson_INCLUDE_DIRS})
  83. # v1.9.4 — jemalloc allocator (overrides glibc malloc/free)
  84. if(JEMALLOC_FOUND)
  85. target_compile_definitions(smartbotic-database PRIVATE HAVE_JEMALLOC)
  86. target_link_libraries(smartbotic-database PRIVATE ${JEMALLOC_LIBRARIES})
  87. target_include_directories(smartbotic-database PRIVATE ${JEMALLOC_INCLUDE_DIRS})
  88. # Position-sensitive: jemalloc must be linked early so its symbols win
  89. # over libc's. -Wl,-no-as-needed forces the linker to record the
  90. # dependency even though we don't reference any jemalloc symbol
  91. # directly (we just want its malloc to override).
  92. target_link_options(smartbotic-database PRIVATE -Wl,-no-as-needed)
  93. message(STATUS "Linking with jemalloc (${JEMALLOC_VERSION})")
  94. else()
  95. message(STATUS "jemalloc not found — falling back to glibc malloc (degraded RSS reclaim under load)")
  96. endif()
  97. # Systemd support
  98. if(SYSTEMD_FOUND)
  99. target_compile_definitions(smartbotic-database PRIVATE HAVE_SYSTEMD)
  100. target_link_libraries(smartbotic-database PRIVATE ${SYSTEMD_LIBRARIES})
  101. target_include_directories(smartbotic-database PRIVATE ${SYSTEMD_INCLUDE_DIRS})
  102. endif()
  103. # Installation
  104. install(TARGETS smartbotic-database
  105. RUNTIME DESTINATION bin
  106. )