# -*- 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               active_variants 1.1
PortGroup               github 1.0
PortGroup               compilers 1.0
PortGroup               compiler_blacklist_versions 1.0

github.setup            scipy   scipy 1.10.1 v
checksums               rmd160  a97f16d6acefbc7e52b81499d250226e0cce114d \
                        sha256  2cf9dfb80a7b4589ba4c40ce7588986d6d5cebc5457cad2c2880f6bc2d42f3a5 \
                        size    42407997
revision                1

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         27 35 36 37 38 39 310 311

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} == 27} {

        github.setup scipy scipy 1.2.3 v
        checksums   rmd160 868076b4d88257a6b536f4c3a4e1e8230b587290 \
                    sha256 beb6123e91a0bdfba4557b6560be4899dac174a38c30876e5cfc60382ecb4ff7 \
                    size   18564471
        revision    1

        livecheck.url https://github.com/scipy/scipy/tree/maintenance/1.2.x/doc/release
        livecheck.regex {title="(1\.2\.[0-9.-]+)-notes.rst"}

        # See https://trac.macports.org/ticket/60520
        compilers.allow_arguments_mismatch yes

        # Fixes general build issues with implicit functions; unknown
        # how these changes might impact functionality; since SciPy
        # 1.2.3 is quite old, no guarantees that any of this works,
        # and there is no point in pushing it upstream, really.
        patchfiles-append patch-xcode12-fixes.27.diff
        patch.pre_args -p1

    } elseif {${python.version} == 35} {

        github.setup scipy scipy 1.4.1 v
        checksums   rmd160  e232695a51ed02b362187ea164ce269cb431e2ab \
                    sha256  4cc4e820d0770225bfb83e2a0ae340bb230fafb5402ea8efa87c6576a3f342cf \
                    size    18892400
        revision    1

        livecheck.url https://github.com/scipy/scipy/tree/maintenance/1.4.x/doc/release
        livecheck.regex {title="(1\.4\.[0-9.-]+)-notes.rst"}

        # See https://trac.macports.org/ticket/60520
        compilers.allow_arguments_mismatch yes

    } elseif {${python.version} == 36} {

        github.setup scipy scipy 1.5.4 v
        checksums   rmd160  9668f24955efe3e955a462459369162895fff936 \
                    sha256  91366228947f17c97c422fafac4e5e6cc217e3732716ed2e75757d822c427829 \
                    size    19838989
        revision    1

        livecheck.url https://github.com/scipy/scipy/tree/maintenance/1.5.x/doc/release
        livecheck.regex {title="(1\.5\.[0-9.-]+)-notes.rst"}

        # See https://trac.macports.org/ticket/60520
        compilers.allow_arguments_mismatch yes

    } else {

        # Python 3.7 and newer
        PortGroup          legacysupport 1.1

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

        # current release, which uses Boost
        PortGroup           boost 1.0

        python.pep517       no

        depends_build-append port:py${python.version}-pythran
        depends_lib-append  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}

        github.tarball_from releases

        if {${python.version} == 37} {

            boost.version       1.71

            github.setup scipy scipy 1.7.3 v
            checksums   rmd160  fed96617a5baeaa41571fd0ea39b68091d897bab \
                        sha256  ab5875facfdef77e0a47d5fd39ea178b58e60e454a4c85aa1e52fcb80db7babf \
                        size    36102562
            revision    0

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

            # 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__.37.diff

            livecheck.url https://github.com/scipy/scipy/tree/maintenance/1.7.x/doc/release
            livecheck.regex {title="(1\.7\.[0-9.-]+)-notes.rst"}

        } else {

            boost.version       1.76

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

            # 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

        }
    }

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

    depends_build-append    port:swig-python

    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
    }
}
