Skip to content

Commit

Permalink
feat(build): Add option to emit rerun-if-changed instructions (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickvangelderen committed Jun 21, 2022
1 parent 10f6d2f commit 1d2083a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tonic-build/src/prost.rs
Expand Up @@ -27,6 +27,7 @@ pub fn configure() -> Builder {
emit_package: true,
protoc_args: Vec::new(),
include_file: None,
emit_rerun_if_changed: std::env::var_os("CARGO").is_some(),
}
}

Expand Down Expand Up @@ -224,6 +225,7 @@ pub struct Builder {
pub(crate) compile_well_known_types: bool,
pub(crate) protoc_args: Vec<OsString>,
pub(crate) include_file: Option<PathBuf>,
pub(crate) emit_rerun_if_changed: bool,

out_dir: Option<PathBuf>,
}
Expand Down Expand Up @@ -368,6 +370,24 @@ impl Builder {
self
}

/// Enable or disable emitting
/// [`cargo:rerun-if-changed=PATH`](https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed)
/// instructions for Cargo.
///
/// If set, writes instructions to `stdout` for Cargo so that it understands
/// when to rerun the build script. By default, this setting is enabled if
/// the `CARGO` environment variable is set. The `CARGO` environment
/// variable is set by Cargo for build scripts. Therefore, this setting
/// should be enabled automatically when run from a build script. However,
/// the method of detection is not completely reliable since the `CARGO`
/// environment variable can have been set by anything else. If writing the
/// instructions to `stdout` is undesireable, you can disable this setting
/// explicitly.
pub fn emit_rerun_if_changed(mut self, enable: bool) -> Self {
self.emit_rerun_if_changed = enable;
self
}

/// Compile the .proto files and execute code generation.
pub fn compile(
self,
Expand Down Expand Up @@ -415,6 +435,19 @@ impl Builder {
config.protoc_arg(arg);
}

if self.emit_rerun_if_changed {
for path in protos.iter() {
println!("cargo:rerun-if-changed={}", path.as_ref().display())
}

for path in includes.iter() {
// Cargo will watch the **entire** directory recursively. If we
// could figure out which files are imported by our protos we
// could specify only those files instead.
println!("cargo:rerun-if-changed={}", path.as_ref().display())
}
}

config.service_generator(self.service_generator());

config.compile_protos(protos, includes)?;
Expand Down

0 comments on commit 1d2083a

Please sign in to comment.