diff --git a/crates/papyrus_network/build.rs b/crates/papyrus_network/build.rs index 8a7b721ea5..b1604be3d1 100644 --- a/crates/papyrus_network/build.rs +++ b/crates/papyrus_network/build.rs @@ -1,13 +1,37 @@ -use std::io::Result; +use std::env; +use std::io::{Error, ErrorKind, Result}; +use std::process::Command; fn main() -> Result<()> { println!("Building"); - prost_build::compile_protos( - &[ - "src/protobuf_messages/proto/p2p/proto/header.proto", - "src/protobuf_messages/proto/p2p/proto/state.proto", - ], - &["src/protobuf_messages/proto/"], - )?; - Ok(()) + let protoc = env::var("PROTOC").unwrap_or("protoc".to_string()); + + let protoc_version = String::from_utf8_lossy( + &Command::new(protoc).arg("--version").output().expect("Protoc is not installed.").stdout, + ) + .to_string(); + + let parts: Vec<&str> = protoc_version.split_whitespace().collect(); + let protoc_version_str = parts.get(1).expect("Failed to determine protoc version"); + let mut protoc_version_parts = protoc_version_str + .split('.') + .map(|part| part.parse::().expect("Error parsing protoc version")); + let major = protoc_version_parts.next().expect("Protoc version did not have a major number"); + let minor = protoc_version_parts.next().unwrap_or_default(); + + if major < 3 || (major == 3 && minor < 15) { + Err(Error::new( + ErrorKind::Other, + "protoc version is too old. version 3.15.x or greater is needed.", + )) + } else { + prost_build::compile_protos( + &[ + "src/protobuf_messages/proto/p2p/proto/header.proto", + "src/protobuf_messages/proto/p2p/proto/state.proto", + ], + &["src/protobuf_messages/proto/"], + )?; + Ok(()) + } }