//! Embed and hash-pin Smith's release components (`SMH-SPEC-PLUG0001`). #![expect( clippy::disallowed_methods, reason = "build script reads only checked-in pins and bounded release components" )] #![expect( clippy::expect_used, reason = "Cargo build scripts must stop when required Cargo values or output writes fail" )] #![expect( clippy::panic, reason = "Cargo build scripts must stop with diagnostics when required inputs are absent" )] #[path = "../smith/src/sha256.rs"] mod sha256; use std::{env, fs, path::PathBuf}; const SHA256_HEX_LENGTH: usize = 64; const COMPONENT_ARTIFACT_DIR_ENV: &str = "SMITH_COMPONENT_ARTIFACT_DIR"; const COMPONENTS: &[(&str, &str, &str)] = &[ ( "smith_plugin_protocol_openai_responses.wasm", "plugins/protocol-openai-responses/component.sha256", "SMITH_OPENAI_RESPONSES_COMPONENT_SHA256", ), ( "smith_plugin_oauth.wasm", "plugins/oauth/component.sha256", "SMITH_OAUTH_COMPONENT_SHA256", ), ( "smith_plugin_protocol_openai_compatible.wasm", "plugins/protocol-openai-compatible/component.sha256", "SMITH_OPENAI_COMPATIBLE_COMPONENT_SHA256", ), ( "smith_plugin_protocol_anthropic.wasm", "plugins/protocol-anthropic/component.sha256", "SMITH_ANTHROPIC_COMPONENT_SHA256", ), ( "smith_plugin_protocol_google.wasm", "plugins/protocol-google/component.sha256", "SMITH_GOOGLE_COMPONENT_SHA256", ), ( "smith_plugin_coding_tools.wasm", "plugins/coding-tools/component.sha256", "SMITH_CODING_TOOLS_COMPONENT_SHA256", ), ]; fn main() { let root = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("Cargo sets manifest dir")) .join(".."); println!("cargo::rerun-if-env-changed={COMPONENT_ARTIFACT_DIR_ENV}"); let target = env::var_os(COMPONENT_ARTIFACT_DIR_ENV).map_or_else( || { root.join(env::var_os("CARGO_TARGET_DIR").unwrap_or_else(|| "target".into())) .join("wasm32-wasip2/release") }, PathBuf::from, ); let output = PathBuf::from(env::var_os("OUT_DIR").expect("Cargo sets output dir")); for (name, pin_path, variable) in COMPONENTS { let artifact = target.join(name); let pin = root.join(pin_path); println!("cargo::rerun-if-changed={}", artifact.display()); println!("cargo::rerun-if-changed={}", pin.display()); let expected = fs::read_to_string(&pin).unwrap_or_else(|error| { panic!( "missing protocol component hash pin {}: {error}", pin.display() ) }); let expected = expected.trim(); assert!( expected.len() == SHA256_HEX_LENGTH && expected.bytes().all(|byte| byte.is_ascii_hexdigit()), "invalid protocol component SHA-256 pin {}", pin.display() ); let bytes = fs::read(&artifact).unwrap_or_else(|error| { panic!( "missing protocol component {}: {error}; build it with cargo x check or cargo x e2e", artifact.display() ) }); assert_eq!( sha256::digest_hex(&bytes), expected, "protocol component hash mismatch: rebuild then update {} deliberately", pin.display() ); fs::write(output.join(name), bytes).expect("write embedded protocol component"); println!("cargo::rustc-env={variable}={expected}"); } embed_coding_tool_metadata(&root, &output); } fn embed_coding_tool_metadata(root: &std::path::Path, output: &std::path::Path) { let metadata = root.join("plugins/coding-tools/metadata.json"); let pin = root.join("plugins/coding-tools/metadata.sha256"); println!("cargo::rerun-if-changed={}", metadata.display()); println!("cargo::rerun-if-changed={}", pin.display()); let expected = fs::read_to_string(&pin).unwrap_or_else(|error| { panic!( "missing coding tool metadata hash pin {}: {error}", pin.display() ) }); let expected = expected.trim(); assert!( expected.len() == SHA256_HEX_LENGTH && expected.bytes().all(|byte| byte.is_ascii_hexdigit()), "invalid coding tool metadata SHA-256 pin {}", pin.display() ); let bytes = fs::read(&metadata).unwrap_or_else(|error| { panic!( "missing coding tool metadata {}: {error}", metadata.display() ) }); assert_eq!( sha256::digest_hex(&bytes), expected, "coding tool metadata hash mismatch: update {} deliberately", pin.display() ); fs::write(output.join("coding_tools_metadata.json"), bytes) .expect("write embedded coding tool metadata"); }