load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
load("@fbcode_macros//build_defs:cpp_python_extension.bzl", "cpp_python_extension")
# 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.
fbcode_target(_kind = runtime.export_file,
    name = "pybindings.cpp",
    visibility = ["//executorch/extension/pybindings/..."],
)

fbcode_target(_kind = runtime.export_file,
    name = "pybindings_cuda.h",
    visibility = ["//executorch/extension/pybindings/..."],
)

fbcode_target(_kind = runtime.export_file,
    name = "pybindings_dlpack.h",
    visibility = ["//executorch/extension/pybindings/..."],
)

fbcode_target(_kind = runtime.export_file,
    name = "pybindings_executorch_result.h",
    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.
fbcode_target(_kind = 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
fbcode_target(_kind = runtime.genrule,
    name = "pybindings_types_gen",
    srcs = [":pybinding_types"],
    outs = {
        "aten_lib.pyi": ["aten_lib.pyi"],
        "core.pyi": ["core.pyi"],
        "_C.pyi": ["_C.pyi"],
    },
    cmd = "cp $(location :pybinding_types)/* $OUT/_C.pyi && cp $(location :pybinding_types)/* $OUT/aten_lib.pyi && cp $(location :pybinding_types)/* $OUT/core.pyi",
    visibility = ["//executorch/extension/pybindings/..."],
)

fbcode_target(_kind = executorch_pybindings,
    cppdeps = PORTABLE_MODULE_DEPS,
    python_module_name = "core",
    types = ["//executorch/extension/pybindings:pybindings_types_gen[core.pyi]"],
    visibility = ["PUBLIC"],
)

fbcode_target(_kind = executorch_pybindings,
    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 = "_C",
    types = ["//executorch/extension/pybindings:pybindings_types_gen[_C.pyi]"],
    visibility = ["PUBLIC"],
)

fbcode_target(_kind = executorch_pybindings,
    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]"],
    use_aten = True,
    visibility = ["PUBLIC"],
)

fbcode_target(_kind = runtime.python_library,
    name = "portable_lib",
    srcs = ["portable_lib.py"],
    visibility = ["PUBLIC"],
    deps = [
        ":_C",
    ],
)

# Header-only library that provides PyDataLoader for external pybinding extensions.
# This allows external libraries (like PTEZ) to create custom data loaders that can
# be passed to _load_for_executorch_from_data_loader().
fbcode_target(
    _kind = cpp_library,
    name = "data_loader_types",
    headers = ["pybindings_data_loader.h"],
    exported_deps = [
        "//executorch/runtime/core:core",
    ],
    visibility = ["PUBLIC"],
)

# Python extension that registers the PyDataLoader type.
# This allows external libraries to create PyDataLoader instances without
# importing the full core pybindings.
fbcode_target(
    _kind = cpp_python_extension,
    name = "data_loader",
    srcs = ["pybindings_data_loader.cpp"],
    headers = [
        "pybindings_data_loader.h",
    ],
    base_module = "executorch.extension.pybindings",
    deps = [
        ":data_loader_types",
    ],
    external_deps = [
        "pybind11",
    ],
    visibility = ["PUBLIC"],
)
