CMakeLists.txt 4.2 KB

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