Skip to content

Commit

Permalink
feat: create descriptor on gen
Browse files Browse the repository at this point in the history
For an eventual native lcm implementation in rust, with protobuf support, we need the proto filedescriptor in order to decode protomessages,  also a reflective name method is added to allow communication with einride go implementation of lcm
  • Loading branch information
ColinAtEinride committed Oct 9, 2022
1 parent 24469aa commit f93ab9c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 35 deletions.
76 changes: 46 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ edition = "2018"
[dependencies]
anyhow = "1.0.57"
codegen = "0.1.3"
prost-build = "0.10"
prost-build = "0.11.1"
structopt = "0.3.26"
walkdir = "2.3.2"
tonic-build = {version = "0.7", default-features = false, features = ["prost", "transport"] }
tonic-build = {version = "0.8.0", default-features = false, features = ["prost", "transport"] }
prost-reflect-build = "0.9.0"

[dev-dependencies]
tempdir = "0.3.7"
1 change: 1 addition & 0 deletions src/Cargo.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ bytes = "1.0"
prost = "0.7"
prost-types = "0.7"
tonic = { version = "0.4", default-features = false, features = ["transport", "prost"] }
once_cell = "1.15.0"
34 changes: 31 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use walkdir::WalkDir;

use module::Module;

pub const DESCRIPTOR_PATH: &str = "resources";

#[derive(StructOpt, Debug)]
#[structopt(name = env!("CARGO_PKG_NAME"))]
struct Opt {
Expand Down Expand Up @@ -42,6 +44,7 @@ fn main() -> Result<()> {
let opt = Opt::from_args();
let src_dir = opt.output_dir.join("src");
let _ignore_err = std::fs::remove_dir_all(&src_dir);
fs::create_dir_all(DESCRIPTOR_PATH).context(format!("create dir ({})", src_dir.display()))?;
fs::create_dir_all(&src_dir).context(format!("create dir ({})", src_dir.display()))?;
{
// Find all .proto files in any of the root paths.
Expand All @@ -57,10 +60,27 @@ fn main() -> Result<()> {
})
.collect();
proto_paths.sort();
// And generate protobuf/gRPC code.

let descriptor_path = PathBuf::from(&src_dir)
.parent()
.unwrap()
.join(DESCRIPTOR_PATH)
.join("descriptor.bin");

let mut config = prost_build::Config::new();
config
.file_descriptor_set_path(descriptor_path)
.out_dir(&src_dir);
let mut builder = prost_reflect_build::Builder::new();
builder
.configure(&mut config, &proto_paths[..], &opt.root[..])
.context(format!(
"generate reflective protobuf ({})",
src_dir.display()
))?;
tonic_build::configure()
.out_dir(&src_dir)
.compile(&proto_paths[..], &opt.root[..])
.out_dir(&src_dir) // needed because tonic looks in a different place for the out dir than prost
.compile_with_config(config, &proto_paths[..], &opt.root[..])
.context(format!("generate protobuf ({})", src_dir.display()))?;
}
// Generate a lib.rs file containing all the module definitions and use statements.
Expand All @@ -70,6 +90,14 @@ fn main() -> Result<()> {
scope.raw("#![allow(clippy::wrong_self_convention)]");
scope.raw("#![allow(clippy::large_enum_variant)]");
scope.raw("#![allow(clippy::unreadable_literal)]");

// Adding getter for descriptor pool
let line = format!("static DESCRIPTOR_POOL: Lazy<DescriptorPool>
= Lazy::new(|| DescriptorPool::decode(include_bytes!(\"{}/descriptor.bin\").as_ref()).unwrap());", DESCRIPTOR_PATH);
scope.import("prost_reflect", "DescriptorPool");
scope.import("once_cell::sync", "Lazy");
scope.raw(line.as_str());

Module::build(Path::new(&src_dir), &[&lib_rs_path])?.codegen(&mut scope);
File::create(&lib_rs_path)
.context("create lib.rs")?
Expand Down

0 comments on commit f93ab9c

Please sign in to comment.