# -*- 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           github 1.0
PortGroup           python 1.0

github.setup        apple tensorflow_macos 0.1 v alpha3
name                py-${github.project}
version             ${github.version}.${github.tag_suffix}
revision            0

platforms           darwin
categories-append   lang
license             restrictive/distributable
maintainers         nomaintainer

description         Mac-optimized TensorFlow and TensorFlow Addons

long_description    Hardware-accelerated TensorFlow and TensorFlow \
                    Addons for macOS 11.0+. Native hardware \
                    acceleration is supported on M1 Macs and \
                    Intel-based Macs through Apple’s ML Compute \
                    framework.

# https://github.com/apple/tensorflow_macos/blob/master/scripts/download_and_install.sh
distfiles           ${github.project}-${github.version}${extract.suffix}:tarball \
                    ${github.project}-${github.version}${github.tag_suffix}${extract.suffix}:releases

master_sites        ${github.homepage}/tarball/${git.branch}:tarball \
                    github.master_sites ${github.homepage}/releases/download/${git.branch}:releases

checksums           [lindex [split [lindex ${distfiles} 0] :] 0] \
                    rmd160  975a2ee2508df2fe801b841d3f6f8a0172854292 \
                    sha256  623237c06941d26f52280535c0139c9d58fc214e0378b4513803a65bfda48ff6 \
                    size    8095 \
                    [lindex [split [lindex ${distfiles} 1] :] 0] \
                    rmd160  812afc151dd87abb657df6d8e66ad8681c91a3bd \
                    sha256  80807f8b0fc4a98ffa0b7395304fa61239f246a84f1eedbca70a5a1847d4dead \
                    size    376699291

extract.cmd         ${portutil::autoconf::tar_command}
extract.pre_args    -xzf
extract.post_args

if {${os.platform} eq "darwin" && ${os.major} < 20} {
    known_fail      yes
    pre-fetch {
        ui_error "TensorFlow with ML Compute acceleration is only available \
            on macOS 11.0 and later."
        error {unsupported platform}
    }
}

python.versions 38

if {${name} ne ${subport}} {
    conflicts       py${python.version}-tensorflow \
                    py${python.version}-tensorflow1 \
                    py${python.version}-tensorflow-addons

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

    depends_run-append \
                    port:py${python.version}-absl \
                    port:py${python.version}-astunparse \
                    port:py${python.version}-flatbuffers \
                    port:py${python.version}-gast \
                    port:py${python.version}-grpcio \
                    port:py${python.version}-h5py \
                    port:py${python.version}-keras_preprocessing \
                    port:py${python.version}-numpy \
                    port:py${python.version}-opt_einsum \
                    port:py${python.version}-protobuf3 \
                    port:py${python.version}-tensorflow_estimator \
                    port:py${python.version}-scipy \
                    port:py${python.version}-tensorboard \
                    port:py${python.version}-termcolor \
                    port:py${python.version}-typing_extensions \
                    port:py${python.version}-wrapt \
                    port:py${python.version}-typeguard
    
    use_configure   no

    build {}

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

    destroot {
        foreach whl [glob -type f ${workpath}/${github.project}/${build_arch}/tensorflow*cp${python.version}*.whl] {
            system "${destroot.cmd} ${destroot.target} ${destroot.args} ${whl}"
        }
    }

    post-destroot {
        # avoid conflict with py${python.version}-tensorboard
        delete      ${destroot}${python.prefix}/bin/tensorboard \
                    ${destroot}${prefix}/bin/tensorboard-${python.branch}

        set github_project_path [glob -nocomplain ${workpath}/${github.author}-${github.project}-*]
        set docdir ${prefix}/share/doc/${subport}
        xinstall -d ${destroot}${docdir}
        xinstall -m 0644 -W ${github_project_path} LICENSE.txt README.md \
            ${destroot}${docdir}        
    }

    livecheck.type  none
}
