diff --git a/rust/release_crates/protobuf_codegen/src/lib.rs b/rust/release_crates/protobuf_codegen/src/lib.rs index fb708e6de2eaf..6d817e88086a2 100644 --- a/rust/release_crates/protobuf_codegen/src/lib.rs +++ b/rust/release_crates/protobuf_codegen/src/lib.rs @@ -5,6 +5,8 @@ pub struct CodeGen { inputs: Vec, output_dir: PathBuf, includes: Vec, + options: Vec, + c_includes: Vec, } const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -62,6 +64,8 @@ impl CodeGen { inputs: Vec::new(), output_dir: PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("protobuf_generated"), includes: Vec::new(), + options: Vec::new(), + c_includes: Vec::new(), } } @@ -90,6 +94,16 @@ impl CodeGen { self } + pub fn c_include(&mut self, include: impl AsRef) -> &mut Self { + self.c_includes.push(include.as_ref().to_owned()); + self + } + + pub fn option(&mut self, option: String) -> &mut Self { + self.options.push(option); + self + } + fn expected_generated_rs_files(&self) -> Vec { self.inputs .iter() @@ -154,6 +168,9 @@ impl CodeGen { for include in &self.includes { cmd.arg(format!("--proto_path={}", include.display())); } + for option in &self.options { + cmd.arg(format!("--rust_opt={}", option)); + } let output = cmd.output().map_err(|e| format!("failed to run protoc: {}", e))?; println!("{}", std::str::from_utf8(&output.stdout).unwrap()); eprintln!("{}", std::str::from_utf8(&output.stderr).unwrap()); @@ -172,6 +189,10 @@ impl CodeGen { .include(self.output_dir.clone()) .flag("-std=c99"); + for path in &self.c_includes { + cc_build.include(path); + } + for path in &self.expected_generated_rs_files() { if !path.exists() { return Err(format!("expected generated file {} does not exist", path.display())); diff --git a/rust/release_crates/protobuf_example/Cargo-template.toml b/rust/release_crates/protobuf_example/Cargo-template.toml index ffa002877b613..d430d15a576a6 100644 --- a/rust/release_crates/protobuf_example/Cargo-template.toml +++ b/rust/release_crates/protobuf_example/Cargo-template.toml @@ -7,6 +7,8 @@ license = "BSD-3-Clause" [dependencies] protobuf = { version = "{VERSION}", path = "../protobuf", package = "protobuf" } +protobuf_well_known_types = { version = "{VERSION}", path = "../protobuf_well_known_types", package = "protobuf-well-known-types" } [build-dependencies] protobuf-codegen = { version = "{VERSION}", path = "../protobuf_codegen", package = "protobuf-codegen" } +protobuf_well_known_types = { version = "{VERSION}", path = "../protobuf_well_known_types", package = "protobuf-well-known-types" } diff --git a/rust/release_crates/protobuf_example/build.rs b/rust/release_crates/protobuf_example/build.rs index 0924c662d408a..3aff590af7b39 100644 --- a/rust/release_crates/protobuf_example/build.rs +++ b/rust/release_crates/protobuf_example/build.rs @@ -4,6 +4,9 @@ fn main() { CodeGen::new() .inputs(["proto_example/foo.proto", "proto_example/bar/bar.proto"]) .include("proto") + .include(protobuf_well_known_types::import_path()) + .c_include(protobuf_well_known_types::include_path()) + .option(format!("bazel_crate_mapping={}", protobuf_well_known_types::crate_mapping().display())) .generate_and_compile() .unwrap(); } diff --git a/rust/release_crates/protobuf_example/proto/proto_example/foo.proto b/rust/release_crates/protobuf_example/proto/proto_example/foo.proto index 83f3c4aaabaa2..19c12903ea6d6 100644 --- a/rust/release_crates/protobuf_example/proto/proto_example/foo.proto +++ b/rust/release_crates/protobuf_example/proto/proto_example/foo.proto @@ -2,6 +2,7 @@ edition = "2023"; package proto_example; +import "google/protobuf/timestamp.proto"; import "proto_example/bar/bar.proto"; message Foo { @@ -9,4 +10,5 @@ message Foo { repeated int32 numbers = 2; string name = 3; Bar bar = 4; + google.protobuf.Timestamp timestamp = 5; } diff --git a/rust/release_crates/protobuf_well_known_types/crate_mapping.txt b/rust/release_crates/protobuf_well_known_types/crate_mapping.txt new file mode 100644 index 0000000000000..2bd6558148285 --- /dev/null +++ b/rust/release_crates/protobuf_well_known_types/crate_mapping.txt @@ -0,0 +1,12 @@ +protobuf_well_known_types +10 +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 diff --git a/rust/release_crates/protobuf_well_known_types/src/lib.rs b/rust/release_crates/protobuf_well_known_types/src/lib.rs index 9a7c8b7e9bb45..72d911076226c 100644 --- a/rust/release_crates/protobuf_well_known_types/src/lib.rs +++ b/rust/release_crates/protobuf_well_known_types/src/lib.rs @@ -1 +1,16 @@ +use std::path::Path; +use std::path::PathBuf; + include!(concat!(env!("OUT_DIR"), "/protobuf_generated/google/protobuf/generated.rs")); + +pub fn import_path() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("proto") +} + +pub fn include_path() -> PathBuf { + Path::new(env!("OUT_DIR")).join("protobuf_generated") +} + +pub fn crate_mapping() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("crate_mapping.txt") +}