load("@prelude//platforms:defs.bzl", "host_configuration")
load("//build:bench.bzl", "rust_benchmark")
load("//build:loom.bzl", "rust_loom_test")

_DEPS = [
    "//lib/util:util",
    "//lib/cpu-local:cpu-local",
    "//lib/spin:spin",
    "//lib/fastrand:fastrand",
    "//lib/arrayvec:arrayvec",
    "//third-party:cordyceps",
    "//third-party:static_assertions",
    "//third-party:cfg-if",
    "//third-party:mycelium-bitfield",
    "//third-party:tracing",
    "//third-party:bitflags",
    "//third-party:pin-project",
    "//third-party:futures",
    "//third-party:maitake-sync"
]

_TEST_DEPS = [
    "//third-party:tracing-subscriber",
    "//third-party:lazy_static",
    "//third-party:criterion",
]

rust_library(
    name = "kasync",
    srcs = glob(["src/**/*.rs"]),
    features = select({
        "constraints//:env[kernel]": ["unwind", "counters"],
        "DEFAULT": [],
    }),
    deps = _DEPS + select({
        "constraints//:env[kernel]": ["//sys/panic-unwind:panic-unwind"],
        "DEFAULT": [],
    }),
    tests = [":kasync_tests", ":kasync_loom_tests", ":kasync_benchmarks"],
    visibility = ["PUBLIC"],
)

rust_test(
    name = "kasync_tests",
    srcs = glob(["src/**/*.rs"]),
    crate = "kasync",
    deps = _DEPS + _TEST_DEPS,
    features = ["counters"],
    target_compatible_with = [host_configuration.os, host_configuration.cpu],
    visibility = ["PUBLIC"],
)

rust_loom_test(
    name = "kasync_loom_tests",
    srcs = glob(["src/**/*.rs"]),
    crate = "kasync",
    deps = _DEPS + _TEST_DEPS + ["//third-party:loom"],
    features = ["counters"],
)

# Host-only rebuild of kasync with the `__bench` feature enabled, which
# exposes `kasync::test_util` (used by criterion benches).
rust_library(
    name = "kasync_bench",
    srcs = glob(["src/**/*.rs"]),
    crate = "kasync",
    features = ["__bench"],
    deps = _DEPS,
    target_compatible_with = [host_configuration.os, host_configuration.cpu],
    visibility = ["PUBLIC"],
)

rust_benchmark(
    name = "kasync_benchmarks",
    srcs = glob(["benches/*.rs"]),
    crate_root = "./benches/spawn.rs",
    deps = [
        ":kasync_bench",
        "//lib/fastrand:fastrand",
        "//third-party:criterion",
        "//third-party:lazy_static",
    ],
    visibility = ["PUBLIC"],
)
