summaryrefslogtreecommitdiff
path: root/crates/windows-kernel-sys/build.rs
blob: 8ff935c99267509c0f150395663083d5cee4a0db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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();
}