load("//tools/lint:lint.bzl", "add_lint_tests")
load(
    "//tools/skylark:drake_cc.bzl",
    "drake_cc_binary",
    "drake_cc_googletest",
    "drake_cc_library",
    "drake_cc_package_library",
)
load(
    "//tools/skylark:drake_py.bzl",
    "drake_py_test",
    "drake_py_unittest",
)

package(default_visibility = ["//visibility:public"])

drake_cc_package_library(
    name = "lcm",
    visibility = ["//visibility:public"],
    deps = [
        ":drake_lcm",
        ":drake_lcm_base",
        ":drake_lcm_params",
        ":interface",
        ":lcm_log",
        ":lcm_messages",
    ],
)

drake_cc_library(
    name = "lcm_messages",
    srcs = ["lcm_messages.cc"],
    hdrs = ["lcm_messages.h"],
    deps = [
        "//common:essential",
        "//common:nice_type_name",
    ],
)

drake_cc_library(
    name = "interface",
    srcs = ["drake_lcm_interface.cc"],
    hdrs = ["drake_lcm_interface.h"],
    deps = [
        ":lcm_messages",
        "//common:essential",
    ],
)

drake_cc_library(
    name = "drake_lcm_base",
    srcs = ["drake_lcm_base.cc"],
    hdrs = ["drake_lcm_base.h"],
    deps = [
        ":interface",
        "//common:nice_type_name",
    ],
)

drake_cc_library(
    name = "drake_lcm_params",
    hdrs = ["drake_lcm_params.h"],
    deps = [
        "//common:essential",
        "//common:name_value",
    ],
)

drake_cc_library(
    name = "drake_lcm",
    srcs = ["drake_lcm.cc"],
    hdrs = ["drake_lcm.h"],
    interface_deps = [
        ":drake_lcm_params",
        ":interface",
        "//common:essential",
    ],
    deps = [
        "//common:network_policy",
        "@glib",
        "@lcm",
    ],
)

drake_cc_library(
    name = "lcm_log",
    srcs = ["drake_lcm_log.cc"],
    hdrs = ["drake_lcm_log.h"],
    interface_deps = [
        ":interface",
        "//common:essential",
    ],
    deps = [
        "//common:string_container",
        "@lcm",
    ],
)

drake_cc_library(
    name = "lcmt_drake_signal_utils",
    testonly = 1,
    srcs = ["lcmt_drake_signal_utils.cc"],
    hdrs = ["lcmt_drake_signal_utils.h"],
    deps = [
        "//common:essential",
        "//lcmtypes:drake_signal",
    ],
)

# === test/ ===

drake_cc_googletest(
    name = "lcm_messages_test",
    deps = [
        ":lcm_messages",
        ":lcmt_drake_signal_utils",
        "//common/test_utilities",
        "//lcmtypes:drake_signal",
    ],
)

drake_cc_googletest(
    name = "drake_lcm_interface_test",
    deps = [
        ":drake_lcm",
        ":lcmt_drake_signal_utils",
        "//common/test_utilities",
    ],
)

drake_cc_googletest(
    name = "drake_lcm_base_test",
    deps = [
        ":drake_lcm_base",
        "//common/test_utilities:expect_throws_message",
    ],
)

drake_cc_googletest(
    name = "drake_lcm_test",
    allow_network = ["lcm"],
    flaky = True,
    deps = [
        ":drake_lcm",
        ":lcmt_drake_signal_utils",
        "//common/test_utilities:expect_throws_message",
        "@lcm",
    ],
)

drake_cc_googletest(
    name = "drake_lcm_denied_test",
    allow_network = ["none"],
    deps = [
        ":drake_lcm",
        "//common/test_utilities:expect_throws_message",
    ],
)

drake_cc_googletest(
    name = "drake_lcm_thread_test",
    flaky = True,
    deps = [
        ":drake_lcm",
        ":lcmt_drake_signal_utils",
        "@lcm",
    ],
)

drake_cc_binary(
    name = "initialization_sequence_test_stub",
    testonly = True,
    srcs = ["test/initialization_sequence_test_stub.cc"],
    deps = [
        ":drake_lcm",
        "//common:add_text_logging_gflags",
        "//lcmtypes:drake_signal",
        "@gflags",
    ],
)

drake_py_unittest(
    name = "initialization_sequence_test",
    allow_network = ["lcm"],
    data = [
        ":initialization_sequence_test_stub",
    ],
    deps = [
        "@rules_python//python/runfiles",
    ],
)

drake_cc_googletest(
    name = "drake_lcm_log_test",
    deps = [
        ":lcm_log",
        ":lcmt_drake_signal_utils",
    ],
)

drake_cc_googletest(
    name = "lcmt_drake_signal_utils_test",
    deps = [
        ":lcmt_drake_signal_utils",
    ],
)

drake_py_test(
    name = "import_lcm_test",
    # TODO(jwnimmer-tri) Port this to drake_py_unittest.
    allow_import_unittest = True,
    deps = ["@lcm//:lcm-python"],
)

add_lint_tests()
