Improve backends handling and make DECOMPRESSION_BACKEND and PDF_BACKEND settings easier to discover

This commit is contained in:
luisangelsm
2026-03-29 22:55:16 +02:00
parent 27c94bcb53
commit 52dd4e4c3c
8 changed files with 250 additions and 105 deletions

View File

@ -2,6 +2,8 @@
# 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")
@ -12,35 +14,62 @@ elseif(PDF_BACKEND STREQUAL "pdfium")
message(STATUS "PDF backend: pdfium")
target_compile_definitions(pdf_backend_iface INTERFACE USE_PDFIUM)
if(MSVC)
target_include_directories(pdf_backend_iface INTERFACE
"${CMAKE_SOURCE_DIR}/dependencies/pdfium/win/public")
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64" OR CMAKE_CXX_COMPILER_ARCHITECTURE_ID STREQUAL "ARM64")
target_link_directories(pdf_backend_iface INTERFACE
"${CMAKE_SOURCE_DIR}/dependencies/pdfium/win/arm64")
else()
target_link_directories(pdf_backend_iface INTERFACE
"${CMAKE_SOURCE_DIR}/dependencies/pdfium/win/x64")
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()
target_link_libraries(pdf_backend_iface INTERFACE pdfium)
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)
target_include_directories(pdf_backend_iface INTERFACE
"${CMAKE_SOURCE_DIR}/dependencies/pdfium/macx/include")
target_link_directories(pdf_backend_iface INTERFACE
"${CMAKE_SOURCE_DIR}/dependencies/pdfium/macx/bin")
target_link_libraries(pdf_backend_iface INTERFACE pdfium)
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)
else()
# Linux: try pkg-config first, then system path
pkg_check_modules(PDFIUM QUIET IMPORTED_TARGET libpdfium)
if(PDFIUM_FOUND)
# 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)
endif()
if(TARGET PkgConfig::PDFIUM)
message(STATUS " Found pdfium via pkg-config")
target_link_libraries(pdf_backend_iface INTERFACE PkgConfig::PDFIUM)
elseif(EXISTS "/usr/include/pdfium")
message(STATUS " Found pdfium at /usr/include/pdfium")
target_include_directories(pdf_backend_iface INTERFACE /usr/include/pdfium)
target_link_libraries(pdf_backend_iface INTERFACE pdfium)
else()
message(FATAL_ERROR "Could not find libpdfium. Install it or use a different PDF_BACKEND.")
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()
endif()
endif()
@ -60,20 +89,31 @@ elseif(PDF_BACKEND STREQUAL "poppler")
target_link_libraries(pdf_backend_iface INTERFACE Poppler::Qt6)
else()
# Fall back to pkg-config (standard on Linux; on Windows requires vcpkg pkgconf)
pkg_check_modules(POPPLER QUIET IMPORTED_TARGET poppler-qt6)
if(POPPLER_FOUND)
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)
elseif(NOT MSVC AND EXISTS "/usr/include/poppler/qt6")
message(STATUS " Found poppler-qt6 at /usr/include/poppler/qt6")
target_include_directories(pdf_backend_iface INTERFACE /usr/include/poppler/qt6)
target_link_libraries(pdf_backend_iface INTERFACE poppler-qt6)
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()
message(FATAL_ERROR "Could not find poppler-qt6. Install libpoppler-qt6-dev or use a different PDF_BACKEND.")
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()
endif()
endif()