#[cfg(any(feature = "proto-build", feature = "test-servers"))]
use std::env;
#[cfg(any(feature = "proto-build", feature = "test-servers"))]
fn use_vendored_protoc() -> Result<(), Box<dyn std::error::Error>> {
unsafe {
env::set_var("PROTOC", protoc_bin_vendored::protoc_bin_path()?);
}
Ok(())
}
#[cfg(feature = "proto-build")]
fn compile_main_protos() -> Result<(), Box<dyn std::error::Error>> {
use_vendored_protoc()?;
let out_dir = std::path::PathBuf::from(env::var("OUT_DIR").unwrap());
tonic_prost_build::configure()
.file_descriptor_set_path(out_dir.join("helloworld_descriptor.bin"))
.compile_protos(&["tests/server/helloworld.proto"], &["tests/server"])?;
Ok(())
}
#[cfg(not(feature = "proto-build"))]
fn compile_main_protos() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
#[cfg(feature = "test-servers")]
fn compile_test_server_protos() -> Result<(), Box<dyn std::error::Error>> {
use_vendored_protoc()?;
let test_proto_dir = std::path::Path::new("tests/servers/proto");
if !test_proto_dir.exists() {
return Ok(());
}
let out_dir = std::path::PathBuf::from(env::var("OUT_DIR").unwrap());
let proto_files = std::fs::read_dir(test_proto_dir)?
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().is_some_and(|ext| ext == "proto"))
.map(|e| e.path())
.collect::<Vec<_>>();
if proto_files.is_empty() {
return Ok(());
}
for proto in &proto_files {
println!("cargo:rerun-if-changed={}", proto.display());
}
tonic_prost_build::configure()
.file_descriptor_set_path(out_dir.join("test_servers_descriptor.bin"))
.compile_protos(&proto_files, &[test_proto_dir.to_path_buf()])?;
Ok(())
}
#[cfg(not(feature = "test-servers"))]
fn compile_test_server_protos() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
compile_main_protos()?;
compile_test_server_protos()?;
Ok(())
}