Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/release_crates/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
18 changes: 15 additions & 3 deletions rust/release_crates/cargo_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions rust/release_crates/protobuf_well_known_types/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
12 changes: 12 additions & 0 deletions rust/release_crates/protobuf_well_known_types/Cargo-template.toml
Original file line number Diff line number Diff line change
@@ -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" }
2 changes: 2 additions & 0 deletions rust/release_crates/protobuf_well_known_types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This crate contains the protobuf
[well-known types](https://protobuf.dev/reference/protobuf/google.protobuf/).
20 changes: 20 additions & 0 deletions rust/release_crates/protobuf_well_known_types/build.rs
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 1 addition & 0 deletions rust/release_crates/protobuf_well_known_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include!(concat!(env!("OUT_DIR"), "/protobuf_generated/google/protobuf/any.u.pb.rs"));
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/rust/enum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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$
Expand Down
Loading