################################################################################
# Protocol Buffers Compiler - Java code generator
################################################################################

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

cc_library(
    name = "names",
    hdrs = ["names.h"],
    copts = COPTS,
    strip_include_prefix = "/src",
    visibility = ["//visibility:public"],
    deps = [
        ":names_internal",
        "//src/google/protobuf",
    ],
)

cc_library(
    name = "names_internal",
    srcs = [
        "helpers.cc",
        "name_resolver.cc",
        "names.cc",
    ],
    hdrs = [
        "generator.h",
        "helpers.h",
        "name_resolver.h",
        "names.h",
        "options.h",
    ],
    copts = COPTS,
    strip_include_prefix = "/src",
    visibility = ["//pkg:__pkg__"],
    deps = [
        ":java_features_bootstrap",
        "//src/google/protobuf",
        "//src/google/protobuf:port",
        "//src/google/protobuf/compiler:code_generator",
        "//src/google/protobuf/compiler:versions",
        "//src/google/protobuf/io:printer",
        "//src/google/protobuf/io:tokenizer",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@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",
    ],
)

cc_library(
    name = "java_features_bootstrap",
    srcs = ["java_features.pb.cc"],
    hdrs = ["java_features.pb.h"],
    strip_include_prefix = "/src",
    deps = [
        "//src/google/protobuf",
        "//src/google/protobuf:arena",
        "//src/google/protobuf:port",
        "//src/google/protobuf:protobuf_lite",
        "//src/google/protobuf/io",
    ],
)

cc_library(
    name = "java",
    srcs = [
        "context.cc",
        "doc_comment.cc",
        "enum.cc",
        "enum_field.cc",
        "enum_field_lite.cc",
        "enum_lite.cc",
        "extension.cc",
        "extension_lite.cc",
        "field.cc",
        "file.cc",
        "generator.cc",
        "generator_factory.cc",
        "kotlin_generator.cc",
        "map_field.cc",
        "map_field_lite.cc",
        "message.cc",
        "message_builder.cc",
        "message_builder_lite.cc",
        "message_field.cc",
        "message_field_lite.cc",
        "message_lite.cc",
        "message_serialization.cc",
        "primitive_field.cc",
        "primitive_field_lite.cc",
        "service.cc",
        "shared_code_generator.cc",
        "string_field.cc",
        "string_field_lite.cc",
    ],
    hdrs = [
        "context.h",
        "doc_comment.h",
        "enum.h",
        "enum_field.h",
        "enum_field_lite.h",
        "enum_lite.h",
        "extension.h",
        "extension_lite.h",
        "field.h",
        "file.h",
        "generator.h",
        "generator_factory.h",
        "java_features.pb.h",
        "kotlin_generator.h",
        "map_field.h",
        "map_field_lite.h",
        "message.h",
        "message_builder.h",
        "message_builder_lite.h",
        "message_field.h",
        "message_field_lite.h",
        "message_lite.h",
        "message_serialization.h",
        "primitive_field.h",
        "primitive_field_lite.h",
        "service.h",
        "shared_code_generator.h",
        "string_field.h",
        "string_field_lite.h",
    ],
    copts = COPTS,
    strip_include_prefix = "/src",
    visibility = [
        "//pkg:__pkg__",
        "//src/google/protobuf/compiler:__pkg__",
    ],
    deps = [
        ":java_features_bootstrap",
        ":names",
        ":names_internal",
        "//src/google/protobuf",
        "//src/google/protobuf:arena",
        "//src/google/protobuf:port",
        "//src/google/protobuf:protobuf_lite",
        "//src/google/protobuf/compiler:code_generator",
        "//src/google/protobuf/compiler:retention",
        "//src/google/protobuf/compiler:versions",
        "//src/google/protobuf/io",
        "//src/google/protobuf/io:printer",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@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",
    ],
)

cc_test(
    name = "doc_comment_unittest",
    srcs = ["doc_comment_unittest.cc"],
    data = [
        "//:well_known_type_protos",
        "//conformance:conformance_proto",
        "//src/google/protobuf:descriptor_proto_srcs",
    ],
    deps = [
        ":java",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "plugin_unittest",
    srcs = ["plugin_unittest.cc"],
    deps = [
        ":java",
        "//:protobuf",
        "//src/google/protobuf/compiler:command_line_interface",
        "//src/google/protobuf/io",
        "//src/google/protobuf/io:printer",
        "//src/google/protobuf/stubs:lite",
        "//src/google/protobuf/testing",
        "//src/google/protobuf/testing:file",
        "@com_google_absl//absl/log:absl_check",
        "@com_google_absl//absl/strings",
        "@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",
        ],
        allow_empty = True,
    ),
    visibility = ["//src/google/protobuf/compiler:__pkg__"],
)
