|
21 | 21 | // SOFTWARE.
|
22 | 22 | //
|
23 | 23 | #![cfg_attr(docsrs, feature(doc_cfg))]
|
24 |
| -use std::collections::HashMap; |
25 |
| -use std::env; |
| 24 | +#![allow(dead_code)] |
| 25 | + |
26 | 26 | #[cfg_attr(docsrs, doc(cfg(feature = "regenerate")))]
|
27 | 27 | #[cfg(feature = "regenerate")]
|
28 |
| -use std::path::PathBuf; |
29 |
| - |
30 |
| -use regex::Regex; |
| 28 | +pub use regen::main; |
31 | 29 |
|
32 |
| -pub struct BindgenConfig { |
33 |
| - pub blocklist_types: Vec<String>, |
34 |
| - pub raw_lines: Vec<String>, |
| 30 | +#[cfg_attr(docsrs, doc(cfg(not(feature = "regenerate"))))] |
| 31 | +#[cfg(not(feature = "regenerate"))] |
| 32 | +fn main() { |
| 33 | + println!("Using vendored bindings, build script skipped."); |
35 | 34 | }
|
36 | 35 |
|
37 |
| -#[rustfmt::skip] |
38 |
| -impl Default for BindgenConfig { |
39 |
| - fn default() -> Self { |
40 |
| - let type_overrides: HashMap<_, _> = HashMap::from([ |
41 |
| - ("NTSTATUS", "windows::Win32::Foundation::NTSTATUS"), |
42 |
| - ("BOOL", "windows::Win32::Foundation::BOOL"), |
43 |
| - ("BOOLEAN", "windows::Win32::Foundation::BOOLEAN"), |
44 |
| - ("UNICODE_STRING", "nt_string::unicode_string::NtUnicodeString"), |
45 |
| - ("_UNICODE_STRING", "nt_string::unicode_string::NtUnicodeString"), |
46 |
| - ]) |
47 |
| - .into_iter() |
48 |
| - .map(|(k, v)| (k.to_owned(), v.to_owned())) |
49 |
| - .collect(); |
50 |
| - |
51 |
| - let blocklist_types = type_overrides.clone().into_keys().collect(); |
52 |
| - let raw_lines = type_overrides |
53 |
| - .into_iter() |
54 |
| - .map(|(key, value)| format!("pub use {value} as {key};")) |
55 |
| - .collect(); |
56 |
| - |
57 |
| - Self { |
58 |
| - blocklist_types, |
59 |
| - raw_lines, |
60 |
| - } |
| 36 | +#[cfg_attr(docsrs, doc(cfg(feature = "regenerate")))] |
| 37 | +#[cfg(feature = "regenerate")] |
| 38 | +mod regen { |
| 39 | + use regex::Regex; |
| 40 | + use std::collections::HashMap; |
| 41 | + use std::env; |
| 42 | + use std::path::PathBuf; |
| 43 | + |
| 44 | + pub struct BindgenConfig { |
| 45 | + pub blocklist_types: Vec<String>, |
| 46 | + pub raw_lines: Vec<String>, |
61 | 47 | }
|
62 |
| -} |
63 | 48 |
|
64 |
| -impl BindgenConfig { |
65 |
| - pub fn new(blocklist_types: Vec<String>, raw_lines: Vec<String>) -> Self { |
66 |
| - Self { |
67 |
| - blocklist_types, |
68 |
| - raw_lines, |
| 49 | + #[rustfmt::skip] |
| 50 | + impl Default for BindgenConfig { |
| 51 | + fn default() -> Self { |
| 52 | + let type_overrides: HashMap<_, _> = HashMap::from([ |
| 53 | + ("NTSTATUS", "windows::Win32::Foundation::NTSTATUS"), |
| 54 | + ("BOOL" , "windows::Win32::Foundation::BOOL"), |
| 55 | + ("BOOLEAN" , "windows::Win32::Foundation::BOOLEAN"), |
| 56 | + ( "UNICODE_STRING", "nt_string::unicode_string::NtUnicodeString"), |
| 57 | + ("_UNICODE_STRING", "nt_string::unicode_string::NtUnicodeString"), |
| 58 | + ]) |
| 59 | + .into_iter() |
| 60 | + .map(|(k, v)| (k.to_owned(), v.to_owned())) |
| 61 | + .collect(); |
| 62 | + |
| 63 | + let blocklist_types = type_overrides.clone().into_keys().collect(); |
| 64 | + let raw_lines = type_overrides |
| 65 | + .into_iter() |
| 66 | + .map(|(key, value)| format!("pub use {value} as {key};")) |
| 67 | + .collect(); |
| 68 | + |
| 69 | + Self { |
| 70 | + blocklist_types, |
| 71 | + raw_lines, |
| 72 | + } |
69 | 73 | }
|
70 | 74 | }
|
71 | 75 |
|
72 |
| - pub fn generate_bindings(&self) -> Result<bindgen::Bindings, bindgen::BindgenError> { |
73 |
| - let allowlist_regexpr = Regex::new( |
74 |
| - format!( |
75 |
| - r"({}\\deps\\phnt-nightly\\.*\.h)|winnt\.h|ntstatus\.h", |
76 |
| - regex::escape(env!("CARGO_MANIFEST_DIR")) |
| 76 | + impl BindgenConfig { |
| 77 | + pub fn new(blocklist_types: Vec<String>, raw_lines: Vec<String>) -> Self { |
| 78 | + Self { |
| 79 | + blocklist_types, |
| 80 | + raw_lines, |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + pub fn generate_bindings(&self) -> Result<bindgen::Bindings, bindgen::BindgenError> { |
| 85 | + let allowlist_regexpr = Regex::new( |
| 86 | + format!( |
| 87 | + r"({}\\deps\\phnt-nightly\\.*\.h)|winnt\.h|ntstatus\.h", |
| 88 | + regex::escape(env!("CARGO_MANIFEST_DIR")) |
| 89 | + ) |
| 90 | + .as_str(), |
77 | 91 | )
|
78 |
| - .as_str(), |
79 |
| - ) |
80 |
| - .unwrap(); |
81 |
| - |
82 |
| - let blocklist_regexpr = |
83 |
| - Regex::new(&format!(r"({})", self.blocklist_types.join("|"))).unwrap(); |
84 |
| - |
85 |
| - let mut raw_lines = vec![ |
86 |
| - format!("// Generated at {}", chrono::offset::Local::now()), |
87 |
| - "use cty;".into(), |
88 |
| - ]; |
89 |
| - raw_lines.append(&mut self.raw_lines.clone()); |
90 |
| - |
91 |
| - let clang_args = vec![ |
92 |
| - "-Iwindows.h", |
93 |
| - "-Iwinnt.h", |
94 |
| - concat!("-I", env!("CARGO_MANIFEST_DIR"), "\\deps\\phnt-nightly/"), |
95 |
| - ]; |
96 |
| - |
97 |
| - bindgen::builder() |
98 |
| - .header(concat!(env!("CARGO_MANIFEST_DIR"), "/src/ffi/wrapper.h")) |
99 |
| - .raw_line(raw_lines.join("\r\n").as_str()) |
100 |
| - .clang_args(clang_args) |
101 |
| - .allowlist_file(allowlist_regexpr.as_str()) |
102 |
| - .blocklist_type(blocklist_regexpr.as_str()) |
103 |
| - .type_alias("NTSTATUS") |
104 |
| - .opaque_type("std::.*") |
105 |
| - .ctypes_prefix("cty") |
106 |
| - .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) |
107 |
| - .default_enum_style(bindgen::EnumVariation::Rust { |
108 |
| - non_exhaustive: true, |
109 |
| - }) |
110 |
| - .default_alias_style(::bindgen::AliasVariation::TypeAlias) |
111 |
| - .default_macro_constant_type(bindgen::MacroTypeVariation::Unsigned) |
112 |
| - .default_non_copy_union_style(bindgen::NonCopyUnionStyle::ManuallyDrop) |
113 |
| - .translate_enum_integer_types(true) |
114 |
| - .derive_copy(true) |
115 |
| - .derive_default(true) |
116 |
| - .size_t_is_usize(true) |
117 |
| - .allowlist_recursively(true) |
118 |
| - .merge_extern_blocks(true) |
119 |
| - .generate_inline_functions(true) |
120 |
| - .vtable_generation(true) |
121 |
| - .generate_comments(true) |
122 |
| - .generate_block(true) |
123 |
| - .detect_include_paths(true) |
124 |
| - .prepend_enum_name(false) |
125 |
| - .block_extern_crate(false) |
126 |
| - .fit_macro_constants(false) |
127 |
| - .layout_tests(false) |
128 |
| - .use_core() |
129 |
| - .emit_builtins() |
130 |
| - .enable_function_attribute_detection() |
131 |
| - .generate() |
| 92 | + .unwrap(); |
| 93 | + |
| 94 | + let blocklist_regexpr = |
| 95 | + Regex::new(&format!(r"({})", self.blocklist_types.join("|"))).unwrap(); |
| 96 | + |
| 97 | + let mut raw_lines = vec![ |
| 98 | + format!("// Generated at {}", chrono::offset::Local::now()), |
| 99 | + "use cty;".into(), |
| 100 | + ]; |
| 101 | + raw_lines.append(&mut self.raw_lines.clone()); |
| 102 | + |
| 103 | + let clang_args = vec![ |
| 104 | + "-Iwindows.h", |
| 105 | + "-Iwinnt.h", |
| 106 | + concat!("-I", env!("CARGO_MANIFEST_DIR"), "\\deps\\phnt-nightly/"), |
| 107 | + ]; |
| 108 | + |
| 109 | + bindgen::builder() |
| 110 | + .header(concat!(env!("CARGO_MANIFEST_DIR"), "/src/ffi/wrapper.h")) |
| 111 | + .raw_line(raw_lines.join("\r\n").as_str()) |
| 112 | + .clang_args(clang_args) |
| 113 | + .allowlist_file(allowlist_regexpr.as_str()) |
| 114 | + .blocklist_type(blocklist_regexpr.as_str()) |
| 115 | + .type_alias("NTSTATUS") |
| 116 | + .opaque_type("std::.*") |
| 117 | + .ctypes_prefix("cty") |
| 118 | + .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) |
| 119 | + .default_enum_style(bindgen::EnumVariation::Rust { |
| 120 | + non_exhaustive: true, |
| 121 | + }) |
| 122 | + .default_alias_style(::bindgen::AliasVariation::TypeAlias) |
| 123 | + .default_macro_constant_type(bindgen::MacroTypeVariation::Unsigned) |
| 124 | + .default_non_copy_union_style(bindgen::NonCopyUnionStyle::ManuallyDrop) |
| 125 | + .translate_enum_integer_types(true) |
| 126 | + .derive_copy(true) |
| 127 | + .derive_default(true) |
| 128 | + .size_t_is_usize(true) |
| 129 | + .allowlist_recursively(true) |
| 130 | + .merge_extern_blocks(true) |
| 131 | + .generate_inline_functions(true) |
| 132 | + .vtable_generation(true) |
| 133 | + .generate_comments(true) |
| 134 | + .generate_block(true) |
| 135 | + .detect_include_paths(true) |
| 136 | + .prepend_enum_name(false) |
| 137 | + .block_extern_crate(false) |
| 138 | + .fit_macro_constants(false) |
| 139 | + .layout_tests(false) |
| 140 | + .use_core() |
| 141 | + .emit_builtins() |
| 142 | + .enable_function_attribute_detection() |
| 143 | + .generate() |
| 144 | + } |
132 | 145 | }
|
133 |
| -} |
134 | 146 |
|
135 |
| -#[cfg_attr(docsrs, doc(cfg(not(feature = "regenerate"))))] |
136 |
| -#[cfg(not(feature = "regenerate"))] |
137 |
| -fn main() { |
138 |
| - println!("Using vendored bindings, build script skipped."); |
139 |
| -} |
| 147 | + pub fn main() { |
| 148 | + std::process::Command::new("git") |
| 149 | + .args(["submodule", "update", "--remote", "--recursive"]) |
| 150 | + .output() |
| 151 | + .expect("phnt/build.rs: failed to update the `phnt-nightly` submodule!"); |
140 | 152 |
|
141 |
| -// TODO(chore): Make vendored the default behavior and instead respect the "regenerate" |
142 |
| -#[cfg_attr(docsrs, doc(cfg(feature = "regenerate")))] |
143 |
| -#[cfg(feature = "regenerate")] |
144 |
| -fn main() { |
145 |
| - std::process::Command::new("git") |
146 |
| - .args(["submodule", "update", "--remote", "--recursive"]) |
147 |
| - .output() |
148 |
| - .expect("phnt/build.rs: failed to update the `phnt-nightly` submodule!"); |
149 |
| - |
150 |
| - println!(concat!( |
151 |
| - "cargo:rerun-if-changed=", |
152 |
| - env!("CARGO_MANIFEST_DIR"), |
153 |
| - "\\deps\\phnt-nightly" |
154 |
| - )); |
155 |
| - |
156 |
| - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("generated.rs"); |
157 |
| - |
158 |
| - BindgenConfig::default() |
159 |
| - .generate_bindings() |
160 |
| - .expect("Unable to generate bindings!") |
161 |
| - .write_to_file(out_path) |
162 |
| - .expect("Unable to write bindings"); |
163 |
| - |
164 |
| - println!("Generated bindings successfully."); |
| 153 | + println!(concat!( |
| 154 | + "cargo:rerun-if-changed=", |
| 155 | + env!("CARGO_MANIFEST_DIR"), |
| 156 | + "\\deps\\phnt-nightly" |
| 157 | + )); |
| 158 | + |
| 159 | + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("generated.rs"); |
| 160 | + |
| 161 | + BindgenConfig::default() |
| 162 | + .generate_bindings() |
| 163 | + .expect("Unable to generate bindings!") |
| 164 | + .write_to_file(out_path) |
| 165 | + .expect("Unable to write bindings"); |
| 166 | + |
| 167 | + println!("Generated bindings successfully."); |
| 168 | + } |
165 | 169 | }
|
0 commit comments