# -*- 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               deprecated 1.0

deprecated.eol_version  yes

github.setup            scipy scipy 1.2.3 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from     tarball
checksums               rmd160 868076b4d88257a6b536f4c3a4e1e8230b587290 \
                        sha256 beb6123e91a0bdfba4557b6560be4899dac174a38c30876e5cfc60382ecb4ff7 \
                        size   18564471
revision                1

name                    py27-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

python.add_archflags    no
universal_variant       no

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

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-replace  -p0 -p1

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

depends_build-append    port:swig-python \
                        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}
destroot.env-append     PYTHONPATH=${compat_path}

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
}
