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

name                py-mkl
if {${os.platform} eq "darwin" && ${os.major} < 19} {
    version         2019.0
    revision        1
} else {
    version         2023.2.1
    revision        0
}
supported_archs     x86_64

# https://software.intel.com/en-us/license/intel-simplified-software-license
license             Restrictive/Distributable

maintainers         {jonesc @cjones051073} openmaintainer

description         Math library for Intel and compatible processors
long_description    ${description}.

if {${configure.build_arch} ne "x86_64"} {
    known_fail      yes
    pre-fetch {
        ui_error "MKL is only available on Intel x86_64 architectures."
        error {unsupported platform}
    }
}

extract.suffix      .whl
extract.only

python.versions     310 311 312 313 314

# Intel only supports 10.12 and newer
if {${os.platform} eq "darwin" && ${os.major} <= 15} {
    known_fail yes
    pre-fetch {
        ui_error "${name} is not supported on this OS X release."
        return -code error "Unsupported OS X version"
    }
}

# add sub-ports for headers
foreach _py ${python.versions} {
    subport py${_py}-mkl-include {
        python.rootname     mkl_include
        supported_archs     noarch
    }
}

if {${name} ne ${subport}} {

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

    build {}

    homepage        https://pypi.org/project/${python.rootname}

    if {[string match "*-include" $subport]} {

        if {${os.platform} eq "darwin" && ${os.major} < 19} {

            master_sites    https://files.pythonhosted.org/packages/4f/49/c24113b33981a2c3e6915eb94f50c56ea61639963339e03eaed37787cc81/
            distname        mkl_include-${version}-py2.py3-none-macosx_10_12_intel.macosx_10_12_x86_64

            checksums       rmd160  097dd5bcbcc0a704e065cbdc629c7a10bf9e0b71 \
                            sha256  dd9e2224dcdbede569c996f971e663f64f184a432ccb01f2dceca768a77cb2b4 \
                            size    871122

        } else {

            python.pep517   no

            master_sites    https://files.pythonhosted.org/packages/89/d3/93eefe93873e725e41881856b9decd58f055313647b31aa611a62a236519/
            distname        mkl_include-${version}-py2.py3-none-macosx_10_15_x86_64.macosx_11_0_x86_64

            checksums       rmd160  01d7559ee69a50f0895bb1225f657527bfe2f268 \
                            sha256  367ef3fcc2cc2d74bee2a1bff11801d9638dabb15afc6ce8f1f0f80d5b5d0511 \
                            size    1061332

        }

        depends_lib-append  port:py${python.version}-mkl

    } else {

        supported_archs x86_64

        if {${os.platform} eq "darwin" && ${os.major} < 19} {

            master_sites    https://files.pythonhosted.org/packages/ac/1e/c713b011b90cd238023df1c0025130c40bc40870a46273d942e89114233c/
            distname        mkl-${version}-py2.py3-none-macosx_10_12_intel.macosx_10_12_x86_64

            checksums       rmd160  62011c74574b354c8996edfdd1d6b3d5e1aa2623 \
                            sha256  23c8e8ba2cac703d8bc357d2bf10519e91dc4371e7dd1decf461f70db20b9783 \
                            size    193800193
        } else {

            python.pep517   no

            master_sites    https://files.pythonhosted.org/packages/f3/77/5cfa3161c078bfc47cf60b97a8903eecbd610b022694a34136e2081b5f7d
            distname        mkl-${version}-py2.py3-none-macosx_10_15_x86_64.macosx_11_0_x86_64

            checksums       rmd160  260b7951c6f3b2542ab8c2c0ebf20a63d724c882 \
                            sha256  953a55a778d87cea9fd44f777c3b0a7a5804e9e810dc5f22852b2c4a42a79bcb \
                            size    187623390

        }

        depends_lib-append  port:libomp \
                            port:tbb

        post-destroot {
            foreach dlib [glob -directory ${destroot}${python.prefix}/lib *.dylib] {
                system "install_name_tool -add_rpath ${prefix}/lib         ${dlib} ; echo true"
                system "install_name_tool -add_rpath ${prefix}/lib/libomp  ${dlib} ; echo true"
                system "install_name_tool -add_rpath ${prefix}/libexec/tbb ${dlib} ; echo true"
            }
        }

    }

    destroot.cmd    pip-${python.branch}
    destroot.args          \
        --ignore-installed \
        --no-cache-dir     \
        --no-dependencies  \
        --root ${destroot} \
        ${distpath}/${distfiles}
    destroot.post_args
}
