# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4

PortSystem              1.0
PortGroup               python 1.0
PortGroup               github 1.0
PortGroup               compilers 1.0

name                    py-numpy
categories-append       math
license                 BSD
platforms               darwin
maintainers             {michaelld @michaelld} openmaintainer
description             The core utilities for the scientific library scipy for Python
long_description        ${description}

github.setup            numpy numpy 1.16.3 v
checksums               rmd160 98918157c1577c289c9d0d7802a244c1a3177d5e \
                        sha256 669c2cc5499ac6e2b67210cea1bb523f6cd6e5eaf31a20fc8e58d3dc6374428b \
                        size   4671871
revision                0

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         26 27 33 34 35 36 37

# http://trac.macports.org/ticket/34562
python.consistent_destroot yes

# TODO: Fix the +universal variant.  This port is a fairly deep dependency and needs to be able to support +universal.
# https://trac.macports.org/ticket/48263
#universal_variant no

if {${name} ne ${subport}} {
    if {${python.version} == 26 || ${python.version} == 33} {
        github.setup        numpy numpy 1.11.3 v
        checksums           rmd160 8cc323d5944301aefa1f66f17d4af14c364c6b32 \
                            sha256 dbdda9104c7dbc2d8ae691367a2bb1815a62598c5278097b4eee035b85abf6a1 \
                            size   4387593
        patchfiles-append   patch-numpy_core_setup.py.26.diff \
                            patch-numpy_distutils_fcompiler_gnu.py.26.diff \
                            patch-numpy_tests_test_scripts.py.26.diff \
                            patch-fcompiler_g95.26.diff \
                            patch-f2py_setup.py.diff
        post-patch {
            reinplace "s|@@MPORTS_PYTHON@@|${python.bin}|" \
                ${worksrcpath}/numpy/f2py/setup.py
        }
    } else {
        patchfiles-append   patch-numpy_core_setup.py.diff \
                            patch-numpy_tests_test_scripts.py.diff \
                            patch-fcompiler_g95.diff
#                            patch-numpy_distutils_fcompiler_gnu.py.diff 
    }
    compilers.setup         -clang -gcc44 -gcc45 \
                            -gcc46 -gcc47 -gcc48 -g95 clang37

    depends_lib-append      port:fftw-3 \
                            port:py${python.version}-nose \
                            port:py${python.version}-cython

    pre-build {
        if {[vercmp [macports_version] 2.5.99] >= 0} {
        build.env-append        ARCHFLAGS=[get_canonical_archflags ld]
        } else {
        build.env-append        ARCHFLAGS="[get_canonical_archflags ld]"
        }
    }

    pre-destroot {
        if {[vercmp [macports_version] 2.5.99] >= 0} {
        destroot.env-append     ARCHFLAGS=[get_canonical_archflags ld]
        } else {
        destroot.env-append     ARCHFLAGS="[get_canonical_archflags ld]"
        }
    }

    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 polyfit. see also:
    # https://trac.macports.org/ticket/56954
    # https://github.com/numpy/numpy/issues/12230
    if {![variant_isset atlas] &&
        ![variant_isset openblas]} {
        default_variants +openblas
    }

    # when using ATLAS (whether by default or specified by the user via
    # the +atlas variant) ...
    if {[variant_isset atlas]} {

        # when using non-Apple GCC for universal install, it can
        # create binaries only for the native OS architecture, at
        # either 32 or 64 bits.  Restrict the supported archs
        # accordingly.
        if {${os.arch} eq "i386"} {
            supported_archs         i386 x86_64
        } elseif {${os.arch} eq "powerpc"} {
            supported_archs         ppc ppc64
        }

        # force LDFLAGS for correct linking of the linalg module
        # for non-Apple GCC compilers
        patchfiles-append       patch-numpy_linalg_setup.py.diff

        pre-fetch {
            # numpy 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.\
numpy needs a fortran enabled atlas. Please rebuild atlas without the +nofortran\
variant."
                }
            }
        }

        # 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

    } elseif {[variant_isset openblas]} {

        # use MacPorts OpenBLAS
        build.env-append    OPENBLAS=${prefix}/lib \
                            ATLAS=None
        destroot.env-append OPENBLAS=${prefix}/lib \
                            ATLAS=None
        # force LDFLAGS for correct linking of the modules
        # for non-Apple GCC compilers
        patchfiles-append   patch-numpy_linalg_setup.py.diff

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

    platform darwin 8 {
        patchfiles-append patch-Tiger-without-dflcn-header.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
        }

        if {[variant_isset atlas]} {
            # We must link against libSatlas or libTatlas, not libAtlas
            if {[file exists ${prefix}/lib/libtatlas.dylib]} {
                reinplace -E \
                    "s|_lib_atlas = \\\['atlas'\\\]|_lib_atlas = \\\['tatlas'\\\]|" \
                    ${worksrcpath}/numpy/distutils/system_info.py
            } elseif {[file exists ${prefix}/lib/libsatlas.dylib]} {
                reinplace -E \
                    "s|_lib_atlas = \\\['atlas'\\\]|_lib_atlas = \\\['satlas'\\\]|" \
                    ${worksrcpath}/numpy/distutils/system_info.py
            } else {
                return -code error "Unable to find Atlas dylibs. Bailing out."
            }
        }
    }

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

    # 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_isset universal]} {
            if {[variant_isset atlas]} {
                # uses fortran as a linker, which does not handle multiple -arch values properly
                return 1
            }
            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
        }
    }

    livecheck.type        none
} else {
    github.livecheck.regex  {([\d+(?:.\d+)*"]+)}
}