Skip to content

Commit 1ebdb6c

Browse files
Regenerate windows sys bindings (#1132)
1 parent 6890f3f commit 1ebdb6c

File tree

5 files changed

+81
-119
lines changed

5 files changed

+81
-119
lines changed

dev-tools/gen-windows-sys-binding/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ publish = false
77
[dependencies]
88
windows-bindgen = "0.58"
99
tempfile = "3"
10+
regex = "1"

dev-tools/gen-windows-sys-binding/src/main.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
44
use std::{
55
fs,
6-
io::{self, Read, Write},
6+
io::{BufWriter, Write as _},
77
};
88

9+
use regex::Regex;
10+
911
/// This is printed to the file before the rest of the contents.
1012
const PRELUDE: &str = r#"// This file is autogenerated.
1113
//
@@ -14,10 +16,9 @@ const PRELUDE: &str = r#"// This file is autogenerated.
1416
// ```
1517
// cd generate-windows-sys/
1618
// cargo run
17-
// ```
18-
"#;
19+
// ```"#;
1920

20-
fn main() -> io::Result<()> {
21+
fn main() {
2122
let manifest_dir = env!("CARGO_MANIFEST_DIR");
2223
let temp_file = tempfile::Builder::new()
2324
.suffix(".rs")
@@ -50,16 +51,39 @@ fn main() -> io::Result<()> {
5051
// Generate bindings.
5152
windows_bindgen::bindgen(&args).expect("running bindgen failed");
5253

53-
let mut bindings = String::new();
54-
fs::File::open(temp_file.path())
55-
.expect("failed to open temp windows_sys.rs")
56-
.read_to_string(&mut bindings)
57-
.expect("failed to read temp windows_sys.rs");
54+
let bindings =
55+
fs::read_to_string(temp_file.path()).expect("failed to read temp windows_sys.rs");
5856

5957
let mut f = fs::File::create(format!("{manifest_dir}/../../src/windows/windows_sys.rs"))
58+
.map(BufWriter::new)
6059
.expect("failed to create windows_sys.rs");
61-
f.write_all(PRELUDE.as_bytes())?;
62-
f.write_all(bindings.as_bytes())?;
6360

64-
Ok(())
61+
write!(&mut f, "{PRELUDE}\n{bindings}\n").unwrap();
62+
63+
let mut dll_names: Vec<&str> = Regex::new(r#"link!\("(.*)\.dll""#)
64+
.unwrap()
65+
.captures_iter(&bindings)
66+
.map(|caps| caps.extract().1)
67+
.map(|[dll_name]| dll_name)
68+
.filter(|dll_name| *dll_name != "kernel32")
69+
.collect();
70+
71+
if !dll_names.is_empty() {
72+
dll_names.sort_unstable();
73+
dll_names.dedup();
74+
75+
for dll_name in dll_names {
76+
write!(&mut f, r#"#[link(name = "{dll_name}")]"#).unwrap();
77+
f.write_all("\n".as_bytes()).unwrap();
78+
}
79+
80+
f.write_all(r#"extern "C" {}"#.as_bytes()).unwrap();
81+
f.write_all("\n".as_bytes()).unwrap();
82+
}
83+
84+
f.write_all(r#"use super::windows_targets;"#.as_bytes())
85+
.unwrap();
86+
f.write_all("\n".as_bytes()).unwrap();
87+
88+
f.into_inner().unwrap().sync_all().unwrap();
6589
}

src/windows/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ pub mod find_tools;
66

77
#[cfg(windows)]
88
pub(crate) mod windows_sys;
9+
#[cfg(windows)]
10+
mod windows_targets;
911

1012
#[cfg(windows)]
1113
mod registry;

src/windows/windows_sys.rs

Lines changed: 23 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// cd generate-windows-sys/
77
// cargo run
88
// ```
9-
// Bindings generated by `windows-bindgen` 0.57.0
9+
// Bindings generated by `windows-bindgen` 0.58.0
1010

1111
#![allow(
1212
non_snake_case,
@@ -15,112 +15,22 @@
1515
dead_code,
1616
clippy::all
1717
)]
18-
#[link(name = "advapi32")]
19-
extern "system" {
20-
pub fn RegCloseKey(hkey: HKEY) -> WIN32_ERROR;
21-
}
22-
#[link(name = "advapi32")]
23-
extern "system" {
24-
pub fn RegEnumKeyExW(
25-
hkey: HKEY,
26-
dwindex: u32,
27-
lpname: PWSTR,
28-
lpcchname: *mut u32,
29-
lpreserved: *const u32,
30-
lpclass: PWSTR,
31-
lpcchclass: *mut u32,
32-
lpftlastwritetime: *mut FILETIME,
33-
) -> WIN32_ERROR;
34-
}
35-
#[link(name = "advapi32")]
36-
extern "system" {
37-
pub fn RegOpenKeyExW(
38-
hkey: HKEY,
39-
lpsubkey: PCWSTR,
40-
uloptions: u32,
41-
samdesired: REG_SAM_FLAGS,
42-
phkresult: *mut HKEY,
43-
) -> WIN32_ERROR;
44-
}
45-
#[link(name = "advapi32")]
46-
extern "system" {
47-
pub fn RegQueryValueExW(
48-
hkey: HKEY,
49-
lpvaluename: PCWSTR,
50-
lpreserved: *const u32,
51-
lptype: *mut REG_VALUE_TYPE,
52-
lpdata: *mut u8,
53-
lpcbdata: *mut u32,
54-
) -> WIN32_ERROR;
55-
}
56-
#[link(name = "kernel32")]
57-
extern "system" {
58-
pub fn FreeLibrary(hlibmodule: HMODULE) -> BOOL;
59-
}
60-
#[link(name = "kernel32")]
61-
extern "system" {
62-
pub fn GetMachineTypeAttributes(
63-
machine: u16,
64-
machinetypeattributes: *mut MACHINE_ATTRIBUTES,
65-
) -> HRESULT;
66-
}
67-
#[link(name = "kernel32")]
68-
extern "system" {
69-
pub fn GetProcAddress(hmodule: HMODULE, lpprocname: PCSTR) -> FARPROC;
70-
}
71-
#[link(name = "kernel32")]
72-
extern "system" {
73-
pub fn LoadLibraryA(lplibfilename: PCSTR) -> HMODULE;
74-
}
75-
#[link(name = "kernel32")]
76-
extern "system" {
77-
pub fn OpenSemaphoreA(dwdesiredaccess: u32, binherithandle: BOOL, lpname: PCSTR) -> HANDLE;
78-
}
79-
#[link(name = "kernel32")]
80-
extern "system" {
81-
pub fn PeekNamedPipe(
82-
hnamedpipe: HANDLE,
83-
lpbuffer: *mut core::ffi::c_void,
84-
nbuffersize: u32,
85-
lpbytesread: *mut u32,
86-
lptotalbytesavail: *mut u32,
87-
lpbytesleftthismessage: *mut u32,
88-
) -> BOOL;
89-
}
90-
#[link(name = "kernel32")]
91-
extern "system" {
92-
pub fn ReleaseSemaphore(
93-
hsemaphore: HANDLE,
94-
lreleasecount: i32,
95-
lppreviouscount: *mut i32,
96-
) -> BOOL;
97-
}
98-
#[link(name = "kernel32")]
99-
extern "system" {
100-
pub fn WaitForSingleObject(hhandle: HANDLE, dwmilliseconds: u32) -> WAIT_EVENT;
101-
}
102-
#[link(name = "ole32")]
103-
extern "system" {
104-
pub fn CoCreateInstance(
105-
rclsid: *const GUID,
106-
punkouter: *mut core::ffi::c_void,
107-
dwclscontext: CLSCTX,
108-
riid: *const GUID,
109-
ppv: *mut *mut core::ffi::c_void,
110-
) -> HRESULT;
111-
}
112-
#[link(name = "ole32")]
113-
extern "system" {
114-
pub fn CoInitializeEx(pvreserved: *const core::ffi::c_void, dwcoinit: u32) -> HRESULT;
115-
}
116-
#[link(name = "oleaut32")]
117-
extern "system" {
118-
pub fn SysFreeString(bstrstring: BSTR);
119-
}
120-
#[link(name = "oleaut32")]
121-
extern "system" {
122-
pub fn SysStringLen(pbstr: BSTR) -> u32;
123-
}
18+
windows_targets::link!("advapi32.dll" "system" fn RegCloseKey(hkey : HKEY) -> WIN32_ERROR);
19+
windows_targets::link!("advapi32.dll" "system" fn RegEnumKeyExW(hkey : HKEY, dwindex : u32, lpname : PWSTR, lpcchname : *mut u32, lpreserved : *const u32, lpclass : PWSTR, lpcchclass : *mut u32, lpftlastwritetime : *mut FILETIME) -> WIN32_ERROR);
20+
windows_targets::link!("advapi32.dll" "system" fn RegOpenKeyExW(hkey : HKEY, lpsubkey : PCWSTR, uloptions : u32, samdesired : REG_SAM_FLAGS, phkresult : *mut HKEY) -> WIN32_ERROR);
21+
windows_targets::link!("advapi32.dll" "system" fn RegQueryValueExW(hkey : HKEY, lpvaluename : PCWSTR, lpreserved : *const u32, lptype : *mut REG_VALUE_TYPE, lpdata : *mut u8, lpcbdata : *mut u32) -> WIN32_ERROR);
22+
windows_targets::link!("kernel32.dll" "system" fn FreeLibrary(hlibmodule : HMODULE) -> BOOL);
23+
windows_targets::link!("kernel32.dll" "system" fn GetMachineTypeAttributes(machine : u16, machinetypeattributes : *mut MACHINE_ATTRIBUTES) -> HRESULT);
24+
windows_targets::link!("kernel32.dll" "system" fn GetProcAddress(hmodule : HMODULE, lpprocname : PCSTR) -> FARPROC);
25+
windows_targets::link!("kernel32.dll" "system" fn LoadLibraryA(lplibfilename : PCSTR) -> HMODULE);
26+
windows_targets::link!("kernel32.dll" "system" fn OpenSemaphoreA(dwdesiredaccess : u32, binherithandle : BOOL, lpname : PCSTR) -> HANDLE);
27+
windows_targets::link!("kernel32.dll" "system" fn PeekNamedPipe(hnamedpipe : HANDLE, lpbuffer : *mut core::ffi::c_void, nbuffersize : u32, lpbytesread : *mut u32, lptotalbytesavail : *mut u32, lpbytesleftthismessage : *mut u32) -> BOOL);
28+
windows_targets::link!("kernel32.dll" "system" fn ReleaseSemaphore(hsemaphore : HANDLE, lreleasecount : i32, lppreviouscount : *mut i32) -> BOOL);
29+
windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObject(hhandle : HANDLE, dwmilliseconds : u32) -> WAIT_EVENT);
30+
windows_targets::link!("ole32.dll" "system" fn CoCreateInstance(rclsid : *const GUID, punkouter : * mut core::ffi::c_void, dwclscontext : CLSCTX, riid : *const GUID, ppv : *mut *mut core::ffi::c_void) -> HRESULT);
31+
windows_targets::link!("ole32.dll" "system" fn CoInitializeEx(pvreserved : *const core::ffi::c_void, dwcoinit : u32) -> HRESULT);
32+
windows_targets::link!("oleaut32.dll" "system" fn SysFreeString(bstrstring : BSTR));
33+
windows_targets::link!("oleaut32.dll" "system" fn SysStringLen(pbstr : BSTR) -> u32);
12434
pub type ADVANCED_FEATURE_FLAGS = u16;
12535
pub type BOOL = i32;
12636
pub type BSTR = *const u16;
@@ -203,3 +113,9 @@ pub const WAIT_FAILED: WAIT_EVENT = 4294967295u32;
203113
pub const WAIT_OBJECT_0: WAIT_EVENT = 0u32;
204114
pub const WAIT_TIMEOUT: WAIT_EVENT = 258u32;
205115
pub type WIN32_ERROR = u32;
116+
117+
#[link(name = "advapi32")]
118+
#[link(name = "ole32")]
119+
#[link(name = "oleaut32")]
120+
extern "C" {}
121+
use super::windows_targets;

src/windows/windows_targets.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Provides the `link!` macro used by the generated windows bindings.
2+
//!
3+
//! This is a simple wrapper around an `extern` block with a `#[link]` attribute.
4+
//! It's very roughly equivalent to the windows-targets crate.
5+
6+
macro_rules! link_macro {
7+
($library:literal $abi:literal $($link_name:literal)? $(#[$doc:meta])? fn $($function:tt)*) => (
8+
// Note: the windows-targets crate uses a pre-built Windows.lib import library which we don't
9+
// have in this repo. So instead we always link kernel32.lib and add the rest of the import
10+
// libraries below by using an empty extern block. This works because extern blocks are not
11+
// connected to the library given in the #[link] attribute.
12+
#[link(name = "kernel32")]
13+
extern $abi {
14+
$(#[link_name=$link_name])?
15+
pub fn $($function)*;
16+
}
17+
)
18+
}
19+
pub(crate) use link_macro as link;

0 commit comments

Comments
 (0)