diff --git a/examples/csharp/csharp_proto_compile/BUILD.bazel b/examples/csharp/csharp_proto_compile/BUILD.bazel new file mode 100644 index 00000000..56dd5d12 --- /dev/null +++ b/examples/csharp/csharp_proto_compile/BUILD.bazel @@ -0,0 +1,16 @@ +load("@rules_proto_grpc_csharp//:defs.bzl", "csharp_proto_compile") + +csharp_proto_compile( + name = "person_csharp_proto", + protos = ["@rules_proto_grpc_example_protos//:person_proto"], +) + +csharp_proto_compile( + name = "place_csharp_proto", + protos = ["@rules_proto_grpc_example_protos//:place_proto"], +) + +csharp_proto_compile( + name = "thing_csharp_proto", + protos = ["@rules_proto_grpc_example_protos//:thing_proto"], +) diff --git a/examples/csharp/csharp_proto_compile/MODULE.bazel b/examples/csharp/csharp_proto_compile/MODULE.bazel new file mode 100644 index 00000000..21fc5234 --- /dev/null +++ b/examples/csharp/csharp_proto_compile/MODULE.bazel @@ -0,0 +1,20 @@ +bazel_dep(name = "rules_proto_grpc", version = "0.0.0.rpg.version.placeholder") +bazel_dep(name = "rules_proto_grpc_example_protos", version = "0.0.0.rpg.version.placeholder") +bazel_dep(name = "rules_proto_grpc_csharp", version = "0.0.0.rpg.version.placeholder") + +local_path_override( + module_name = "rules_proto_grpc", + path = "../../../modules/core", +) + +local_path_override( + module_name = "rules_proto_grpc_example_protos", + path = "../../../modules/example_protos", +) + +local_path_override( + module_name = "rules_proto_grpc_csharp", + path = "../../../modules/csharp", +) + +bazel_dep(name = "protobuf", version = "27.1") diff --git a/examples/csharp/csharp_proto_compile/WORKSPACE b/examples/csharp/csharp_proto_compile/WORKSPACE new file mode 100644 index 00000000..e69de29b diff --git a/modules/csharp/BUILD.bazel b/modules/csharp/BUILD.bazel new file mode 100644 index 00000000..6046544f --- /dev/null +++ b/modules/csharp/BUILD.bazel @@ -0,0 +1,21 @@ +load("@rules_proto_grpc//:defs.bzl", "proto_plugin") + +proto_plugin( + name = "proto_plugin", + exclusions = [ + "google/protobuf", + ], + outputs = ["{basename|pascal}.cs"], + protoc_plugin_name = "chsarp", + visibility = ["//visibility:public"], +) + +proto_plugin( + name = "grpc_plugin", + exclusions = [ + "google/protobuf", + ], + output_directory = True, + tool = "@grpc//src/compiler:grpc_csharp_plugin", + visibility = ["//visibility:public"], +) diff --git a/modules/csharp/MODULE.bazel b/modules/csharp/MODULE.bazel new file mode 100644 index 00000000..a26d2247 --- /dev/null +++ b/modules/csharp/MODULE.bazel @@ -0,0 +1,26 @@ +module( + name = "rules_proto_grpc_csharp", + version = "0.0.0.rpg.version.placeholder", + compatibility_level = 1, +) + +bazel_dep(name = "grpc", version = "1.65.0") +bazel_dep(name = "protobuf", version = "27.1") +bazel_dep(name = "rules_dotnet", version = "0.17.5") +bazel_dep(name = "rules_proto_grpc", version = "0.0.0.rpg.version.placeholder") +bazel_dep(name = "rules_proto_grpc_example_protos", version = "0.0.0.rpg.version.placeholder") + +register_toolchains("@dotnet_toolchains//:all") +local_path_override( + module_name = "rules_proto_grpc", + path = "../../modules/core", +) + +local_path_override( + module_name = "rules_proto_grpc_example_protos", + path = "../../modules/example_protos", +) + +dotnet = use_extension("@rules_dotnet//dotnet:extensions.bzl", "dotnet") +dotnet.toolchain(dotnet_version = "8.0.200") +use_repo(dotnet, "dotnet_toolchains") diff --git a/modules/csharp/csharp_grpc_compile.bzl b/modules/csharp/csharp_grpc_compile.bzl new file mode 100644 index 00000000..9f523ab6 --- /dev/null +++ b/modules/csharp/csharp_grpc_compile.bzl @@ -0,0 +1,27 @@ +"""Generated definition of csharp_grpc_compile.""" + +load( + "@rules_proto_grpc//:defs.bzl", + "ProtoPluginInfo", + "proto_compile_attrs", + "proto_compile_impl", + "proto_compile_toolchains", +) + +# Create compile rule +csharp_grpc_compile = rule( + implementation = proto_compile_impl, + attrs = dict( + proto_compile_attrs, + _plugins = attr.label_list( + providers = [ProtoPluginInfo], + default = [ + Label("//:proto_plugin"), + Label("//:grpc_plugin"), + ], + cfg = "exec", + doc = "List of protoc plugins to apply", + ), + ), + toolchains = proto_compile_toolchains, +) diff --git a/modules/csharp/csharp_grpc_library.bzl b/modules/csharp/csharp_grpc_library.bzl new file mode 100644 index 00000000..d6f8a892 --- /dev/null +++ b/modules/csharp/csharp_grpc_library.bzl @@ -0,0 +1,38 @@ +"""Generated definition of csharp_grpc_library.""" + +load("//:csharp_grpc_compile.bzl", "csharp_grpc_compile") +load("@rules_proto_grpc//:defs.bzl", "bazel_build_rule_common_attrs", "filter_files", "proto_compile_attrs") +load("@rules_dotnet//dotnet:defs.bzl", "csharp_library") + +def csharp_grpc_library(name, **kwargs): + # Compile protos + name_pb = name + "_pb" + csharp_grpc_compile( + name = name_pb, + **{ + k: v + for (k, v) in kwargs.items() + if k in proto_compile_attrs.keys() or + k in bazel_build_rule_common_attrs + } # Forward args + ) + + # Create csharp library + csharp_library( + name = name, + srcs = [name_pb], + deps = GRPC_DEPS + kwargs.get("deps", []), + **{ + k: v + for (k, v) in kwargs.items() + if k in bazel_build_rule_common_attrs + } # Forward Bazel common args + ) + +GRPC_DEPS = [ + Label("@protobuf//:protobuf"), + # Label("@grpc//:grpc++"), + # "@grpc.net.client//:lib", + # "@grpc.aspnetcore//:lib", + # "@core_sdk_stdlib//:libraryset", +] diff --git a/modules/csharp/csharp_proto_compile.bzl b/modules/csharp/csharp_proto_compile.bzl new file mode 100644 index 00000000..911933c0 --- /dev/null +++ b/modules/csharp/csharp_proto_compile.bzl @@ -0,0 +1,26 @@ +"""Generated definition of csharp_proto_compile.""" + +load( + "@rules_proto_grpc//:defs.bzl", + "ProtoPluginInfo", + "proto_compile_attrs", + "proto_compile_impl", + "proto_compile_toolchains", +) + +# Create compile rule +csharp_proto_compile = rule( + implementation = proto_compile_impl, + attrs = dict( + proto_compile_attrs, + _plugins = attr.label_list( + providers = [ProtoPluginInfo], + default = [ + Label("//:proto_plugin"), + ], + cfg = "exec", + doc = "List of protoc plugins to apply", + ), + ), + toolchains = proto_compile_toolchains, +) diff --git a/modules/csharp/csharp_proto_library.bzl b/modules/csharp/csharp_proto_library.bzl new file mode 100644 index 00000000..3cc1fe6a --- /dev/null +++ b/modules/csharp/csharp_proto_library.bzl @@ -0,0 +1,36 @@ +"""Generated definition of csharp_proto_library.""" + +load("@rules_proto_grpc//:defs.bzl", "bazel_build_rule_common_attrs", "filter_files", "proto_compile_attrs") +load("//:csharp_proto_compile.bzl", "csharp_proto_compile") +load("@rules_dotnet//dotnet:defs.bzl", "csharp_library") + + +def csharp_proto_library(name, **kwargs): # buildifier: disable=function-docstring + # Compile protos + name_pb = name + "_pb" + csharp_proto_compile( + name = name_pb, + **{ + k: v + for (k, v) in kwargs.items() + if k in proto_compile_attrs.keys() or + k in bazel_build_rule_common_attrs + } # Forward args + ) + + # Create csharp library + csharp_library( + name = name, + srcs = [name_pb], + deps = PROTO_DEPS + kwargs.get("deps", []), + **{ + k: v + for (k, v) in kwargs.items() + if k in bazel_build_rule_common_attrs + } # Forward Bazel common args + ) + +PROTO_DEPS = [ + Label("@protobuf//:protobuf"), + # Label("@core_sdk_stdlib//:libraryset"), # TODO needed? +] diff --git a/modules/csharp/defs.bzl b/modules/csharp/defs.bzl new file mode 100644 index 00000000..29e58f37 --- /dev/null +++ b/modules/csharp/defs.bzl @@ -0,0 +1,12 @@ +"""csharp protobuf and grpc rules.""" + +load(":csharp_proto_compile.bzl", _csharp_proto_compile = "csharp_proto_compile") +load(":csharp_grpc_compile.bzl", _csharp_grpc_compile = "csharp_grpc_compile") +load(":csharp_proto_library.bzl", _csharp_proto_library = "csharp_proto_library") +load(":csharp_grpc_library.bzl", _csharp_grpc_library = "csharp_grpc_library") + +# Export csharp rules +csharp_proto_compile = _csharp_proto_compile +csharp_grpc_compile = _csharp_grpc_compile +csharp_proto_library = _csharp_proto_library +csharp_grpc_library = _csharp_grpc_library