From 480a79409c4cb9a1c680e57d0f74ad1d4f18beaa Mon Sep 17 00:00:00 2001 From: Greg Melton Date: Wed, 7 Apr 2021 14:36:46 -0700 Subject: [PATCH] feat(build): Add `prostoc_args` (#577) --- tonic-build/src/prost.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tonic-build/src/prost.rs b/tonic-build/src/prost.rs index 49c4bfde3..596c60bd5 100644 --- a/tonic-build/src/prost.rs +++ b/tonic-build/src/prost.rs @@ -2,6 +2,7 @@ use super::{client, server}; use proc_macro2::TokenStream; use prost_build::{Config, Method, Service}; use quote::ToTokens; +use std::ffi::OsString; use std::io; use std::path::{Path, PathBuf}; @@ -22,6 +23,7 @@ pub fn configure() -> Builder { #[cfg(feature = "rustfmt")] format: true, emit_package: true, + protoc_args: Vec::new(), } } @@ -206,6 +208,7 @@ pub struct Builder { pub(crate) proto_path: String, pub(crate) emit_package: bool, pub(crate) compile_well_known_types: bool, + pub(crate) protoc_args: Vec, out_dir: Option, #[cfg(feature = "rustfmt")] @@ -287,6 +290,14 @@ impl Builder { self } + /// Configure Prost `protoc_args` build arguments. + /// + /// Note: Enabling `--experimental_allow_proto3_optional` requires protobuf >= 3.12. + pub fn protoc_arg>(mut self, arg: A) -> Self { + self.protoc_args.push(arg.as_ref().into()); + self + } + /// Emits GRPC endpoints with no attached package. Effectively ignores protofile package declaration from grpc context. /// /// This effectively sets prost's exported package to an empty string. @@ -348,6 +359,11 @@ impl Builder { if self.compile_well_known_types { config.compile_well_known_types(); } + + for arg in self.protoc_args.iter() { + config.protoc_arg(arg); + } + config.service_generator(Box::new(ServiceGenerator::new(self))); config.compile_protos(protos, includes)?;