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

PortSystem          1.0

name                pdfium
version             20261030
revision            0
categories          devel graphics
platforms           {darwin >= 22}
supported_archs     arm64 x86_64
maintainers         {i0ntempest @i0ntempest} openmaintainer
license             BSD Apache-2 MIT

description         PDF rendering and manipulation library
long_description    PDFium is the PDF rendering engine used by Chromium. \
                    This port provides a shared library and public C headers \
                    for rendering, reading and manipulating PDF documents.
homepage            https://pdfium.googlesource.com/pdfium/

# Revisions from PDFium's DEPS file: tag, repository, commit, destination, format.
set source_archives {
    {pdfium https://pdfium.googlesource.com/pdfium 55ed4ec4229365f466d9d634956bb088b1c3a399 . gitiles}
    {build https://chromium.googlesource.com/chromium/src/build 0c62137b8623a3fab2c6aa5d7c94f719b3aa31f6 build gitiles}
    {buildtools https://chromium.googlesource.com/chromium/src/buildtools d0d4c192e3c718016b3a0c5d44ccec632bccfe96 buildtools gitiles}
    {base_allocator_partition_allocator https://chromium.googlesource.com/chromium/src/base/allocator/partition_allocator 2b864a17731ee7ae5a994ea308178da1e444ba29 base/allocator/partition_allocator gitiles}
    {tools_clang https://chromium.googlesource.com/chromium/src/tools/clang 7ea1bdeeaf498a513c77b66667e56f6f41f9f0b3 tools/clang gitiles}
    {third_party_abseil-cpp https://chromium.googlesource.com/chromium/src/third_party/abseil-cpp 56722f9eb6f40fb1a140caffd36e8f1f61b57aaf third_party/abseil-cpp gitiles}
    {third_party_fast_float_src https://github.com/fastfloat/fast_float b0ab987b3dfdde13fa1915f65ef2a5c068d9208c third_party/fast_float/src github}
    {third_party_dragonbox_src https://github.com/jk-jeon/dragonbox beeeef91cf6fef89a4d4ba5e95d47ca64ccb3a44 third_party/dragonbox/src github}
    {third_party_highway_src https://github.com/google/highway 2607d3b5b0113992fe84d3848859eae13b3b52c1 third_party/highway/src github}
    {third_party_googletest_src https://github.com/google/googletest 4fe3307fb2d9f86d19777c7eb0e4809e9694dde7 third_party/googletest/src github}
    {third_party_test_fonts https://chromium.googlesource.com/chromium/src/third_party/test_fonts 7f51783942943e965cd56facf786544ccfc07713 third_party/test_fonts gitiles}
    {third_party_depot_tools https://chromium.googlesource.com/chromium/tools/depot_tools a03dfbf7f2d1cefa2795d81d59ca4f16c8616a8f third_party/depot_tools gitiles}
    {tools_code_coverage https://chromium.googlesource.com/chromium/src/tools/code_coverage 084f00efb99dc9a1726ed68cc2e05d25bc52802b tools/code_coverage gitiles}
    {tools_memory https://chromium.googlesource.com/chromium/src/tools/memory 3d4610fc0dd49053c0395bc2825dfc6ac72ab64e tools/memory gitiles}
    {third_party_simdutf https://chromium.googlesource.com/chromium/src/third_party/simdutf f7356eed293f8208c40b3c1b344a50bd70971983 third_party/simdutf gitiles}
}

master_sites
distfiles
foreach source ${source_archives} {
    lassign ${source} tag repository commit directory format
    if {${format} eq "gitiles"} {
        master_sites-append ${repository}/+archive/:${tag}
    } else {
        master_sites-append ${repository}/archive/:${tag}
    }
    distfiles-append ${commit}.tar.gz:${tag}
}
worksrcdir          ${name}-${version}

# Gitiles checksums below apply after metadata normalization in post-fetch.
checksums           55ed4ec4229365f466d9d634956bb088b1c3a399.tar.gz \
                    rmd160  619476ec64ca8ce31065cedf7139ffef6d3b9646 \
                    sha256  1c2f8711d782b2e5f27514eb3e8d649591055067695739df2e7b5aa09e7b053a \
                    size    10588600 \
                    0c62137b8623a3fab2c6aa5d7c94f719b3aa31f6.tar.gz \
                    rmd160  093fc33d60b13c7f3e14850c3c0c1f5f130f23d7 \
                    sha256  d2a6fdc820929047ab9017623b062d52cda5b0ed6bb4b7c1bf1e0c533348685a \
                    size    1948902 \
                    d0d4c192e3c718016b3a0c5d44ccec632bccfe96.tar.gz \
                    rmd160  b3ac187bfe3e212730638d50e06f9339ca47e21a \
                    sha256  ef9770f292a409e8abade0fe9b3f0bd0b5434e133b53f07f91cbe99fdc22821a \
                    size    60572 \
                    2b864a17731ee7ae5a994ea308178da1e444ba29.tar.gz \
                    rmd160  de5e1af5f9840afeeab0de1ba320e13a69684c17 \
                    sha256  642e493def27f1cc152e9a957ca1d608aac3e7b92e47f5d24249123c9c5cbeda \
                    size    903239 \
                    7ea1bdeeaf498a513c77b66667e56f6f41f9f0b3.tar.gz \
                    rmd160  7540fcdaffbfe65bb2dbb7e086c3731712da5b45 \
                    sha256  1e37c65e7cdfee9c91e0e413f0bc47b0573090f811a886d7709598bfad3cbeed \
                    size    687498 \
                    56722f9eb6f40fb1a140caffd36e8f1f61b57aaf.tar.gz \
                    rmd160  31955b311b9f5361b67292fc13571631cd06e325 \
                    sha256  74b62830124980b7d74f564fd1f65763cd7e4361a1db5fbbd081d7e76dec4b66 \
                    size    3210060 \
                    b0ab987b3dfdde13fa1915f65ef2a5c068d9208c.tar.gz \
                    rmd160  eb7bb39e859bacc089f462463d53090aa19005c6 \
                    sha256  3abf993dc066593bdef1b046da615338c70ff563e3640fda8cf1c22ba185d364 \
                    size    143435 \
                    beeeef91cf6fef89a4d4ba5e95d47ca64ccb3a44.tar.gz \
                    rmd160  aadade931bc643ed346a94933b495ff199684c86 \
                    sha256  2646de322a13cdabeeff52863665a492adbd68242c66b5279a5cfefbaa301e39 \
                    size    4227394 \
                    2607d3b5b0113992fe84d3848859eae13b3b52c1.tar.gz \
                    rmd160  76ecb456574fb4d1623716e35abf3da28b1f42f3 \
                    sha256  741d705781e0b3e406beda8f1f994fbae01321237ce8023a1ad90fbaf7940c25 \
                    size    3692133 \
                    4fe3307fb2d9f86d19777c7eb0e4809e9694dde7.tar.gz \
                    rmd160  63ddecbf95b3b998f066ec1ed296cf05b06136ef \
                    sha256  f6a5fa03ae05d5cfd6f928051fd62eaa05e59b4efd49605395dcb2bd8cd4c67d \
                    size    889215 \
                    7f51783942943e965cd56facf786544ccfc07713.tar.gz \
                    rmd160  64bf13f3afa3e47581c544019f54be67dd8bf15d \
                    sha256  5443f06e0bda819ffce2dd45fd9c75815675e46a328cfcc48f53f42fefe3f1ad \
                    size    20309 \
                    a03dfbf7f2d1cefa2795d81d59ca4f16c8616a8f.tar.gz \
                    rmd160  880d0605d9fff25e430d971c2d039f7c221a4871 \
                    sha256  1fed2ef159d86be70731d8d6682dd89a8d99fc9be983183fd7ee0cf0bdfc0ec5 \
                    size    1695468 \
                    084f00efb99dc9a1726ed68cc2e05d25bc52802b.tar.gz \
                    rmd160  b530feabc9cf9fa86d4ed8c5af1a39768feb3af7 \
                    sha256  f021ec03e1423832a0400569317536f5b79da39e40ad32d9a16a663acff7d221 \
                    size    114977 \
                    3d4610fc0dd49053c0395bc2825dfc6ac72ab64e.tar.gz \
                    rmd160  6e2fc68bc252dbe45eb8396b1c321babbafe4158 \
                    sha256  f530a3146d10a2460e79881579b2b5457cff8b2c82a1bc26da1d50d7d7b808b0 \
                    size    63509 \
                    f7356eed293f8208c40b3c1b344a50bd70971983.tar.gz \
                    rmd160  0c7642ec497c54428566aa127d405b63a976ba05 \
                    sha256  727c1ed48d99e0171821ff2d0f5a6a8f42a423c80d3898fb88ad947fbc4d5ea3 \
                    size    425206

set python_branch  3.14
set python_version [string map {. {}} ${python_branch}]
configure.python    ${prefix}/bin/python${python_branch}

depends_fetch-append \
                    port:gnutar \
                    port:gzip
depends_build-append \
                    port:gn \
                    port:ninja \
                    port:pkgconfig \
                    port:python${python_version}
depends_lib-append  port:freetype \
                    port:harfbuzz \
                    port:icu \
                    port:lcms2 \
                    path:lib/libjpeg.dylib:libjpeg-turbo \
                    port:libpng \
                    port:openjpeg \
                    port:zlib

compiler.cxx_standard \
                    2020
# The Chromium build snapshot is validated with Clang 21 and newer.
# Its GN toolchain does not support GCC on macOS.
compiler.blacklist-append \
                    *gcc* {clang < 2100} \
                    {macports-clang-[3-9].*} {macports-clang-1[0-9]} macports-clang-20
use_xcode           yes
universal_variant   no

# Gitiles puts download-time timestamps in its tar headers. Canonicalize
# archive metadata before the checksum phase, retaining file contents and
# executable bits. Running this again on a cached archive is idempotent.
post-fetch {
    foreach source ${source_archives} {
        lassign ${source} tag repository commit directory format
        if {${format} ne "gitiles"} {
            continue
        }
        set archive ${distpath}/${commit}.tar.gz
        set temp [mkdtemp ${workpath}/normalize.XXXXXX]
        try {
            system -W ${temp} "${prefix}/bin/gnutar -xf [shellescape ${archive}]"
            system -W ${temp} "${prefix}/bin/gnutar --sort=name --format=gnu --mtime=@0 --owner=0 --group=0 --numeric-owner --mode='u+rwX,go+rX,go-w' -cf - . | ${prefix}/bin/gzip -n > [shellescape ${archive}.normalized]"
            file rename -force ${archive}.normalized ${archive}
        } finally {
            delete ${temp}
            delete ${archive}.normalized
        }
    }
}

extract {
    foreach source ${source_archives} {
        lassign ${source} tag repository commit directory format
        set target ${worksrcpath}/${directory}
        xinstall -d ${target}
        set strip {}
        if {${format} eq "github"} {
            set strip --strip-components=1
        }
        system -W ${target} "${prefix}/bin/gnutar -xf [shellescape ${distpath}/${commit}.tar.gz] ${strip}"
    }
}

patchfiles          patch-macports.diff
patch.pre_args      -p1

post-patch {
    reinplace "s|//third_party/cpython3/host/bin/python3|${configure.python}|" \
        ${worksrcpath}/.gn
    # Reuse Chromium's system-ICU target and header forwarding generator.
    xinstall -d ${worksrcpath}/third_party/icu
    copy -force ${worksrcpath}/build/linux/unbundle/icu.gn \
        ${worksrcpath}/third_party/icu/BUILD.gn
    xinstall -d ${worksrcpath}/tools/generate_shim_headers
    copy ${filespath}/generate_shim_headers.py \
        ${worksrcpath}/tools/generate_shim_headers/generate_shim_headers.py
    reinplace "1s|^#!.*|#!${configure.python}|" \
        ${worksrcpath}/tools/generate_shim_headers/generate_shim_headers.py
    set fd [open ${worksrcpath}/build/config/gclient_args.gni w]
    puts ${fd} {build_with_chromium = false
checkout_android = false
checkout_libpng = false
checkout_skia = false
checkout_google_benchmark = false
checkout_src_internal = false}
    close ${fd}
}

configure.cmd       ${prefix}/bin/gn
configure.dir       ${worksrcpath}
configure.pre_args
configure.args      gen out/Release --fail-on-unused-args

pre-configure {
    set cpu ${configure.build_arch}
    if {${cpu} eq "x86_64"} {
        set cpu x64
    }
    # Query the chosen compiler, including Xcode's real toolchain when
    # configure.cc is the /usr/bin/clang driver.
    set clang_resource_dir [file normalize [string trim [exec ${configure.cc} -print-resource-dir]]]
    set clang_base_path [file dirname [file dirname [file dirname ${clang_resource_dir}]]]
    set clang_version [file tail ${clang_resource_dir}]
    xinstall -d ${worksrcpath}/out/Release
    set fd [open ${worksrcpath}/out/Release/args.gn w]
    puts ${fd} "is_debug = false
is_component_build = false
pdf_is_standalone = false
pdf_enable_v8 = false
pdf_enable_xfa = false
pdf_use_skia = false
pdf_use_libyuv = false
pdf_use_partition_alloc = false
pdf_enable_brotli = false
pdf_bundle_freetype = false
use_system_freetype = true
use_system_harfbuzz = true
use_system_libjpeg = true
use_system_libpng = true
use_system_zlib = true
use_system_lcms2 = true
use_system_libopenjpeg2 = true
use_custom_libcxx = false
use_custom_libcxx_for_host = false
use_siso = false
use_remoteexec = false
use_sysroot = false
clang_use_chrome_plugins = false
treat_warnings_as_errors = false
clang_base_path = \"${clang_base_path}\"
clang_version = \"${clang_version}\"
is_clang = true
use_lld = false
symbol_level = 0
target_cpu = \"${cpu}\"
mac_deployment_target = \"${macosx_deployment_target}\"
mac_min_system_version = \"${macosx_deployment_target}\"
use_system_xcode = true
pkg_config = \"${prefix}/bin/pkg-config\""
    close ${fd}
}

build.cmd           ${prefix}/bin/ninja
build.dir           ${worksrcpath}
build.pre_args      -C out/Release
build.target        pdfium

destroot {
    xinstall -m 755 ${worksrcpath}/out/Release/libpdfium.dylib ${destroot}${prefix}/lib
    system "install_name_tool -id ${prefix}/lib/libpdfium.dylib [shellescape ${destroot}${prefix}/lib/libpdfium.dylib]"
    system "codesign --force --sign - [shellescape ${destroot}${prefix}/lib/libpdfium.dylib]"
    xinstall -d ${destroot}${prefix}/include/pdfium/cpp ${destroot}${prefix}/lib/pkgconfig
    xinstall -m 644 {*}[glob ${worksrcpath}/public/*.h] ${destroot}${prefix}/include/pdfium
    xinstall -m 644 {*}[glob ${worksrcpath}/public/cpp/*.h] ${destroot}${prefix}/include/pdfium/cpp
    set fd [open ${destroot}${prefix}/lib/pkgconfig/pdfium.pc w]
    puts ${fd} "prefix=${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: PDFium
Description: PDF rendering and manipulation library
Version: ${version}
Libs: -L\${libdir} -lpdfium
Cflags: -I\${includedir}/pdfium"
    close ${fd}
    set docdir ${destroot}${prefix}/share/doc/${name}
    xinstall -d ${docdir}
    xinstall -m 644 -W ${worksrcpath} LICENSE AUTHORS README.md ${docdir}
    xinstall -m 644 ${portpath}/Portfile ${docdir}
    foreach component {agg23 abseil-cpp dragonbox/src fast_float/src highway/src} {
        set licdir ${docdir}/licenses/${component}
        xinstall -d ${licdir}
        foreach licensefile [glob -nocomplain ${worksrcpath}/third_party/${component}/LICENSE* ${worksrcpath}/third_party/${component}/copying] {
            xinstall -m 644 ${licensefile} ${licdir}
        }
    }
}

livecheck.type      git
livecheck.url       https://pdfium.googlesource.com/pdfium.git
livecheck.version   [lindex [lindex ${source_archives} 0] 2]

notes {
    This build provides PDF rendering without JavaScript, XFA, Skia or the
    experimental Brotli decoder. Use pkg-config --cflags --libs pdfium when
    building applications against the library.
}
