# -*- 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           python 1.0
PortGroup           compiler_wrapper 1.0

name                py-matplotlib
version             3.8.3
revision            0

categories-append   graphics math
license             {PSF BSD}
maintainers         {reneeotten @reneeotten} openmaintainer

description         Matplotlib is a python plotting library
long_description    Matplotlib strives to produce publication quality 2D \
                    graphics for interactive graphing, scientific publishing, \
                    user interface development and web application servers \
                    targeting multiple user interfaces and hardcopy output \
                    formats. There is a 'pylab' mode which emulates matlab \
                    graphics. The library uses numpy for handling large data \
                    sets and supports a variety of output backends. This port \
                    provides variants for the different GUIs (gtk2, gtk3, \
                    tkinter, qt4, qt5, cairo, latex).

homepage            https://matplotlib.org/

checksums           rmd160  1fb02e7340e3ab16fb5174c098f830d22ed8278d \
                    sha256  7b416239e9ae38be54b028abbf9048aff5054a9aba5416bef0bd17f9162ce161 \
                    size    35879872

use_parallel_build  no

python.versions     27 37 38 39 310 311 312
python.pep517       no

if {${name} ne ${subport}} {
    depends_build-append \
                    port:pkgconfig \
                    port:py${python.version}-certifi \
                    port:py${python.version}-pybind11 \
                    port:py${python.version}-setuptools \
                    port:py${python.version}-setuptools_scm

    depends_lib-append \
                    port:freetype \
                    port:qhull \
                    port:py${python.version}-contourpy \
                    port:py${python.version}-cycler \
                    port:py${python.version}-dateutil \
                    port:py${python.version}-fonttools \
                    port:py${python.version}-kiwisolver \
                    port:py${python.version}-numpy \
                    port:py${python.version}-parsing \
                    port:py${python.version}-packaging \
                    port:py${python.version}-Pillow

    if {${python.version} < 310} {
        depends_lib-append \
                    port:py${python.version}-importlib-resources
    }

    patchfiles      patch-v38-mplsetup.cfg.diff \
                    patch-v38-setupext.py.diff \
                    patch-v38-setupext-Werror.diff

    set configfile mplsetup.cfg

    if {${python.version} == 38 && ${os.platform} eq "darwin" && ${os.major} > 16} {
        version     3.7.4
        revision    0
        checksums   rmd160  f7a3fcd9c325779c236828ec1a23f4baadfa0f6b \
                    sha256  7cd4fef8187d1dd0d9dcfdbaa06ac326d396fb8c71c647129f0bf56835d77026 \
                    size    38098243
        patchfiles  patch-v37-mplsetup.cfg.diff \
                    patch-v37-setupext.py.diff
    } elseif {${python.version} == 37 || ${python.version} > 37 && ${os.platform} eq "darwin" && ${os.major} <= 16} {
        version     3.5.3
        revision    1
        checksums   rmd160  6eef2bea598dcd4b004e861349c081fe712a11c1 \
                    sha256  339cac48b80ddbc8bfd05daae0a3a73414651a8596904c2a881cfd1edb65f26c \
                    size    35236343
        patchfiles  patch-v35-mplsetup.cfg.diff \
                    patch-v35-src-macosx.m.diff \
                    patch-v35-setupext.py.diff \
                    patch-v35-setup.py.diff
        depends_build-append \
                    port:py${python.version}-setuptools_scm_git_archive
        depends_lib-delete \
                    port:py${python.version}-contourpy
    } elseif {${python.version} == 27} {
        version     2.2.5
        revision    4
        checksums   rmd160  4532a205e8f40d6f40346b2e461d3dca144b38b9 \
                    sha256  a3037a840cd9dfdc2df9fee8af8f76ca82bfab173c0f9468193ca7a89a2b60ea \
                    size    36678633

        depends_lib-append \
                    port:py${python.version}-backports-functools_lru_cache \
                    port:py${python.version}-six \
                    port:py${python.version}-subprocess32 \
                    port:py${python.version}-tz

        patchfiles  patch-v2-setup.cfg.diff \
                    patch-v2-qhull.diff \
                    patch-v2-setupext.py.diff \
                    patch-v2-jquery-ui.diff

        variant gtk2 description "Enable GTKAgg backend" {
            depends_lib-append  port:py${python.version}-pygtk
            post-patch {
                reinplace "s|^gtk=False|gtk=True|" ${worksrcpath}/${configfile}
                reinplace "s|^gtkagg=False|gtkagg=True|" ${worksrcpath}/${configfile}
            }
        }

        depends_lib-replace \
                    port:py${python.version}-Pillow \
                    port:libpng

        depends_build-delete \
                    port:py${python.version}-setuptools_scm \
                    port:py${python.version}-setuptools_scm_git_archive \
                    port:py${python.version}-certifi \
                    port:py${python.version}-pybind11

        depends_lib-delete \
                    port:py${python.version}-fonttools \
                    port:py${python.version}-packaging \
                    port:py${python.version}-contourpy

        post-patch {
            reinplace "s|@@PREFIX@@|${prefix}|" ${worksrcpath}/${configfile}
        }

        set configfile setup.cfg

        variant qt4 description "Enable Qt4Agg backend" {
            depends_lib-append  port:py${python.version}-pyqt4
            post-patch {
                reinplace "s|^qt4agg=False|qt4agg=True|" ${worksrcpath}/${configfile}
            }
        }

        variant pyside description "Enable PySide backend" {
            depends_lib-append  port:py${python.version}-pyside
            post-patch {
                reinplace "s|^pyside=False|pyside=True|" ${worksrcpath}/${configfile}
            }
        }

        # jquery-ui is included in the PyPI files, patch added for setup.py that avoids trying
        # to install if from the cache or download from URL. Just copy the directory in the post-
        # build phase.
        post-build {
            set libdir [glob -nocomplain ${worksrcpath}/build/lib*]
            if {![file exists ${libdir}/matplotlib/backends/web_backend/jquery-ui-1.12.1]} {
                file copy ${worksrcpath}/lib/matplotlib/backends/web_backend/jquery-ui-1.12.1 \
                    ${libdir}/matplotlib/backends/web_backend/ \
            }
        }
    }

    build.env-append \
                    PKG_CONFIG_PATH=${python.prefix}/lib/pkgconfig \
                    CFLAGS=${configure.cxxflags} \
                    CXXFLAGS=${configure.cxxflags} \

    destroot.env-append \
                    PKG_CONFIG_PATH=${python.prefix}/lib/pkgconfig \
                    CFLAGS=${configure.cxxflags} \
                    CXXFLAGS=${configure.cxxflags} \

    # build fails with gcc-4.0 on Leopard, use gcc-4.2 (#37069)
    compiler.blacklist gcc-4.0

    # additionally on Leopard the macosx backend cannot be compiled anymore, see Trac ticket:
    # https://trac.macports.org/ticket/61757
    if {${os.platform} eq "darwin" && ${os.major} < 10} {
        post-patch {
            reinplace "s|^macosx=True|macosx=False|" ${worksrcpath}/${configfile}
        }
    }

    post-destroot {
        xinstall -d ${destroot}${prefix}/share/doc/${subport} \
            ${destroot}${prefix}/share/${subport}
        xinstall -m 0644 {*}[glob ${worksrcpath}/README.*] \
            ${destroot}${prefix}/share/doc/${subport}

        if {[vercmp ${version} 3.5.0] < 0} {
            xinstall -m 0644 -W ${worksrcpath} matplotlibrc.template \
                ${destroot}${prefix}/share/doc/${subport}
        }

        if {[vercmp ${version} 3.8.0] < 0} {
            file copy ${worksrcpath}/examples \
                ${destroot}${prefix}/share/${subport}
        } else {
            file copy ${worksrcpath}/galleries \
                ${destroot}${prefix}/share/${subport}
        }
    }

    variant webagg description "Enable WebAgg backend" {
        if {[vercmp ${version} 3.5.0] < 0} {
            depends_lib-append  port:py${python.version}-tornado
        }
    }

    variant tkinter description "Enable TkAgg backend" {
        depends_lib-append  port:py${python.version}-tkinter

        if {${python.version} == 27} {
            post-patch {
                reinplace "s|^tkagg=False|tkagg=True|" ${worksrcpath}/${configfile}
            }
        }
    }

    variant gtk3 description "Enable GTK3Agg backend" {
        depends_lib-append  port:py${python.version}-gobject3 \
                            path:lib/pkgconfig/gtk+-3.0.pc:gtk3

        if {${python.version} == 27} {
            post-patch {
                reinplace "s|^gtk3agg=False|gtk3agg=True|" ${worksrcpath}/${configfile}
            }
        }
    }

    variant qt5 description "Enable Qt5Agg backend" {
        depends_lib-append  port:py${python.version}-pyqt5

        if {${python.version} == 27} {
            post-patch {
                reinplace "s|^qt5agg=False|qt5agg=True|" ${worksrcpath}/${configfile}
            }
        }
    }

    variant cairo description "Enable Cairo backends" {
        depends_lib-append  port:py${python.version}-cairo

        if {${python.version} == 27} {
            post-patch {
                reinplace "s|^cairo=False|cairo=True|" ${worksrcpath}/${configfile}
            }
        }
    }

    if {${python.version} > 27} {
        variant pyside2 description "Enable PySide2 backend" {
            depends_lib-append  port:py${python.version}-pyside2
        }
    }

    variant dvipng description "Enable dvipng support" {
        depends_run-append  port:dvipng
    }

    variant ghostscript description "Enable GhostScript support" {
        depends_run-append  port:ghostscript
    }

    variant latex description "Enable LaTeX support" {
        depends_run-append  bin:latex:texlive
    }

    variant pdftops description "Enable pdftops support" {
        depends_run-append  bin:pdftops:poppler
    }

    default_variants +webagg

    if {![variant_isset gtk2] && ![variant_isset tkinter] && ![variant_isset qt5] &&
        ![variant_isset qt4] && ![variant_isset pyside] && ![variant_isset pyside2]} {
            default_variants-append +cairo
    }

    if {[variant_isset gtk3] && [variant_isset cairo] && ${python.version} == 27} {
        post-patch {
            reinplace "s|^gtk3cairo=False|gtk3cairo=True|" ${worksrcpath}/${configfile}
        }
    }

    livecheck.type  none

    notes "The default backend is the interactive Mac OS X backend. Different\
        backends can be specified using the ~/.matplotlib/matplotlibrc file.\
        More details regarding backends can be found in the matplotlib FAQ:

        https://matplotlib.org/stable/users/explain/backends.html"
}
