Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental raw-dylib support #2149

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/windows-sys.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
- name: Prepare
run: rustup update --no-self-update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- name: Check
run: cargo check -p windows-sys --all-features
run: cargo check -p windows-sys
9 changes: 1 addition & 8 deletions crates/libs/bindgen/src/extensions/matrix3x2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ pub fn gen() -> TokenStream {
pub x: f32,
pub y: f32,
}
#[link(name = "windows")]
extern "system" {
fn D2D1MakeRotateMatrix(
angle: f32,
center: D2D_POINT_2F,
matrix: *mut Matrix3x2,
);
}
::windows::core::windows_link!("d2d1.dll", "stdcall" fn D2D1MakeRotateMatrix(angle: f32, center: D2D_POINT_2F, matrix: *mut Matrix3x2) -> ());
let mut matrix = Self::default();
unsafe {
D2D1MakeRotateMatrix(angle, D2D_POINT_2F{x, y}, &mut matrix);
Expand Down
5 changes: 1 addition & 4 deletions crates/libs/bindgen/src/extensions/matrix4x4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ pub fn gen() -> TokenStream {
}
}
pub fn rotation_y(degree:f32) -> Self {
#[link(name = "windows")]
extern "system" {
fn D2D1SinCos(angle: f32, sin: *mut f32, cos: *mut f32);
}
::windows::core::windows_link!("d2d1.dll", "stdcall" fn D2D1SinCos(angle: f32, sin: *mut f32, cos: *mut f32) -> ());
let angle = degree * (3.141592654 / 180.0);
let mut sin = 0.0;
let mut cos = 0.0;
Expand Down
30 changes: 16 additions & 14 deletions crates/libs/bindgen/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ fn gen_sys_function(gen: &Gen, def: MethodDef) -> TokenStream {
let name = to_ident(gen.reader.method_def_name(def));
let signature = gen.reader.method_def_signature(def, &[]);
let cfg = gen.reader.signature_cfg(&signature);
let doc = gen.cfg_doc(&cfg);
let features = gen.cfg_features(&cfg);
let return_type = gen.return_sig(&signature);
let abi = gen.reader.method_def_extern_abi(def);
let impl_map = gen.reader.method_def_impl_map(def).expect("ImplMap not found");
let scope = gen.reader.impl_map_scope(impl_map);
let link = gen.reader.module_ref_name(scope).to_lowercase();

let params = signature.params.iter().map(|p| {
let name = gen.param_name(p.def);
Expand All @@ -23,9 +26,8 @@ fn gen_sys_function(gen: &Gen, def: MethodDef) -> TokenStream {
});

quote! {
#doc
#features
pub fn #name(#(#params),*) #return_type;
::windows_sys::core::windows_link!(#link, #abi fn #name(#(#params),*) #return_type);
}
}

Expand All @@ -51,21 +53,21 @@ fn gen_win_function(gen: &Gen, def: MethodDef) -> TokenStream {
fn #name(#(#abi_params),*) #abi_return_type;
}
}
} else if gen.namespace.starts_with("Windows.") {
quote! {
#[cfg_attr(windows, link(name = "windows"))]
extern #extern_abi {
fn #name(#(#abi_params),*) #abi_return_type;
}
}
} else {
let impl_map = gen.reader.method_def_impl_map(def).expect("ImplMap not found");
let scope = gen.reader.impl_map_scope(impl_map);
let link = gen.reader.module_ref_name(scope).to_lowercase();
quote! {
#[link(name = #link)]
extern #extern_abi {
fn #name(#(#abi_params),*) #abi_return_type;

if gen.namespace.starts_with("Windows.") {
quote! {
::windows::core::windows_link!(#link, #extern_abi fn #name(#(#abi_params),*) #abi_return_type);
}
} else {
quote! {
#[link(name = #link)]
extern #extern_abi {
fn #name(#(#abi_params),*) #abi_return_type;
}
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ impl<'a> Gen<'a> {
} else if self.reader.method_def_does_not_return(signature.def) {
quote! { -> ! }
} else {
quote! {}
quote! { -> () }
}
}
pub fn win32_args(&self, params: &[SignatureParam], kind: SignatureKind) -> TokenStream {
Expand Down
21 changes: 4 additions & 17 deletions crates/libs/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn namespace(gen: &Gen, tree: &Tree) -> String {
tokens.combine(&quote! { pub mod #name; });
}

let mut functions = BTreeMap::<&str, BTreeMap<&str, TokenStream>>::new();
let mut functions = BTreeMap::<&str, TokenStream>::new();
let mut types = BTreeMap::<TypeKind, BTreeMap<&str, TokenStream>>::new();

for def in gen.reader.namespace_types(tree.namespace) {
Expand All @@ -58,8 +58,7 @@ pub fn namespace(gen: &Gen, tree: &Tree) -> String {
} else {
for method in gen.reader.type_def_methods(def) {
let name = gen.reader.method_def_name(method);
let extern_abi = gen.reader.method_def_extern_abi(method);
functions.entry(extern_abi).or_default().entry(name).or_default().combine(&functions::gen(gen, method));
functions.entry(name).or_default().combine(&functions::gen(gen, method));
}
for field in gen.reader.type_def_fields(def) {
let name = gen.reader.field_name(field);
Expand All @@ -86,20 +85,8 @@ pub fn namespace(gen: &Gen, tree: &Tree) -> String {
}
}

for (extern_abi, functions) in functions {
let functions = functions.values();
if gen.sys {
tokens.combine(&quote! {
#[cfg_attr(windows, link(name = "windows"))]
extern #extern_abi {
#(#functions)*
}
});
} else {
tokens.combine(&quote! {
#(#functions)*
});
}
for function in functions.values() {
tokens.combine(function);
}

for ty in types.values().flat_map(|v| v.values()) {
Expand Down
4 changes: 4 additions & 0 deletions crates/libs/sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,7 @@ Win32_UI_WindowsAndMessaging = ["Win32_UI"]
Win32_UI_Wpf = ["Win32_UI"]
Win32_UI_Xaml = ["Win32_UI"]
Win32_UI_Xaml_Diagnostics = ["Win32_UI_Xaml"]

# These features are unstable and require the nightly Rust compiler:
raw_dylib = []
Copy link

@udoprog udoprog Nov 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're confident in your compatibility layer and want to merge this feature; you could opt for putting it behind a --cfg windows_nightly option instead of a feature flag. Such a flag wouldn't be prone to transitive activation through dependencies, and it would allow people to more easily experiment with unstable features and avoid the bitrot associated with maintaining a branch.

See e.g. tokio_unstable for reference.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, that looks handy - thanks! Features really aren't great for this kind of thing.


Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#[cfg_attr(windows, link(name = "windows"))]
extern "system" {
#[doc = "*Required features: `\"Win32_AI_MachineLearning_DirectML\"`, `\"Win32_Graphics_Direct3D12\"`*"]
#[cfg(feature = "Win32_Graphics_Direct3D12")]
pub fn DMLCreateDevice(d3d12device: super::super::super::Graphics::Direct3D12::ID3D12Device, flags: DML_CREATE_DEVICE_FLAGS, riid: *const ::windows_sys::core::GUID, ppv: *mut *mut ::core::ffi::c_void) -> ::windows_sys::core::HRESULT;
#[doc = "*Required features: `\"Win32_AI_MachineLearning_DirectML\"`, `\"Win32_Graphics_Direct3D12\"`*"]
#[cfg(feature = "Win32_Graphics_Direct3D12")]
pub fn DMLCreateDevice1(d3d12device: super::super::super::Graphics::Direct3D12::ID3D12Device, flags: DML_CREATE_DEVICE_FLAGS, minimumfeaturelevel: DML_FEATURE_LEVEL, riid: *const ::windows_sys::core::GUID, ppv: *mut *mut ::core::ffi::c_void) -> ::windows_sys::core::HRESULT;
}
#[cfg(feature = "Win32_Graphics_Direct3D12")]
::windows_sys::core::windows_link ! ( "directml.dll" ,"system" fn DMLCreateDevice ( d3d12device : super::super::super::Graphics::Direct3D12:: ID3D12Device , flags : DML_CREATE_DEVICE_FLAGS , riid : *const :: windows_sys::core::GUID , ppv : *mut *mut ::core::ffi::c_void ) -> :: windows_sys::core::HRESULT );
#[cfg(feature = "Win32_Graphics_Direct3D12")]
::windows_sys::core::windows_link ! ( "directml.dll" ,"system" fn DMLCreateDevice1 ( d3d12device : super::super::super::Graphics::Direct3D12:: ID3D12Device , flags : DML_CREATE_DEVICE_FLAGS , minimumfeaturelevel : DML_FEATURE_LEVEL , riid : *const :: windows_sys::core::GUID , ppv : *mut *mut ::core::ffi::c_void ) -> :: windows_sys::core::HRESULT );
pub type IDMLBindingTable = *mut ::core::ffi::c_void;
pub type IDMLCommandRecorder = *mut ::core::ffi::c_void;
pub type IDMLCompiledOperator = *mut ::core::ffi::c_void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#[cfg_attr(windows, link(name = "windows"))]
extern "system" {
#[doc = "*Required features: `\"Win32_AI_MachineLearning_WinML\"`*"]
pub fn MLCreateOperatorRegistry(registry: *mut IMLOperatorRegistry) -> ::windows_sys::core::HRESULT;
#[doc = "*Required features: `\"Win32_AI_MachineLearning_WinML\"`*"]
pub fn WinMLCreateRuntime(runtime: *mut IWinMLRuntime) -> ::windows_sys::core::HRESULT;
}
::windows_sys::core::windows_link ! ( "windows.ai.machinelearning.dll" ,"system" fn MLCreateOperatorRegistry ( registry : *mut IMLOperatorRegistry ) -> :: windows_sys::core::HRESULT );
::windows_sys::core::windows_link ! ( "winml.dll" ,"system" fn WinMLCreateRuntime ( runtime : *mut IWinMLRuntime ) -> :: windows_sys::core::HRESULT );
pub type IMLOperatorAttributes = *mut ::core::ffi::c_void;
pub type IMLOperatorKernel = *mut ::core::ffi::c_void;
pub type IMLOperatorKernelContext = *mut ::core::ffi::c_void;
Expand Down
13 changes: 4 additions & 9 deletions crates/libs/sys/src/Windows/Win32/Data/HtmlHelp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#[cfg_attr(windows, link(name = "windows"))]
extern "system" {
#[doc = "*Required features: `\"Win32_Data_HtmlHelp\"`, `\"Win32_Foundation\"`*"]
#[cfg(feature = "Win32_Foundation")]
pub fn HtmlHelpA(hwndcaller: super::super::Foundation::HWND, pszfile: ::windows_sys::core::PCSTR, ucommand: u32, dwdata: usize) -> super::super::Foundation::HWND;
#[doc = "*Required features: `\"Win32_Data_HtmlHelp\"`, `\"Win32_Foundation\"`*"]
#[cfg(feature = "Win32_Foundation")]
pub fn HtmlHelpW(hwndcaller: super::super::Foundation::HWND, pszfile: ::windows_sys::core::PCWSTR, ucommand: u32, dwdata: usize) -> super::super::Foundation::HWND;
}
#[cfg(feature = "Win32_Foundation")]
::windows_sys::core::windows_link ! ( "htmlhelp.dll" ,"system" fn HtmlHelpA ( hwndcaller : super::super::Foundation:: HWND , pszfile : :: windows_sys::core::PCSTR , ucommand : u32 , dwdata : usize ) -> super::super::Foundation:: HWND );
#[cfg(feature = "Win32_Foundation")]
::windows_sys::core::windows_link ! ( "htmlhelp.dll" ,"system" fn HtmlHelpW ( hwndcaller : super::super::Foundation:: HWND , pszfile : :: windows_sys::core::PCWSTR , ucommand : u32 , dwdata : usize ) -> super::super::Foundation:: HWND );
pub type IITDatabase = *mut ::core::ffi::c_void;
pub type IITPropList = *mut ::core::ffi::c_void;
pub type IITResultSet = *mut ::core::ffi::c_void;
Expand Down
Loading