CMakeLists.txt 4.5 KB

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