load("@llvm-project//llvm:lit_test.bzl", "lit_test", "package_path")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")

package(
    # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
    licenses = ["notice"],
)

[
    lit_test(
        name = "%s.test" % src,
        srcs = [src],
        data = [
            "lit.cfg.py",
            "lit.site.cfg.py",
            "//tensorflow/compiler/xla/mlir_hlo:mlir-hlo-opt",
            "@llvm-project//llvm:FileCheck",
            "@llvm-project//llvm:count",
            "@llvm-project//llvm:not",
            "@llvm-project//mlir:libmlir_c_runner_utils.so",
            "@llvm-project//mlir:libmlir_runner_utils.so",
            "@llvm-project//mlir:mlir-cpu-runner",
        ],
        # copybara:uncomment driver = "@llvm-project//mlir:run_lit",
        tags = [
            "nomsan",  # The execution engine doesn't work with msan, see b/248097619.
        ],
        deps = ["@pypi_lit//:pkg"],
    )
    for src in glob(["**/*.mlir"])
]

cc_library(
    name = "capi_test",
    srcs = ["capi_test.c"],
    deps = [
        "//tensorflow/compiler/xla/mlir_hlo:CAPI",
    ],
)

build_test(
    name = "capi_build_test",
    testonly = True,
    targets = ["capi_test"],
)

# Generates lit config input file by applying path placeholder substitutions
# similar to the configure_lit_site_cfg CMake macro.
expand_template(
    name = "lit_site_cfg_py",
    testonly = True,
    out = "lit.site.cfg.py",
    substitutions = {
        "@LIT_SITE_CFG_IN_HEADER@": "# Autogenerated, do not edit.",
        "@LLVM_TOOLS_DIR@": package_path("@llvm-project//llvm:BUILD"),
        "@SHLIBEXT@": ".so",
        "@ENABLE_SHARED@": "1",
        "@ENABLE_ASSERTIONS@": "1",
        "@MLIR_LIB_DIR@": package_path("@llvm-project//mlir:BUILD"),
        "@MLIR_BINARY_DIR@": package_path("@llvm-project//mlir:BUILD"),
        # Separate '@' (reference to main repo) to avoid confusing copybara.
        "@MLIR_HLO_TOOLS_DIR@": package_path("@" + "//tensorflow/compiler/xla/mlir_hlo:BUILD"),
        "@MLIR_HLO_SOURCE_DIR@": package_path("@" + "//tensorflow/compiler/xla/mlir_hlo:BUILD"),
    },
    template = "lit.site.cfg.py.in",
    visibility = ["//visibility:private"],
)
