# -*- 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
PortGroup                   debug 1.0
PortGroup                   legacysupport 1.1
PortGroup                   meson 1.0
PortGroup                   muniversal 1.0
PortGroup                   xcodeversion 1.0

# memmem
# https://gitlab.freedesktop.org/cairo/cairo/-/issues/635
legacysupport.newest_darwin_requires_legacy 10

# Please keep the cairo and cairo-devel ports as similar as possible.

set my_name                 cairo
name                        ${my_name}
conflicts                   ${my_name}-devel
version                     1.18.4
revision                    2
epoch                       1
categories                  graphics
platforms                   darwin macosx
maintainers                 {ryandesign @ryandesign} {mascguy @mascguy}
license                     {LGPL-2.1 MPL-1.1}
description                 vector graphics library with cross-device output support
long_description            Cairo is a ${description}. It is designed to produce \
                            identical output on all output media while taking \
                            advantage of display hardware acceleration when \
                            available (e.g. through the X Render Extension).
homepage                    https://www.cairographics.org
master_sites                ${homepage}/releases/ \
                            https://gitlab.freedesktop.org/cairo/cairo/-/archive/${version}/
dist_subdir                 ${my_name}
distname                    ${my_name}-${version}
use_xz                      yes
checksums                   rmd160  757415ba8e1b5df92474644c887865a5ade9fd93 \
                            sha256  445ed8208a6e4823de1226a74ca319d3600e83f6369f99b14265006599c32ccb \
                            size    32578804

# Disable unexpected download of subprojects
meson.wrap_mode             nodownload

set py_ver                  3.14
set py_ver_nodot            [string map {. {}} ${py_ver}]
configure.python            ${prefix}/bin/python${py_ver}

patch.pre_args-replace      -p0 -p1

# Upstream patch to correctly set dylib versions
patchfiles-append           patch-darwin-dylib-versions.diff

# Our patches
# Note, currently we diverge from cairo-devel here, since there is no override port for the latter.

# Allow building Quartz font with a fallback implementation on 10.6.
# On 10.5 fallback code also fails: https://github.com/macos-powerpc/powerpc-ports/issues/98
if {${os.platform} eq "darwin" && ${os.major} > 9} {
    patchfiles-append       cairo-enable-quartz-font.patch
    # cairo-quartz-surface.c:1944:5: error: implicit declaration of function 'CTFontDrawGlyphs'
    patchfiles-append       cairo-ctfontdrawglyphs-fallback.patch
}

# Fix conflicting typedef:
patchfiles-append           cairo-test-pdiff.h.patch

post-patch {
    # Disable xcb-shm: MIT-SHM causes X11 BadAccess errors
    # See https://trac.macports.org/ticket/40811
    reinplace "s|xcbshm_dep = dependency('xcb-shm', required: get_option('xcb'))|xcbshm_dep = disabler()|" \
        ${worksrcpath}/meson.build

    fs-traverse f ${worksrcpath} {
        if {[string match *.py ${f}]} {
            ui_info "patching env python3: ${f}"
            reinplace -q "s|/usr/bin/env python3$|${configure.python}|" ${f}
        }
    }
}

depends_build-append        path:bin/pkg-config:pkgconfig \
                            port:python${py_ver_nodot}

depends_lib-append          port:expat \
                            path:lib/pkgconfig/fontconfig.pc:fontconfig \
                            path:lib/pkgconfig/freetype2.pc:freetype \
                            path:lib/pkgconfig/glib-2.0.pc:glib2 \
                            path:lib/pkgconfig/pixman-1.pc:libpixman \
                            port:libpng \
                            port:lzo2 \
                            port:zlib

configure.args-append       --default-library=both \
                            -Dfontconfig=enabled \
                            -Dfreetype=enabled \
                            -Dglib=enabled \
                            -Dlzo=enabled \
                            -Dpng=enabled \
                            -Dquartz=disabled \
                            -Dspectre=disabled \
                            -Dsymbol-lookup=disabled \
                            -Dtee=enabled \
                            -Dtests=disabled \
                            -Dxcb=disabled \
                            -Dxlib=disabled \
                            -Dxlib-xcb=disabled

compiler.c_standard         2011
# Top-level meson.build sets C++11.
compiler.cxx_standard       2011

platform macosx {
    variant quartz {
        configure.args-replace \
                            -Dquartz=disabled -Dquartz=enabled
    }

    if {${os.major} > 8} {
        default_variants        +quartz
        # Don't allow Quartz support to be disabled. Keep the variant for awhile in
        # case any dependents are using the active_variants portgroup to check for it.
        variant_set             quartz
    }
}

variant x11 {
    depends_lib-append      port:xorg-libXext \
                            port:xorg-xcb-util \
                            port:xrender

    configure.args-replace  -Dxcb=disabled -Dxcb=enabled \
                            -Dxlib=disabled -Dxlib=enabled \
                            -Dxlib-xcb=disabled -Dxlib-xcb=enabled
}

proc port_test_ver_check {p_name p_ver p_rev} {
    if { [catch {set port_ver_info [lindex [registry_active ${p_name}] 0]}] } {
        error "Tests require that ${p_name} be active; install, then re-run tests"
    } else {
        set test_ver ${p_ver}_${p_rev}
        set port_ver [lindex ${port_ver_info} 1]_[lindex ${port_ver_info} 2]
        ui_info "port_test_ver_check: ${p_name}: test_ver: ${test_ver}; port_ver: ${port_ver}"

        if { [vercmp ${port_ver} ${test_ver}] != 0 } {
            error "Tests require installed version of ${p_name} to match port; update, then re-run tests"
        }
    }
}

variant tests description "Enable tests" {
    pre-configure {
        port_test_ver_check ${subport} ${version} ${revision}
    }

    depends_build-append    port:ghostscript \
                            path:lib/pkgconfig/librsvg-2.0.pc:librsvg \
                            path:lib/pkgconfig/poppler.pc:poppler

    configure.args-replace  -Dtests=disabled -Dtests=enabled

    test.run                yes
}

default_variants            +x11
if {${os.platform} ne "darwin" || ${os.subplatform} ne "macosx"} {
    # When not on macOS, don't allow X11 support to be disabled, since it is the
    # only available option.
    variant_set             x11
}

livecheck.type              regex
livecheck.url               ${homepage}/news/
livecheck.regex             ${my_name}-(\[0-9.\]+)
