# -*- 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           active_variants 1.1
PortGroup           debug 1.0
PortGroup           meson 1.0
PortGroup           perl5 1.0

name                gimp3-devel
set my_name         gimp3
conflicts           gimp2 gimp2-devel
version             3.2.8
revision            1

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

description         The GNU Image Manipulation Program
long_description    The GNU Image Manipulation Program (GIMP) is a powerful \
                    tool for the preparation and manipulation of digital images. \
                    The GIMP provides the user with a wide variety of image \
                    manipulation, painting, processing, and rendering tools. \
                    This is the GTK3 based development version leading \
                    to a future 3.0 release based on a recent snapshot of \
                    git master.
homepage            https://gimp.org/

set branch          [join [lrange [split ${version} .] 0 1] .]
master_sites        gimp:gimp/v${branch}/
dist_subdir         ${my_name}
distname            gimp-${version}
use_xz              yes

checksums           rmd160  e1a73088f8da58ad68a76d2377057c27de3e9476 \
                    sha256  16a2696cadc9e03a2ef97ccb895e062f654cb2b8211fb57074db85f26646c847 \
                    size    35071988

# Disable unexpected download of subprojects
meson.wrap_mode     nodownload

depends_build-append \
                    port:gettext \
                    port:perl${perl5.major} \
                    path:bin/pkg-config:pkgconfig \
                    port:realpath

depends_lib-append \
                    port:aalib \
                    port:appstream \
                    port:atk \
                    path:lib/pkgconfig/babl-0.1.pc:babl \
                    port:bzip2 \
                    path:lib/pkgconfig/cairo.pc:cairo \
                    port:cfitsio \
                    port:curl \
                    port:dbus-glib \
                    port:desktop-file-utils \
                    port:fontconfig \
                    port:freetype \
                    path:lib/pkgconfig/gdk-pixbuf-2.0.pc:gdk-pixbuf2 \
                    path:lib/pkgconfig/gegl-0.4.pc:gegl \
                    port:gexiv2 \
                    port:ghostscript \
                    port:glib-networking \
                    path:lib/pkgconfig/gobject-introspection-1.0.pc:gobject-introspection \
                    path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                    path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    path:lib/pkgconfig/icu-uc.pc:icu \
                    port:iso-codes \
                    port:json-c \
                    port:json-glib \
                    port:lcms2 \
                    port:libarchive \
                    port:libde265 \
                    port:libheif \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libjxl \
                    port:libmng \
                    port:libmypaint \
                    port:libpng \
                    path:lib/pkgconfig/librsvg-2.0.pc:librsvg \
                    port:libwmf \
                    port:libxml2 \
                    port:libxslt \
                    port:maxflow \
                    port:mypaint-brushes \
                    port:nasm \
                    port:openexr \
                    port:openjpeg \
                    path:lib/pkgconfig/pango.pc:pango \
                    path:lib/pkgconfig/poppler.pc:poppler \
                    port:poppler-data \
                    port:qoi \
                    port:shared-mime-info \
                    port:suitesparse_umfpack \
                    port:tiff \
                    port:webp \
                    port:x265 \
                    port:xmlto \
                    port:xz \
                    port:zlib

depends_run-append \
                    port:adwaita-icon-theme \
                    port:hicolor-icon-theme

# gcc-4.2 5493 and 5666.3_13: gimpcpuaccel.c:180: error: can't find a register in class 'BREG' while reloading 'asm'
# redefinition of typedef is invalid in C [-Wtypedef-redefinition] (#50329)
# as of version 2.10.0 requires a C++14 compatible compiler to configure

compiler.cxx_standard 2014
compiler.blacklist-append \
                    *gcc-3.* *gcc-4.* {clang < 700}

patchfiles-append   patch-etc-gimprc.in.diff
patchfiles-append   patch-mypaint-brushes-dir.diff
#patchfiles-append   patch-quartz-32bit.diff

platform darwin {
    if {${os.major} < 11} {
        # avoid Cursor type conflict between X11 and Quickdraw
        # error: typedef redefinition with different types ('struct Cursor' vs 'XID' (aka 'unsigned long'))
        # not an issue on 10.7 and later
        patchfiles-append \
                    patch-x11-widgets-fix.diff
    }

    # gimppickbutton-quartz.c:187:20: error: 'CGWindowListCreateImage' is unavailable: obsoleted in macOS 15.0 -
    #   Please use ScreenCaptureKit instead.
    if {${os.major} >= 24} {
        macosx_deployment_target 14.0
    }
}

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

configure.perl      ${perl5.bin}

configure.env-append \
                    CC_FOR_BUILD=${configure.cc}

if {[vercmp $xcodeversion 4.3] < 0 && [string match "*macports*" ${configure.compiler}]} {
    # Xcode 4.2 fails with load commands in the newer toolchain
    depends_build-append \
                    port:cctools

    configure.env-append \
                    AR=${prefix}/bin/ar \
                    RANLIB=${prefix}/bin/ranlib
}

configure.args-append \
                    -Dalsa=disabled \
                    -Dbug-report-url=https://guide.macports.org/#project.tickets \
                    -Denable-console-bin=true \
                    -Dgi-docgen=disabled \
                    -Dgudev=disabled \
                    -Dheif=enabled \
                    -Djavascript=disabled \
                    -Djpeg2000=enabled \
                    -Djpeg-xl=enabled \
                    -Dlibbacktrace=false \
                    -Dlibunwind=false \
                    -Dlua=false \
                    -Dmng=enabled \
                    -Dopenexr=enabled \
                    -Dopenmp=disabled \
                    -Dvala=disabled \
                    -Dwebkit-unmaintained=false \
                    -Dwebp=enabled \
                    -Dwmf=enabled

# keep empty GIMP font directory
# silences warning message on startup:
# GIMP-Message: Some fonts failed to load:
# - /opt/local/share/gimp/3.0/fonts/
destroot.keepdirs-append \
                    ${destroot}${prefix}/share/gimp/3.0/fonts

proc py_setup {py_ver} {
    global env prefix frameworks_dir
    set py_ver_nodot [string map {. {}} ${py_ver}]

    set python_frameworks_dir \
                    ${frameworks_dir}/Python.framework/Versions/${py_ver}

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

    configure.pkg_config_path-append \
                    ${python_frameworks_dir}/lib/pkgconfig

    configure.env-append \
                    PATH=${python_frameworks_dir}/bin:$env(PATH)
    build.env-append \
                    PATH=${python_frameworks_dir}/bin:$env(PATH)
    destroot.env-append \
                    PATH=${python_frameworks_dir}/bin:$env(PATH)

    depends_lib-append \
                    port:py${py_ver_nodot}-cairo \
                    port:py${py_ver_nodot}-gobject3
}

variant python312 conflicts python313 python314 description {Build with python plugin support using python 3.12} {
    py_setup 3.12
}

variant python313 conflicts python312 python314 description {Build with python plugin support using python 3.13} {
    py_setup 3.13
}

variant python314 conflicts python312 python313 description {Build with python plugin support using python 3.14} {
    py_setup 3.14
}

if {![variant_isset python312] && \
    ![variant_isset python313] && \
    ![variant_isset python314]} {
    default_variants-append +python314
}

# meson.build uses the GTK+ 3 backend to determine whether to enable X11.
variant x11 conflicts quartz {
    require_active_variants gtk3 x11

    depends_lib-append \
                    port:xdg-utils \
                    port:xorg-libXcursor \
                    port:xorg-libXmu \
                    port:xorg-libXext \
                    port:xorg-libXfixes \
                    port:xpm
}

variant quartz conflicts x11 {
    require_active_variants gtk3 quartz

    depends_lib-append \
                    port:gtk-osx-application-gtk3
}

if {![variant_isset quartz]} {
    default_variants-append +x11
}

post-activate {
    system "${prefix}/bin/gtk-update-icon-cache -f -t ${prefix}/share/icons/hicolor"
    system "${prefix}/bin/update-desktop-database ${prefix}/share/applications"
}

livecheck.type      regex
livecheck.url       https://download.gimp.org/gimp/v${branch}/
livecheck.regex     "gimp-(${branch}(?:\\.\\d+)*)${extract.suffix}"
