#[cfg(feature = "c_api")]
fn gen_c_api() {
use std::env;
use std::path::PathBuf;
if std::env::var("_CBINDGEN_IS_RUNNING").is_ok() {
return;
}
fn get_build_profile_name() -> String {
let out_dir = std::env::var("OUT_DIR")
.expect("OUT_DIR is not set, cannot determine build profile, aborting");
out_dir
.split(std::path::MAIN_SEPARATOR)
.nth_back(3)
.expect("Cannot determine build profile, aborting")
.to_string()
}
fn target_dir() -> PathBuf {
if let Ok(target) = env::var("CARGO_TARGET_DIR") {
PathBuf::from(target)
} else if option_env!("CARGO_PRIMARY_PACKAGE").is_some() {
PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.join(format!("target/{}", get_build_profile_name()))
} else {
let out_dir = std::env::var("OUT_DIR")
.expect("OUT_DIR is not set, cannot find place to store header, aborting");
let out_dir_path = std::path::PathBuf::from(out_dir);
out_dir_path.ancestors().nth(3).unwrap().join("deps")
}
}
extern crate cbindgen;
let crate_dir: PathBuf = env::var("CARGO_MANIFEST_DIR").unwrap().into();
let package_name = env::var("CARGO_PKG_NAME").unwrap();
let output_file = target_dir().join(format!("{package_name}.h"));
cbindgen::Builder::new()
.with_crate(crate_dir.as_path())
.with_config(cbindgen::Config::from_file(crate_dir.join("cbindgen.toml")).unwrap())
.generate()
.unwrap()
.write_to_file(output_file);
}
fn main() {
#[cfg(feature = "c_api")]
gen_c_api()
}