if(DBEAVER_CREDS_BACKEND STREQUAL "native")
  if(APPLE)
    set(BACKEND_SOURCE macos.c)
  elseif(WIN32)
    set(BACKEND_SOURCE win32.c)
  else()
    message(FATAL_ERROR
      "DBEAVER_CREDS_BACKEND=native is only available on Apple or Windows targets.")
  endif()
elseif(DBEAVER_CREDS_BACKEND STREQUAL "openssl")
  set(BACKEND_SOURCE openssl.c)
else()
  message(FATAL_ERROR "Unknown DBEAVER_CREDS_BACKEND: '${DBEAVER_CREDS_BACKEND}'.")
endif()

set(CORE_SOURCES api.c ${BACKEND_SOURCE})
if(WIN32)
  list(APPEND CORE_SOURCES win_utf8.c)
endif()

add_library(dbeaver_creds_core STATIC ${CORE_SOURCES})
target_include_directories(dbeaver_creds_core PUBLIC ${CMAKE_SOURCE_DIR}/include)
set_property(TARGET dbeaver_creds_core PROPERTY POSITION_INDEPENDENT_CODE ON)

if(DBEAVER_CREDS_BACKEND STREQUAL "openssl")
  target_link_libraries(dbeaver_creds_core PUBLIC OpenSSL::Crypto)
endif()
if(WIN32)
  target_link_libraries(dbeaver_creds_core PUBLIC bcrypt)
endif()

if(NOT SKBUILD)
  install(TARGETS dbeaver_creds_core ARCHIVE DESTINATION lib64)
endif()

if(BUILD_CLI)
  add_executable(dbeaver-creds main.c)
  target_link_libraries(dbeaver-creds PRIVATE dbeaver_creds_core)
  if(WIN32)
    target_link_libraries(dbeaver-creds PRIVATE shell32)
    configure_file(version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
    target_sources(dbeaver-creds PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
  endif()
  install(TARGETS dbeaver-creds RUNTIME DESTINATION bin)
endif()
