licenses(["notice"])  # Apache 2.0

package(
    default_visibility = ["//visibility:private"],
)

load("//tensorflow/compiler/aot:tfcompile.bzl", "tf_library")
load("//tensorflow:tensorflow.bzl", "tf_cc_test")
load("//tensorflow:tensorflow.bzl", "tf_cc_binary")

# Optional runtime utilities for use by code generated by tfcompile.
cc_library(
    name = "runtime",
    srcs = ["runtime.cc"],
    hdrs = ["runtime.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//tensorflow/core:framework_lite",
    ],
)

tf_cc_test(
    name = "runtime_test",
    srcs = ["runtime_test.cc"],
    deps = [
        ":runtime",
        "//tensorflow/core:framework",
        "//tensorflow/core:test",
        "//tensorflow/core:test_main",
    ],
)

# Don't depend on this directly; this is only used for the benchmark test
# generated by tf_library.
cc_library(
    name = "tf_library_test_main",
    testonly = 1,
    visibility = ["//visibility:public"],
    deps = ["//tensorflow/core:test_main"],
)

cc_library(
    name = "tfcompile_lib",
    srcs = [
        "codegen.cc",
        "compile.cc",
        "flags.cc",
    ],
    hdrs = [
        "codegen.h",
        "compile.h",
        "flags.h",
    ],
    deps = [
        ":embedded_protocol_buffers",
        ":runtime",  # needed by codegen to print aligned_buffer_bytes
        "//tensorflow/compiler/tf2xla",
        "//tensorflow/compiler/tf2xla:common",
        "//tensorflow/compiler/tf2xla:tf2xla_proto",
        "//tensorflow/compiler/tf2xla:tf2xla_util",
        "//tensorflow/compiler/tf2xla:xla_compiler",
        "//tensorflow/compiler/tf2xla/kernels:xla_cpu_only_ops",
        "//tensorflow/compiler/tf2xla/kernels:xla_ops",
        "//tensorflow/compiler/xla:shape_util",
        "//tensorflow/compiler/xla:statusor",
        "//tensorflow/compiler/xla:util",
        "//tensorflow/compiler/xla:xla_data_proto",
        "//tensorflow/compiler/xla/client:client_library",
        "//tensorflow/compiler/xla/client:compile_only_client",
        "//tensorflow/compiler/xla/service:compiler",
        "//tensorflow/compiler/xla/service/cpu:cpu_compiler",
        "//tensorflow/core:core_cpu_internal",
        "//tensorflow/core:framework_internal",
        "//tensorflow/core:lib",
        "//tensorflow/core:protos_all_cc",
    ],
)

tf_cc_test(
    name = "codegen_test",
    srcs = ["codegen_test.cc"],
    data = [
        "codegen_test_h.golden",
        "codegen_test_o.golden",
    ],
    deps = [
        ":tfcompile_lib",
        "//tensorflow/compiler/xla:shape_util",
        "//tensorflow/core:lib",
        "//tensorflow/core:test",
        "//tensorflow/core:test_main",
        "@llvm//:support",  # fixdeps: keep
        "@llvm//:x86_code_gen",  # fixdeps: keep
    ],
)

tf_cc_binary(
    name = "tfcompile",
    visibility = ["//visibility:public"],
    deps = [":tfcompile_main"],
)

cc_library(
    name = "tfcompile_main",
    srcs = ["tfcompile_main.cc"],
    visibility = ["//visibility:public"],
    deps = [
        ":tfcompile_lib",
        "//tensorflow/compiler/tf2xla:tf2xla_proto",
        "//tensorflow/compiler/tf2xla:tf2xla_util",
        "//tensorflow/compiler/xla/legacy_flags:debug_options_flags",
        "//tensorflow/compiler/xla/service:compiler",
        "//tensorflow/core:core_cpu",
        "//tensorflow/core:core_cpu_internal",
        "//tensorflow/core:framework",
        "//tensorflow/core:framework_internal",
        "//tensorflow/core:graph",
        "//tensorflow/core:lib",
        "//tensorflow/core:protos_all_cc",
    ],
)

# NOTE: Most end-to-end tests are in the "tests" subdirectory, to ensure that
# tfcompile.bzl correctly handles usage from outside of the package that it is
# defined in.

# A simple test of tf_library from a text protobuf, mostly to enable the
# benchmark_test.
tf_library(
    name = "test_graph_tfadd",
    testonly = 1,
    config = "test_graph_tfadd.config.pbtxt",
    cpp_class = "AddComp",
    graph = "test_graph_tfadd.pbtxt",
    tags = [
        "manual",
    ],
)

# A test of tf_library that includes a graph with an unknown op, but where
# the compilation works because the unknown op is not needed for the fetches.
tf_library(
    name = "test_graph_tfunknownop",
    testonly = 1,
    config = "test_graph_tfunknownop.config.pbtxt",
    cpp_class = "UnknownOpAddComp",
    graph = "test_graph_tfunknownop.pbtxt",
    tags = [
        "manual",
    ],
)

# A test of tf_library that includes a graph with an unknown op, but where
# the compilation works because the op between the unknown op and the
# fetches is a feed.
tf_library(
    name = "test_graph_tfunknownop2",
    testonly = 1,
    config = "test_graph_tfunknownop2.config.pbtxt",
    cpp_class = "UnknownOpAddComp",
    graph = "test_graph_tfunknownop.pbtxt",
    tags = [
        "manual",
    ],
)

# A test of tf_library that includes a graph with an unknown op, but where
# the compilation works because the unknown op is fed.
tf_library(
    name = "test_graph_tfunknownop3",
    testonly = 1,
    config = "test_graph_tfunknownop3.config.pbtxt",
    cpp_class = "UnknownOpAddComp",
    graph = "test_graph_tfunknownop.pbtxt",
    tags = [
        "manual",
    ],
)

# Utility library for benchmark binaries, used by the *_benchmark rules that are
# added by the tfcompile bazel macro.
cc_library(
    name = "benchmark",
    srcs = ["benchmark.cc"],
    hdrs = ["benchmark.h"],
    visibility = ["//visibility:public"],
    deps = [
        # The purpose of the benchmark library is to support building an aot
        # binary with minimal dependencies, to demonstrate small binary sizes.
        #
        # KEEP THE DEPENDENCIES MINIMAL.
        "//tensorflow/core:framework_lite",
    ],
)

cc_library(
    name = "benchmark_extra_android",
    tags = [
        "manual",
    ],
    visibility = ["//visibility:public"],
)

cc_library(
    name = "embedded_protocol_buffers",
    srcs = ["embedded_protocol_buffers.cc"],
    hdrs = ["embedded_protocol_buffers.h"],
    deps = [
        "//tensorflow/compiler/tf2xla:common",
        "//tensorflow/compiler/xla:statusor",
        "//tensorflow/compiler/xla:util",
        "//tensorflow/compiler/xla/service/llvm_ir:llvm_util",
        "//tensorflow/core:lib",
        "@llvm//:core",
        "@llvm//:execution_engine",
        "@llvm//:support",
        "@llvm//:target",
    ],
)

tf_cc_test(
    name = "benchmark_test",
    srcs = ["benchmark_test.cc"],
    tags = ["manual"],
    deps = [
        ":benchmark",
        ":test_graph_tfadd",
        "//tensorflow/core:test",
        "//tensorflow/core:test_main",
    ],
)

test_suite(
    name = "all_tests",
    tags = ["manual"],
    tests = [
        ":benchmark_test",
        ":codegen_test",
        ":runtime_test",
        ":test_graph_tfadd_test",
        ":test_graph_tfunknownop2_test",
        ":test_graph_tfunknownop3_test",
        ":test_graph_tfunknownop_test",
        "//tensorflow/compiler/aot/tests:all_tests",
    ],
)

exports_files([
    "benchmark_main.template",  # used by tf_library(...,gen_benchmark=True)
    "test.cc",  # used by tf_library(...,gen_test=True)
])

# -----------------------------------------------------------------------------

filegroup(
    name = "all_files",
    srcs = glob(
        ["**/*"],
        exclude = [
            "**/METADATA",
            "**/OWNERS",
        ],
    ),
    visibility = ["//tensorflow:__subpackages__"],
)
