From 60da27b934dd391442fab0777289287cb5f01fa8 Mon Sep 17 00:00:00 2001 From: Stanimal Date: Fri, 29 May 2020 09:52:48 +0200 Subject: [PATCH] Emit cargo:rerun-if-changed directives automatically for protobuf Add a builder option to enable emitting `rerun-if-changed` cargo build directives for all proto files. This removes the need to manually maintain a list or proto files in build.rs. --- base_layer/core/build.rs | 1 + base_layer/core/src/base_node/proto/mod.rs | 1 + base_layer/core/src/proto/generated/mod.rs | 2 +- base_layer/p2p/build.rs | 3 +-- common/src/protobuf_build.rs | 13 +++++++++++++ comms/build.rs | 1 + comms/dht/build.rs | 5 +---- 7 files changed, 19 insertions(+), 7 deletions(-) diff --git a/base_layer/core/build.rs b/base_layer/core/build.rs index 087630f2a3..b0d54f0186 100644 --- a/base_layer/core/build.rs +++ b/base_layer/core/build.rs @@ -29,6 +29,7 @@ fn main() { "src/base_node/proto", "src/transactions/transaction_protocol/proto", ]) + .emit_rerun_if_changed_directives() .compile() .unwrap(); } diff --git a/base_layer/core/src/base_node/proto/mod.rs b/base_layer/core/src/base_node/proto/mod.rs index 30d6933b9b..bed326877c 100644 --- a/base_layer/core/src/base_node/proto/mod.rs +++ b/base_layer/core/src/base_node/proto/mod.rs @@ -20,6 +20,7 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#[cfg(feature = "base_node_proto")] pub use crate::proto::generated::base_node; #[cfg(feature = "base_node")] diff --git a/base_layer/core/src/proto/generated/mod.rs b/base_layer/core/src/proto/generated/mod.rs index c14b649f01..6cd44cb0cc 100644 --- a/base_layer/core/src/proto/generated/mod.rs +++ b/base_layer/core/src/proto/generated/mod.rs @@ -22,7 +22,7 @@ //! Imports of rust files generated from protos -#[cfg(feature = "base_node")] +#[cfg(feature = "base_node_proto")] #[path = "tari.base_node.rs"] pub mod base_node; #[path = "tari.core.rs"] diff --git a/base_layer/p2p/build.rs b/base_layer/p2p/build.rs index b7d1b6449e..f4306cb611 100644 --- a/base_layer/p2p/build.rs +++ b/base_layer/p2p/build.rs @@ -24,8 +24,7 @@ fn main() { tari_common::protobuf_build::ProtoCompiler::new() .proto_paths(&["src/proto"]) .out_dir("src/proto") + .emit_rerun_if_changed_directives() .compile() .unwrap(); - println!("cargo:rerun-if-changed=src/proto/liveness.proto"); - println!("cargo:rerun-if-changed=src/proto/message_type.proto"); } diff --git a/common/src/protobuf_build.rs b/common/src/protobuf_build.rs index 2027da3633..e6754c7b80 100644 --- a/common/src/protobuf_build.rs +++ b/common/src/protobuf_build.rs @@ -57,6 +57,7 @@ pub struct ProtoCompiler { field_attributes: HashMap<&'static str, &'static str>, proto_paths: Vec, include_paths: Vec, + emit_rerun_if_changed_directives: bool, } impl ProtoCompiler { @@ -67,6 +68,7 @@ impl ProtoCompiler { field_attributes: HashMap::new(), proto_paths: Vec::new(), include_paths: Vec::new(), + emit_rerun_if_changed_directives: false, } } @@ -92,6 +94,11 @@ impl ProtoCompiler { self } + pub fn emit_rerun_if_changed_directives(&mut self) -> &mut Self { + self.emit_rerun_if_changed_directives = true; + self + } + pub fn include_paths>(&mut self, include_paths: &[P]) -> &mut Self { self.include_paths .extend(include_paths.iter().map(|p| p.as_ref().to_path_buf())); @@ -166,6 +173,12 @@ impl ProtoCompiler { fs::remove_dir_all(&tmp_out_dir).map_err(|err| format!("Failed to remove temporary dir: {}", err))?; + if self.emit_rerun_if_changed_directives { + for p in &protos { + println!("cargo:rerun-if-changed={}", p.to_string_lossy()); + } + } + Ok(()) } } diff --git a/comms/build.rs b/comms/build.rs index 521800df67..f4306cb611 100644 --- a/comms/build.rs +++ b/comms/build.rs @@ -24,6 +24,7 @@ fn main() { tari_common::protobuf_build::ProtoCompiler::new() .proto_paths(&["src/proto"]) .out_dir("src/proto") + .emit_rerun_if_changed_directives() .compile() .unwrap(); } diff --git a/comms/dht/build.rs b/comms/dht/build.rs index c2ed565471..f4306cb611 100644 --- a/comms/dht/build.rs +++ b/comms/dht/build.rs @@ -24,10 +24,7 @@ fn main() { tari_common::protobuf_build::ProtoCompiler::new() .proto_paths(&["src/proto"]) .out_dir("src/proto") + .emit_rerun_if_changed_directives() .compile() .unwrap(); - println!("cargo:rerun-if-changed=src/proto/dht.proto"); - println!("cargo:rerun-if-changed=src/proto/envelope.proto"); - println!("cargo:rerun-if-changed=src/proto/message_header.proto"); - println!("cargo:rerun-if-changed=src/proto/store_forward.proto"); }