# -*- 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             2.3.3
revision            0

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  5c79d0e70b6fe9168f788357cda05c2fb1a8bc20 \
                    sha256  ddc7c39727ba62b80dfdbedf400d1c10ddfa8eefbd7ec8dcb118be8b56d31029 \
                    size    20576648

compiler.c_standard 2011
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-fcompiler_g95.diff \
                    patch-pocketfft-PR29768.diff

    if {${python.version} == 39} {
        version     2.0.2
        revision    0
        checksums   rmd160  0037d601479123b2a02788c0c3b6ccf03bbdcf75 \
                    sha256  883c987dee1880e2a864ab0dc9892292582510604156762362d9326444636e78 \
                    size    18902015
        patchfiles-append   patch-numpy-tests-PY39.diff \
                            patch-missing-include-PY39.diff
    } elseif {${python.version} == 310} {
        version     2.2.6
        revision    0
        checksums   rmd160  3de77b0a3e356425ee6320009e3c0133190f540e \
                    sha256  e29554e2bef54a90aa5cc07da6ce955accb83f21ab5de01a62c8478897b264fd \
                    size    20276440
        patchfiles-append   patch-numpy-tests-PY310.diff
    } else {
        patchfiles-append   patch-numpy-tests.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

    post-patch {
        reinplace "s|@@PYVER@@|${python.branch}|g" \
            ${worksrcpath}/numpy/_core/tests/test_cython.py \
            ${worksrcpath}/numpy/tests/test_configtool.py \
            ${worksrcpath}/numpy/_core/tests/test_limited_api.py

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