# -*- 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               compiler_blacklist_versions 1.0
PortGroup               github 1.0
PortGroup               legacysupport 1.1
PortGroup               python 1.0

# __sincos https://trac.macports.org/ticket/63233
legacysupport.newest_darwin_requires_legacy 12

github.setup            scipy scipy 1.11.4 v
revision                0

checksums               rmd160  8120272e76e1065f8f6b5aaf3fa7fd450d56e4c3 \
                        sha256  90a2b78e7f5733b9de748f589f09225013685f9b218275257f8a8168ededaeaa \
                        size    56336202

github.tarball_from     releases

name                    py-scipy
license                 BSD
maintainers             {michaelld @michaelld} openmaintainer
description             An opensource library of scientific tools for Python
long_description        {*}${description}

homepage                https://www.scipy.org/

python.versions         38 39 310 311

# When updating to meson.build later, please do it conditionally,
# unless build is confirmed on older systems as well.
python.pep517           no

python.add_archflags    no
universal_variant       no

compilers.setup         require_fortran -clang -gcc44 -gcc45 -gcc46 \
                        -gcc47 -gcc48 -g95

github.livecheck.regex {([0-9.-]+)}

if {${name} ne ${subport}} {
    if {${python.version} == 38} {
        # Support for Python 3.8 dropped in 1.11.x:
        # https://github.com/scipy/scipy/commit/890ffe9febc7b199e84ca9444ceb7b2f9c0a33aa
        github.setup    scipy scipy 1.10.1 v
        revision        2
        checksums       rmd160  a97f16d6acefbc7e52b81499d250226e0cce114d \
                        sha256  2cf9dfb80a7b4589ba4c40ce7588986d6d5cebc5457cad2c2880f6bc2d42f3a5 \
                        size    42407997

        boost.version           1.76

        patchfiles-append       patch-allow-MP-boost-1.10.diff

        depends_build-append    port:py${python.version}-cython-compat

        set compat_path [string replace ${python.pkgd} 0 [string length ${python.prefix}]-1 ${prefix}/lib/py${python.version}-cython-compat]
        build.env-append    PYTHONPATH=${compat_path}
    } else {
        boost.version           1.81

        post-extract {
            move ${worksrcpath}/_setup.py ${worksrcpath}/setup.py
        }

        patchfiles-append       patch-allow-MP-boost.diff

        depends_build-append    port:py${python.version}-cython
    }

    depends_build-append \
                    port:py${python.version}-pythran \
                    port:swig-python

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

    compiler.c_standard 2011
    compiler.cxx_standard 2014
    compiler.thread_local_storage yes
    # Requires threads.h, missing in Xcode clang
    # https://build.macports.org/builders/ports-10.11_x86_64-builder/builds/136856/steps/install-port/logs/stdio
    compiler.blacklist-append {clang < 900}

    # force disable STDC THREADS from <threads.h> and instead just use __thread
    # https://trac.macports.org/ticket/62488
    patchfiles-append   patch-use__STDC_NO_THREADS__.diff

    # https://github.com/scipy/scipy/issues/19718
    patchfiles-append   patch-macosx-version-min.diff

    # There is some conflict between legacysupport cmath and gcc on old systems, which makes isnan/isinf undefined.
    if {${os.platform} eq "darwin" && ${os.major} < 13 && [string match *gcc* ${configure.compiler}]} {
        patchfiles-append \
                        patch-special-fix-isnan-isinf.diff

        if {${python.version} == 38} {
                    patchfiles-append \
                        patch-ckdtree-fix-isinf-1.10.diff
        } else {
            patchfiles-append \
                        patch-ckdtree-fix-isinf.diff
        }
    }

    post-patch {
        reinplace "s|@MACOS_V@|${macosx_deployment_target}|g" ${worksrcpath}/scipy/_build_utils/compiler_helper.py
    }

    worksrcdir              ${distname}
    build.env-append        "CCFLAGS=-I${prefix}/include -L${prefix}/lib"
    destroot.env-append     "CCFLAGS=-I${prefix}/include -L${prefix}/lib"
    configure.fflags-append -fno-second-underscore

    build.env-append        SETUPTOOLS_USE_DISTUTILS=stdlib
    destroot.env-append     SETUPTOOLS_USE_DISTUTILS=stdlib

    pre-configure {
        set fc_options      "config_fc --fcompiler gnu95 --f77exec ${configure.f77} --f77flags='${configure.f77_archflags} ${configure.fflags}' --f90exec ${configure.f90} --f90flags='${configure.f90_archflags} ${configure.fflags}'"
        set config_options  "config --cc ${configure.cc} --include-dirs ${prefix}/include --library-dirs ${prefix}/lib"
        build.cmd-append    ${fc_options} ${config_options}
        destroot.cmd-append ${fc_options} ${config_options}

        build.env-append    CC=${configure.cc} \
                            CXX=${configure.cxx} \
                            CFLAGS=${configure.cc_archflags} \
                            CXXFLAGS=${configure.cxx_archflags} \
                            CPPFLAGS=${configure.cppflags}
        destroot.env-append CC=${configure.cc} \
                            CXX=${configure.cxx} \
                            CFLAGS=${configure.cc_archflags} \
                            CXXFLAGS=${configure.cxx_archflags} \
                            CPPFLAGS=${configure.cppflags}
    }

    post-destroot {
        # for some reason read-world is not set
        system "chmod -R a+r ${destroot}${prefix}"
    }

    variant atlas conflicts openblas mkl description "Use MacPorts ATLAS libraries" {
        depends_lib-append  port:atlas
    }

    variant openblas conflicts atlas mkl description "Use MacPorts OpenBLAS Libraries" {
        depends_lib-append  path:lib/libopenblas.dylib:OpenBLAS
    }

    variant mkl conflicts atlas openblas description "Use MacPorts MKL Libraries" {
        depends_lib-append  port:py${python.version}-mkl \
                            port:py${python.version}-mkl-include
    }

    # Make +openblas a default variant, at least temporarily, to
    # overcome issues with Apple's /usr/lib/libblas.* and
    # /usr/lib/liblapack.* missing a symbol. see also:
    # https://trac.macports.org/ticket/57829
    if {![variant_isset atlas] &&
        ![variant_isset openblas] &&
        ![variant_isset mkl]} {
        default_variants +openblas
    }

    if {[variant_isset atlas]} {
        # use MacPorts atlas
        build.env-append    OPENBLAS=None \
                            MKLROOT=None \
                            ATLAS=${prefix}/lib \
                            LAPACK=${prefix}/lib \
                            BLAS=${prefix}/lib
        destroot.env-append OPENBLAS=None \
                            MKLROOT=None \
                            ATLAS=${prefix}/lib \
                            LAPACK=${prefix}/lib \
                            BLAS=${prefix}/lib

        pre-fetch {
            # scipy needs fortran; so we only need to check if atlas is
            # compiled with +nofortran
            if {![catch {set result [active_variants atlas "" nofortran]}]} {
                if {!$result} {
                    return -code error \
"You have selected the +atlas variant but atlas was built with +nofortran.\
scipy needs a fortran enabled atlas. Please rebuild atlas without the +nofortran\
variant."
                }
            }

            # also check that numpy has the atlas variant active
            if {![catch {set result [active_variants py${python.version}-numpy atlas ""]}]} {
                if {!$result} {

                    return -code error \
"You have selected the +atlas variant but py${python.version}-numpy does not\
have the +atlas variant active. Please ensure that numpy is activated with the\
+atlas variant."
                }
            }
        }

    } elseif {[variant_isset openblas]} {
        # use MacPorts OpenBLAS
        build.env-append    OPENBLAS=${prefix}/lib \
                            ATLAS=None \
                            MKLROOT=None
        destroot.env-append OPENBLAS=${prefix}/lib \
                            ATLAS=None \
                            MKLROOT=None

    } elseif {[variant_isset mkl]} {
        # use MacPorts MKL
        build.env-append    OPENBLAS=None \
                            ATLAS=None \
                            MKLROOT=${python.prefix}
        destroot.env-append OPENBLAS=None \
                            ATLAS=None \
                            MKLROOT=${python.prefix}

        pre-fetch {
            # check that numpy has the mkl variant active
            if {![catch {set result [active_variants py${python.version}-numpy mkl ""]}]} {
                if {!$result} {

                    return -code error \
"You have selected the +mkl variant but py${python.version}-numpy does not\
have the +mkl variant active. Please ensure that numpy is activated with the\
+mkl variant."
                }
            }
        }

        # set absolute path to remove references to @rpath/libmkl_rt.2.dylib
        post-destroot {
            foreach soname [exec find ${destroot}${python.pkgd}/scipy -name "*.so"] {
                system "install_name_tool -change @rpath/libmkl_rt.2.dylib ${python.prefix}/lib/libmkl_rt.2.dylib ${soname}"
                }
        }

    } else {
        # use Accelerate BLAS
        build.env-append    OPENBLAS=None \
                            ATLAS=None \
                            LAPACK=/usr/lib \
                            BLAS=/usr/lib
        destroot.env-append OPENBLAS=None \
                            ATLAS=None \
                            LAPACK=/usr/lib \
                            BLAS=/usr/lib
    }
}
