# -*- 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           compiler_blacklist_versions 1.0
PortGroup           github 1.0
PortGroup           gobject_introspection 1.0
PortGroup           meson 1.0
PortGroup           muniversal 1.1

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

github.setup        harfbuzz harfbuzz 11.1.0
checksums           rmd160  44603018f339ccb866a93889b705b7cc2422bded \
                    sha256  477f0d48c34dc32093b45304178eb9733361ca1832b5159879c99e6d40227969 \
                    size    18049188

name                harfbuzz-devel
conflicts           harfbuzz
set my_name         harfbuzz
categories          graphics
maintainers         {ryandesign @ryandesign} {mascguy @mascguy} openmaintainer
license             MIT
description         OpenType text shaping engine
long_description    HarfBuzz is an {*}${description}.
homepage            https://harfbuzz.github.io

github.tarball_from releases
dist_subdir         ${my_name}
use_xz              yes

# Disable unexpected download of subprojects
meson.wrap_mode     nodownload

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

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

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

# Disable legacy non-underscore macros, defined by AssertMacros.h, on older macOS releases; fixes:
#
#   ../harfbuzz-10.1.0/src/hb-buffer.hh:214:9: error: too many arguments provided to function-like macro invocation
#     214 |                hb_font_t          *font,
#         |                ^
#   /usr/include/AssertMacros.h:1339:11: note: macro 'verify' defined here
#     1339 |                 #define verify(assertion) __Verify(assertion)
#          |                         ^
patchfiles-append   patch-meson-assert-macros.diff

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

# https://github.com/harfbuzz/harfbuzz/issues/1642
compiler.cxx_standard \
                    2011

# error: invalid operands to binary expression
# https://github.com/harfbuzz/harfbuzz/issues/4371
compiler.blacklist-append \
                    {clang < 700}

# Options common across main and subport
configure.args-append \
                    -Dchafa=disabled \
                    -Ddocs=disabled

# Do not use -Werror. New compilers may have new warnings; we don't want users
# to encounter build failures because of that.
configure.cflags-append     -DHB_NO_PRAGMA_GCC_DIAGNOSTIC_ERROR
configure.cxxflags-append   -DHB_NO_PRAGMA_GCC_DIAGNOSTIC_ERROR

if {${name} eq ${subport}} {
    revision        0

    gobject_introspection yes

    # NOTE: lib dep for gobject-introspection added via pg gobject_introspection
    depends_lib-append \
                    path:lib/pkgconfig/cairo.pc:cairo \
                    port:freetype \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:graphite2

    # NOTE: -Dintrospection set via pg gobject_introspection
    configure.args-append \
                    -Dcairo=enabled \
                    -Dfreetype=enabled \
                    -Dglib=enabled \
                    -Dgraphite2=enabled \
                    -Dgobject=enabled \
                    -Dicu=disabled

    test.run        yes

    post-destroot {
        set docdir ${prefix}/share/doc/${name}
        xinstall -d ${destroot}${docdir}
        xinstall -m 0644 -W ${worksrcpath} \
                    AUTHORS \
                    COPYING \
                    NEWS \
                    README.md \
                    THANKS \
                    ${destroot}${docdir}
    }

    if {${os.platform} eq "darwin" && ${os.major} >= 9} {
        configure.args-append \
                    -Dcoretext=enabled
    } else {
        configure.args-append \
                    -Dcoretext=disabled
    }

    if {${os.platform} eq "darwin" && ${os.major} < 10} {
        depends_test-append \
                    port:python27
        test.env-append \
                    PATH=${frameworks_dir}/Python.framework/Versions/2.7/bin:$env(PATH)
    }
} else {
    livecheck.type  none
}

subport harfbuzz-icu-devel {
    revision        0

    conflicts       harfbuzz-icu

    description     HarfBuzz ICU support library
    long_description This library adds support for ICU (the International Components for Unicode) to HarfBuzz.

    gobject_introspection no

    depends_lib-append \
                    port:harfbuzz-devel \
                    path:lib/pkgconfig/icu-uc.pc:icu

    # ICU requires C++17
    compiler.cxx_standard \
                    2017

    # NOTE: -Dintrospection set via pg gobject_introspection
    configure.args-append \
                    -Dcairo=disabled \
                    -Dfreetype=disabled \
                    -Dglib=disabled \
                    -Dgraphite2=disabled \
                    -Dicu=enabled

    # Deconflict with parent port contents
    post-destroot {
        ui_info "${subport}: removing files from parent"

        foreach f [exec port -q contents ${name}] {
            ui_info "${subport}: deleting file: ${f}"
            delete ${destroot}${f}
        }
    }
}
