Skip to content

Commit

Permalink
Vendor windows-sys crate (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
NobodyXu committed Jul 26, 2023
1 parent df2f86c commit b388631
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 35 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ jobserver = { version = "0.1.16", optional = true }
[target.'cfg(unix)'.dependencies]
libc = "0.2.62"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.48.0", features = ["Win32_Foundation", "Win32_System_Pipes", "Win32_Security", "Win32_System_Com", "Win32_System_Registry"] }

[features]
parallel = ["jobserver"]

Expand Down
12 changes: 12 additions & 0 deletions gen-windows-sys-binding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "gen-windows-sys-binding"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies]
windows-bindgen = "0.49"

# Prevent this from interfering with workspaces
[workspace]
members = ["."]
62 changes: 62 additions & 0 deletions gen-windows-sys-binding/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! Adapted from
//! https://github.com/rust-lang/rust/blob/master/src/tools/generate-windows-sys/src/main.rs

use std::{
fs,
io::{self, Write},
};

/// This is printed to the file before the rest of the contents.
const PRELUDE: &str = r#"// This file is autogenerated.
//
// To add bindings, edit windows_sys.lst then run:
//
// ```
// cd generate-windows-sys/
// cargo run
// ```
"#;

const POSTLUDE: &str = r#"
/// Adapted from
/// [`core::ptr::invalid_mut()`](https://doc.rust-lang.org/src/core/ptr/mod.rs.html#600-607).
///
/// This function should actually use `core::mem::transmute` but due to msrv
/// we use `as` casting instead.
///
/// Once msrv is bumped to 1.56, replace this with `core::mem::transmute` since
/// it is const stablised in 1.56
///
/// NOTE that once supports `strict_provenance` we would also have to update
/// this.
const fn invalid_mut<T>(addr: usize) -> *mut T {
addr as *mut T
}
"#;

fn main() -> io::Result<()> {
// Load the list of APIs
let buffer = fs::read_to_string("windows_sys.list")?;
let names: Vec<&str> = buffer
.lines()
.filter_map(|line| {
let line = line.trim();
if line.is_empty() || line.starts_with("//") {
None
} else {
Some(line)
}
})
.collect();

// Write the bindings to windows-sys.rs
let bindings =
windows_bindgen::standalone_std(&names).replace("::core::ptr::invalid_mut", "invalid_mut");

let mut f = fs::File::create("../src/windows_sys.rs")?;
f.write_all(PRELUDE.as_bytes())?;
f.write_all(bindings.as_bytes())?;
f.write_all(POSTLUDE.as_bytes())?;

Ok(())
}
27 changes: 27 additions & 0 deletions gen-windows-sys-binding/windows_sys.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Windows.Win32.Foundation.FILETIME
Windows.Win32.Foundation.INVALID_HANDLE_VALUE
Windows.Win32.Foundation.ERROR_NO_MORE_ITEMS
Windows.Win32.Foundation.ERROR_SUCCESS
Windows.Win32.Foundation.SysFreeString
Windows.Win32.Foundation.SysStringLen
Windows.Win32.Foundation.S_FALSE
Windows.Win32.Foundation.S_OK

Windows.Win32.System.Com.SAFEARRAY
Windows.Win32.System.Com.SAFEARRAYBOUND
Windows.Win32.System.Com.CLSCTX_ALL
Windows.Win32.System.Com.COINIT_MULTITHREADED
Windows.Win32.System.Com.CoCreateInstance
Windows.Win32.System.Com.CoInitializeEx

Windows.Win32.System.Pipes.CreatePipe

Windows.Win32.System.Registry.RegCloseKey
Windows.Win32.System.Registry.RegEnumKeyExW
Windows.Win32.System.Registry.RegOpenKeyExW
Windows.Win32.System.Registry.RegQueryValueExW
Windows.Win32.System.Registry.HKEY
Windows.Win32.System.Registry.HKEY_LOCAL_MACHINE
Windows.Win32.System.Registry.KEY_READ
Windows.Win32.System.Registry.KEY_WOW64_32KEY
Windows.Win32.System.Registry.REG_SZ
15 changes: 7 additions & 8 deletions src/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

#![allow(unused)]

use crate::winapi::{IUnknown, Interface};
use crate::{
winapi::{IUnknown, Interface},
windows_sys::{
CoInitializeEx, SysFreeString, SysStringLen, BSTR, COINIT_MULTITHREADED, HRESULT, S_FALSE,
S_OK,
},
};
use std::{
ffi::{OsStr, OsString},
mem::ManuallyDrop,
Expand All @@ -16,13 +22,6 @@ use std::{
ptr::{null, null_mut},
slice::from_raw_parts,
};
use windows_sys::{
core::{BSTR, HRESULT},
Win32::{
Foundation::{SysFreeString, SysStringLen, S_FALSE, S_OK},
System::Com::{CoInitializeEx, COINIT_MULTITHREADED},
},
};

pub fn initialize() -> Result<(), HRESULT> {
let err = unsafe { CoInitializeEx(null(), COINIT_MULTITHREADED) };
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ mod com;
mod setup_config;
#[cfg(windows)]
mod vs_instances;
#[cfg(windows)]
mod windows_sys;

pub mod windows_registry;

Expand Down
2 changes: 1 addition & 1 deletion src/os_pipe/windows.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::windows_sys::{CreatePipe, INVALID_HANDLE_VALUE};
use std::{fs::File, io, os::windows::prelude::*, ptr};
use windows_sys::Win32::{Foundation::INVALID_HANDLE_VALUE, System::Pipes::CreatePipe};

/// NOTE: These pipes do not support IOCP.
///
Expand Down
13 changes: 5 additions & 8 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::windows_sys::{
RegCloseKey, RegEnumKeyExW, RegOpenKeyExW, RegQueryValueExW, ERROR_NO_MORE_ITEMS,
ERROR_SUCCESS, HKEY, HKEY_LOCAL_MACHINE, KEY_READ, KEY_WOW64_32KEY, REG_SZ,
};
use std::{
ffi::{OsStr, OsString},
io,
ops::RangeFrom,
os::windows::prelude::*,
ptr::null_mut,
};
use windows_sys::Win32::{
Foundation::{ERROR_NO_MORE_ITEMS, ERROR_SUCCESS},
System::Registry::{
RegCloseKey, RegEnumKeyExW, RegOpenKeyExW, RegQueryValueExW, HKEY, HKEY_LOCAL_MACHINE,
KEY_READ, KEY_WOW64_32KEY, REG_SZ,
},
};

/// Must never be `HKEY_PERFORMANCE_DATA`.
pub(crate) struct RegistryKey(Repr);
Expand Down Expand Up @@ -59,7 +56,7 @@ impl RegistryKey {
/// Open a sub-key of `self`.
pub fn open(&self, key: &OsStr) -> io::Result<RegistryKey> {
let key = key.encode_wide().chain(Some(0)).collect::<Vec<_>>();
let mut ret = 0;
let mut ret = null_mut();
let err = unsafe {
RegOpenKeyExW(
self.raw(),
Expand Down
8 changes: 1 addition & 7 deletions src/setup_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ use crate::{
IUnknown, IUnknownVtbl, Interface, LCID, LPCOLESTR, LPCWSTR, LPFILETIME, LPSAFEARRAY,
PULONGLONG, ULONG,
},
windows_sys::{CoCreateInstance, BSTR, CLSCTX_ALL, HRESULT, S_FALSE},
};

use std::{
ffi::OsString,
ptr::{null, null_mut},
};
use windows_sys::{
core::{BSTR, HRESULT},
Win32::{
Foundation::S_FALSE,
System::Com::{CoCreateInstance, CLSCTX_ALL},
},
};

// Bindings to the Setup.Configuration stuff
pub type InstanceState = u32;
Expand Down
9 changes: 1 addition & 8 deletions src/winapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,12 @@ use std::os::raw;

pub type wchar_t = u16;

pub use windows_sys::{
core::GUID,
Win32::{
Foundation::FILETIME,
System::Com::{SAFEARRAY, SAFEARRAYBOUND},
},
};
pub use crate::windows_sys::{FILETIME, GUID, HRESULT, SAFEARRAY, SAFEARRAYBOUND};

pub type REFIID = *const IID;
pub type IID = GUID;
pub type ULONG = raw::c_ulong;
pub type DWORD = u32;
pub type HRESULT = raw::c_long;
pub type LPFILETIME = *mut FILETIME;
pub type OLECHAR = WCHAR;
pub type WCHAR = wchar_t;
Expand Down

0 comments on commit b388631

Please sign in to comment.