Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Nov 28, 2024
1 parent 2fadf9c commit 0f87171
Show file tree
Hide file tree
Showing 21 changed files with 564 additions and 26 deletions.
5 changes: 1 addition & 4 deletions crates/libs/bindgen/src/tables/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ impl Attribute {
0x50 => Value::TypeName(TypeName::parse(values.read_str())),
0x55 => {
let tn = TypeName::parse(name);
let def = reader
.with_full_name(tn.namespace(), tn.name())
.next()
.expect("Type not found");
let def = reader.unwrap_full_name(tn.namespace(), tn.name());
name = values.read_str();
let underlying_type = def.underlying_type();
values.read_integer(underlying_type)
Expand Down
15 changes: 15 additions & 0 deletions crates/libs/bindgen/src/types/cpp_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ impl CppEnum {
}
}

pub fn dependencies(&self, dependencies: &mut TypeMap) {
if let Some(attribute) = self.def.find_attribute("AlsoUsableForAttribute") {
if let Some((_, Value::Str(type_name))) = attribute.args().first() {
if let Some(ty) = self
.def
.reader()
.with_full_name(self.def.namespace(), type_name)
.next()
{
ty.dependencies(dependencies);
}
}
}
}

pub fn size(&self) -> usize {
self.def.underlying_type().size()
}
Expand Down
56 changes: 54 additions & 2 deletions crates/libs/bindgen/src/types/cpp_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ impl CppFn {

let link = self.write_link(writer, false);
let cfg = writer.write_cfg(self.method, self.namespace, &dependencies, false);

let window_long = self.write_window_long();
if writer.config.sys {
return quote! {
#cfg
#link
#window_long
};
}

Expand All @@ -99,7 +100,7 @@ impl CppFn {
let generics = method.write_generics();
let abi_return_type = method.write_return(writer);

match method.return_hint {
let wrapper = match method.return_hint {
ReturnHint::Query(..) => {
let where_clause = method.write_where(writer, true);

Expand Down Expand Up @@ -239,13 +240,64 @@ impl CppFn {
}
}
}
};

quote! {
#wrapper
#window_long
}
}

fn write_window_long(&self) -> TokenStream {
match self.method.name() {
"GetWindowLongPtrA" => quote! {
#[cfg(target_pointer_width = "32")]
pub use GetWindowLongA as GetWindowLongPtrA;
},
"GetWindowLongPtrW" => quote! {
#[cfg(target_pointer_width = "32")]
pub use GetWindowLongW as GetWindowLongPtrW;
},
"SetWindowLongPtrA" => quote! {
#[cfg(target_pointer_width = "32")]
pub use SetWindowLongA as SetWindowLongPtrA;
},
"SetWindowLongPtrW" => quote! {
#[cfg(target_pointer_width = "32")]
pub use SetWindowLongW as SetWindowLongPtrW;
},
_ => quote! {},
}
}

pub fn dependencies(&self, dependencies: &mut TypeMap) {
self.method
.signature(self.namespace, &[])
.dependencies(dependencies);

match self.method.name() {
"GetWindowLongPtrA" => self
.method
.reader()
.unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "GetWindowLongA")
.dependencies(dependencies),
"GetWindowLongPtrW" => self
.method
.reader()
.unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "GetWindowLongW")
.dependencies(dependencies),
"SetWindowLongPtrA" => self
.method
.reader()
.unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "SetWindowLongA")
.dependencies(dependencies),
"SetWindowLongPtrW" => self
.method
.reader()
.unwrap_full_name("Windows.Win32.UI.WindowsAndMessaging", "SetWindowLongW")
.dependencies(dependencies),
_ => {}
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions crates/libs/bindgen/src/types/cpp_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ impl CppStruct {
for field in self.def.fields() {
field.ty(Some(self)).dependencies(dependencies);
}

if let Some(attribute) = self.def.find_attribute("AlsoUsableForAttribute") {
if let Some((_, Value::Str(type_name))) = attribute.args().first() {
if let Some(ty) = self
.def
.reader()
.with_full_name(self.def.namespace(), type_name)
.next()
{
ty.dependencies(dependencies);
}
}
}
}

pub fn is_convertible(&self) -> bool {
Expand Down
25 changes: 6 additions & 19 deletions crates/libs/bindgen/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,8 @@ impl Type {
}
}

if let Some(ty) = code
.reader()
.with_full_name(code_name.namespace(), code_name.name())
.next()
{
ty
} else {
panic!("type not found: {code_name}")
}
code.reader()
.unwrap_full_name(code_name.namespace(), code_name.name())
}

pub fn from_blob(blob: &mut Blob, enclosing: Option<&CppStruct>, generics: &[Self]) -> Self {
Expand Down Expand Up @@ -235,9 +228,7 @@ impl Type {

let mut ty = blob
.reader()
.with_full_name(code_name.namespace(), code_name.name())
.next()
.unwrap_or_else(|| panic!("type not found: {code_name}"));
.unwrap_full_name(code_name.namespace(), code_name.name());

let mut item_generics = vec![];

Expand Down Expand Up @@ -513,18 +504,14 @@ impl Type {
let base = ty
.def
.reader()
.with_full_name(ty.def.namespace(), ty.def.name())
.next()
.unwrap();
.unwrap_full_name(ty.def.namespace(), ty.def.name());
(base, ty.generics.clone())
}
Self::Delegate(ty) if !ty.generics.is_empty() => {
let base = ty
.def
.reader()
.with_full_name(ty.def.namespace(), ty.def.name())
.next()
.unwrap();
.unwrap_full_name(ty.def.namespace(), ty.def.name());
(base, ty.generics.clone())
}
_ => (self.clone(), vec![]),
Expand Down Expand Up @@ -595,7 +582,7 @@ impl Type {
Self::CppFn(ty) => ty.dependencies(dependencies),
Self::CppInterface(ty) => ty.dependencies(dependencies),
Self::CppStruct(ty) => ty.dependencies(dependencies),
Self::CppEnum(..) => {}
Self::CppEnum(ty) => ty.dependencies(dependencies),

Self::IUnknown => {
Self::GUID.dependencies(dependencies);
Expand Down
8 changes: 8 additions & 0 deletions crates/libs/bindgen/src/winmd/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ impl Reader {
reader
}

pub fn unwrap_full_name(&self, namespace: &str, name: &str) -> Type {
if let Some(ty) = self.with_full_name(namespace, name).next() {
ty
} else {
panic!("type not found: {namespace}.{name}")
}
}

/// Gets all types matching the given namespace and name.
pub fn with_full_name(&self, namespace: &str, name: &str) -> impl Iterator<Item = Type> + '_ {
self.get(namespace)
Expand Down
1 change: 1 addition & 0 deletions crates/libs/core/src/imp/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub type WAIT_EVENT = u32;
pub type BOOL = i32;
pub type CO_MTA_USAGE_COOKIE = *mut core::ffi::c_void;
pub type HANDLE = *mut core::ffi::c_void;
pub type HINSTANCE = *mut core::ffi::c_void;
pub type HMODULE = *mut core::ffi::c_void;
#[repr(C)]
#[derive(Clone, Copy)]
Expand Down
1 change: 1 addition & 0 deletions crates/libs/result/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub type LOAD_LIBRARY_FLAGS = u32;
pub type WIN32_ERROR = u32;
pub type BOOL = i32;
pub type HANDLE = *mut core::ffi::c_void;
pub type HINSTANCE = *mut core::ffi::c_void;
pub type HMODULE = *mut core::ffi::c_void;
pub const ERROR_INVALID_DATA: WIN32_ERROR = 13u32;
pub const ERROR_NO_UNICODE_TRANSLATION: WIN32_ERROR = 1113u32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,12 @@ windows_targets::link!("user32.dll" "system" fn GetWindowInfo(hwnd : super::supe
windows_targets::link!("user32.dll" "system" fn GetWindowLongA(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX) -> i32);
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec", target_arch = "x86_64"))]
windows_targets::link!("user32.dll" "system" fn GetWindowLongPtrA(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX) -> isize);
#[cfg(target_pointer_width = "32")]
pub use GetWindowLongA as GetWindowLongPtrA;
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec", target_arch = "x86_64"))]
windows_targets::link!("user32.dll" "system" fn GetWindowLongPtrW(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX) -> isize);
#[cfg(target_pointer_width = "32")]
pub use GetWindowLongW as GetWindowLongPtrW;
windows_targets::link!("user32.dll" "system" fn GetWindowLongW(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX) -> i32);
windows_targets::link!("user32.dll" "system" fn GetWindowModuleFileNameA(hwnd : super::super::Foundation:: HWND, pszfilename : windows_sys::core::PSTR, cchfilenamemax : u32) -> u32);
windows_targets::link!("user32.dll" "system" fn GetWindowModuleFileNameW(hwnd : super::super::Foundation:: HWND, pszfilename : windows_sys::core::PWSTR, cchfilenamemax : u32) -> u32);
Expand Down Expand Up @@ -408,8 +412,12 @@ windows_targets::link!("user32.dll" "system" fn SetWindowDisplayAffinity(hwnd :
windows_targets::link!("user32.dll" "system" fn SetWindowLongA(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX, dwnewlong : i32) -> i32);
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec", target_arch = "x86_64"))]
windows_targets::link!("user32.dll" "system" fn SetWindowLongPtrA(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX, dwnewlong : isize) -> isize);
#[cfg(target_pointer_width = "32")]
pub use SetWindowLongA as SetWindowLongPtrA;
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec", target_arch = "x86_64"))]
windows_targets::link!("user32.dll" "system" fn SetWindowLongPtrW(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX, dwnewlong : isize) -> isize);
#[cfg(target_pointer_width = "32")]
pub use SetWindowLongW as SetWindowLongPtrW;
windows_targets::link!("user32.dll" "system" fn SetWindowLongW(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX, dwnewlong : i32) -> i32);
windows_targets::link!("user32.dll" "system" fn SetWindowPlacement(hwnd : super::super::Foundation:: HWND, lpwndpl : *const WINDOWPLACEMENT) -> super::super::Foundation:: BOOL);
windows_targets::link!("user32.dll" "system" fn SetWindowPos(hwnd : super::super::Foundation:: HWND, hwndinsertafter : super::super::Foundation:: HWND, x : i32, y : i32, cx : i32, cy : i32, uflags : SET_WINDOW_POS_FLAGS) -> super::super::Foundation:: BOOL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,8 @@ where
windows_targets::link!("user32.dll" "system" fn GetWindowLongPtrA(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX) -> isize);
GetWindowLongPtrA(hwnd.param().abi(), core::mem::transmute(nindex))
}
#[cfg(target_pointer_width = "32")]
pub use GetWindowLongA as GetWindowLongPtrA;
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec", target_arch = "x86_64"))]
#[inline]
pub unsafe fn GetWindowLongPtrW<P0>(hwnd: P0, nindex: WINDOW_LONG_PTR_INDEX) -> isize
Expand All @@ -1555,6 +1557,8 @@ where
windows_targets::link!("user32.dll" "system" fn GetWindowLongPtrW(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX) -> isize);
GetWindowLongPtrW(hwnd.param().abi(), core::mem::transmute(nindex))
}
#[cfg(target_pointer_width = "32")]
pub use GetWindowLongW as GetWindowLongPtrW;
#[inline]
pub unsafe fn GetWindowLongW<P0>(hwnd: P0, nindex: WINDOW_LONG_PTR_INDEX) -> i32
where
Expand Down Expand Up @@ -3021,6 +3025,8 @@ where
windows_targets::link!("user32.dll" "system" fn SetWindowLongPtrA(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX, dwnewlong : isize) -> isize);
SetWindowLongPtrA(hwnd.param().abi(), core::mem::transmute(nindex), core::mem::transmute(dwnewlong))
}
#[cfg(target_pointer_width = "32")]
pub use SetWindowLongA as SetWindowLongPtrA;
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec", target_arch = "x86_64"))]
#[inline]
pub unsafe fn SetWindowLongPtrW<P0>(hwnd: P0, nindex: WINDOW_LONG_PTR_INDEX, dwnewlong: isize) -> isize
Expand All @@ -3030,6 +3036,8 @@ where
windows_targets::link!("user32.dll" "system" fn SetWindowLongPtrW(hwnd : super::super::Foundation:: HWND, nindex : WINDOW_LONG_PTR_INDEX, dwnewlong : isize) -> isize);
SetWindowLongPtrW(hwnd.param().abi(), core::mem::transmute(nindex), core::mem::transmute(dwnewlong))
}
#[cfg(target_pointer_width = "32")]
pub use SetWindowLongW as SetWindowLongPtrW;
#[inline]
pub unsafe fn SetWindowLongW<P0>(hwnd: P0, nindex: WINDOW_LONG_PTR_INDEX, dwnewlong: i32) -> i32
where
Expand Down
1 change: 1 addition & 0 deletions crates/tests/bindgen/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ pub type FARPROC = Option<unsafe extern "system" fn() -> isize>;
pub type LOAD_LIBRARY_FLAGS = u32;
pub type BOOL = i32;
pub type HANDLE = *mut core::ffi::c_void;
pub type HINSTANCE = *mut core::ffi::c_void;
pub type HMODULE = *mut core::ffi::c_void;
pub const LOAD_LIBRARY_SEARCH_DEFAULT_DIRS: LOAD_LIBRARY_FLAGS = 4096u32;
8 changes: 8 additions & 0 deletions crates/tests/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ pub mod struct_win;
pub mod struct_with_cpp_interface;
pub mod struct_with_cpp_interface_sys;
pub mod struct_with_generic;
pub mod window_long_get_a;
pub mod window_long_get_a_sys;
pub mod window_long_get_w;
pub mod window_long_get_w_sys;
pub mod window_long_set_a;
pub mod window_long_set_a_sys;
pub mod window_long_set_w;
pub mod window_long_set_w_sys;
81 changes: 81 additions & 0 deletions crates/tests/bindgen/src/window_long_get_a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#![allow(
non_snake_case,
non_upper_case_globals,
non_camel_case_types,
dead_code,
clippy::all
)]

#[inline]
pub unsafe fn GetWindowLongA<P0>(hwnd: P0, nindex: WINDOW_LONG_PTR_INDEX) -> i32
where
P0: windows_core::Param<HWND>,
{
windows_targets::link!("user32.dll" "system" fn GetWindowLongA(hwnd : HWND, nindex : WINDOW_LONG_PTR_INDEX) -> i32);
GetWindowLongA(hwnd.param().abi(), core::mem::transmute(nindex))
}
#[cfg(any(
target_arch = "aarch64",
target_arch = "arm64ec",
target_arch = "x86_64"
))]
#[inline]
pub unsafe fn GetWindowLongPtrA<P0>(hwnd: P0, nindex: WINDOW_LONG_PTR_INDEX) -> isize
where
P0: windows_core::Param<HWND>,
{
windows_targets::link!("user32.dll" "system" fn GetWindowLongPtrA(hwnd : HWND, nindex : WINDOW_LONG_PTR_INDEX) -> isize);
GetWindowLongPtrA(hwnd.param().abi(), core::mem::transmute(nindex))
}
#[cfg(target_pointer_width = "32")]
pub use GetWindowLongA as GetWindowLongPtrA;
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct WINDOW_LONG_PTR_INDEX(pub i32);
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HANDLE(pub *mut core::ffi::c_void);
impl windows_core::TypeKind for HANDLE {
type TypeKind = windows_core::CopyType;
}
impl HANDLE {
pub fn is_invalid(&self) -> bool {
self.0 == -1 as _ || self.0 == 0 as _
}
}
impl windows_core::Free for HANDLE {
#[inline]
unsafe fn free(&mut self) {
if !self.is_invalid() {
windows_targets::link!("kernel32.dll" "system" fn CloseHandle(hobject : *mut core::ffi::c_void) -> i32);
CloseHandle(self.0);
}
}
}
impl Default for HANDLE {
fn default() -> Self {
unsafe { core::mem::zeroed() }
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HWND(pub *mut core::ffi::c_void);
impl windows_core::TypeKind for HWND {
type TypeKind = windows_core::CopyType;
}
impl HWND {
pub fn is_invalid(&self) -> bool {
self.0.is_null()
}
}
impl Default for HWND {
fn default() -> Self {
unsafe { core::mem::zeroed() }
}
}
impl windows_core::imp::CanInto<HANDLE> for HWND {}
impl From<HWND> for HANDLE {
fn from(value: HWND) -> Self {
Self(value.0)
}
}
Loading

0 comments on commit 0f87171

Please sign in to comment.