# 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")
load("@fbsource//xplat/executorch/extension/pybindings:pybindings.bzl", "ATEN_MODULE_DEPS", "MODELS_ATEN_OPS_ATEN_MODE_GENERATED_LIB", "MODELS_ATEN_OPS_LEAN_MODE_GENERATED_LIB", "PORTABLE_MODULE_DEPS", "executorch_pybindings")

oncall("executorch")

# Export this so the internal fb/ subdir can create pybindings with custom internal deps
# without forking the pybinding source.
runtime.export_file(
    name = "pybindings.cpp",
    visibility = ["//executorch/extension/pybindings/..."],
)

# cxx_python_extension kwarg 'types' can't take export_file rules directly and we need to rename the .pyi
# file to match the lib anyway, so we just expose the file like this and then have genrules consume and
# rename it before passing it to executorch pybindings.
runtime.filegroup(
    name = "pybinding_types",
    srcs = ["pybindings.pyi"],
    visibility = ["//executorch/extension/pybindings/..."],
)

# In order to have pyre recognize the pybindings module, the name of the .pyi must exactly match the
# name of the lib. To avoid copy pasting the pyi file in tree a whole bunch of times we use genrules
# to do it for us
runtime.genrule(
    name = "pybindings_types_gen",
    srcs = [":pybinding_types"],
    outs = {
        "aten_lib.pyi": ["aten_lib.pyi"],
        "core.pyi": ["core.pyi"],
        "_portable_lib.pyi": ["_portable_lib.pyi"],
    },
    cmd = "cp $(location :pybinding_types)/* $OUT/_portable_lib.pyi && cp $(location :pybinding_types)/* $OUT/aten_lib.pyi && cp $(location :pybinding_types)/* $OUT/core.pyi",
    visibility = ["//executorch/extension/pybindings/..."],
)

executorch_pybindings(
    compiler_flags = ["-std=c++17"],
    cppdeps = PORTABLE_MODULE_DEPS,
    python_module_name = "core",
    types = ["//executorch/extension/pybindings:pybindings_types_gen[core.pyi]"],
    visibility = ["PUBLIC"],
)

executorch_pybindings(
    compiler_flags = ["-std=c++17"],
    cppdeps = PORTABLE_MODULE_DEPS + MODELS_ATEN_OPS_LEAN_MODE_GENERATED_LIB,
    # Give this an underscore prefix because it has a pure python wrapper.
    python_module_name = "_portable_lib",
    types = ["//executorch/extension/pybindings:pybindings_types_gen[_portable_lib.pyi]"],
    visibility = ["PUBLIC"],
)

executorch_pybindings(
    compiler_flags = ["-std=c++17"],
    cppdeps = ATEN_MODULE_DEPS + MODELS_ATEN_OPS_ATEN_MODE_GENERATED_LIB,
    python_module_name = "aten_lib",
    types = ["//executorch/extension/pybindings:pybindings_types_gen[aten_lib.pyi]"],
    visibility = ["PUBLIC"],
)

runtime.python_library(
    name = "portable_lib",
    srcs = ["portable_lib.py"],
    visibility = [
        "//executorch/exir/...",
        "//executorch/runtime/...",
        "@EXECUTORCH_CLIENTS",
    ],
    deps = [
        ":_portable_lib",
        "//executorch/exir:_warnings",
    ],
)
