-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
379 lines (325 loc) · 11.1 KB
/
Copy pathbuild.rs
File metadata and controls
379 lines (325 loc) · 11.1 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use std::collections::BTreeSet;
#[cfg(feature = "kmdf-runtime")]
use std::{fs, io, path::Path};
use std::fmt::format;
use std::str::FromStr;
#[cfg(feature = "kmdf-runtime")]
use regex::Regex;
#[cfg(feature = "kmdf-runtime")]
struct NtStatusEntry {
macro_name: String,
value: String,
}
#[cfg(feature = "kmdf-runtime")]
pub fn generate_is_wdf_type_impls(
types: &[String],
) -> String {
use std::collections::BTreeSet;
let mut generated = String::new();
for name in types.iter().cloned().collect::<BTreeSet<_>>() {
generated.push_str(&format!(
"impl crate::op::IsWdfType for wdk_sys::{name} {{}}\n"
));
}
generated
}
#[cfg(feature = "kmdf-runtime")]
fn collect_ntstatus_values_from_wdf_header(
wdf_header_path: &std::path::Path,
) -> std::io::Result<Vec<NtStatusEntry>> {
let content = std::fs::read_to_string(wdf_header_path)?;
let regex = Regex::new(
r#"^#define\s+(STATUS_[A-Z0-9_]+)\s+\(\(NTSTATUS\)(0x[0-9A-F]+)L\)"#
).unwrap();
let mut entries = Vec::new();
for line in content.lines() {
let line = line.trim();
let Some(captures) = regex.captures(line) else {
continue;
};
let macro_name = captures.get(1).unwrap().as_str();
let value = captures.get(2).unwrap().as_str();
entries.push(NtStatusEntry {
macro_name: macro_name.to_string(),
value: value.to_string(),
});
}
Ok(entries)
}
fn generate_single_match_branch(left: &str, right: &str) -> String {
format!(" {left} => {right},\n")
}
#[cfg(feature = "kmdf-runtime")]
fn generate_nt_status_as_ntstatus_impl(
statuses: &[NtStatusEntry],
) -> String {
let nt_entry_struct = String::from("\n\
#[derive(Debug, Copy, Clone)]\n\
struct NtStatusEntry2 {\n\
msg: &'static str,\n\
value: u32,\n\
}\n\
");
let nt_status_enum_decl_open = String::from("\n\
#[derive(\n\
Debug,\n\
Copy,\n\
Clone,\n\
Eq,\n\
PartialEq,\n\
)]\n\
pub enum NtStatus {\n");
let nts_status_enum_decl_close = String::from("\
Unregistered((wdk_sys::NTSTATUS, core::option::Option<&'static str>)),\n\
Success,\n\
}\n\
");
let from_nt_status_for_nt_open = String::from("\n\
impl From<NtStatus> for wdk_sys::NTSTATUS {\n\
fn from(status: NtStatus) -> Self {\n\
match status {\n\
NtStatus::Unregistered((status, _)) => status,\n\
NtStatus::Success => 0,\n\
");
let match_fn_declaration_with_impl_close = String::from("\n\
}\n\
}\n\
}\n\
");
let from_nt_to_nt_status_open = String::from("\n\
impl From<wdk_sys::NTSTATUS> for NtStatus {\n\
fn from(status: wdk_sys::NTSTATUS) -> Self {\n\
match status {\n\
_ => NtStatus::Unregistered((status, core::option::Option::None)),\n\
0 => NtStatus::Success,\n\
");
let format_functions_impls = String::from("\
impl crate::op::AsNtStatus for NtStatus { }\n\
impl PartialEq<NtStatus> for wdk_sys::NTSTATUS {\n\
fn eq(&self, other: &NtStatus) -> bool {\n\
*self == wdk_sys::NTSTATUS::from(*other)
}\n\
}\n\
impl PartialEq<wdk_sys::NTSTATUS> for NtStatus {\n\
fn eq(&self, other: &wdk_sys::NTSTATUS) -> bool {\n\
wdk_sys::NTSTATUS::from(*self) == *other\n\
}\n\
}\n\
impl PartialOrd<wdk_sys::NTSTATUS> for NtStatus {\n\
fn partial_cmp(\n\
&self,\n\
other: &wdk_sys::NTSTATUS,\n\
) -> Option<core::cmp::Ordering> {\n\
let lhs = wdk_sys::NTSTATUS::from(*self);\n\
lhs.partial_cmp(other)\n\
}\n\
}\n\
");
let with_info_fn_def_open = String::from("\
pub fn with_info(self, info: &'static str) -> Self {\n\
match self {\n\
NtStatus::Unregistered((ntstatus, _)) => NtStatus::Unregistered((\
ntstatus, core::option::Option::Some(info)\
)),\n\
NtStatus::Success => NtStatus::Success,\n\
");
let match_fn_close = String::from("\
}\n\
}\n\
");
let from_nt_with_info_fn_def = String::from("\
pub fn from_nt_with_info(ntstatus: wdk_sys::NTSTATUS, info: &'static str) -> Self {\n\
NtStatus::from(ntstatus).with_info(info)\n\
}\n\
");
let ntstatus_impl_open = String::from("\
impl NtStatus {\n"
);
let enum_nt_vals = generate_enum_def(statuses);
let from_nt_to_nt_status = generate_matches(statuses, |value, name| {
let raw = u32::from_str_radix(
value.trim_start_matches("0x"),
16,
)
.unwrap();
let signed = raw as i32;
if signed != 0 {
Some((signed.to_string(), format!("NtStatus::{}(core::option::Option::None)", name.to_pascal_case().replace("Status", ""))))
} else {
None
}
});
let from_nt_status_for_nt = generate_matches(statuses, |value, name| {
let raw = u32::from_str_radix(
value.trim_start_matches("0x"),
16,
)
.unwrap();
let signed = raw as i32;
if signed != 0 {
Some((format!("NtStatus::{}(_)", name.to_pascal_case().replace("Status", "")), signed.to_string()))
} else {
None
}
});
let with_info_match = generate_matches(statuses, |value, name| {
let raw = u32::from_str_radix(
value.trim_start_matches("0x"),
16,
)
.unwrap();
let signed = raw as i32;
if signed != 0 {
Some((format!("NtStatus::{}(_)", name.to_pascal_case().replace("Status", "")), format!("NtStatus::{}(core::option::Option::Some(info))", name.to_pascal_case().replace("Status", ""))))
} else {
None
}
});
let ntstatus_impl_close = String::from("}\n");
format!("\
/// NT status errors generated from ntstatus.h\n\
{nt_status_enum_decl_open}\
{enum_nt_vals}\
{nts_status_enum_decl_close}\
\n\
{from_nt_status_for_nt_open}\
{from_nt_status_for_nt}\
{match_fn_declaration_with_impl_close}\
\n\
{from_nt_to_nt_status_open}\
{from_nt_to_nt_status}\
{match_fn_declaration_with_impl_close}\
\n\
{format_functions_impls}\n\
\n\
{ntstatus_impl_open}\n\
{with_info_fn_def_open}\n\
{with_info_match}\n\
{match_fn_close}\n\
{from_nt_with_info_fn_def}\n\
{ntstatus_impl_close}\n\
")
}
fn generate_enum_def(statuses: &[NtStatusEntry]) -> String {
statuses.iter().filter_map(|status| {
let raw = u32::from_str_radix(
status.value.trim_start_matches("0x"),
16,
).unwrap();
let signed = raw as i32;
if (signed != 0) {
// Enum
Some(format!(
" {}(core::option::Option<&'static str>),\n",
status.macro_name.to_pascal_case().replace("Status", "")
))
} else {
None
}
}).collect()
}
fn generate_matches(statuses: &[NtStatusEntry], op: impl Fn(String, String) -> Option<(String, String)>) -> String {
statuses.iter().filter_map(|status| {
let (name, value) = op(status.value.clone(), status.macro_name.clone())?;
Some(generate_single_match_branch(name.as_str(), value.as_str()))
}).collect()
}
#[cfg(feature = "kmdf-runtime")]
fn write_generated_file(
generated: &str,
) -> std::io::Result<()> {
let out_dir = std::path::PathBuf::from(
std::env::var("OUT_DIR").unwrap(),
);
fs::write(out_dir.join("wdkgen.rs"), generated)
}
use std::sync::{Arc, Mutex};
use bindgen::callbacks::ParseCallbacks;
#[derive(Default, Debug)]
pub struct WdfTypeCollector {
pub types: Arc<Mutex<BTreeSet<String>>>,
}
use bindgen::callbacks::ItemInfo;
use heck::ToPascalCase;
impl ParseCallbacks for WdfTypeCollector {
fn item_name(
&self,
item_info: ItemInfo<'_>,
) -> Option<String> {
let name = item_info.name;
if name.starts_with("WDF") && name.ends_with("__") {
self.types.lock().unwrap().insert(name.to_string());
}
None
}
}
#[cfg(feature = "kmdf-runtime")]
fn main() -> Result<(), wdk_build::ConfigError> {
wdk_build::configure_wdk_library_build_and_then(
|config| {
let ntstatus = config.include_paths()?.find_map(|path| {
let candidate = path.join("ntstatus.h");
candidate.exists().then_some(candidate)
}).ok_or(
wdk_build::ConfigError::WdkContentRootDetectionError,
)?;
let collector = WdfTypeCollector::default();
let collected = collector.types.clone();
let header = config.bindgen_header_contents([
wdk_build::ApiSubset::Base,
wdk_build::ApiSubset::Wdf,
]).map_err(|_| {
wdk_build::ConfigError::WdkContentRootDetectionError
})?;
let mut builder = bindgen::Builder::default().header_contents("wrapper.h", &header).parse_callbacks(Box::new(collector));
for path in config.include_paths()? {
builder = builder.clang_arg(format!(
"-I{}",
path.display()
));
}
builder = builder.clang_args(
wdk_build::Config::wdk_bindgen_compiler_flags(),
);
for (name, value) in config.preprocessor_definitions() {
builder = match value {
Some(v) => builder.clang_arg(format!("-D{name}={v}")),
None => builder.clang_arg(format!("-D{name}")),
};
}
let _bindings = builder.generate().map_err(|e| {
println!("cargo:warning=bindgen error: {e}");
wdk_build::ConfigError::WdkContentRootDetectionError
})?;
let statuses = collect_ntstatus_values_from_wdf_header(&ntstatus).map_err(|_| {
wdk_build::ConfigError::WdkContentRootDetectionError
})?;
println!(
"cargo:warning=Collected {} NTSTATUS values from {}",
statuses.len(),
ntstatus.display()
);
let ntstatus_bindings = generate_nt_status_as_ntstatus_impl(
&statuses,
);
let wdf_types = collected.lock().unwrap();
println!(
"cargo:warning=Collected {} WDF types",
wdf_types.len()
);
let wdf_types_bindings = generate_is_wdf_type_impls(
&wdf_types.iter().cloned().collect::<Vec<_>>(),
);
let generated = format!("\
{ntstatus_bindings}\n\
{wdf_types_bindings}\n\
");
write_generated_file(&generated).map_err(|_| {
wdk_build::ConfigError::WdkContentRootDetectionError
})?;
Ok(())
},
)
}
#[cfg(feature = "test-runtime")]
fn main() {}