# -*- 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.6.2 v
checksums               rmd160  0b81720965bb42ae53ea0a981f7fe8fd7bcd1023 \
                        sha256  f1c101da8046211d5e837b8ddadf3a5eea5d731c0d1d791809431a86b1e2741e \
                        size    21926080
revision                1

name                    py-scipy
platforms               darwin
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

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    0

        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's 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    0

        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    0

        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 {

        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}

    }

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

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

    depends_test-append     port:py${python.version}-py \
                            port:py${python.version}-pytest

    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

    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 description "Use MacPorts ATLAS libraries" {
        depends_lib-append  port:atlas
    }

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

    # 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]} {
        default_variants +openblas
    }

    if {[variant_isset atlas]} {
        # use MacPorts atlas
        build.env-append    OPENBLAS=None \
                            ATLAS=${prefix}/lib \
                            LAPACK=${prefix}/lib \
                            BLAS=${prefix}/lib
        destroot.env-append OPENBLAS=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
        destroot.env-append OPENBLAS=${prefix}/lib \
                            ATLAS=None
    } 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
    }
}
