From f93ab9c8dbe148f9457db7878b9704795b8878b6 Mon Sep 17 00:00:00 2001 From: Colin Mikolajczak Date: Mon, 19 Sep 2022 15:57:18 +0200 Subject: [PATCH] feat: create descriptor on gen 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 --- Cargo.lock | 76 +++++++++++++++++++++++++++------------------ Cargo.toml | 5 +-- src/Cargo.toml.tmpl | 1 + src/main.rs | 34 ++++++++++++++++++-- 4 files changed, 81 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6bf838e..4d285be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,12 +46,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" -[[package]] -name = "cc" -version = "1.0.73" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" - [[package]] name = "cfg-if" version = "1.0.0" @@ -73,15 +67,6 @@ dependencies = [ "vec_map", ] -[[package]] -name = "cmake" -version = "0.1.48" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a" -dependencies = [ - "cc", -] - [[package]] name = "codegen" version = "0.1.3" @@ -190,9 +175,9 @@ checksum = "21a41fed9d98f27ab1c6d161da622a4fa35e8a54a8adc24bbf3ddd0ef70b0e50" [[package]] name = "log" -version = "0.4.16" +version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6389c490849ff5bc16be905ae24bc913a9c8892e19b2341dbc175e14c341c2b8" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if", ] @@ -258,9 +243,9 @@ dependencies = [ [[package]] name = "prost" -version = "0.10.1" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a07b0857a71a8cb765763950499cae2413c3f9cede1133478c43600d9e146890" +checksum = "399c3c31cdec40583bb68f0b18403400d01ec4289c383aa047560439952c4dd7" dependencies = [ "bytes", "prost-derive", @@ -268,13 +253,11 @@ dependencies = [ [[package]] name = "prost-build" -version = "0.10.1" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "120fbe7988713f39d780a58cf1a7ef0d7ef66c6d87e5aa3438940c05357929f4" +checksum = "7f835c582e6bd972ba8347313300219fed5bfa52caf175298d860b61ff6069bb" dependencies = [ "bytes", - "cfg-if", - "cmake", "heck 0.4.0", "itertools", "lazy_static", @@ -290,9 +273,9 @@ dependencies = [ [[package]] name = "prost-derive" -version = "0.10.1" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b670f45da57fb8542ebdbb6105a925fe571b67f9e7ed9f47a06a84e72b4e7cc" +checksum = "7345d5f0e08c0536d7ac7229952590239e77abf0a0100a1b1d890add6ea96364" dependencies = [ "anyhow", "itertools", @@ -301,11 +284,43 @@ dependencies = [ "syn", ] +[[package]] +name = "prost-reflect" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d378290cd658b119ce87621931ef448017ef1a0044d7b681159d779e7e07b8f6" +dependencies = [ + "prost", + "prost-reflect-derive", + "prost-types", +] + +[[package]] +name = "prost-reflect-build" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08aa774b486000d80ef9a63875c4cba1acc4024d4a23a3bb588f8b680e2c64fb" +dependencies = [ + "prost-build", + "prost-reflect", +] + +[[package]] +name = "prost-reflect-derive" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90bf611b6adcffdb7c42d1826a751b520d078f753f96112ae4ec9ba2eb78663" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "prost-types" -version = "0.10.1" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d0a014229361011dc8e69c8a1ec6c2e8d0f2af7c91e3ea3f5b2170298461e68" +checksum = "4dfaa718ad76a44b3415e6c4d53b17c8f99160dcb3a99b10470fce8ad43f6e3e" dependencies = [ "bytes", "prost", @@ -313,11 +328,12 @@ dependencies = [ [[package]] name = "protocrate" -version = "0.1.0" +version = "0.1.1" dependencies = [ "anyhow", "codegen", "prost-build", + "prost-reflect-build", "structopt", "tempdir", "tonic-build", @@ -488,9 +504,9 @@ dependencies = [ [[package]] name = "tonic-build" -version = "0.7.1" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c03447cdc9eaf8feffb6412dcb27baf2db11669a6c4789f29da799aabfb99547" +checksum = "2fbcd2800e34e743b9ae795867d5f77b535d3a3be69fd731e39145719752df8c" dependencies = [ "prettyplease", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 0722f0e..442925c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/Cargo.toml.tmpl b/src/Cargo.toml.tmpl index 506ddbf..64608ad 100644 --- a/src/Cargo.toml.tmpl +++ b/src/Cargo.toml.tmpl @@ -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" diff --git a/src/main.rs b/src/main.rs index bb10823..739cbb5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { @@ -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. @@ -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. @@ -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 + = 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")?