-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
79 lines (62 loc) · 2.37 KB
/
build.rs
File metadata and controls
79 lines (62 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#[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>> {
// Use vendored protoc binary (no system protoc required)
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>> {
// Skip proto compilation when proto-build feature is not enabled
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());
// Find all proto files in test server directory
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(());
}
// Print rerun-if-changed for all proto files
for proto in &proto_files {
println!("cargo:rerun-if-changed={}", proto.display());
}
// Compile test server protos
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 (optional, feature-gated)
compile_main_protos()?;
// Compile test server protos (always if they exist)
compile_test_server_protos()?;
Ok(())
}