Rust: set up a new crate for the well-known types

It's not yet possible to import any of the well-known types from a proto file
in another crate. To enable this I think we'll need to generate a function that
dependent crates can call to determine the directory that contains the
well-known types.

PiperOrigin-RevId: 706745114
pull/19559/head
Adam Cozzette 2 months ago committed by Copybara-Service
parent 4681c2bd96
commit 512319bc78
  1. 1
      rust/release_crates/BUILD
  2. 18
      rust/release_crates/cargo_test.sh
  3. 38
      rust/release_crates/protobuf_well_known_types/BUILD.bazel
  4. 12
      rust/release_crates/protobuf_well_known_types/Cargo-template.toml
  5. 2
      rust/release_crates/protobuf_well_known_types/README.md
  6. 20
      rust/release_crates/protobuf_well_known_types/build.rs
  7. 1
      rust/release_crates/protobuf_well_known_types/src/lib.rs
  8. 2
      src/google/protobuf/compiler/rust/enum.cc

@ -8,6 +8,7 @@ sh_binary(
"//rust/release_crates/protobuf:protobuf_crate",
"//rust/release_crates/protobuf_codegen:protobuf_codegen_crate",
"//rust/release_crates/protobuf_example:protobuf_example_crate",
"//rust/release_crates/protobuf_well_known_types:crate",
"//src/google/protobuf/compiler:protoc",
"//upb_generator/minitable:protoc-gen-upb_minitable",
],

@ -40,15 +40,15 @@ mkdir $CRATE_ROOT
PROTOBUF_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf/protobuf_crate.tar)
echo "Expanding protobuf crate tar"
tar -xvf $PROTOBUF_TAR -C $CRATE_ROOT
tar -xvf $PROTOBUF_TAR -C $CRATE_ROOT
CODEGEN_ROOT=$TMP_DIR/protobuf_codegen
mkdir $CODEGEN_ROOT
CODEGEN_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf_codegen/protobuf_codegen_crate.tar)
echo "Expanding protbuf_codegen crate tar"
tar -xvf $CODEGEN_TAR -C $CODEGEN_ROOT
echo "Expanding protobuf_codegen crate tar"
tar -xvf $CODEGEN_TAR -C $CODEGEN_ROOT
EXAMPLE_ROOT=$TMP_DIR/protobuf_example
mkdir $EXAMPLE_ROOT
@ -58,6 +58,14 @@ EXAMPLE_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf_example
echo "Expanding protobuf_example crate tar"
tar -xvf $EXAMPLE_TAR -C $EXAMPLE_ROOT
WELL_KNOWN_TYPES_ROOT=$TMP_DIR/protobuf_well_known_types
mkdir $WELL_KNOWN_TYPES_ROOT
WELL_KNOWN_TYPES_TAR=$(rlocation com_google_protobuf/rust/release_crates/protobuf_well_known_types/crate.tar)
echo "Expanding protobuf_well_known_types crate tar"
tar -xvf $WELL_KNOWN_TYPES_TAR -C $WELL_KNOWN_TYPES_ROOT
# Put the Bazel-built protoc and plugin at the beginning of $PATH
PATH=$(dirname $(rlocation com_google_protobuf/protoc)):$PATH
PATH=$(dirname $(rlocation com_google_protobuf/upb_generator/minitable/protoc-gen-upb_minitable)):$PATH
@ -72,6 +80,10 @@ CARGO_HOME=$CARGO_HOME cargo publish --dry-run
cd $EXAMPLE_ROOT
CARGO_HOME=$CARGO_HOME cargo test
cd $WELL_KNOWN_TYPES_ROOT
CARGO_HOME=$CARGO_HOME cargo test
# TODO: Cannot enable this dry-run yet because it checks that the versions of
# its dependencies are published on crates.io, which they are definitely not
# in this case.

@ -0,0 +1,38 @@
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("//:protobuf_version.bzl", "PROTOBUF_RUST_VERSION")
pkg_tar(
name = "crate",
srcs = [
":crate_root_files",
":well_known_types",
"//:LICENSE",
],
visibility = ["//rust:__subpackages__"],
)
pkg_files(
name = "crate_root_files",
srcs = [":srcs"],
strip_prefix = strip_prefix.from_root("rust/release_crates/protobuf_well_known_types"),
)
genrule(
name = "gen_cargo_toml",
srcs = ["Cargo-template.toml"],
outs = ["Cargo.toml"],
cmd = "cat $(SRCS) | sed -e 's/{{VERSION}}/{0}-beta/g' > $(OUTS)".format(PROTOBUF_RUST_VERSION),
)
filegroup(
name = "srcs",
srcs = [":gen_cargo_toml"] + glob(["**/*"]),
visibility = ["//rust:__subpackages__"],
)
pkg_files(
name = "well_known_types",
srcs = ["//src/google/protobuf:well_known_type_protos"],
prefix = "proto/google/protobuf",
)

@ -0,0 +1,12 @@
[package]
name = "staging-protobuf-well-known-types"
version = "{VERSION}"
edition = "2021"
description = "Protobuf Well-Known Types"
license = "BSD-3-Clause"
[dependencies]
protobuf = { version = "{VERSION}", path = "../protobuf", package = "staging-protobuf" }
[build-dependencies]
protobuf-codegen = { version = "{VERSION}", path = "../protobuf_codegen", package = "staging-protobuf-codegen" }

@ -0,0 +1,2 @@
This crate contains the protobuf
[well-known types](https://protobuf.dev/reference/protobuf/google.protobuf/).

@ -0,0 +1,20 @@
use protobuf_codegen::CodeGen;
fn main() {
CodeGen::new()
.inputs([
"google/protobuf/any.proto",
"google/protobuf/api.proto",
"google/protobuf/duration.proto",
"google/protobuf/empty.proto",
"google/protobuf/field_mask.proto",
"google/protobuf/source_context.proto",
"google/protobuf/struct.proto",
"google/protobuf/timestamp.proto",
"google/protobuf/type.proto",
"google/protobuf/wrappers.proto",
])
.include("proto")
.generate_and_compile()
.unwrap();
}

@ -0,0 +1 @@
include!(concat!(env!("OUT_DIR"), "/protobuf_generated/google/protobuf/any.u.pb.rs"));

@ -176,7 +176,7 @@ void GenerateEnumDefinition(Context& ctx, const EnumDescriptor& desc) {
}
}}},
R"rs(
fn constant_name(&self) -> Option<&'static str> {
fn constant_name(&self) -> $Option$<&'static str> {
#[allow(unreachable_patterns)] // In the case of aliases, just emit them all and let the first one match.
Some(match self.0 {
$name_cases$

Loading…
Cancel
Save