load("//build:split_debuginfo.bzl", "split_debuginfo")
load(":kcfg.bzl", "LOG_LEVEL")

# config_setting targets — use these in select() anywhere in the build graph.

LOG_LEVEL.config_setting(name = "log_level-error", value = "error")
LOG_LEVEL.config_setting(name = "log_level-warn",  value = "warn")
LOG_LEVEL.config_setting(name = "log_level-info",  value = "info")
LOG_LEVEL.config_setting(name = "log_level-debug", value = "debug")
LOG_LEVEL.config_setting(name = "log_level-trace", value = "trace")

_DEPS = [
    # internal libraries
    "//sys/loader-api:loader-api",
    "//sys/async:kasync",
    "//sys/cpus:cpus",
    "//lib/cpu-local:cpu-local",
    "//lib/trap:trap",
    "//lib/spin:spin",
    "//lib/unwind:unwind",
    "//lib/wavltree:wavltree",
    "//lib/fdt:fdt",
    "//lib/sharded-slab:sharded-slab",
    "//lib/test:test",
    "//lib/addr2line:addr2line",
    "//lib/uart-16550:uart-16550",
    "//lib/wast:wast",
    "//lib/fastrand:fastrand",
    "//lib/abort:abort",
    "//sys/panic-unwind:panic-unwind",
    "//lib/util:util",
    "//lib/mem-core:mem-core",
    "//lib/arrayvec:arrayvec",
    "//sys/backtrace:backtrace",
    # third-party dependencies
    "//third-party:rustc-demangle",
    "//third-party:log",
    "//third-party:cfg-if",
    "//third-party:talc",
    "//third-party:static_assertions",
    "//third-party:bitflags",
    "//third-party:xmas-elf",
    "//third-party:fallible-iterator",
    "//third-party:rand_chacha",
    "//third-party:rand",
    "//third-party:pin-project",
    "//third-party:smallvec",
    "//third-party:hashbrown",
    "//third-party:gimli",
    "//third-party:bumpalo",
    "//third-party:mycelium-bitfield",
    "//third-party:tracing",
    "//third-party:tracing-core",
    "//third-party:anyhow",
    "//third-party:futures",
    "//third-party:cordyceps",
    "//third-party:wasmparser",
    "//third-party:target-lexicon",
    "//third-party:cranelift-codegen",
    "//third-party:cranelift-frontend",
    "//third-party:cranelift-entity",
    "//third-party:wasmtime-slab",
    "//third-party:maitake-sync"
] + select({
    "prelude//cpu/constraints:riscv64": ["//lib/riscv:riscv"],
    "DEFAULT": [],
})

rust_binary(
    name = "kernel-unstripped",
    crate = "k23",
    srcs = glob(["src/**/*.rs"]),
    crate_root = "src/main.rs",
    default_target_platform = "//platforms:riscv64",
    target_compatible_with = ["prelude//cpu/constraints:riscv64"],
    incoming_transition = "//build:kernel",
    deps = _DEPS,
)

split_debuginfo(
    name = "kernel",
    binary = ":kernel-unstripped",
    default_target_platform = "//platforms:riscv64",
    target_compatible_with = ["prelude//cpu/constraints:riscv64"],
    visibility = ["PUBLIC"],
)

rust_binary(
    name = "kernel-tests-unstripped",
    crate = "k23",
    srcs = glob(["src/**/*.rs"]),
    mapped_srcs = {"//:wast_tests": "wast"},
    crate_root = "src/main.rs",
    default_target_platform = "//platforms:riscv64",
    target_compatible_with = ["prelude//cpu/constraints:riscv64"],
    incoming_transition = "//build:kernel-tests",
    rustc_flags = ["--cfg=test"],
    deps = _DEPS,
)

split_debuginfo(
    name = "kernel-tests",
    binary = ":kernel-tests-unstripped",
    default_target_platform = "//platforms:riscv64",
    target_compatible_with = ["prelude//cpu/constraints:riscv64"],
    visibility = ["PUBLIC"],
)
