cmake_minimum_required(VERSION 3.24)
project(pdf_reader LANGUAGES CXX)
# Plugin-owned PDF engine: pdf-reader loads it through vtour.native
# (library.lua). PDFium (shared, beside the vtour executable for now), the
# files module (atomic capture writes) and stb_image_write (PNG captures).
# No vtour, Lua, ImGui or GL dependency.
find_package(Threads REQUIRED)
set(PDF_READER_SOURCES pdf.cpp document.cpp)
function(pdf_reader_configure target)
  target_compile_features(${target} PUBLIC cxx_std_17)
  target_include_directories(${target} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
  target_include_directories(${target} SYSTEM PRIVATE "${CMAKE_SOURCE_DIR}/vendor")  # third/stb_image_write.h
  target_link_libraries(${target} PUBLIC vtour_pdfium nlohmann_json::nlohmann_json)
  target_link_libraries(${target} PRIVATE vtour_files Threads::Threads)
  if(WIN32)
    target_compile_definitions(${target} PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN)
  endif()
  add_dependencies(${target} vtour_pdfium_runtime)
endfunction()
add_library(vtour_pdf_reader SHARED ${PDF_READER_SOURCES})
pdf_reader_configure(vtour_pdf_reader)
set_target_properties(vtour_pdf_reader PROPERTIES PREFIX "" OUTPUT_NAME "pdf-reader"
  CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN YES)
# PDFium sits beside the vtour executable (cmake/Pdfium.cmake).
if(APPLE)
  set(_pdf_rpath "@executable_path;@loader_path/../..")
elseif(UNIX)
  set(_pdf_rpath "$ORIGIN/../..")  # bin/plugins/pdf-reader -> bin
endif()
if(DEFINED _pdf_rpath)
  set_target_properties(vtour_pdf_reader PROPERTIES BUILD_RPATH "${_pdf_rpath}" INSTALL_RPATH "${_pdf_rpath}")
endif()
# Tests use pdf::Document and the same C ABI statically.
add_library(vtour_pdf_reader_static STATIC ${PDF_READER_SOURCES})
pdf_reader_configure(vtour_pdf_reader_static)
target_compile_definitions(vtour_pdf_reader_static PUBLIC PDF_STATIC)
