# -*- 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           cmake 1.1
PortGroup           gobject_introspection 1.0
PortGroup           legacysupport 1.1

name                poppler-devel
conflicts           poppler xpdf-tools
set my_name         poppler
version             26.11.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} \
                    gentoo:4e \
                    https://fossies.org/linux/misc/
distname            ${my_name}-${version}
dist_subdir         ${my_name}
use_xz              yes

set testdata_repo   https://gitlab.freedesktop.org/poppler/test.git

checksums           rmd160  7d084325f0e489475d0c6b2364e1e4b40ba2771b \
                    sha256  41c32680160addf5a8dd7a830e4869c01e9f6bee1abb91e44fabd1ba9d7828fb \
                    size    2059960

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

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

depends_lib-append \
                    port:brotli \
                    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:gpgmepp \
                    path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    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

configure.python    ${prefix}/bin/python${py_ver}

gobject_introspection yes

# Upstream migrated from 2020 to 2023, as of version 25.x
compiler.cxx_standard 2023
compiler.c_standard   2011
compiler.thread_local_storage yes

legacysupport.newest_darwin_requires_legacy 18
# legacysupport.use_mp_libcxx yes

if {${os.platform} eq "darwin" && ${os.major} < 19} {
    if {${configure.cxx_stdlib} eq "libc++"} {
        # use C++ header files included in the specific version of clang instead of macports-libcxx
        # in order to avoid the error "no member named 'ranges' in namespace 'std'"
        set llvmversion     17
        configure.compiler  macports-clang-${llvmversion}
        depends_lib-append  port:macports-libcxx

        configure.cflags-append \
            -isystem${workpath}/include/c++/v1 \
            -nostdinc++ \
            -isystem${prefix}/include/LegacySupport
        configure.cxxflags-append \
            -isystem${workpath}/include/c++/v1 \
            -nostdinc++ \
            -isystem${prefix}/include/LegacySupport \
            -stdlib=libc++
        configure.ldflags-append \
            -L${prefix}/lib/libcxx

        post-extract {
            system -W ${workpath} "mkdir -p ./include/c++"
            copy  ${prefix}/libexec/llvm-${llvmversion}/include/c++/v1 ${workpath}/include/c++/
            # disable Apple libc++ availability tests, as we're using a new libc++ with these headers
            system -W ${workpath}/include/c++/v1 "patch -p0 < ${filespath}/patch-disable-availability.diff"
        }
    }
}

# 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 find_package for boost
patchfiles-append   patch-CMakeLists.txt-fix-boost.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 \
                    -DBUILD_SHARED_LIBS=ON \
                    -DENABLE_BOOST=OFF \
                    -DENABLE_BROTLI=ON \
                    -DENABLE_CPP=ON \
                    -DENABLE_GLIB=ON \
                    -DENABLE_GPGME=ON \
                    -DENABLE_GTK_DOC=OFF \
                    -DENABLE_HARFBUZZ=ON \
                    -DENABLE_LCMS=ON \
                    -DENABLE_LIBCURL=ON \
                    -DENABLE_LIBJPEG=ON \
                    -DENABLE_LIBOPENJPEG=ON \
                    -DENABLE_LIBTIFF=ON \
                    -DENABLE_NSS3=ON \
                    -DENABLE_QT5=OFF \
                    -DENABLE_QT6=OFF \
                    -DENABLE_UNSTABLE_API_ABI_HEADERS=ON \
                    -DENABLE_UTILS=ON \
                    -DGLIB2_MKENUMS_PYTHON=${configure.python} \
                    -DINSTALL_GLIB_DEMO=OFF \
                    -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.88
    configure.args-replace \
                    -DENABLE_BOOST=OFF \
                    -DENABLE_BOOST=ON
}
default_variants-append +boost

variant tests description {Enable tests} {
    # Note: other items enabled later, as appropriate for subport
    configure.args-replace \
                    -DBUILD_MANUAL_TESTS=OFF \
                    -DBUILD_MANUAL_TESTS=ON

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

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

    depends_extract-append \
                    port:git

    depends_lib-append \
                    port:${name}

    post-extract {
        system -W ${workpath} \
                    "${prefix}/bin/git clone --depth=1 ${testdata_repo}"
    }

    configure.env-append \
                    MOCQT5=${qt_bins_dir}/moc

    configure.args-replace \
                    -DENABLE_CPP=ON \
                    -DENABLE_CPP=OFF
    configure.args-replace \
                    -DENABLE_GLIB=ON \
                    -DENABLE_GLIB=OFF
    configure.args-replace \
                    -DENABLE_QT5=OFF \
                    -DENABLE_QT5=ON
    configure.args-replace \
                    -DENABLE_UNSTABLE_API_ABI_HEADERS=ON \
                    -DENABLE_UNSTABLE_API_ABI_HEADERS=OFF
    configure.args-replace \
                    -DENABLE_UTILS=ON \
                    -DENABLE_UTILS=OFF

    post-destroot {
        set destroot_headers_to_delete \
            [glob -nocomplain -directory ${destroot}${prefix}/include/poppler *.h]
        foreach f ${destroot_headers_to_delete} {
            ui_info "${subport}: deleting header: ${f}"
            delete ${f}
        }

        set destroot_libs_to_delete \
            [glob -nocomplain ${destroot}${prefix}/lib/libpoppler.*]
        foreach f ${destroot_libs_to_delete} {
            ui_info "${subport}: deleting lib: ${f}"
            delete ${f}
        }

        set destroot_files_to_delete [list \
            ${destroot}${prefix}/include/poppler/fofi \
            ${destroot}${prefix}/include/poppler/goo \
            ${destroot}${prefix}/include/poppler/splash \
            ${destroot}${prefix}/lib/pkgconfig/poppler.pc \
            ${destroot}${prefix}/lib/pkgconfig/poppler-cairo.pc \
            ${destroot}${prefix}/lib/pkgconfig/poppler-splash.pc \
        ]
        foreach f ${destroot_files_to_delete} {
            ui_info "${subport}: deleting file: ${f}"
            delete ${f}
        }
    }

    if {[variant_isset tests]} {
        configure.args-replace \
                    -DBUILD_QT5_TESTS=OFF \
                    -DBUILD_QT5_TESTS=ON
    }
}

if {${subport} eq ${name}} {
    if {[variant_isset tests]} {
        configure.args-replace \
                    -DBUILD_CPP_TESTS=OFF \
                    -DBUILD_CPP_TESTS=ON
    }
}

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