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

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

drake_cc_package_library(
    name = "lcm",
    visibility = ["//visibility:public"],
    deps = [
        ":lcm_buses",
        ":lcm_config_functions",
        ":lcm_interface_system",
        ":lcm_log_playback_system",
        ":lcm_publisher_system",
        ":lcm_pubsub_system",
        ":lcm_scope_system",
        ":lcm_subscriber_system",
        ":lcm_system_graphviz",
        ":serializer",
    ],
)

drake_cc_library(
    name = "serializer",
    srcs = ["serializer.cc"],
    hdrs = ["serializer.h"],
    visibility = ["//visibility:private"],
    deps = [
        "//common:value",
    ],
)

drake_cc_library(
    name = "lcm_system_graphviz",
    srcs = ["lcm_system_graphviz.cc"],
    hdrs = ["lcm_system_graphviz.h"],
    deps = [
        "//lcm:interface",
        "//systems/framework:system_base",
    ],
)

drake_cc_library(
    name = "lcm_publisher_system",
    srcs = ["lcm_publisher_system.cc"],
    hdrs = ["lcm_publisher_system.h"],
    interface_deps = [
        ":serializer",
        "//lcm:interface",
        "//systems/framework:leaf_system",
    ],
    deps = [
        ":lcm_system_graphviz",
        "//lcm:drake_lcm",
    ],
)

drake_cc_library(
    name = "lcm_scope_system",
    srcs = ["lcm_scope_system.cc"],
    hdrs = ["lcm_scope_system.h"],
    deps = [
        ":lcm_publisher_system",
        "//lcm:interface",
        "//lcmtypes:scope",
        "//systems/framework:diagram_builder",
        "//systems/framework:leaf_system",
    ],
)

drake_cc_library(
    name = "lcm_subscriber_system",
    srcs = ["lcm_subscriber_system.cc"],
    hdrs = ["lcm_subscriber_system.h"],
    interface_deps = [
        ":serializer",
        "//lcm:interface",
        "//systems/framework:leaf_system",
    ],
    deps = [
        ":lcm_system_graphviz",
        "//lcm:drake_lcm",
    ],
)

drake_cc_library(
    name = "lcm_interface_system",
    srcs = ["lcm_interface_system.cc"],
    hdrs = ["lcm_interface_system.h"],
    interface_deps = [
        "//lcm:drake_lcm_params",
        "//systems/framework:leaf_system",
    ],
    deps = [
        ":lcm_system_graphviz",
        "//lcm:drake_lcm",
    ],
)

# This is a convenience alias to get all three systems at once.
drake_cc_library(
    name = "lcm_pubsub_system",
    deps = [
        ":lcm_interface_system",
        ":lcm_publisher_system",
        ":lcm_subscriber_system",
    ],
)

drake_cc_library(
    name = "lcm_log_playback_system",
    srcs = [
        "lcm_log_playback_system.cc",
    ],
    hdrs = [
        "lcm_log_playback_system.h",
    ],
    deps = [
        "//lcm:lcm_log",
        "//systems/framework:leaf_system",
    ],
)

drake_cc_library(
    name = "lcm_buses",
    srcs = ["lcm_buses.cc"],
    hdrs = ["lcm_buses.h"],
    deps = [
        "//common:string_container",
        "//lcm:interface",
    ],
)

drake_cc_library(
    name = "lcm_config_functions",
    srcs = ["lcm_config_functions.cc"],
    hdrs = ["lcm_config_functions.h"],
    interface_deps = [
        ":lcm_buses",
        ":lcm_interface_system",
        "//lcm:drake_lcm_params",
        "//systems/framework:diagram_builder",
    ],
    deps = [
        "//lcm:drake_lcm",
        "//systems/primitives:shared_pointer_system",
    ],
)

# === test ===

drake_cc_googletest(
    name = "lcm_buses_test",
    deps = [
        ":lcm_buses",
        "//lcm",
    ],
)

drake_cc_googletest(
    name = "lcm_config_functions_test",
    deps = [
        ":lcm_config_functions",
        "//common/test_utilities:expect_throws_message",
        "//lcm:drake_lcm",
        "//lcmtypes:drake_signal",
    ],
)

drake_cc_googletest(
    name = "lcm_interface_system_test",
    flaky = True,
    deps = [
        ":lcm_interface_system",
        ":lcm_subscriber_system",
        "//lcm:drake_lcm",
        "//lcmtypes:drake_signal",
        "//systems/analysis:simulator",
        "//systems/framework:diagram_builder",
    ],
)

drake_cc_googletest(
    name = "lcm_publisher_system_test",
    deps = [
        ":lcm_publisher_system",
        "//common/test_utilities:is_dynamic_castable",
        "//lcm:drake_lcm",
        "//lcm:lcmt_drake_signal_utils",
        "//systems/analysis:simulator",
        "//systems/framework:diagram_builder",
    ],
)

drake_cc_googletest(
    name = "lcm_log_playback_test",
    deps = [
        ":lcm_log_playback_system",
        ":lcm_pubsub_system",
        "//lcmtypes:drake_signal",
        "//systems/analysis:simulator",
        "//systems/framework:diagram_builder",
    ],
)

drake_cc_googletest(
    name = "lcm_scope_system_test",
    deps = [
        ":lcm_interface_system",
        ":lcm_scope_system",
        "//lcmtypes:scope",
        "//systems/analysis:simulator",
        "//systems/framework:diagram",
        "//systems/framework:diagram_builder",
        "//systems/primitives:sine",
    ],
)

drake_cc_googletest(
    name = "lcm_system_graphviz_test",
    deps = [
        ":lcm_system_graphviz",
        "//lcm:drake_lcm",
        "//lcmtypes:drake_signal",
        "@abseil_cpp_internal//absl/strings",
    ],
)

drake_cc_googletest(
    name = "lcm_subscriber_system_test",
    deps = [
        ":lcm_subscriber_system",
        "//common/test_utilities:expect_throws_message",
        "//lcm:drake_lcm",
        "//lcm:lcmt_drake_signal_utils",
    ],
)

drake_cc_googletest(
    name = "serializer_test",
    deps = [
        ":serializer",
        "//lcm:lcmt_drake_signal_utils",
    ],
)

add_lint_tests()
