# Protobuf IO library.

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
load("//build_defs:cpp_opts.bzl", "COPTS")

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

cc_library(
    name = "io",
    srcs = [
        "coded_stream.cc",
        "zero_copy_stream.cc",
        "zero_copy_stream_impl.cc",
        "zero_copy_stream_impl_lite.cc",
    ],
    hdrs = [
        "coded_stream.h",
        "zero_copy_stream.h",
        "zero_copy_stream_impl.h",
        "zero_copy_stream_impl_lite.h",
    ],
    copts = COPTS,
    include_prefix = "google/protobuf/io",
    deps = [
        ":io_win32",
        "//src/google/protobuf:arena",
        "//src/google/protobuf/stubs:lite",
        "@com_google_absl//absl/log:absl_check",
        "@com_google_absl//absl/log:absl_log",
        "@com_google_absl//absl/strings:internal",
    ],
)

cc_library(
    name = "test_zero_copy_stream",
    testonly = 1,
    hdrs = ["test_zero_copy_stream.h"],
    copts = COPTS,
    include_prefix = "google/protobuf/io",
    deps = [
        ":io",
        "//src/google/protobuf/stubs",
        "@com_google_absl//absl/log:absl_check",
        "@com_google_absl//absl/types:optional",
    ],
)

cc_test(
    name = "test_zero_copy_stream_test",
    srcs = ["test_zero_copy_stream_test.cc"],
    copts = COPTS,
    deps = [
        ":test_zero_copy_stream",
        "//src/google/protobuf/stubs",
        "@com_google_absl//absl/types:optional",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "zero_copy_sink",
    srcs = ["zero_copy_sink.cc"],
    hdrs = ["zero_copy_sink.h"],
    copts = COPTS,
    strip_include_prefix = "/src",
    deps = [
        ":io",
        "//src/google/protobuf/stubs",
    ],
)

cc_test(
    name = "zero_copy_sink_test",
    srcs = ["zero_copy_sink_test.cc"],
    copts = COPTS,
    deps = [
        ":zero_copy_sink",
        "//src/google/protobuf/stubs",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "printer",
    srcs = ["printer.cc"],
    hdrs = ["printer.h"],
    copts = COPTS,
    include_prefix = "google/protobuf/io",
    deps = [
        ":zero_copy_sink",
        "//src/google/protobuf/stubs",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/cleanup",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/functional:function_ref",
        "@com_google_absl//absl/log:absl_check",
        "@com_google_absl//absl/log:absl_log",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:optional",
        "@com_google_absl//absl/types:span",
        "@com_google_absl//absl/types:variant",
    ],
)

cc_library(
    name = "tokenizer",
    srcs = [
        "strtod.cc",
        "tokenizer.cc",
    ],
    hdrs = [
        "strtod.h",
        "tokenizer.h",
    ],
    copts = COPTS,
    include_prefix = "google/protobuf/io",
    deps = [
        ":io",
        "//src/google/protobuf/stubs",
        "@com_google_absl//absl/log:absl_log",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
    ],
)

cc_library(
    name = "gzip_stream",
    srcs = ["gzip_stream.cc"],
    hdrs = ["gzip_stream.h"],
    copts = COPTS,
    include_prefix = "google/protobuf/io",
    deps = [
        ":io",
        "//src/google/protobuf/stubs",
        "@com_google_absl//absl/log:absl_check",
        "@com_google_absl//absl/log:absl_log",
    ] + select({
        "//build_defs:config_msvc": [],
        "//conditions:default": ["@zlib"],
    }),
)

cc_library(
    name = "io_win32",
    srcs = ["io_win32.cc"],
    hdrs = ["io_win32.h"],
    copts = COPTS,
    include_prefix = "google/protobuf/io",
    visibility = [
        "//pkg:__pkg__",
        "//src/google/protobuf/compiler:__pkg__",
    ],
    deps = [
        "//src/google/protobuf:arena",
        "//src/google/protobuf/stubs:lite",
    ],
)

cc_test(
    name = "io_test",
    srcs = [
        "coded_stream_unittest.cc",
        "printer_death_test.cc",
        "printer_unittest.cc",
        "tokenizer_unittest.cc",
        "zero_copy_stream_unittest.cc",
    ],
    copts = COPTS,
    data = [
        "//src/google/protobuf:testdata",
    ],
    deps = [
        ":gzip_stream",
        ":io",
        "//:protobuf",
        "//src/google/protobuf:test_util2",
        "//src/google/protobuf/testing",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log:scoped_mock_log",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "win32_test",
    srcs = ["io_win32_unittest.cc"],
    tags = [
        "manual",
        "windows",
    ],
    deps = [
        "//:protobuf_lite",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

################################################################################
# Distribution packaging
################################################################################

pkg_files(
    name = "dist_files",
    srcs = glob(["**/*"]),
    strip_prefix = strip_prefix.from_root(""),
    visibility = ["//src:__pkg__"],
)

filegroup(
    name = "test_srcs",
    srcs = glob([
        "*_test.cc",
        "*unittest.cc",
    ]),
    visibility = ["//pkg:__pkg__"],
)
