use std::{env, path::PathBuf, process::Command}; fn main() { println!("cargo:rerun-if-changed=native/w32time.idl"); println!("cargo:rerun-if-changed=native/clock_rpc.c"); println!("cargo:rerun-if-changed=native/clock_endpoints.c"); if env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") { return; } assert_eq!( env::var("CARGO_CFG_TARGET_ARCH").as_deref(), Ok("x86_64"), "Windows clock RPC is qualified only for x86-64" ); let out = PathBuf::from(env::var_os("OUT_DIR").unwrap()); let idl = env::current_dir().unwrap().join("native/w32time.idl"); if env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") { // MIDL and CL come from the explicitly selected native x64 SDK/MSVC // environment. `win64` means IA64 in MIDL: AMD64 must be explicit. println!("cargo:rerun-if-env-changed=RMM_MIDL_EXE"); let midl = env::var_os("RMM_MIDL_EXE").expect("Select the native SDK's verified midl.exe"); assert!( Command::new(midl) .current_dir(&out) .args([ "/nologo", "/env", "amd64", "/Oicf", "/robust", "/Zp8", "/client", "stub", "/server", "none", "/h", "w32time.h", "/cstub", "w32time_c.c", ]) .arg(&idl) .status() .expect("Install native Microsoft SDK MIDL; no generated-stub fallback") .success(), "Native Microsoft MIDL clock stub generation failed" ); // Keep RPC's __try/__except entirely inside native C. Existing C ABI // assertions remain active; never substitute C++/Rust catch_unwind. cc::Build::new() .flag("/std:c11") .flag("/Zp8") .warnings_into_errors(true) .include(&out) .file(out.join("w32time_c.c")) .file("native/clock_rpc.c") .file("native/clock_endpoints.c") .compile("rmm_clock_rpc"); println!("cargo:rustc-link-lib=rpcrt4"); println!("cargo:rustc-link-lib=advapi32"); return; } assert_eq!( env::var("CARGO_CFG_TARGET_ENV").as_deref(), Ok("gnu"), "Windows clock RPC supports native MSVC or the qualified GNU/clang toolchain" ); for args in [ vec!["-h", "-H", "w32time.h"], vec!["-c", "-o", "w32time_c.c"], ] { assert!( Command::new("x86_64-w64-mingw32-widl") .current_dir(&out) .args(["--win64", "-Oif"]) .args(args) .arg(&idl) .status() .expect("Install mingw-w64-tools for the generated read-only RPC stub") .success(), "WIDL clock stub generation failed" ); } // Clang implements x64 Windows SEH in the C boundary. MinGW GCC's __try // spelling is not sufficient; do not let an RPC exception unwind into Rust. cc::Build::new() .compiler("clang") .flag("--target=x86_64-w64-windows-gnu") .flag("-fms-extensions") .flag("-Werror") .include(&out) .file(out.join("w32time_c.c")) .file("native/clock_rpc.c") .file("native/clock_endpoints.c") .compile("rmm_clock_rpc"); println!("cargo:rustc-link-lib=rpcrt4"); println!("cargo:rustc-link-lib=advapi32"); }