Use Find*.cmake files for backend discovery

This commit is contained in:
luisangelsm
2026-03-29 23:15:24 +02:00
parent 52dd4e4c3c
commit 268b23376b
6 changed files with 221 additions and 144 deletions

View File

@ -2,8 +2,6 @@
# Creates an INTERFACE target 'pdf_backend_iface' that propagates
# compile definitions and link libraries based on PDF_BACKEND.
include(BackendHelpers)
add_library(pdf_backend_iface INTERFACE)
if(PDF_BACKEND STREQUAL "no_pdf")
@ -14,65 +12,21 @@ elseif(PDF_BACKEND STREQUAL "pdfium")
message(STATUS "PDF backend: pdfium")
target_compile_definitions(pdf_backend_iface INTERFACE USE_PDFIUM)
if(WIN32)
yacreader_get_windows_arch_subdir(_pdfium_arch)
set(_pdfium_include_dir "${CMAKE_SOURCE_DIR}/dependencies/pdfium/win/public")
set(_pdfium_implib "${CMAKE_SOURCE_DIR}/dependencies/pdfium/win/${_pdfium_arch}/pdfium.lib")
set(_pdfium_dll "${CMAKE_SOURCE_DIR}/dependencies/pdfium/win/${_pdfium_arch}/pdfium.dll")
if(NOT EXISTS "${_pdfium_include_dir}/fpdfview.h"
OR NOT EXISTS "${_pdfium_implib}"
OR NOT EXISTS "${_pdfium_dll}")
message(FATAL_ERROR
"Could not find bundled pdfium for ${_pdfium_arch}. "
"Expected files under dependencies/pdfium/win/${_pdfium_arch}.")
endif()
message(STATUS " Using bundled pdfium (${_pdfium_arch}, Windows)")
yacreader_add_imported_library(YACReader::pdfium
TYPE SHARED
LOCATION "${_pdfium_dll}"
IMPORTED_IMPLIB "${_pdfium_implib}"
INCLUDE_DIR "${_pdfium_include_dir}")
target_link_libraries(pdf_backend_iface INTERFACE YACReader::pdfium)
elseif(APPLE)
set(_pdfium_include_dir "${CMAKE_SOURCE_DIR}/dependencies/pdfium/macx/include")
set(_pdfium_library "${CMAKE_SOURCE_DIR}/dependencies/pdfium/macx/bin/libpdfium.a")
if(NOT EXISTS "${_pdfium_include_dir}/fpdfview.h" OR NOT EXISTS "${_pdfium_library}")
message(FATAL_ERROR "Could not find bundled pdfium under dependencies/pdfium/macx.")
endif()
message(STATUS " Using bundled pdfium (macOS)")
yacreader_add_imported_library(YACReader::pdfium
TYPE STATIC
LOCATION "${_pdfium_library}"
INCLUDE_DIR "${_pdfium_include_dir}")
target_link_libraries(pdf_backend_iface INTERFACE YACReader::pdfium)
find_package(pdfium QUIET CONFIG)
if(TARGET pdfium::pdfium)
message(STATUS " Found pdfium via CMake config")
else()
# Linux: try pkg-config first, then a normal CMake search.
find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
pkg_check_modules(PDFIUM QUIET IMPORTED_TARGET libpdfium)
find_package(pdfium QUIET MODULE)
if(NOT TARGET pdfium::pdfium)
message(FATAL_ERROR "Could not find libpdfium. Install it or use a different PDF_BACKEND.")
endif()
if(TARGET PkgConfig::PDFIUM)
message(STATUS " Found pdfium via pkg-config")
target_link_libraries(pdf_backend_iface INTERFACE PkgConfig::PDFIUM)
else()
find_path(PDFIUM_INCLUDE_DIR NAMES fpdfview.h PATH_SUFFIXES pdfium)
find_library(PDFIUM_LIBRARY NAMES pdfium libpdfium)
if(PDFIUM_INCLUDE_DIR AND PDFIUM_LIBRARY)
message(STATUS " Found pdfium via CMake search")
yacreader_add_imported_library(YACReader::pdfium
TYPE UNKNOWN
LOCATION "${PDFIUM_LIBRARY}"
INCLUDE_DIR "${PDFIUM_INCLUDE_DIR}")
target_link_libraries(pdf_backend_iface INTERFACE YACReader::pdfium)
else()
message(FATAL_ERROR "Could not find libpdfium. Install it or use a different PDF_BACKEND.")
endif()
if(pdfium_PROVIDER)
message(STATUS " Found pdfium via ${pdfium_PROVIDER}")
endif()
endif()
target_link_libraries(pdf_backend_iface INTERFACE pdfium::pdfium)
elseif(PDF_BACKEND STREQUAL "poppler")
message(STATUS "PDF backend: poppler")
target_compile_definitions(pdf_backend_iface INTERFACE USE_POPPLER)
@ -85,38 +39,24 @@ elseif(PDF_BACKEND STREQUAL "poppler")
# On Windows: vcpkg install poppler[qt6]:x64-windows, then pass the vcpkg toolchain file
find_package(Poppler QUIET CONFIG)
if(TARGET Poppler::Qt6)
message(STATUS " Found poppler-qt6 via cmake config")
target_link_libraries(pdf_backend_iface INTERFACE Poppler::Qt6)
message(STATUS " Found poppler-qt6 via CMake config")
else()
# Fall back to pkg-config (standard on Linux; on Windows requires vcpkg pkgconf)
find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
pkg_check_modules(POPPLER QUIET IMPORTED_TARGET poppler-qt6)
endif()
if(TARGET PkgConfig::POPPLER)
message(STATUS " Found poppler-qt6 via pkg-config")
target_link_libraries(pdf_backend_iface INTERFACE PkgConfig::POPPLER)
find_package(PopplerQt6 QUIET MODULE)
if(TARGET Poppler::Qt6)
if(PopplerQt6_PROVIDER)
message(STATUS " Found poppler-qt6 via ${PopplerQt6_PROVIDER}")
endif()
elseif(MSVC)
message(FATAL_ERROR "Could not find poppler-qt6. "
"Install via vcpkg: vcpkg install poppler[qt6]:x64-windows "
"then configure cmake with the vcpkg toolchain file.")
else()
find_path(POPPLER_QT6_INCLUDE_DIR NAMES poppler-qt6.h PATH_SUFFIXES poppler/qt6)
find_library(POPPLER_QT6_LIBRARY NAMES poppler-qt6)
if(POPPLER_QT6_INCLUDE_DIR AND POPPLER_QT6_LIBRARY)
message(STATUS " Found poppler-qt6 via CMake search")
yacreader_add_imported_library(YACReader::poppler_qt6
TYPE UNKNOWN
LOCATION "${POPPLER_QT6_LIBRARY}"
INCLUDE_DIR "${POPPLER_QT6_INCLUDE_DIR}")
target_link_libraries(pdf_backend_iface INTERFACE YACReader::poppler_qt6)
else()
message(FATAL_ERROR "Could not find poppler-qt6. Install libpoppler-qt6-dev or use a different PDF_BACKEND.")
endif()
message(FATAL_ERROR "Could not find poppler-qt6. Install libpoppler-qt6-dev or use a different PDF_BACKEND.")
endif()
endif()
target_link_libraries(pdf_backend_iface INTERFACE Poppler::Qt6)
elseif(PDF_BACKEND STREQUAL "pdfkit")
message(STATUS "PDF backend: pdfkit (macOS)")
if(NOT APPLE)