()
| 7 | const PROTO_REL: &str = "../../proto"; |
| 8 | |
| 9 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 10 | // --- Git-derived version --- |
| 11 | // Compute a version from `git describe` for local builds. In Docker/CI |
| 12 | // builds where .git is absent, this silently does nothing and the binary |
| 13 | // falls back to CARGO_PKG_VERSION (which is already sed-patched by the |
| 14 | // build pipeline). |
| 15 | println!("cargo:rerun-if-changed=../../.git/HEAD"); |
| 16 | println!("cargo:rerun-if-changed=../../.git/refs/tags"); |
| 17 | |
| 18 | if let Some(version) = git_version() { |
| 19 | println!("cargo:rustc-env=OPENSHELL_GIT_VERSION={version}"); |
| 20 | } |
| 21 | |
| 22 | // --- Protobuf compilation --- |
| 23 | // Re-run when anything under proto/ changes (including newly added .proto files). |
| 24 | println!("cargo:rerun-if-changed={PROTO_REL}"); |
| 25 | // Use bundled protoc from protobuf-src. The system protoc (from apt-get) |
| 26 | // does not bundle the well-known type includes (google/protobuf/struct.proto |
| 27 | // etc.), so we must use protobuf-src which ships both the binary and the |
| 28 | // include tree. |
| 29 | // SAFETY: This is run at build time in a single-threaded build script context. |
| 30 | // No other threads are reading environment variables concurrently. |
| 31 | #[allow(unsafe_code)] |
| 32 | unsafe { |
| 33 | env::set_var("PROTOC", protobuf_src::protoc()); |
| 34 | } |
| 35 | |
| 36 | let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?); |
| 37 | let proto_root = manifest_dir.join(PROTO_REL); |
| 38 | |
| 39 | let mut proto_files = Vec::new(); |
| 40 | collect_proto_files(&proto_root, &mut proto_files)?; |
| 41 | proto_files.sort(); |
| 42 | |
| 43 | let out_dir = PathBuf::from(env::var("OUT_DIR")?); |
| 44 | let descriptor_path = out_dir.join("openshell_descriptor.bin"); |
| 45 | |
| 46 | // Configure tonic/prost protobuf code generation. |
| 47 | tonic_prost_build::configure() |
| 48 | .build_server(true) |
| 49 | .build_client(true) |
| 50 | // Emit a binary FileDescriptorSet so the server can enumerate every |
| 51 | // RPC at runtime (used by the per-handler auth exhaustiveness test). |
| 52 | .file_descriptor_set_path(&descriptor_path) |
| 53 | .compile_protos(&proto_files, &[proto_root])?; |
| 54 | |
| 55 | println!( |
| 56 | "cargo:rustc-env=OPENSHELL_DESCRIPTOR_PATH={}", |
| 57 | descriptor_path.display() |
| 58 | ); |
| 59 | |
| 60 | Ok(()) |
| 61 | } |
| 62 | |
| 63 | fn collect_proto_files(dir: &Path, out: &mut Vec<PathBuf>) -> std::io::Result<()> { |
| 64 | for entry in std::fs::read_dir(dir)? { |
nothing calls this directly
no test coverage detected