load("@prelude//platforms:defs.bzl", "host_configuration")

_DEPS = [
    "//lib/human-bytes:human-bytes",
    "//third-party:mycelium-bitfield",
    "//third-party:cfg-if",
] + select({
    "prelude//cpu/constraints:riscv64": ["//lib/riscv:riscv"],
    "DEFAULT": [],
})

rust_library(
    name = "mem-core",
    srcs = glob(["src/**/*.rs"]),
    deps = _DEPS,
    visibility = ["PUBLIC"],
)

# Tests live in `tests/` as out-of-crate integration tests (not inline `#[cfg(test)]`
# modules) so they can depend on the std-backed `mem-testkit` harness without forming a
# dependency cycle through `mem-core`. Each is its own test crate; they see only the
# public API.
_TEST_DEPS = [
    ":mem-core",
    "//lib/mem-testkit:mem-testkit",
    "//lib/human-bytes:human-bytes",
    "//third-party:proptest",
    "//third-party:test-log",
    "//third-party:tracing-subscriber",
]

_TEST_ENV = {
    # miri blocks getcwd so we need to disable proptest's getcwd-based failure persistence.
    "PROPTEST_DISABLE_FAILURE_PERSISTENCE": "true",
}

rust_test(
    name = "address_tests",
    srcs = ["tests/address.rs"],
    crate_root = "tests/address.rs",
    deps = _TEST_DEPS,
    env = _TEST_ENV,
    target_compatible_with = [host_configuration.os, host_configuration.cpu],
    visibility = ["PUBLIC"],
)

rust_test(
    name = "address_range_tests",
    srcs = ["tests/address_range.rs"],
    crate_root = "tests/address_range.rs",
    deps = _TEST_DEPS,
    env = _TEST_ENV,
    target_compatible_with = [host_configuration.os, host_configuration.cpu],
    visibility = ["PUBLIC"],
)

rust_test(
    name = "memory_attributes_tests",
    srcs = ["tests/memory_attributes.rs"],
    crate_root = "tests/memory_attributes.rs",
    deps = _TEST_DEPS,
    env = _TEST_ENV,
    target_compatible_with = [host_configuration.os, host_configuration.cpu],
    visibility = ["PUBLIC"],
)

rust_test(
    name = "physmap_tests",
    srcs = ["tests/physmap.rs"],
    crate_root = "tests/physmap.rs",
    deps = _TEST_DEPS,
    env = _TEST_ENV,
    target_compatible_with = [host_configuration.os, host_configuration.cpu],
    visibility = ["PUBLIC"],
)
