Skip to content

Commit

Permalink
Emit cargo:rerun-if-changed directives automatically for protobuf
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sdbondi committed May 29, 2020
1 parent ee06f9e commit 60da27b
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions base_layer/core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn main() {
"src/base_node/proto",
"src/transactions/transaction_protocol/proto",
])
.emit_rerun_if_changed_directives()
.compile()
.unwrap();
}
1 change: 1 addition & 0 deletions base_layer/core/src/base_node/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/proto/generated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 1 addition & 2 deletions base_layer/p2p/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
13 changes: 13 additions & 0 deletions common/src/protobuf_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct ProtoCompiler {
field_attributes: HashMap<&'static str, &'static str>,
proto_paths: Vec<PathBuf>,
include_paths: Vec<PathBuf>,
emit_rerun_if_changed_directives: bool,
}

impl ProtoCompiler {
Expand All @@ -67,6 +68,7 @@ impl ProtoCompiler {
field_attributes: HashMap::new(),
proto_paths: Vec::new(),
include_paths: Vec::new(),
emit_rerun_if_changed_directives: false,
}
}

Expand All @@ -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<P: AsRef<Path>>(&mut self, include_paths: &[P]) -> &mut Self {
self.include_paths
.extend(include_paths.iter().map(|p| p.as_ref().to_path_buf()));
Expand Down Expand Up @@ -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(())
}
}
1 change: 1 addition & 0 deletions comms/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
5 changes: 1 addition & 4 deletions comms/dht/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 60da27b

Please sign in to comment.