Skip to content

Commit

Permalink
Rust: make generated code refer to ::protobuf instead of ::__pb
Browse files Browse the repository at this point in the history
This required a tweak to the Bazel aspect to make it alias the appropriate
runtime (C++ or upb) under the name `protobuf`.

The nice thing about this is that it allows the same generated code to work
with both Bazel and Cargo. With Bazel the runtime crate is named `protobuf_cpp`
or `protobuf_upb`, whereas with the Cargo build it is just `protobuf`.

PiperOrigin-RevId: 703143472
  • Loading branch information
acozzette authored and copybara-github committed Dec 5, 2024
1 parent f19007f commit c18384c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
10 changes: 8 additions & 2 deletions rust/aspects.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def _compile_cc(
linking_context = linking_context,
)

def _compile_rust(ctx, attr, src, extra_srcs, deps):
def _compile_rust(ctx, attr, src, extra_srcs, deps, runtime):
"""Compiles a Rust source file.
Eventually this function could be upstreamed into rules_rust and be made present in rust_common.
Expand All @@ -208,6 +208,7 @@ def _compile_rust(ctx, attr, src, extra_srcs, deps):
src (File): The crate root source file to be compiled.
extra_srcs ([File]): Additional source files to include in the crate.
deps (List[DepVariantInfo]): A list of dependencies needed.
runtime: The protobuf runtime target.
Returns:
A DepVariantInfo provider.
Expand Down Expand Up @@ -246,7 +247,11 @@ def _compile_rust(ctx, attr, src, extra_srcs, deps):
srcs = depset([src] + extra_srcs),
deps = depset(deps),
proc_macro_deps = depset([]),
aliases = {},
# Make "protobuf" into an alias for the runtime. This allows the
# generated code to use a consistent name, even though the actual
# name of the runtime crate varies depending on the protobuf kernel
# and build system.
aliases = {runtime: "protobuf"},
output = lib,
metadata = rmeta,
edition = "2021",
Expand Down Expand Up @@ -355,6 +360,7 @@ def _rust_proto_aspect_common(target, ctx, is_upb):
src = gencode[0],
extra_srcs = gencode[1:],
deps = [dep_variant_info_for_runtime, dep_variant_info_for_native_gencode] + dep_variant_infos,
runtime = runtime,
)
return [RustProtoInfo(
dep_variant_infos = [dep_variant_info],
Expand Down
6 changes: 1 addition & 5 deletions rust/release_crates/protobuf_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ impl CodeGen {

// Adjust the generated Rust code to work with the crate structure.
fn fix_rs_gencode(path: &Path) {
let contents = fs::read_to_string(path)
.unwrap()
.replace("crate::", "")
.replace("protobuf_upb", "protobuf")
.replace("::__pb", "__pb");
let contents = fs::read_to_string(path).unwrap().replace("crate::", "");
let mut file = OpenOptions::new().write(true).truncate(true).open(path).unwrap();
file.write(contents.as_bytes()).unwrap();
}
Expand Down
11 changes: 3 additions & 8 deletions src/google/protobuf/compiler/rust/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,15 @@ bool RustGenerator::Generate(const FileDescriptor* file,
// Convenience shorthands for common symbols.
auto v = ctx.printer().WithVars({
{"std", "::std"},
{"pb", "::__pb"},
{"pbi", "::__pb::__internal"},
{"pbr", "::__pb::__runtime"},
{"pb", "::protobuf"},
{"pbi", "::protobuf::__internal"},
{"pbr", "::protobuf::__runtime"},
{"NonNull", "::std::ptr::NonNull"},
{"Phantom", "::std::marker::PhantomData"},
{"Result", "::std::result::Result"},
{"Option", "::std::option::Option"},
});

ctx.Emit({{"kernel", KernelRsName(ctx.opts().kernel)}}, R"rs(
extern crate protobuf_$kernel$ as __pb;
)rs");

std::vector<const FileDescriptor*> file_contexts(
files_in_current_crate.begin(), files_in_current_crate.end());

Expand Down
6 changes: 3 additions & 3 deletions src/google/protobuf/compiler/rust/naming.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ std::string RsTypePath(Context& ctx, const FieldDescriptor& field) {
case RustFieldType::DOUBLE:
return "f64";
case RustFieldType::BYTES:
return "::__pb::ProtoBytes";
return "::protobuf::ProtoBytes";
case RustFieldType::STRING:
return "::__pb::ProtoString";
return "::protobuf::ProtoString";
case RustFieldType::MESSAGE:
return GetFullyQualifiedPath(ctx, *field.message_type());
case RustFieldType::ENUM:
Expand All @@ -169,7 +169,7 @@ std::string RsViewType(Context& ctx, const FieldDescriptor& field,
case RustFieldType::BYTES:
return absl::StrFormat("&%s [u8]", lifetime);
case RustFieldType::STRING:
return absl::StrFormat("&%s ::__pb::ProtoStr", lifetime);
return absl::StrFormat("&%s ::protobuf::ProtoStr", lifetime);
case RustFieldType::MESSAGE:
if (lifetime.empty()) {
return absl::StrFormat(
Expand Down
8 changes: 5 additions & 3 deletions src/google/protobuf/compiler/rust/oneof.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ std::string RsTypeNameView(Context& ctx, const FieldDescriptor& field) {
case RustFieldType::BYTES:
return "&'msg [u8]";
case RustFieldType::STRING:
return "&'msg ::__pb::ProtoStr";
return "&'msg ::protobuf::ProtoStr";
case RustFieldType::MESSAGE:
return absl::StrCat("::__pb::View<'msg, ", RsTypePath(ctx, field), ">");
return absl::StrCat("::protobuf::View<'msg, ", RsTypePath(ctx, field),
">");
case RustFieldType::ENUM:
return absl::StrCat("::__pb::View<'msg, ", RsTypePath(ctx, field), ">");
return absl::StrCat("::protobuf::View<'msg, ", RsTypePath(ctx, field),
">");
}

ABSL_LOG(FATAL) << "Unexpected field type: " << field.type_name();
Expand Down

0 comments on commit c18384c

Please sign in to comment.