CMakeLists.txt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Smartbotic Database
  2. # Standalone document database with gRPC API
  3. cmake_minimum_required(VERSION 3.20)
  4. # Read version from VERSION file
  5. file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" SMARTBOTIC_DB_VERSION)
  6. string(STRIP "${SMARTBOTIC_DB_VERSION}" SMARTBOTIC_DB_VERSION)
  7. # v2.8.0 — build provenance. BUILD_GIT_COMMIT was computed by build.sh and passed
  8. # as a Docker --build-arg since v2.4, and Dockerfile.build declared the ARG, but
  9. # nothing ever consumed it: unlike BUILD_VERSION it was never promoted to an ENV,
  10. # so no commit reached the binary or the package. `dpkg -s` showed a version and
  11. # nothing else, which is the same triage gap that made --version useless for the
  12. # whole 2.x line before v2.4.1.
  13. #
  14. # Prefer the environment (set by the Docker build), fall back to asking git, and
  15. # accept "unknown" rather than failing a build outside a checkout.
  16. if(DEFINED ENV{BUILD_GIT_COMMIT})
  17. set(SMARTBOTIC_DB_GIT_COMMIT "$ENV{BUILD_GIT_COMMIT}")
  18. else()
  19. execute_process(
  20. COMMAND git -C "${CMAKE_CURRENT_SOURCE_DIR}" rev-parse --short HEAD
  21. OUTPUT_VARIABLE SMARTBOTIC_DB_GIT_COMMIT
  22. OUTPUT_STRIP_TRAILING_WHITESPACE
  23. ERROR_QUIET)
  24. endif()
  25. if(NOT SMARTBOTIC_DB_GIT_COMMIT)
  26. set(SMARTBOTIC_DB_GIT_COMMIT "unknown")
  27. endif()
  28. # Detect standalone vs embedded mode
  29. if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  30. # Building as standalone project
  31. project(smartbotic-database VERSION ${SMARTBOTIC_DB_VERSION} LANGUAGES CXX C)
  32. set(SMARTBOTIC_DB_STANDALONE ON)
  33. else()
  34. # Building as embedded submodule
  35. set(SMARTBOTIC_DB_STANDALONE OFF)
  36. endif()
  37. # Shared libraries for standalone (packaging), static for embedded (submodule)
  38. option(BUILD_SHARED_LIBS "Build shared libraries" ${SMARTBOTIC_DB_STANDALONE})
  39. # C++ settings
  40. set(CMAKE_CXX_STANDARD 20)
  41. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  42. set(CMAKE_CXX_EXTENSIONS OFF)
  43. # Build options
  44. option(SMARTBOTIC_DB_BUILD_CLIENT "Build the database client library" ON)
  45. option(SMARTBOTIC_DB_BUILD_SERVICE "Build the database service executable" ${SMARTBOTIC_DB_STANDALONE})
  46. # Find required packages
  47. find_package(Protobuf REQUIRED)
  48. find_package(gRPC CONFIG QUIET)
  49. if(NOT gRPC_FOUND)
  50. find_package(PkgConfig REQUIRED)
  51. pkg_check_modules(GRPC REQUIRED grpc++)
  52. endif()
  53. find_package(nlohmann_json 3.2.0 QUIET)
  54. if(NOT nlohmann_json_FOUND)
  55. find_package(PkgConfig QUIET)
  56. if(PKG_CONFIG_FOUND)
  57. pkg_check_modules(NLOHMANN_JSON QUIET nlohmann_json)
  58. endif()
  59. if(NOT nlohmann_json_FOUND AND NOT NLOHMANN_JSON_FOUND)
  60. # Not available on system - parent project or FetchContent provides it
  61. if(NOT TARGET nlohmann_json::nlohmann_json)
  62. include(FetchContent)
  63. FetchContent_Declare(
  64. nlohmann_json
  65. GIT_REPOSITORY https://github.com/nlohmann/json.git
  66. GIT_TAG v3.11.3
  67. GIT_SHALLOW TRUE
  68. )
  69. set(JSON_BuildTests OFF CACHE BOOL "" FORCE)
  70. set(JSON_Install OFF CACHE BOOL "" FORCE)
  71. FetchContent_MakeAvailable(nlohmann_json)
  72. endif()
  73. elseif(NLOHMANN_JSON_FOUND AND NOT TARGET nlohmann_json::nlohmann_json)
  74. add_library(nlohmann_json::nlohmann_json INTERFACE IMPORTED)
  75. set_target_properties(nlohmann_json::nlohmann_json PROPERTIES
  76. INTERFACE_INCLUDE_DIRECTORIES "${NLOHMANN_JSON_INCLUDE_DIRS}"
  77. )
  78. endif()
  79. endif()
  80. # Find spdlog
  81. find_package(spdlog QUIET)
  82. if(NOT spdlog_FOUND)
  83. find_package(PkgConfig REQUIRED)
  84. pkg_check_modules(SPDLOG REQUIRED spdlog)
  85. endif()
  86. # Optional: systemd support
  87. find_package(PkgConfig)
  88. if(PKG_CONFIG_FOUND)
  89. pkg_check_modules(SYSTEMD QUIET libsystemd)
  90. endif()
  91. # Proto compilation
  92. add_subdirectory(proto)
  93. # Client library
  94. if(SMARTBOTIC_DB_BUILD_CLIENT)
  95. add_subdirectory(client)
  96. endif()
  97. # Service executable
  98. if(SMARTBOTIC_DB_BUILD_SERVICE)
  99. add_subdirectory(service)
  100. endif()
  101. # CLI admin tool (standalone build only)
  102. option(SMARTBOTIC_DB_BUILD_CLI "Build CLI admin tool" ${SMARTBOTIC_DB_STANDALONE})
  103. if(SMARTBOTIC_DB_BUILD_CLI)
  104. add_subdirectory(cli)
  105. endif()
  106. # Tests (standalone build only)
  107. option(SMARTBOTIC_DB_BUILD_TESTS "Build unit/integration tests" ${SMARTBOTIC_DB_STANDALONE})
  108. if(SMARTBOTIC_DB_BUILD_TESTS)
  109. add_subdirectory(tests)
  110. endif()
  111. # Print configuration summary
  112. if(SMARTBOTIC_DB_STANDALONE)
  113. message(STATUS "")
  114. message(STATUS "Smartbotic Database ${SMARTBOTIC_DB_VERSION}")
  115. message(STATUS " Build client library: ${SMARTBOTIC_DB_BUILD_CLIENT}")
  116. message(STATUS " Build service: ${SMARTBOTIC_DB_BUILD_SERVICE}")
  117. message(STATUS " Build tests: ${SMARTBOTIC_DB_BUILD_TESTS}")
  118. message(STATUS " Systemd support: ${SYSTEMD_FOUND}")
  119. message(STATUS "")
  120. endif()