use cbindgen::{Config, Language};
use std::env;
use std::path::PathBuf;
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let package_name = env::var("CARGO_PKG_NAME").unwrap();
let output_file = target_dir()
.join(format!("{}.h", package_name))
.display()
.to_string();
let config = Config {
autogen_warning: Some(String::from("// Autogenerated header file")),
namespace: Some(String::from("ffi")),
language: Language::C,
documentation: false,
include_version: true,
package_version: true,
pragma_once: true,
..Default::default()
};
cbindgen::generate_with_config(crate_dir, config)
.unwrap()
.write_to_file(output_file);
if cfg!(target_os = "windows") {
let mut res = winres::WindowsResource::new();
if let Some(version) = retrieve_app_version_from_git_repository() {
res.set("ProductVersion", &version);
}
res.compile().unwrap();
}
}
fn target_dir() -> PathBuf {
if let Ok(target) = env::var("CARGO_TARGET_DIR") {
PathBuf::from(target)
} else {
PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("target")
}
}
fn retrieve_app_version_from_git_repository() -> Option<String> {
{ std::process::Command::new("git").args(["describe", "--tags", "--dirty"]) }
.output()
.ok()
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_owned())
}