Skip to content

Commit b388631

Browse files
authored
Vendor windows-sys crate (#837)
1 parent df2f86c commit b388631

File tree

11 files changed

+316
-35
lines changed

11 files changed

+316
-35
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ jobserver = { version = "0.1.16", optional = true }
2323
[target.'cfg(unix)'.dependencies]
2424
libc = "0.2.62"
2525

26-
[target.'cfg(windows)'.dependencies]
27-
windows-sys = { version = "0.48.0", features = ["Win32_Foundation", "Win32_System_Pipes", "Win32_Security", "Win32_System_Com", "Win32_System_Registry"] }
28-
2926
[features]
3027
parallel = ["jobserver"]
3128

gen-windows-sys-binding/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "gen-windows-sys-binding"
3+
version = "0.0.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
windows-bindgen = "0.49"
9+
10+
# Prevent this from interfering with workspaces
11+
[workspace]
12+
members = ["."]

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//! Adapted from
2+
//! https://github.com/rust-lang/rust/blob/master/src/tools/generate-windows-sys/src/main.rs
3+
4+
use std::{
5+
fs,
6+
io::{self, Write},
7+
};
8+
9+
/// This is printed to the file before the rest of the contents.
10+
const PRELUDE: &str = r#"// This file is autogenerated.
11+
//
12+
// To add bindings, edit windows_sys.lst then run:
13+
//
14+
// ```
15+
// cd generate-windows-sys/
16+
// cargo run
17+
// ```
18+
"#;
19+
20+
const POSTLUDE: &str = r#"
21+
/// Adapted from
22+
/// [`core::ptr::invalid_mut()`](https://doc.rust-lang.org/src/core/ptr/mod.rs.html#600-607).
23+
///
24+
/// This function should actually use `core::mem::transmute` but due to msrv
25+
/// we use `as` casting instead.
26+
///
27+
/// Once msrv is bumped to 1.56, replace this with `core::mem::transmute` since
28+
/// it is const stablised in 1.56
29+
///
30+
/// NOTE that once supports `strict_provenance` we would also have to update
31+
/// this.
32+
const fn invalid_mut<T>(addr: usize) -> *mut T {
33+
addr as *mut T
34+
}
35+
"#;
36+
37+
fn main() -> io::Result<()> {
38+
// Load the list of APIs
39+
let buffer = fs::read_to_string("windows_sys.list")?;
40+
let names: Vec<&str> = buffer
41+
.lines()
42+
.filter_map(|line| {
43+
let line = line.trim();
44+
if line.is_empty() || line.starts_with("//") {
45+
None
46+
} else {
47+
Some(line)
48+
}
49+
})
50+
.collect();
51+
52+
// Write the bindings to windows-sys.rs
53+
let bindings =
54+
windows_bindgen::standalone_std(&names).replace("::core::ptr::invalid_mut", "invalid_mut");
55+
56+
let mut f = fs::File::create("../src/windows_sys.rs")?;
57+
f.write_all(PRELUDE.as_bytes())?;
58+
f.write_all(bindings.as_bytes())?;
59+
f.write_all(POSTLUDE.as_bytes())?;
60+
61+
Ok(())
62+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Windows.Win32.Foundation.FILETIME
2+
Windows.Win32.Foundation.INVALID_HANDLE_VALUE
3+
Windows.Win32.Foundation.ERROR_NO_MORE_ITEMS
4+
Windows.Win32.Foundation.ERROR_SUCCESS
5+
Windows.Win32.Foundation.SysFreeString
6+
Windows.Win32.Foundation.SysStringLen
7+
Windows.Win32.Foundation.S_FALSE
8+
Windows.Win32.Foundation.S_OK
9+
10+
Windows.Win32.System.Com.SAFEARRAY
11+
Windows.Win32.System.Com.SAFEARRAYBOUND
12+
Windows.Win32.System.Com.CLSCTX_ALL
13+
Windows.Win32.System.Com.COINIT_MULTITHREADED
14+
Windows.Win32.System.Com.CoCreateInstance
15+
Windows.Win32.System.Com.CoInitializeEx
16+
17+
Windows.Win32.System.Pipes.CreatePipe
18+
19+
Windows.Win32.System.Registry.RegCloseKey
20+
Windows.Win32.System.Registry.RegEnumKeyExW
21+
Windows.Win32.System.Registry.RegOpenKeyExW
22+
Windows.Win32.System.Registry.RegQueryValueExW
23+
Windows.Win32.System.Registry.HKEY
24+
Windows.Win32.System.Registry.HKEY_LOCAL_MACHINE
25+
Windows.Win32.System.Registry.KEY_READ
26+
Windows.Win32.System.Registry.KEY_WOW64_32KEY
27+
Windows.Win32.System.Registry.REG_SZ

src/com.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
#![allow(unused)]
99

10-
use crate::winapi::{IUnknown, Interface};
10+
use crate::{
11+
winapi::{IUnknown, Interface},
12+
windows_sys::{
13+
CoInitializeEx, SysFreeString, SysStringLen, BSTR, COINIT_MULTITHREADED, HRESULT, S_FALSE,
14+
S_OK,
15+
},
16+
};
1117
use std::{
1218
ffi::{OsStr, OsString},
1319
mem::ManuallyDrop,
@@ -16,13 +22,6 @@ use std::{
1622
ptr::{null, null_mut},
1723
slice::from_raw_parts,
1824
};
19-
use windows_sys::{
20-
core::{BSTR, HRESULT},
21-
Win32::{
22-
Foundation::{SysFreeString, SysStringLen, S_FALSE, S_OK},
23-
System::Com::{CoInitializeEx, COINIT_MULTITHREADED},
24-
},
25-
};
2625

2726
pub fn initialize() -> Result<(), HRESULT> {
2827
let err = unsafe { CoInitializeEx(null(), COINIT_MULTITHREADED) };

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ mod com;
8181
mod setup_config;
8282
#[cfg(windows)]
8383
mod vs_instances;
84+
#[cfg(windows)]
85+
mod windows_sys;
8486

8587
pub mod windows_registry;
8688

src/os_pipe/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use crate::windows_sys::{CreatePipe, INVALID_HANDLE_VALUE};
12
use std::{fs::File, io, os::windows::prelude::*, ptr};
2-
use windows_sys::Win32::{Foundation::INVALID_HANDLE_VALUE, System::Pipes::CreatePipe};
33

44
/// NOTE: These pipes do not support IOCP.
55
///

src/registry.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use crate::windows_sys::{
12+
RegCloseKey, RegEnumKeyExW, RegOpenKeyExW, RegQueryValueExW, ERROR_NO_MORE_ITEMS,
13+
ERROR_SUCCESS, HKEY, HKEY_LOCAL_MACHINE, KEY_READ, KEY_WOW64_32KEY, REG_SZ,
14+
};
1115
use std::{
1216
ffi::{OsStr, OsString},
1317
io,
1418
ops::RangeFrom,
1519
os::windows::prelude::*,
1620
ptr::null_mut,
1721
};
18-
use windows_sys::Win32::{
19-
Foundation::{ERROR_NO_MORE_ITEMS, ERROR_SUCCESS},
20-
System::Registry::{
21-
RegCloseKey, RegEnumKeyExW, RegOpenKeyExW, RegQueryValueExW, HKEY, HKEY_LOCAL_MACHINE,
22-
KEY_READ, KEY_WOW64_32KEY, REG_SZ,
23-
},
24-
};
2522

2623
/// Must never be `HKEY_PERFORMANCE_DATA`.
2724
pub(crate) struct RegistryKey(Repr);
@@ -59,7 +56,7 @@ impl RegistryKey {
5956
/// Open a sub-key of `self`.
6057
pub fn open(&self, key: &OsStr) -> io::Result<RegistryKey> {
6158
let key = key.encode_wide().chain(Some(0)).collect::<Vec<_>>();
62-
let mut ret = 0;
59+
let mut ret = null_mut();
6360
let err = unsafe {
6461
RegOpenKeyExW(
6562
self.raw(),

src/setup_config.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,13 @@ use crate::{
1414
IUnknown, IUnknownVtbl, Interface, LCID, LPCOLESTR, LPCWSTR, LPFILETIME, LPSAFEARRAY,
1515
PULONGLONG, ULONG,
1616
},
17+
windows_sys::{CoCreateInstance, BSTR, CLSCTX_ALL, HRESULT, S_FALSE},
1718
};
1819

1920
use std::{
2021
ffi::OsString,
2122
ptr::{null, null_mut},
2223
};
23-
use windows_sys::{
24-
core::{BSTR, HRESULT},
25-
Win32::{
26-
Foundation::S_FALSE,
27-
System::Com::{CoCreateInstance, CLSCTX_ALL},
28-
},
29-
};
3024

3125
// Bindings to the Setup.Configuration stuff
3226
pub type InstanceState = u32;

src/winapi.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,12 @@ use std::os::raw;
1111

1212
pub type wchar_t = u16;
1313

14-
pub use windows_sys::{
15-
core::GUID,
16-
Win32::{
17-
Foundation::FILETIME,
18-
System::Com::{SAFEARRAY, SAFEARRAYBOUND},
19-
},
20-
};
14+
pub use crate::windows_sys::{FILETIME, GUID, HRESULT, SAFEARRAY, SAFEARRAYBOUND};
2115

2216
pub type REFIID = *const IID;
2317
pub type IID = GUID;
2418
pub type ULONG = raw::c_ulong;
2519
pub type DWORD = u32;
26-
pub type HRESULT = raw::c_long;
2720
pub type LPFILETIME = *mut FILETIME;
2821
pub type OLECHAR = WCHAR;
2922
pub type WCHAR = wchar_t;

0 commit comments

Comments
 (0)