################################################################################
# Protocol Buffers Compiler - Python code generator
################################################################################

load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
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_binary(
    name = "protoc-gen-python",
    srcs = ["plugin_main.cc"],
    visibility = ["//visibility:public"],
    deps = [
        ":python",
        "//src/google/protobuf/compiler:plugin",
    ],
)

cc_binary(
    name = "protoc-gen-pyi",
    srcs = ["pyi_plugin_main.cc"],
    visibility = ["//visibility:public"],
    deps = [
        ":python",
        "//src/google/protobuf/compiler:plugin",
    ],
)

cc_library(
    name = "names",
    srcs = ["names.cc"],
    hdrs = ["names.h"],
    copts = COPTS,
    strip_include_prefix = "/src",
    visibility = ["//visibility:public"],
    deps = [
        "//src/google/protobuf",
        "//src/google/protobuf:port",
        "//src/google/protobuf/compiler:code_generator",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/strings",
    ],
)

cc_library(
    name = "python",
    srcs = [
        "generator.cc",
        "helpers.cc",
        "pyi_generator.cc",
    ],
    hdrs = [
        "generator.h",
        "helpers.h",
        "pyi_generator.h",
    ],
    copts = COPTS,
    strip_include_prefix = "/src",
    visibility = [
        "//pkg:__pkg__",
        "//src/google/protobuf/compiler:__pkg__",
        "@com_github_grpc_grpc//tools/distrib/python/grpcio_tools:__subpackages__",
    ],
    deps = [
        ":names",
        "//src/google/protobuf",
        "//src/google/protobuf:port",
        "//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",
        "//src/google/protobuf/io:tokenizer",
        "@abseil-cpp//absl/container:flat_hash_map",
        "@abseil-cpp//absl/container:flat_hash_set",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/log:absl_log",
        "@abseil-cpp//absl/memory",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:str_format",
        "@abseil-cpp//absl/synchronization",
    ],
)

cc_test(
    name = "plugin_unittest",
    srcs = ["plugin_unittest.cc"],
    copts = COPTS,
    deps = [
        ":python",
        "//src/google/protobuf",
        "//src/google/protobuf/compiler:code_generator",
        "//src/google/protobuf/compiler:command_line_interface",
        "//src/google/protobuf/compiler:command_line_interface_tester",
        "//src/google/protobuf/compiler/cpp",
        "//src/google/protobuf/io",
        "//src/google/protobuf/io:printer",
        "//src/google/protobuf/testing",
        "//src/google/protobuf/testing:file",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/strings",
        "@googletest//:gtest",
        "@googletest//:gtest_main",
    ],
)

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

filegroup(
    name = "test_srcs",
    srcs = glob(
        [
            "*_test.cc",
            "*unittest.cc",
        ],
        allow_empty = True,
    ),
    visibility = ["//src/google/protobuf/compiler:__pkg__"],
)
