Skip to content

Commit

Permalink
feat(build): Add prostoc_args (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
grippy authored Apr 7, 2021
1 parent 380049d commit 480a794
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tonic-build/src/prost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -22,6 +23,7 @@ pub fn configure() -> Builder {
#[cfg(feature = "rustfmt")]
format: true,
emit_package: true,
protoc_args: Vec::new(),
}
}

Expand Down Expand Up @@ -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<OsString>,

out_dir: Option<PathBuf>,
#[cfg(feature = "rustfmt")]
Expand Down Expand Up @@ -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<A: AsRef<str>>(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.
Expand Down Expand Up @@ -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)?;
Expand Down

0 comments on commit 480a794

Please sign in to comment.