Skip to content

Commit

Permalink
Update example crate to import a .proto file from the well-known types
Browse files Browse the repository at this point in the history
  • Loading branch information
acozzette committed Feb 25, 2025
1 parent f3c5386 commit 0e9b4be
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
21 changes: 21 additions & 0 deletions rust/release_crates/protobuf_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub struct CodeGen {
inputs: Vec<PathBuf>,
output_dir: PathBuf,
includes: Vec<PathBuf>,
options: Vec<String>,
c_includes: Vec<PathBuf>,
}

const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -90,6 +94,16 @@ impl CodeGen {
self
}

pub fn c_include(&mut self, include: impl AsRef<Path>) -> &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<PathBuf> {
self.inputs
.iter()
Expand Down Expand Up @@ -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());
Expand All @@ -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()));
Expand Down
2 changes: 2 additions & 0 deletions rust/release_crates/protobuf_example/Cargo-template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
3 changes: 3 additions & 0 deletions rust/release_crates/protobuf_example/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ edition = "2023";

package proto_example;

import "google/protobuf/timestamp.proto";
import "proto_example/bar/bar.proto";

message Foo {
int32 int = 1;
repeated int32 numbers = 2;
string name = 3;
Bar bar = 4;
google.protobuf.Timestamp timestamp = 5;
}
12 changes: 12 additions & 0 deletions rust/release_crates/protobuf_well_known_types/crate_mapping.txt
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions rust/release_crates/protobuf_well_known_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 0e9b4be

Please sign in to comment.