# BUILD.bazel generated by BUILD.bazel.PL

load("//third_party/bazel_rules/rules_cc/cc:cc_library.bzl", "cc_library")
load(":build_defs.bzl", "define_perl_proto_c_tests")
load(":c_test_config.bzl", "C_TESTS")

package(
    default_applicable_licenses = ["//third_party/protobuf:license"],
    default_visibility = ["//visibility:public"],
    features = [
        "-layering_check",
        "-use_header_modules",
        "-parse_headers",
    ],
)

# Libraries
# =============================================================================

genrule(
    name = "gen_ppport",
    outs = ["ppport.h"],
    cmd = "perl -MDevel::PPPort -e 'Devel::PPPort::WriteFile(qq{$@})'",
)

cc_library(
    name = "libprotobufperl",
    srcs = [
        "xs/convert.c",
        "xs/convert/sv_to_upb.c",
        "xs/convert/upb_to_sv.c",
        "xs/descriptor.c",
        "xs/descriptor/enum.c",
        "xs/descriptor/enum_value.c",
        "xs/descriptor/field.c",
        "xs/descriptor/file.c",
        "xs/descriptor/message.c",
        "xs/descriptor/method.c",
        "xs/descriptor/oneof.c",
        "xs/descriptor/service.c",
        "xs/descriptor_containers.c",
        "xs/descriptor_containers/by_name_map.c",
        "xs/descriptor_containers/by_number_map.c",
        "xs/descriptor_containers/generic_sequence.c",
        "xs/descriptor_containers/iterators.c",
        "xs/descriptor_pool.c",
        "xs/descriptor_pool/add.c",
        "xs/descriptor_pool/find.c",
        "xs/descriptor_pool/pool.c",
        "xs/extension_dict.c",
        "xs/extension_dict/dict.c",
        "xs/extension_dict/iterator.c",
        "xs/map.c",
        "xs/map/iterator.c",
        "xs/map/map.c",
        "xs/message.c",
        "xs/message/access.c",
        "xs/message/compare.c",
        "xs/message/message.c",
        "xs/message/meta.c",
        "xs/message/serialize.c",
        "xs/protobuf.c",
        "xs/protobuf/arena.c",
        "xs/protobuf/arena_tmpfs.c",
        "xs/protobuf/fast_accessors.c",
        "xs/protobuf/message.c",
        "xs/protobuf/obj_cache.c",
        "xs/protobuf/registry.c",
        "xs/protobuf/utils.c",
        "xs/repeated.c",
        "xs/repeated/composite.c",
        "xs/repeated/repeated.c",
        "xs/unknown_fields.c",
        "xs/unknown_fields/set.c",
    ],
    hdrs = glob([
        "xs/**/*.h",
    ]) + [
        ":gen_ppport",
    ],
    copts = [
        "-I.",
        "-Iperl",
        "-Ithird_party/upb",
        "-I$(GENDIR)/third_party/protobuf/perl",
        "-DGOOGLE3",
        "-Dgoogle_protobuf_FileDescriptorSet=proto2_FileDescriptorSet",
        "-Dgoogle_protobuf_FileDescriptorSet_parse=proto2_FileDescriptorSet_parse",
        "-Dgoogle_protobuf_FileDescriptorProto=proto2_FileDescriptorProto",
        "-Dgoogle_protobuf_FileDescriptorProto_parse=proto2_FileDescriptorProto_parse",
        "-Dgoogle_protobuf_FileDescriptorProto_name=proto2_FileDescriptorProto_name",
        "-Dgoogle_protobuf_FileDescriptorSet_file=proto2_FileDescriptorSet_file",
        "-D_GNU_SOURCE",
        "-Wno-error=attributes",
        "-fPIC",
    ] + select({
        "//conditions:default": [],
    }),
    includes = ["."],
    linkopts = [
    ],
    deps = [
        "//third_party/perl/perl:libperl",
        "//third_party/upb/upb/text",
        "//third_party/upb/upb/message:compare",
        "//third_party/upb/upb/json",
        "//third_party/absl/base",
        "//third_party/upb/upb/base",
        "//third_party/upb/upb/mem",
        "//third_party/upb/upb/message",
        "//third_party/upb/upb/mini_descriptor",
        "//third_party/upb/upb/mini_table",
        "//third_party/upb/upb/reflection",
        "//third_party/upb/upb/wire",
        # Add other necessary deps
    ],
    alwayslink = 1,
)

# C Test Infrastructure
# =============================================================================

cc_library(
    name = "upb_perl_test_lib",
    srcs = ["t/c/upb-perl-test.c"],
    hdrs = ["t/c/upb-perl-test.h"],
    copts = [
        "-I.",
        "-Ithird_party/upb",
        "-DGOOGLE3",
    ],
    deps = [
        ":libprotobufperl",
        "//third_party/pcre2",
    ],
)

genrule(
    name = "test_descriptor_bin",
    srcs = [
        "t/c/test.proto",
        "//third_party/protobuf:test_messages_proto2.proto",
    ],
    outs = ["t/data/test_descriptor.bin"],
    cmd = "$(execpath //net/proto2/compiler/public:protocol_compiler) --descriptor_set_out=$@ --include_imports --proto_path=third_party/protobuf/perl/t/c --proto_path=. $(location t/c/test.proto)",
    tools = ["//net/proto2/compiler/public:protocol_compiler"],
)

# C Tests
# Dynamically instantiate all C-level integration tests via Starlark macro
define_perl_proto_c_tests(tests = C_TESTS)

# Test Suites
# =============================================================================
test_suite(
    name = "c_tests",
    tests = [
        ":c_test_convert-sv_to_upb",
        ":c_test_convert-upb_to_sv",
        ":c_test_descriptor-base",
        ":c_test_descriptor-enum",
        ":c_test_descriptor-enum_value",
        ":c_test_descriptor-field",
        ":c_test_descriptor-file",
        ":c_test_descriptor-message",
        ":c_test_descriptor-method",
        ":c_test_descriptor-oneof",
        ":c_test_descriptor-service",
        ":c_test_descriptor_containers-by_name_map",
        ":c_test_descriptor_containers-by_number_map",
        ":c_test_descriptor_containers-generic_sequence",
        ":c_test_descriptor_containers-iterators",
        ":c_test_descriptor_pool-add",
        ":c_test_descriptor_pool-find",
        ":c_test_descriptor_pool-pool",
        ":c_test_extension_dict-dict",
        ":c_test_harness-leak_thread",
        ":c_test_harness-skip",
        ":c_test_integration-030_asan_test",
        ":c_test_integration-030_protobuf",
        ":c_test_integration-030_protobuf_multi_interp",
        ":c_test_integration-035_croak_test",
        ":c_test_integration-036_asan_leak_test",
        ":c_test_integration-050_convert",
        ":c_test_integration-070_descriptor",
        ":c_test_integration-090_descriptor_containers",
        ":c_test_integration-110_descriptor_pool",
        ":c_test_integration-130_extension_dict",
        ":c_test_integration-150_map",
        ":c_test_integration-170_message",
        ":c_test_integration-190_repeated",
        ":c_test_integration-210_unknown_fields",
        ":c_test_integration-220_all_c_layers",
        ":c_test_map-map",
        ":c_test_message-init",
        ":c_test_message-message",
        ":c_test_message-wkt",
        ":c_test_protobuf-arena",
        ":c_test_protobuf-obj_cache",
        ":c_test_protobuf-obj_cache_lru",
        ":c_test_protobuf-obj_cache_threads",
        ":c_test_protobuf-utils",
        ":c_test_repeated-composite",
        ":c_test_repeated-repeated",
        ":c_test_repeated-scalar",
        ":c_test_unknown_fields-build",
        ":c_test_unknown_fields-set",
    ],
)

# Perl tests would be defined here if @rules_perl is set up
# Example:
# perl_library(
#     name = "Protobuf",
#     srcs = glob(["lib/**/*.pm"]),
#     deps = [":libprotobufperl"], # Native dependency
# )
#
# perl_test(
#     name = "perl_tests",
#     srcs = glob(["t/*.t", "t/integration/*.t"]),
#     deps = [":Protobuf"],
# )
