From 1d2083a1a690edcb3f95343edfe229339c4257b7 Mon Sep 17 00:00:00 2001 From: Mick van Gelderen Date: Tue, 21 Jun 2022 16:27:46 +0200 Subject: [PATCH] feat(build): Add option to emit rerun-if-changed instructions (#1021) --- tonic-build/src/prost.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tonic-build/src/prost.rs b/tonic-build/src/prost.rs index 779414dd9..b6b5bf121 100644 --- a/tonic-build/src/prost.rs +++ b/tonic-build/src/prost.rs @@ -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(), } } @@ -224,6 +225,7 @@ pub struct Builder { pub(crate) compile_well_known_types: bool, pub(crate) protoc_args: Vec, pub(crate) include_file: Option, + pub(crate) emit_rerun_if_changed: bool, out_dir: Option, } @@ -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, @@ -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)?;