# Any targets that should be shared between fbcode and xplat must be defined in
# targets.bzl. This file can contain fbcode-only targets.

load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")

oncall("executorch")

# TODO: this is a placeholder to support internal fbcode build. We should add the coreml backend target properly.
runtime.python_library(
    name = "coreml",
    visibility = [
        "@EXECUTORCH_CLIENTS",
    ],
)

runtime.python_library(
    name = "backend",
    srcs = glob([
        "compiler/*.py",
        "logging.py",
    ]),
    visibility = [
        "@EXECUTORCH_CLIENTS",
    ],
    deps = [
        "fbsource//third-party/pypi/coremltools:coremltools",
        ":executorchcoreml",
        "//executorch/exir/backend:backend_details",
        "//executorch/exir/backend:compile_spec_schema",
    ],
)

runtime.python_library(
    name = "partitioner",
    srcs = glob([
        "partition/*.py",
        "logging.py",
    ]),
    visibility = [
        "@EXECUTORCH_CLIENTS",
    ],
    deps = [
        "fbsource//third-party/pypi/coremltools:coremltools",
        ":backend",
        "//caffe2:torch",
        "//executorch/exir:lib",
        "//executorch/exir/backend:compile_spec_schema",
        "//executorch/exir/backend:partitioner",
        "//executorch/exir/backend:utils",
    ],
)

runtime.python_library(
    name = "quantizer",
    srcs = glob([
        "quantizer/*.py",
    ]),
    visibility = [
        "@EXECUTORCH_CLIENTS",
    ],
)

runtime.python_library(
    name = "coreml_recipes",
    srcs = [
        "recipes/__init__.py",
        "recipes/coreml_recipe_provider.py"
    ],
    visibility = [
        "@EXECUTORCH_CLIENTS",
        "//executorch/export/...",
    ],
    deps = [
        "fbsource//third-party/pypi/coremltools:coremltools",
        ":coreml_recipe_types",
        ":backend",
        ":partitioner",
        ":quantizer",
        "//caffe2:torch",
        "//executorch/exir:lib",
        "//executorch/exir/backend:compile_spec_schema",
        "//executorch/exir/backend:partitioner",
        "//executorch/exir/backend:utils",
        "//executorch/export:lib",
    ],
)

runtime.python_library(
    name = "coreml_recipe_types",
    srcs = [
        "recipes/coreml_recipe_types.py",
    ],
    visibility = [
        "@EXECUTORCH_CLIENTS",
        "//executorch/export/...",
    ],
    deps = [
        "//executorch/export:recipe",
    ],
)

runtime.cxx_python_extension(
    name = "executorchcoreml",
    srcs = [
        "runtime/inmemoryfs/inmemory_filesystem.cpp",
        "runtime/inmemoryfs/inmemory_filesystem_py.cpp",
        "runtime/inmemoryfs/inmemory_filesystem_utils.cpp",
        "runtime/inmemoryfs/memory_buffer.cpp",
        "runtime/inmemoryfs/memory_stream.cpp",
        "runtime/inmemoryfs/reversed_memory_stream.cpp",
        "runtime/util/json_util.cpp",
    ],
    headers = glob([
        "runtime/inmemoryfs/**/*.hpp",
    ]),
    base_module = "executorch.backends.apple.coreml",
    compiler_flags = [
        "-std=c++17",
    ],
    preprocessor_flags = [
        "-Iexecutorch/backends/apple/coreml/runtime/util",
    ],
    types = [
        "executorchcoreml.pyi",
    ],
    visibility = [
        "//executorch/examples/apple/coreml/...",
        "@EXECUTORCH_CLIENTS",
    ],
    deps = [
        "fbsource//third-party/nlohmann-json:nlohmann-json",
        "fbsource//third-party/pybind11:pybind11",
    ],
)

runtime.python_test(
    name = "test",
    srcs = glob([
        "test/*.py",
    ]),
    deps = [
        "fbsource//third-party/pypi/coremltools:coremltools",
        "fbsource//third-party/pypi/pytest:pytest",
        ":partitioner",
        ":quantizer",
        ":coreml_recipes",
        "//caffe2:torch",
        "//pytorch/vision:torchvision",
        "fbsource//third-party/pypi/scikit-learn:scikit-learn",
    ],
)
