Enable support for rust_upb_proto_library and rust_cc_proto_library within the same build

PiperOrigin-RevId: 520314976
pull/12357/head
Jakob Buchgraber 2 years ago committed by Copybara-Service
parent b72eb3f233
commit dbf97258bd
  1. 15
      rust/BUILD
  2. 10
      rust/aspects.bzl
  3. 13
      src/google/protobuf/compiler/rust/generator.cc

@ -37,11 +37,16 @@ rust_test(
# TODO(b/270125787): Move to the right location once rust_proto_library is no longer experimental.
proto_lang_toolchain(
name = "proto_lang_toolchain",
command_line = "--rust_out=experimental-codegen=enabled,kernel=" + select({
":use_upb_kernel": "upb",
"//conditions:default": "cpp",
}) + ":$(OUT)",
name = "proto_rust_upb_toolchain",
command_line = "--rust_out=experimental-codegen=enabled,kernel=upb:$(OUT)",
progress_message = "Generating Rust proto_library %{label}",
runtime = ":protobuf",
visibility = ["//visibility:public"],
)
proto_lang_toolchain(
name = "proto_rust_cpp_toolchain",
command_line = "--rust_out=experimental-codegen=enabled,kernel=cpp:$(OUT)",
progress_message = "Generating Rust proto_library %{label}",
runtime = ":protobuf",
visibility = ["//visibility:public"],

@ -24,7 +24,8 @@ RustProtoInfo = provider(
def _generate_rust_gencode(
ctx,
proto_info,
proto_lang_toolchain):
proto_lang_toolchain,
ext):
"""Generates Rust gencode
This function uses proto_common APIs and a ProtoLangToolchain to register an action
@ -34,6 +35,7 @@ def _generate_rust_gencode(
proto_info (ProtoInfo): ProtoInfo of the proto_library target for which we are generating
gencode
proto_lang_toolchain (ProtoLangToolchainInfo): proto lang toolchain for Rust
ext: the file extension to use for generated files
Returns:
rs_outputs ([File]): generated Rust source files
"""
@ -41,7 +43,7 @@ def _generate_rust_gencode(
rs_outputs = proto_common.declare_generated_files(
actions = actions,
proto_info = proto_info,
extension = ".pb.rs",
extension = ext,
)
proto_common.compile(
@ -164,7 +166,7 @@ def _rust_proto_aspect_common(target, ctx, is_upb):
proto_lang_toolchain = ctx.attr._proto_lang_toolchain[proto_common.ProtoLangToolchainInfo]
gencode = _generate_rust_gencode(ctx, target[ProtoInfo], proto_lang_toolchain)
gencode = _generate_rust_gencode(ctx, target[ProtoInfo], proto_lang_toolchain, (".u.pb.rs" if is_upb else ".c.pb.rs"))
runtime = proto_lang_toolchain.runtime
dep_variant_info_for_runtime = DepVariantInfo(
@ -234,7 +236,7 @@ def _make_proto_library_aspect(is_upb):
cfg = "exec",
),
"_proto_lang_toolchain": attr.label(
default = Label("//rust:proto_lang_toolchain"),
default = Label(("//rust:proto_rust_upb_toolchain" if is_upb else "//rust:proto_rust_cpp_toolchain")),
),
},
fragments = ["cpp"],

@ -88,6 +88,15 @@ std::string GetCrateName(const FileDescriptor* dependency) {
});
}
std::string GetFileExtensionForKernel(Kernel kernel) {
switch (kernel) {
case Kernel::kUpb:
return ".u.pb.rs";
case Kernel::kCpp:
return ".c.pb.rs";
}
}
void GenerateForUpb(const FileDescriptor* file, google::protobuf::io::Printer& p) {
for (int i = 0; i < file->message_type_count(); ++i) {
// The prefix used by the UPB compiler to generate unique function
@ -171,8 +180,8 @@ bool RustGenerator::Generate(const FileDescriptor* file,
}
auto basename = StripProto(file->name());
auto outfile = absl::WrapUnique(
generator_context->Open(absl::StrCat(basename, ".pb.rs")));
auto outfile = absl::WrapUnique(generator_context->Open(
absl::StrCat(basename, GetFileExtensionForKernel(*kernel))));
google::protobuf::io::Printer p(outfile.get());
p.Emit(R"rs(

Loading…
Cancel
Save