# -*- 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
version             2019.0
revision            0
platforms           darwin
supported_archs     noarch

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

extract.suffix      .whl
extract.only

python.versions     27 35 36 37 38

# Intel only supports 10.12 and newer
if { ${os.major} <= 15 } {
    pre-fetch {
        ui_error "${name} is not supported on this OSX release."
        return -code error "Unsupported OSX version"
    }
}

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

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

    supported_archs x86_64

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

    build { }

    # strip pyXY from subport name
    set local_name [string replace ${subport} 0 4 ]

    homepage https://pypi.org/project/${local_name}

    if {[string match "*-include" $subport]} {
        
        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

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

        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

        depends_lib-append port:tbb port:libomp
        
        post-destroot {
            set PythonVersionWithDot [join [split ${python.version} ""] "."]
            set py_lib_root ${prefix}/Library/Frameworks/Python.framework/Versions/${PythonVersionWithDot}/lib
            foreach dlib [glob -directory ${destroot}${py_lib_root} *.dylib] {
                system "install_name_tool -add_rpath ${prefix}/lib        ${dlib}"
                system "install_name_tool -add_rpath ${prefix}/lib/libomp ${dlib}"
            }
        }

    }

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

    livecheck.url   ${homepage}
    livecheck.type  regex
    livecheck.regex "${local_name}\ (\\d+(\\.\\d+)+)"
}