# Protobuf C# runtime
#
# See also code generation logic under /src/google/protobuf/compiler/csharp.

load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("//bazel:proto_library.bzl", "proto_library")
load("//build_defs:internal_shell.bzl", "inline_sh_test")
load("//conformance:defs.bzl", "conformance_test")
load("//editions:defaults.bzl", "compile_edition_defaults", "embed_edition_defaults")
load("//upb/cmake:build_defs.bzl", "staleness_test")

################################################################################
# C# features
################################################################################
filegroup(
    name = "c_sharp_features_proto_srcs",
    srcs = ["google/protobuf/c_sharp_features.proto"],
    visibility = ["//src/google/protobuf:__pkg__"],
)

proto_library(
    name = "c_sharp_features_proto",
    srcs = ["google/protobuf/c_sharp_features.proto"],
    visibility = ["//:__subpackages__"],
    deps = ["//:descriptor_proto"],
)

################################################################################
# Tests
################################################################################

conformance_test(
    name = "conformance_test",
    failure_list = "//conformance:failure_list_csharp.txt",
    maximum_edition = "2026",
    testee = "//conformance:conformance_csharp",
)

conformance_test(
    name = "conformance_performance_test",
    failure_list = "//conformance:failure_list_csharp_performance.txt",
    maximum_edition = "2026",
    performance = True,
    testee = "//conformance:conformance_csharp",
)

################################################################################
# CSharp Runtime
################################################################################

filegroup(
    name = "srcs",
    srcs = glob(
        [
            "keys/*",
            "protos/*",
            "src/**/*.cs*",  # .cs and .csproj
        ],
        exclude = [
            # Exclude generated files.
            "src/*/obj/**/*",
        ],
    ) + [
        "google/protobuf/c_sharp_features.proto",
        "src/Directory.Build.props",
        "src/Google.Protobuf.Test/testprotos.pb",
        "src/Google.Protobuf.sln",
    ],
    visibility = [
        "//conformance:__subpackages__",
        "//csharp:__subpackages__",
    ],
)

filegroup(
    name = "wkt_cs_srcs",
    srcs = [
        "src/Google.Protobuf/Reflection/Descriptor.pb.cs",
        "src/Google.Protobuf/WellKnownTypes/Any.pb.cs",
        "src/Google.Protobuf/WellKnownTypes/Api.pb.cs",
        "src/Google.Protobuf/WellKnownTypes/Duration.pb.cs",
        "src/Google.Protobuf/WellKnownTypes/Empty.pb.cs",
        "src/Google.Protobuf/WellKnownTypes/FieldMask.pb.cs",
        "src/Google.Protobuf/WellKnownTypes/SourceContext.pb.cs",
        "src/Google.Protobuf/WellKnownTypes/Struct.pb.cs",
        "src/Google.Protobuf/WellKnownTypes/Timestamp.pb.cs",
        "src/Google.Protobuf/WellKnownTypes/Type.pb.cs",
        "src/Google.Protobuf/WellKnownTypes/Wrappers.pb.cs",
    ],
    visibility = ["//src/google/protobuf/compiler/csharp:__pkg__"],
)

inline_sh_test(
    name = "tests",
    srcs = [
        "src/Google.Protobuf.sln",
        ":srcs",
        "//conformance:conformance_csharp_proto",
        "//csharp/src/Google.Protobuf.Conformance:srcs",
    ],
    cmd = """
        cp $(rootpath //conformance:conformance_csharp_proto) `dirname $(location src/Google.Protobuf.sln)`/Google.Protobuf.Conformance/
        pushd `dirname $(location src/Google.Protobuf.sln)`/..
        dotnet restore src/Google.Protobuf.sln
        dotnet build -c Release src/Google.Protobuf.sln
        dotnet test -c Release -f net8.0 src/Google.Protobuf.Test/Google.Protobuf.Test.csproj
        popd
    """,
)

################################################################################
# Distribution files
################################################################################

sh_binary(
    name = "release",
    srcs = ["build_release.sh"],
    args = ["$(location build_release.sh)"],
)

################################################################################
# Generated edition defaults (and staleness test)
################################################################################

compile_edition_defaults(
    name = "csharp_edition_defaults",
    srcs = [
        "//:descriptor_proto",
    ],
    maximum_edition = "2026",
    minimum_edition = "PROTO2",
)

# TODO Make bazel tests use this output instead of the checked-in one
embed_edition_defaults(
    name = "embedded_csharp_edition_defaults_generate",
    defaults = "csharp_edition_defaults",
    encoding = "base64",
    output = "generated/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs",
    placeholder = "DEFAULTS_VALUE",
    template = "src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs.template",
)

staleness_test(
    name = "generated_csharp_defaults_staleness_test",
    outs = ["src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs"],
    generated_pattern = "generated/%s",
    tags = ["manual"],
    target_files = ["src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs"],
)
