# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           gobject_introspection 1.0
PortGroup           cmake 1.1
PortGroup           legacysupport 1.1

name                poppler-devel
conflicts           poppler xpdf-tools
set my_name         poppler
version             24.12.0
revision            0

categories          graphics
license             GPL-2+
maintainers         {devans @dbevans} {mascguy @mascguy} openmaintainer

description         Poppler is a PDF rendering library based on the xpdf-3.0 code base.
long_description    {*}${description}
homepage            https://poppler.freedesktop.org/

master_sites        ${homepage}
distname            ${my_name}-${version}
dist_subdir         ${my_name}
use_xz              yes

checksums           rmd160  e98ce406517b884e31b2ad3e5d1edb8efa8b85b4 \
                    sha256  1cf374c3146f3f685d9257701bf0c2866c61d6c202c14d1f5c01a1f3a089028a \
                    size    1934024

set py_ver          3.12
set py_ver_nodot    [string map {. {}} ${py_ver}]

depends_build-append \
                    port:gettext \
                    port:pkgconfig \
                    port:python${py_ver_nodot}

depends_lib-append \
                    port:bzip2 \
                    path:lib/pkgconfig/cairo.pc:cairo \
                    port:curl \
                    port:expat \
                    port:fontconfig \
                    port:freetype \
                    port:gettext-runtime \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:gpgme \
                    port:lcms2 \
                    port:libiconv \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libpng \
                    port:nss \
                    port:openjpeg \
                    port:poppler-data \
                    port:tiff \
                    port:zlib

# remove top-level paths, such that internal headers and
# libraries are used instead of any already-installed ones.
configure.ldflags-delete \
                    -L${prefix}/lib
configure.cppflags-delete \
                    -I${prefix}/include

configure.ldflags-append \
                    -liconv
gobject_introspection yes

compiler.cxx_standard 2020
compiler.c_standard   2011
compiler.thread_local_storage yes

legacysupport.newest_darwin_requires_legacy 18
legacysupport.use_mp_libcxx yes

# match clang, not strequal
patchfiles-append   patch-cmake_modules_PopplerMacros.cmake.diff

# fix header include path ordering for gobject-introspection call to
# be source / build first, then system, as best can be determined
patchfiles-append   patch-glib_CMakeFiles.txt-fix-include-ordering.diff

# fix library paths in generated typelib file
patchfiles-append   patch-glib_CMakeFiles.txt-fix-lib-location.diff

# fix boost find_package to use module mode, rather than config
patchfiles-append   patch-cmake-boost-find.diff

if {${configure.compiler} eq "macports-gcc-7"} {
    patchfiles-append \
                    patch-poppler-gcc7-noexcept.diff
}

configure.args-append \
                    -DBUILD_CPP_TESTS=OFF \
                    -DBUILD_GTK_TESTS=OFF \
                    -DBUILD_MANUAL_TESTS=OFF \
                    -DBUILD_QT5_TESTS=OFF \
                    -DBUILD_QT6_TESTS=OFF \
                    -DENABLE_BOOST=OFF \
                    -DENABLE_QT5=OFF \
                    -DENABLE_QT6=OFF \
                    -DENABLE_UNSTABLE_API_ABI_HEADERS=ON \
                    -DGLIB2_MKENUMS_PYTHON=${prefix}/bin/python${py_ver} \
                    -DRUN_GPERF_IF_PRESENT=OFF \
                    -DWITH_NSS3=ON

# Poppler cmake doesn't understand build type "MacPorts"; use release unless +debug is set
if {[variant_isset debug]} {
    cmake.build_type Debug
} else {
    cmake.build_type RelWithDebInfo
}

variant boost description "Use Boost when building the Splash graphics backend" {
    PortGroup boost 1.0

    boost.version 1.76
    configure.args-replace \
                    -DENABLE_BOOST=OFF \
                    -DENABLE_BOOST=ON
}
default_variants-append +boost

subport ${name}-qt5 {
    PortGroup qt5 1.0

    # Builds with Qt 5.8 (requires toSecsSinceEpoch and fromSecsSinceEpoch)
    # despite CMakeLists.txt in poppler 23.01 saying 5.12 is required
    qt5.min_version 5.8

    configure.env-append \
                    MOCQT5=${qt_bins_dir}/moc
    configure.args-delete \
                    -DENABLE_QT5=OFF \
                    -DENABLE_XPDF_HEADERS=ON
    configure.args-append \
                    -DENABLE_CPP=OFF \
                    -DENABLE_GLIB=OFF \
                    -DENABLE_UTILS=OFF

    post-destroot {
        file delete ${destroot}${prefix}/lib/pkgconfig/poppler-cairo.pc
    }
}

if {${subport} ne ${name}} {
    depends_extract-append \
                    port:git

    depends_lib-append \
                    port:${name}

    post-extract {
        system -W ${workpath} \
                    "${prefix}/bin/git clone --depth=1 https://anongit.freedesktop.org/git/poppler/test"
    }

# currently poppler only provides unit tests for the Qt wrappers

    test.run    yes
    test.dir    test
    test.target test

    post-destroot {
        foreach dot_h [glob -nocomplain -directory ${destroot}${prefix}/include/poppler *.h] {
            delete ${dot_h}
        }
        foreach libpoppler [glob -nocomplain ${destroot}${prefix}/lib/libpoppler.*] {
            delete ${libpoppler}
        }
        delete  ${destroot}${prefix}/include/poppler/fofi \
                ${destroot}${prefix}/include/poppler/goo \
                ${destroot}${prefix}/include/poppler/splash \
                ${destroot}${prefix}/lib/pkgconfig/poppler-splash.pc \
                ${destroot}${prefix}/lib/pkgconfig/poppler.pc
    }
}

if {${subport} eq ${name}} {
    livecheck.type      regex
    livecheck.url       ${homepage}
    livecheck.regex     "${my_name}-(\\d+(?:\\.\\d+)*)${extract.suffix}"
} else {
    livecheck.type      none
}