load("@rules_erlang//:eunit2.bzl", "eunit")
load("@rules_erlang//:xref2.bzl", "xref")
load("@rules_erlang//:dialyze.bzl", "dialyze", "plt")
load(
    "//:rabbitmq.bzl",
    "RABBITMQ_DIALYZER_OPTS",
    "assert_suites",
    "rabbitmq_app",
    "rabbitmq_suite",
)
load(
    ":app.bzl",
    "all_beam_files",
    "all_srcs",
    "all_test_beam_files",
    "test_suite_beam_files",
)

py_binary(
    name = "codegen",
    srcs = [
        "codegen.py",
    ],
    imports = ["../../deps/rabbitmq_codegen"],
    deps = [
        "//deps/rabbitmq_codegen:amqp_codegen",
    ],
)

AMQP_SPEC_1_0 = [
    "//deps/rabbitmq_codegen:amqp-1.0/messaging.xml",
    "//deps/rabbitmq_codegen:amqp-1.0/security.xml",
    "//deps/rabbitmq_codegen:amqp-1.0/transport.xml",
    "//deps/rabbitmq_codegen:amqp-1.0/transactions.xml",
]

genrule(
    name = "generated_headers",
    srcs = AMQP_SPEC_1_0,
    outs = ["include/amqp10_framing.hrl"],
    cmd = "$(location :codegen) hrl $(SRCS) > $@",
    tools = [":codegen"],
)

genrule(
    name = "generated_sources",
    srcs = AMQP_SPEC_1_0,
    outs = ["src/amqp10_framing0.erl"],
    cmd = "$(location :codegen) erl $(SRCS) > $@",
    tools = [":codegen"],
)

APP_EXTRA_KEYS = """%% Hex.pm package informations.
    {licenses, ["MPL-2.0"]},
    {links, [
        {"Website", "https://www.rabbitmq.com/"},
        {"GitHub", "https://github.com/rabbitmq/rabbitmq-server/tree/main/deps/amqp10_common"}
    ]},
    {build_tools, ["make", "rebar3"]},
    {files, [
        "erlang.mk",
        "git-revisions.txt",
        "include",
        "LICENSE*",
        "Makefile",
        "rabbitmq-components.mk",
        "README",
        "README.md",
        "src"
    ]}
"""

all_beam_files(name = "all_beam_files")

all_test_beam_files(name = "all_test_beam_files")

all_srcs(name = "all_srcs")

test_suite_beam_files(name = "test_suite_beam_files")

rabbitmq_app(
    name = "erlang_app",
    srcs = [":all_srcs"],
    hdrs = [":public_hdrs"],
    app_description = "Modules shared by rabbitmq-amqp1.0 and rabbitmq-amqp1.0-client",
    app_extra_keys = APP_EXTRA_KEYS,
    app_name = "amqp10_common",
    beam_files = [":beam_files"],
    license_files = [":license_files"],
    priv = [":priv"],
)

xref(
    name = "xref",
    target = ":erlang_app",
)

plt(
    name = "deps_plt",
    for_target = ":erlang_app",
    plt = "//:base_plt",
)

dialyze(
    name = "dialyze",
    dialyzer_opts = RABBITMQ_DIALYZER_OPTS,
    plt = ":deps_plt",
    target = ":erlang_app",
)

rabbitmq_suite(
    name = "binary_generator_SUITE",
    size = "small",
)

rabbitmq_suite(
    name = "binary_parser_SUITE",
    size = "small",
)

rabbitmq_suite(
    name = "serial_number_SUITE",
    size = "small",
)

rabbitmq_suite(
    name = "prop_SUITE",
    deps = [
        "//deps/rabbitmq_ct_helpers:erlang_app",
    ],
)

assert_suites()

alias(
    name = "amqp10_common",
    actual = ":erlang_app",
    visibility = ["//visibility:public"],
)

eunit(
    name = "eunit",
    target = ":test_erlang_app",
)
