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

name                py-numpy
version             1.26.4
revision            3

# stealth update recipe, due to swith to PyPI
dist_subdir         ${name}/${version}_1

categories-append   math
license             BSD
maintainers         {michaelld @michaelld} {reneeotten @reneeotten}  openmaintainer
description         The core utilities for the scientific library scipy for Python
long_description    {*}${description}

homepage            https://numpy.org

checksums           rmd160  7f094022d62015d8ea78da78b37fd6f4f4e3076b \
                    sha256  2a02aba9ed12e4ac4eb3ea9421c420301a0c6460d9830d74a9df87efa4912010 \
                    size    15786129

compiler.c_standard 1999
compiler.cxx_standard 2017

# port builds with clang-1103.0.32.62 on 10.15 and clang-mp-17 on 10.9
# port fails with clang-1001.0.46.4 on 10.14 ; other failures are due to libomp and can thus not
# be investigated right now
compiler.blacklist-append {clang < 1100}

if {${name} ne ${subport}} {
    # the python PortGroup puts compiler names in build.env and destroot.env for
    # this to be done correctly, the following must be located before python.versions
    pre-build    { set_compilers }
    pre-destroot { set_compilers }
}

python.versions     39 310 311 312 313
python.pep517_backend meson

if {${name} ne ${subport}} {
    patchfiles      patch-numpy_tests_test_scripts.py.diff \
                    patch-arm64_min.diff \
                    patch-fcompiler_g95.diff \
                    patch-build_cython_path.diff \
                    patch-version-caps_pyproject.toml.diff

    # workaround for macOS version detection; do NOT check MACOSX_DEPLOYMENT_TARGET as that
    # will be set to 13.0 in MacPorts eventhough the macOS version is, for example, 13.3
    patchfiles-append \
                    patch_vendored-meson-meson-mesonbuild-dependencies-blas_lapack.py.diff

    # https://github.com/numpy/numpy/pull/27729
    patchfiles-append \
                    patch-threads.diff

    post-patch {
        # https://trac.macports.org/ticket/46392
        if {${os.arch} eq "powerpc"} {
            reinplace "s,'-O3','-O2'," \
                ${worksrcpath}/numpy/distutils/fcompiler/gnu.py
        }

        if { [use_wrapper] } {
            # Prepare wrappers
            file copy -force ${filespath}/wrapper-template \
                ${worksrcpath}/c-wrapper
            file copy -force ${filespath}/wrapper-template \
                ${worksrcpath}/f-wrapper
            file copy -force ${filespath}/wrapper-template \
                ${worksrcpath}/cxx-wrapper

            reinplace   "s|@@@|${configure.cc}|" ${worksrcpath}/c-wrapper
            reinplace   "s|---|\\\\.c|" ${worksrcpath}/c-wrapper
            reinplace   "s|&&&|${prefix}|" ${worksrcpath}/c-wrapper

            reinplace   "s|@@@|${configure.cxx}|" ${worksrcpath}/cxx-wrapper
            reinplace   "s#---#(\\\\.C|\\\\.cpp|\\\\.cc)#" \
                ${worksrcpath}/cxx-wrapper
            reinplace   "s|&&&|${prefix}|" ${worksrcpath}/cxx-wrapper

            reinplace   "s|@@@|${configure.f90}|" ${worksrcpath}/f-wrapper
            reinplace   "s|---|\\\\.f|" ${worksrcpath}/f-wrapper
            reinplace   "s|&&&|${prefix}|" ${worksrcpath}/f-wrapper
        }
    }

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

    build.env-append    CYTHON=${prefix}/bin/cython-${python.branch}

    # do not allow fallback to (slow) NumPy-internal routines if no external BLAS/LAPACK libraries are found
    build.args-append -Csetup-args=-Dallow-noblas=false

    compilers.setup     -clang -gcc44 -gcc45 -gcc46 -gcc47 -gcc48 \
                        -gcc5 -gcc6 -gcc7 --g95

    # https://trac.macports.org/ticket/67136
    depends_run-append  port:py${python.version}-oldest-supported-numpy

    # NumPy (and SciPy) only support the Accelerate framework for macOS 13.3+
    # see: https://github.com/numpy/numpy/blob/v1.26.4/INSTALL.rst#macos
    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 NumPy 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
        # see: https://github.com/numpy/numpy/blob/v1.26.4/INSTALL.rst#macos
        if {${os.platform} eq "darwin" && (${os.major} > 22 || ${os.major} == 22 && ${os.minor} >= 4)} {
            default_variants +accelerate
        } else {
            default_variants +openblas
        }
    }

    if {![fortran_variant_isset]} {
        default_variants-append +gfortran
    }

    test.run        yes
    test.cmd        ${python.bin} runtests.py
    test.args       --no-build --mode=fast
    test.env-append CYTHON=${prefix}/bin/cython-${python.branch} \
                    PYTHONPATH=${destroot}${python.pkgd}

    depends_test-append \
                    port:py${python.version}-cffi \
                    port:py${python.version}-charset-normalizer \
                    port:py${python.version}-cython \
                    port:py${python.version}-hypothesis \
                    port:py${python.version}-mypy \
                    port:py${python.version}-typing_extensions \
                    port:py${python.version}-tz

    # determine if special wrapper values are needed for compiler names
    # a procedure is needed since variants from PortGroup compilers must
    # be evaluated before it can be determined if clang is being used
    proc use_wrapper {} {
        global configure.cc

        if {[variant_exists universal] && [variant_isset universal]} {
            if {![string match *clang* ${configure.cc}]} {
                # gcc compilers do not handle multiple -arch values properly
                return 1
            }
        }
        return 0
    }

    # set compiler names
    proc set_compilers {} {
        global configure.cc configure.cxx configure.fc worksrcpath

        if {[use_wrapper]} {
            # override values set in compilers PortGroup
            configure.cc  ${worksrcpath}/c-wrapper
            configure.cxx ${worksrcpath}/cxx-wrapper
            configure.fc  ${worksrcpath}/f-wrapper
            configure.f77 ${worksrcpath}/f-wrapper
            configure.f90 ${worksrcpath}/f-wrapper
        }
    }
}
