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

name                    py-scipy
version                 1.18.0
revision                1

checksums               rmd160  4ed929aa66c9450d3cec0861c568d275476113c3 \
                        sha256  67b2ad2ad54c72ca6d04975a9b2df8c3638c34ddd5b28738e94fc2b57929d378 \
                        size    30774447

license                 BSD
maintainers             {reneeotten @reneeotten} openmaintainer
description             An opensource library of scientific tools for Python
long_description        {*}${description}

homepage                https://www.scipy.org/

universal_variant       no

python.versions         310 311 312 313 314
python.pep517_backend   meson
python.add_archflags    no

if {${name} ne ${subport}} {
    compiler.c_standard 2017
    compiler.cxx_standard 2017

    # SciPy requires GCC >= 9.1
    compilers.setup     require_fortran -clang -gcc44 -gcc45 -gcc46 \
                        -gcc47 -gcc48 -gcc5 -gcc6 -gcc7 -gcc8 -g95

    # and Clang >= 12.0 (for PY310 subports)
    compiler.blacklist-append {clang < 1200}

    boost.version       1.81

    if {${os.platform} eq "darwin" && [string match *gcc* ${configure.compiler}]} {
        # dfitpackmodule.c: error: passing argument 1 of 'f2py_func'
        # from incompatible pointer type [-Wincompatible-pointer-types]
        patchfiles-append \
                        0001-patch-extra-flags.patch
    }

    if {${python.version} == 310} {
        version         1.15.3
        revision        1
        checksums       rmd160  a29b8fe6ee1fb282c3e2a419d989fead98a80661 \
                        sha256  eae3cf522bc7df64b42cad3925c876e1b0b6c35c1337c93e12c0f366f55b0eaf \
                        size    59419214
        patchfiles-append \
                        patch-v${version}-version-caps-pyproject.toml.diff

        if {${os.platform} eq "darwin" && ${os.major} < 11} {
            # Use legacysupport for strnlen, but for select sources.
            # Adding portgroup breaks the build: https://trac.macports.org/ticket/71272
            depends_lib-append \
                            path:lib/libMacportsLegacySupport.dylib:legacy-support
            patchfiles-append \
                            patch-v${version}-Use-legacysupport-manually-for-strnlen.diff
            post-patch {
                reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/scipy/io/meson.build \
                            ${worksrcpath}/scipy/optimize/meson.build \
                            ${worksrcpath}/scipy/sparse/linalg/_eigen/arpack/meson.build \
                            ${worksrcpath}/scipy/sparse/linalg/_propack/meson.build
            }
        }
    } elseif {${python.version} == 311} {
        version         1.17.1
        revision        1
        checksums       rmd160  fe3552f46fd079218a8f057c81cfef2383c1a274 \
                        sha256  95d8e012d8cb8816c226aef832200b1d45109ed4464303e997c5b13122b297c0 \
                        size    30573822

        # Clang >= 15.0 since version 1.16.0
        compiler.blacklist-append {clang < 1500}

        patchfiles-append   patch-v1.17.1-scipy_meson.build.diff
    } else {
        # Clang >= 15.0 since version 1.16.0
        compiler.blacklist-append {clang < 1500}
    }

    configure.fflags-append -fno-second-underscore

    depends_build-append \
                        port:py${python.version}-cython \
                        path:bin/pkg-config:pkgconfig \
                        port:py${python.version}-pybind11 \
                        port:py${python.version}-pythran

    build.env-append    PATH=${python.prefix}/bin:$env(PATH)

    depends_lib-append  port:py${python.version}-numpy

    # SciPy (and NumPy) only support the Accelerate framework for macOS 13.3+
    # see: https://scipy.github.io/devdocs/building/blas_lapack.html
    if {${os.platform} eq "darwin" && (${os.major} > 22 || ${os.major} == 22 && ${os.minor} >= 4)} {
        variant accelerate conflicts openblas flexiblas blis description "Use Apples Accelerate Framework" {
            build.args-append -Csetup-args=-Dblas=accelerate -Csetup-args=-Dlapack=accelerate
        }
    }

    variant openblas conflicts accelerate flexiblas blis description "Use MacPorts OpenBLAS Libraries" {
        depends_lib-append  path:lib/libopenblas.dylib:OpenBLAS

        build.args-append -Csetup-args=-Dblas=openblas -Csetup-args=-Dlapack=openblas \
                          -Csetup-args=-Dpkg_config_path=${prefix}/lib/pkgconfig
    }

    variant flexiblas conflicts accelerate openblas blis description "Use MacPorts FlexiBLAS libraries" {
        depends_lib-append  port:flexiblas

        build.args-append -Csetup-args=-Dblas=flexiblas -Csetup-args=-Dlapack=flexiblas \
                          -Csetup-args=-Dpkg_config_path=${prefix}/lib/pkgconfig
    }

    variant blis conflicts accelerate openblas flexiblas description "Use MacPorts BLIS/lapack libraries" {
        # TODO: eventually this could/should use libFLAME, but currently SciPy cannot be convinced to use it
        depends_lib-append  port:blis \
                            port:lapack

        build.args-append -Csetup-args=-Dblas=blis -Csetup-args=-Dlapack=lapack \
                          -Csetup-args=-Dpkg_config_path=${prefix}/share/pkgconfig:${prefix}/lib/lapack/pkgconfig
    }

    # set default BLAS/LAPACK variant if none is explicitly selected by the user
    if {![variant_isset accelerate] && ![variant_isset openblas] && ![variant_isset flexiblas] && ![variant_isset blis]} {
        # default to macOS Accelerate framework if macOS 13.3 or higher, otherwise OpenBLAS
        if {${os.platform} eq "darwin" && (${os.major} > 22 || ${os.major} == 22 && ${os.minor} >= 4) && ${python.version} > 39} {
            default_variants +accelerate
        } else {
            default_variants +openblas
        }
    }

    test.run            yes
    test.dir            ${destroot}${python.pkgd}
    test.env-append     PYTHONPATH=${destroot}${python.pkgd}

    depends_test-append port:py${python.version}-hypothesis \
                        port:py${python.version}-pooch \
                        port:py${python.version}-mpmath \
                        port:py${python.version}-gmpy2 \
                        port:py${python.version}-threadpoolctl \
                        port:py${python.version}-scikit-umfpack

    livecheck.type      none
}
