cmake_minimum_required(VERSION 3.20)
project(v2_lmdb_spike CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_BUILD_TYPE Release)
add_compile_options(-O2 -Wall -Wextra -Wno-unused-parameter)

find_package(PkgConfig REQUIRED)
pkg_check_modules(yyjson REQUIRED yyjson)
pkg_check_modules(nlohmann_json IMPORTED_TARGET nlohmann_json)

find_path(LMDB_INCLUDE_DIR lmdb.h)
find_library(LMDB_LIBRARY NAMES lmdb)
if(NOT LMDB_INCLUDE_DIR OR NOT LMDB_LIBRARY)
    message(FATAL_ERROR "LMDB not found. Install liblmdb-dev.")
endif()

# The spike compiles the Phase A bridge + Phase B doc_binary directly so
# it stays standalone — no link against the production service binary.
add_library(spike_doc_helpers STATIC
    ${CMAKE_SOURCE_DIR}/../../src/json_parse.cpp
    ${CMAKE_SOURCE_DIR}/../../src/doc_binary.cpp
)
target_include_directories(spike_doc_helpers PUBLIC
    ${CMAKE_SOURCE_DIR}/../../src
    ${yyjson_INCLUDE_DIRS}
)
target_link_libraries(spike_doc_helpers PUBLIC ${yyjson_LIBRARIES})
if(nlohmann_json_FOUND)
    target_link_libraries(spike_doc_helpers PUBLIC PkgConfig::nlohmann_json)
endif()

add_library(spike_lmdb_store STATIC lmdb_store.cpp)
target_include_directories(spike_lmdb_store PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${LMDB_INCLUDE_DIR}
)
target_link_libraries(spike_lmdb_store PUBLIC
    spike_doc_helpers
    ${LMDB_LIBRARY}
)

add_executable(bench_lmdb_storage bench_lmdb_storage.cpp)
target_link_libraries(bench_lmdb_storage PRIVATE spike_lmdb_store)
