-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
76 lines (63 loc) · 2.53 KB
/
build.rs
File metadata and controls
76 lines (63 loc) · 2.53 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
//! Build script for Grove
//!
//! This script handles:
//! - Proto file compilation with tonic-build
//! - WASM-specific build configuration
//! - Feature-based conditional compilation
//! - Build timestamp generation with vergen
use std::env;
use std::path::PathBuf;
fn main() -> anyhow::Result<()> {
// Generate build timestamp using vergen 8.x for EmitBuilder API compatibility
vergen::EmitBuilder::builder()
.all_build()
.all_cargo()
.all_rustc()
.emit()?;
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let proto_dir = manifest_dir.join("Proto");
println!("cargo:rerun-if-changed={}", proto_dir.join("Grove.proto").display());
println!("cargo:rerun-if-changed={}", proto_dir.display());
// Detect if we're building for WASM
let is_wasm = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default() == "wasm32";
// Check if grpc feature is enabled
let grpc_enabled = env::var("CARGO_FEATURE_GRPC").is_ok();
if !is_wasm && grpc_enabled {
// Only compile protos for native builds with grpc feature
compile_protos();
} else {
if is_wasm {
println!("cargo:warning=Building for WASM target, skipping gRPC proto compilation");
}
if !grpc_enabled {
println!("cargo:warning=grpc feature not enabled, skipping gRPC proto compilation");
}
}
// WASM-specific configuration
configure_wasm_build();
Ok(())
}
fn compile_protos() {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let proto_dir = manifest_dir.join("Proto");
let proto_file = proto_dir.join("Grove.proto");
let out_dir = manifest_dir.join("Source/Protocol/Generated");
// Create the output directory if it doesn't exist
std::fs::create_dir_all(&out_dir).expect(&format!("Failed to create directory: {:?}", out_dir));
// Using tonic-build 0.12 for configure() API stability
tonic_build::configure()
.build_server(true)
.build_client(true)
.out_dir(&out_dir)
.compile_protos(&[proto_file.as_path()], &[proto_dir.as_path()])
.expect("Failed to compile protos");
println!("cargo:rerun-if-changed={}", out_dir.display());
}
fn configure_wasm_build() {
// Set linker flags for WASM
if env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default() == "wasm32" {
// Optimize for smaller WASM binaries
println!("cargo:rustc-cfg=wasm32");
println!("cargo:rustc-cfg=web_sys_unstable_apis");
}
}