b5b6619118
Adopt cbindgen in the build script to keep the C header aligned with the Rust FFI definitions. Store the patient handle as a void pointer to avoid layout mismatches and refresh the generated header and repository guidelines.
51 lines
1.6 KiB
Rust
51 lines
1.6 KiB
Rust
use std::env;
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
fn main() {
|
|
if env::var_os("CARGO_FEATURE_FFI").is_none() {
|
|
return;
|
|
}
|
|
|
|
println!("cargo:rerun-if-changed=src/ffi.rs");
|
|
println!("cargo:rerun-if-changed=cbindgen.toml");
|
|
|
|
let crate_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"));
|
|
let crate_dir_string = crate_dir
|
|
.to_str()
|
|
.expect("crate directory must be valid UTF-8")
|
|
.to_owned();
|
|
|
|
let header_dir = crate_dir.join("ffi");
|
|
fs::create_dir_all(&header_dir).expect("failed to create ffi output directory");
|
|
|
|
let config_path = crate_dir.join("cbindgen.toml");
|
|
let config = if config_path.exists() {
|
|
cbindgen::Config::from_file(&config_path)
|
|
.unwrap_or_else(|err| panic!("failed to load cbindgen config: {err}"))
|
|
} else {
|
|
cbindgen::Config::default()
|
|
};
|
|
|
|
let bindings = cbindgen::Builder::new()
|
|
.with_config(config)
|
|
.with_crate(crate_dir_string)
|
|
.generate()
|
|
.expect("unable to generate cbindgen bindings");
|
|
|
|
let mut generated = Vec::new();
|
|
bindings.write(&mut generated);
|
|
|
|
let header_contents = String::from_utf8(generated).expect("generated header was not valid UTF-8");
|
|
|
|
let header_path = header_dir.join("medicallib.h");
|
|
let needs_write = fs::read_to_string(&header_path)
|
|
.map(|existing| existing != header_contents)
|
|
.unwrap_or(true);
|
|
|
|
if needs_write {
|
|
fs::write(&header_path, header_contents).expect("failed to write medicallib.h");
|
|
println!("cargo:warning=Updated ffi/medicallib.h via cbindgen");
|
|
}
|
|
}
|