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

name                py-tensorflow1
version             1.15.5
revision            0
github.setup        tensorflow tensorflow ${version} v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
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/

livecheck.type      none

checksums           rmd160  1807544e59cb25028af0bfec45ddffd1f861cc4e \
                    sha256  2430b0a48e049078e249190d31d354ecdf565b9e1fb39bde3b50a4c06ac2f3fa \
                    size    46213601

supported_archs     x86_64

set min_darwin 14
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.blacklist-append {clang < 1200.0.32.27} {macports-clang-[4-9].0}
compiler.cxx_standard 2014

patch.pre_args-replace  -p0 -p1

patchfiles-append Fix-build-failures-for-python-3.8.patch
if {${os.major} < 16} {
    # Work around for issues with clock_gettime(CLOCK_REALTIME, &ts);
    # https://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
    # Note bazel does not play nice with legacysupport, so this IS needed instead..
    patchfiles-append CLOCK_REALTIME-Older-OSX.patch
}

# https://github.com/tensorflow/tensorflow/pull/40654
# https://github.com/tensorflow/tensorflow/issues/40688
patchfiles-append patch-numpy-1.19-fix.diff

python.versions  39

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

    PortGroup bazel 1.0

    bazel.version   "0.25"

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

    # Conflicts with tensorflow-macos
    if { (${build_arch} eq {arm64} && (${python.version} eq 38 || ${python.version} eq 39))
        || (${build_arch} eq {x86_64} && ${python.version} eq 38) } {
        conflicts-append \
                    py${python.version}-tensorflow-macos
    }

    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}-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}-protobuf3 \
        port:py${python.version}-six \
        port:py${python.version}-tensorboard1 \
        port:py${python.version}-tensorflow_estimator1 \
        port:py${python.version}-termcolor \
        port:py${python.version}-wheel \
        port:py${python.version}-wrapt

    use_configure yes

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

    # Use correct python in bazel build
    bazel.python_version ${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 --config=opt

    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

}
