use std::{
env,
path::{Path, PathBuf},
};
use bindgen::CodegenConfig;
use wdk_build::{BuilderExt, Config, ConfigError, DriverConfig, KMDFConfig};
fn generate_constants(out_path: &Path, config: Config) -> Result<(), ConfigError> {
Ok(
bindgen::Builder::wdk_default(vec!["src/ntddk-input.h", "src/wdf-input.h"], config)?
.with_codegen_config(CodegenConfig::VARS)
.generate()
.expect("Bindings should succeed to generate")
.write_to_file(out_path.join("constants.rs"))?,
)
}
fn generate_types(out_path: &Path, config: Config) -> Result<(), ConfigError> {
Ok(
bindgen::Builder::wdk_default(vec!["src/ntddk-input.h", "src/wdf-input.h"], config)?
.with_codegen_config(CodegenConfig::TYPES)
.generate()
.expect("Bindings should succeed to generate")
.write_to_file(out_path.join("types.rs"))?,
)
}
fn generate_ntddk(out_path: &Path, config: Config) -> Result<(), ConfigError> {
Ok(
bindgen::Builder::wdk_default(vec!["src/ntddk-input.h"], config)?
.with_codegen_config((CodegenConfig::TYPES | CodegenConfig::VARS).complement())
.generate()
.expect("Bindings should succeed to generate")
.write_to_file(out_path.join("ntddk.rs"))?,
)
}
fn generate_wdf(out_path: &Path, config: Config) -> Result<(), ConfigError> {
Ok(
bindgen::Builder::wdk_default(vec!["src/wdf-input.h"], config)?
.with_codegen_config((CodegenConfig::TYPES | CodegenConfig::VARS).complement())
.allowlist_file("(?i).*wdf.*") .generate()
.expect("Bindings should succeed to generate")
.write_to_file(out_path.join("wdf.rs"))?,
)
}
fn main() -> Result<(), ConfigError> {
tracing_subscriber::fmt::init();
let config = Config {
driver_config: DriverConfig::KMDFConfig(KMDFConfig::new()),
..Config::default()
};
let out_paths = vec![
PathBuf::from("./generated_bindings/"),
PathBuf::from(
env::var("OUT_DIR").expect("OUT_DIR should be exist in Cargo build environment"),
),
];
for out_path in out_paths {
generate_constants(&out_path, config.clone())?;
generate_types(&out_path, config.clone())?;
generate_ntddk(&out_path, config.clone())?;
generate_wdf(&out_path, config.clone())?;
}
config.configure_library_build()?;
Ok(config.export_config()?)
}