cmake_minimum_required(VERSION 3.24)
project(database_query LANGUAGES C CXX)
# Plugin-owned query library: database-browser loads it through vtour.native
# (library.lua). No vtour, Lua or ImGui dependency. Inside the vtour build the
# SQLite and nlohmann_json targets already exist; standalone builds use the
# system packages. ODBC is optional (system ODBC development package).
find_package(Threads REQUIRED)
if(NOT TARGET vtour_sqlite)
  find_package(SQLite3 REQUIRED)
  add_library(vtour_sqlite INTERFACE)
  target_link_libraries(vtour_sqlite INTERFACE SQLite::SQLite3)
endif()
if(NOT TARGET nlohmann_json::nlohmann_json)
  find_package(nlohmann_json 3 REQUIRED)
endif()
if(NOT DEFINED ODBC_FOUND)
  find_package(ODBC QUIET)
endif()
function(database_query_configure target)
  target_compile_features(${target} PUBLIC cxx_std_17)
  target_include_directories(${target} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
  target_link_libraries(${target} PRIVATE vtour_sqlite nlohmann_json::nlohmann_json Threads::Threads ${CMAKE_DL_LIBS})
  if(ODBC_FOUND)
    target_link_libraries(${target} PRIVATE ODBC::ODBC)
    target_compile_definitions(${target} PRIVATE VTOUR_HAS_ODBC=1)
  endif()
endfunction()
add_library(vtour_database_query SHARED query.cpp)
database_query_configure(vtour_database_query)
set_target_properties(vtour_database_query PROPERTIES PREFIX "" OUTPUT_NAME "database-query"
  CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN YES)
# Tests call the same C ABI statically.
add_library(vtour_database_query_static STATIC query.cpp)
database_query_configure(vtour_database_query_static)
target_compile_definitions(vtour_database_query_static PUBLIC DBQ_STATIC)
