Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): Add prostoc_args #577

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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