summaryrefslogtreecommitdiff
path: root/crates/windows-kernel-sys/build.rs
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-01-03 03:20:12 -0800
committerFuwn <[email protected]>2022-01-03 03:20:12 -0800
commit85db2b507f3f69b32811c54a89d9ac7bbbc46121 (patch)
tree2efd66da452f8a6a2cc6c91584c925f237506ddf /crates/windows-kernel-sys/build.rs
downloaddriver-85db2b507f3f69b32811c54a89d9ac7bbbc46121.tar.xz
driver-85db2b507f3f69b32811c54a89d9ac7bbbc46121.zip
feat(driver): commit primer
Diffstat (limited to 'crates/windows-kernel-sys/build.rs')
-rw-r--r--crates/windows-kernel-sys/build.rs135
1 files changed, 135 insertions, 0 deletions
diff --git a/crates/windows-kernel-sys/build.rs b/crates/windows-kernel-sys/build.rs
new file mode 100644
index 0000000..254e2e9
--- /dev/null
+++ b/crates/windows-kernel-sys/build.rs
@@ -0,0 +1,135 @@
+use std::path::PathBuf;
+
+use bindgen::callbacks::*;
+use windows_kernel_build::DirectoryType;
+
+#[derive(Debug)]
+struct Callbacks;
+
+impl ParseCallbacks for Callbacks {
+ fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
+ Some(match name {
+ "TRUE" | "FALSE" => IntKind::UChar,
+ _ => return None,
+ })
+ }
+}
+
+fn generate_base() {
+ println!("cargo:rerun-if-changed=src/wrapper.h");
+
+ let include_dir = windows_kernel_build::get_km_dir(DirectoryType::Include).unwrap();
+ let out_path = PathBuf::from(
+ std::env::var_os("OUT_DIR").expect("the environment variable OUT_DIR is undefined"),
+ );
+
+ bindgen::Builder::default()
+ .header("src/wrapper.h")
+ .use_core()
+ .derive_debug(false)
+ .layout_tests(false)
+ .ctypes_prefix("cty")
+ .default_enum_style(bindgen::EnumVariation::ModuleConsts)
+ .clang_arg(format!("-I{}", include_dir.to_str().unwrap()))
+ .parse_callbacks(Box::new(bindgen::CargoCallbacks))
+ .parse_callbacks(Box::new(Callbacks))
+ .ignore_functions()
+ .generate()
+ .unwrap()
+ .write_to_file(out_path.join("base.rs"))
+ .unwrap();
+}
+
+#[cfg(feature = "intrin")]
+fn generate_intrin() {
+ println!("cargo:rerun-if-changed=src/wrapper_intrin.c");
+
+ let include_dir = windows_kernel_build::get_km_dir(DirectoryType::Include).unwrap();
+
+ cc::Build::new()
+ .flag("/kernel")
+ .include(include_dir)
+ .file("src/wrapper_intrin.c")
+ .compile("wrapper_intrin");
+}
+
+#[cfg(not(feature = "intrin"))]
+fn generate_intrin() {}
+
+#[cfg(feature = "ntoskrnl")]
+fn generate_ntoskrnl() {
+ println!("cargo:rerun-if-changed=src/wrapper.h");
+ println!("cargo:rerun-if-changed=src/wrapper.c");
+ println!("cargo:rustc-link-lib=ntoskrnl");
+
+ let include_dir = windows_kernel_build::get_km_dir(DirectoryType::Include).unwrap();
+ let out_path = PathBuf::from(
+ std::env::var_os("OUT_DIR").expect("the environment variable OUT_DIR is undefined"),
+ );
+
+ bindgen::Builder::default()
+ .header("src/wrapper.h")
+ .use_core()
+ .derive_debug(false)
+ .layout_tests(false)
+ .ctypes_prefix("cty")
+ .default_enum_style(bindgen::EnumVariation::ModuleConsts)
+ .clang_arg(format!("-I{}", include_dir.to_str().unwrap()))
+ .parse_callbacks(Box::new(bindgen::CargoCallbacks))
+ .blocklist_type(".*")
+ .allowlist_function(".*")
+ .allowlist_recursively(false)
+ .generate()
+ .unwrap()
+ .write_to_file(out_path.join("ntoskrnl.rs"))
+ .unwrap();
+
+ cc::Build::new()
+ .flag("/kernel")
+ .include(include_dir)
+ .file("src/wrapper.c")
+ .compile("wrapper_ntoskrnl");
+}
+
+#[cfg(not(feature = "ntoskrnl"))]
+fn generate_ntoskrnl() {}
+
+#[cfg(feature = "netio")]
+fn generate_netio() {
+ println!("cargo:rerun-if-changed=src/wrapper_netio.h");
+ println!("cargo:rustc-link-lib=netio");
+
+ let include_dir = windows_kernel_build::get_km_dir(DirectoryType::Include).unwrap();
+ let out_path = PathBuf::from(
+ std::env::var_os("OUT_DIR").expect("the environment variable OUT_DIR is undefined"),
+ );
+
+ bindgen::Builder::default()
+ .header("src/wrapper.h")
+ .use_core()
+ .derive_debug(false)
+ .layout_tests(false)
+ .ctypes_prefix("cty")
+ .default_enum_style(bindgen::EnumVariation::ModuleConsts)
+ .clang_arg(format!("-I{}", include_dir.to_str().unwrap()))
+ .parse_callbacks(Box::new(bindgen::CargoCallbacks))
+ .blocklist_type(".*")
+ .allowlist_function(".*")
+ .allowlist_recursively(false)
+ .generate()
+ .unwrap()
+ .write_to_file(out_path.join("netio.rs"))
+ .unwrap();
+}
+
+#[cfg(not(feature = "netio"))]
+fn generate_netio() {}
+
+fn main() {
+ println!("cargo:rerun-if-changed=build.rs");
+
+ generate_base();
+ generate_intrin();
+ generate_ntoskrnl();
+ generate_netio();
+}