# -*- 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           github                         1.0
PortGroup           compilers                      1.0
PortGroup           compiler_blacklist_versions    1.0
PortGroup           xcode_workaround               1.0

name                py-tensorflow
version             2.4.1
revision            1
github.setup        tensorflow tensorflow ${version} v
platforms           darwin
supported_archs     x86_64

license             Apache-2
maintainers         {emcrisostomo @emcrisostomo} {jonesc @cjones051073} openmaintainer

description         TensorFlow helps the tensors flow
long_description    ${description}

homepage            https://www.tensorflow.org/

github.livecheck.regex  {([0-9.]+)}

checksums           rmd160  f322fcdc8ba71e48a33bec80a84f47e6f8b6c314 \
                    sha256  69ab8a0ab585dd7d8d489227f0c0dd9dacf96f3e8eb844aa27f1198884773ebe \
                    size    53266547

set min_darwin 16
if { ${os.major} < ${min_darwin} } {
    known_fail yes
    # https://trac.macports.org/ticket/58518
    pre-fetch {
        ui_error "${name} ${version} requires Darwin ${min_darwin} or newer."
        return -code error "incompatible macOS version"
    }
}

compiler.cxx_standard 2014
compiler.blacklist-append {clang < 1001}

# https://trac.macports.org/ticket/59192
xcode_workaround.type                  avoid_xcode_compiler
xcode_workaround.os_versions           18 19
xcode_workaround.fixed_xcode_version   11.4

# Supported python versions
python.versions  37 38 39

# https://trac.macports.org/ticket/61279
patchfiles-append fix-missing-static-symbols.patch

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

    PortGroup bazel 1.0
    # See https://www.tensorflow.org/install/source#macos for tested bazel versions.
    bazel.version   3.1

    # TF versions 1 and 2 cannot be installed at once
    conflicts py${python.version}-tensorflow1

    # Conflicts with tensorflow_macos
    if {${python.version} eq 38} {
        conflicts-append py${python.version}-tensorflow_macos
    }

    variant mkl description {Enable Intel Math Kernel Library support} {
        bazel.extra_build_opts-append --config=mkl
        depends_lib-append port:py${python.version}-mkl
    }
    # Enable MKL by default on 10.15 and newer.
    #if {${os.major} >= 19} {
    #    default_variants-append +mkl
    #}

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

    depends_lib-append \
        port:py${python.version}-absl \
        port:py${python.version}-astor \
        port:py${python.version}-astunparse \
        port:py${python.version}-flatbuffers \
        port:py${python.version}-gast \
        port:py${python.version}-google-pasta \
        port:py${python.version}-grpcio \
        port:py${python.version}-keras \
        port:py${python.version}-numpy \
        port:py${python.version}-opt_einsum \
        port:py${python.version}-protobuf3 \
        port:py${python.version}-six \
        port:py${python.version}-tensorboard \
        port:py${python.version}-tensorflow_estimator \
        port:py${python.version}-termcolor \
        port:py${python.version}-wheel \
        port:py${python.version}-wrapt

    depends_run-append \
        port:py${python.version}-tensorflow-addons

    use_configure yes
    # Options passed to configure script via env vars
    configure.env-append PYTHON_LIB_PATH=${python.prefix}/lib/python${python.branch}/site-packages
    configure.env-append PYTHON_BIN_PATH=${prefix}/bin/python${python.branch}

    bazel.build_target     "//tensorflow/tools/pip_package:build_pip_package"
    bazel.post_build_cmd   ./bazel-bin/tensorflow/tools/pip_package/build_pip_package [list $workpath]
    bazel.extra_build_opts-append --config=opt

    #bazel.limit_build_jobs no

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

    post-destroot {
        file delete ${destroot}${python.prefix}/bin/tensorboard
        file delete ${destroot}${prefix}/bin/tensorboard-${python.branch}
    }

    livecheck.type  none

}
