Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ jobs:
run: cargo clippy -p test_structs --tests
- name: Check test_sys
run: cargo clippy -p test_sys --tests
- name: Check test_sys_fn_ptrs
run: cargo clippy -p test_sys_fn_ptrs --tests
- name: Check test_targets
run: cargo clippy -p test_targets --tests
- name: Check test_threading
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ jobs:
run: cargo test -p test_structs --target ${{ matrix.target }}
- name: Test test_sys
run: cargo test -p test_sys --target ${{ matrix.target }}
- name: Test test_sys_fn_ptrs
run: cargo test -p test_sys_fn_ptrs --target ${{ matrix.target }}
- name: Test test_targets
run: cargo test -p test_targets --target ${{ matrix.target }}
- name: Test test_threading
Expand Down Expand Up @@ -359,10 +361,10 @@ jobs:
run: cargo test -p tool_bindgen --target ${{ matrix.target }}
- name: Test tool_bindings
run: cargo test -p tool_bindings --target ${{ matrix.target }}
- name: Test tool_gnu
run: cargo test -p tool_gnu --target ${{ matrix.target }}
- name: Clean
run: cargo clean
- name: Test tool_gnu
run: cargo test -p tool_gnu --target ${{ matrix.target }}
- name: Test tool_license
run: cargo test -p tool_license --target ${{ matrix.target }}
- name: Test tool_merge
Expand Down
1 change: 1 addition & 0 deletions crates/libs/bindgen/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Config<'a> {
pub package: bool,
pub rustfmt: &'a str,
pub sys: bool,
pub sys_fn_ptrs: bool,
pub implement: bool,
pub specific_deps: bool,
pub derive: &'a Derive,
Expand Down
4 changes: 4 additions & 0 deletions crates/libs/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ use method_names::*;
/// | `--no-comment` | Avoids generating the code generation comment. |
/// | `--no-deps` | Avoids dependencies on the various `windows-*` crates. |
/// | `--sys` | Generates raw or sys-style Rust bindings. |
/// | `--sys-fn-ptrs` | Additionally generates function pointers for sys-style Rust bindings. |
/// | `--implement` | Includes implementation traits for WinRT interfaces. |
/// | `--link` | Overrides the default `windows-link` implementation for system calls. |
///
Expand Down Expand Up @@ -278,6 +279,7 @@ where
let mut rustfmt = String::new();
let mut output = String::new();
let mut sys = false;
let mut sys_fn_ptrs = false;
let mut link = String::new();
let mut index = false;

Expand All @@ -301,6 +303,7 @@ where
"--no-toml" => no_toml = true,
"--package" => package = true,
"--sys" => sys = true,
"--sys-fn-ptrs" => sys_fn_ptrs = true,
"--implement" => implement = true,
"--specific-deps" => specific_deps = true,
"--link" => kind = ArgKind::Link,
Expand Down Expand Up @@ -435,6 +438,7 @@ where
rustfmt: &rustfmt,
output: &output,
sys,
sys_fn_ptrs,
implement,
specific_deps,
link: &link,
Expand Down
51 changes: 46 additions & 5 deletions crates/libs/bindgen/src/types/cpp_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,35 @@ impl CppFn {
self.type_name().write(config, &[])
}

pub fn write_fn_ptr(&self, config: &Config<'_>, underlying_types: bool) -> TokenStream {
let ptr_name = self.method.name().to_string();
let name = to_ident(&ptr_name);
let abi = self.method.calling_convention();
let signature = self.method.signature(self.namespace, &[]);

let params = signature.params.iter().map(|param| {
let name = param.write_ident();
let ty = if underlying_types {
param.underlying_type().write_abi(config)
} else {
param.write_abi(config)
};
quote! { #name: #ty }
});

let return_sig = config.write_return_sig(self.method, &signature, underlying_types);

let vararg = if config.sys && signature.call_flags.contains(MethodCallAttributes::VARARG) {
quote! { , ... }
} else {
quote! {}
};

link_fmt(quote! {
pub type #name = unsafe extern #abi fn(#(#params),* #vararg) #return_sig;
})
}

pub fn write_link(&self, config: &Config, underlying_types: bool) -> TokenStream {
let library = self.method.module_name();
let symbol = self.method.import_name();
Expand Down Expand Up @@ -71,18 +100,30 @@ impl CppFn {
let name = to_ident(self.method.name());
let signature = self.method.signature(self.namespace, &[]);

let fn_ptr = self.write_fn_ptr(config, false);
let link = self.write_link(config, false);
let arches = write_arches(self.method);
let cfg = self.write_cfg(config);
let cfg = quote! { #arches #cfg };
let window_long = self.write_window_long();

if config.sys {
return quote! {
#cfg
#link
#window_long
};
if config.sys_fn_ptrs {
return quote! {
#cfg
#fn_ptr
#window_long
#cfg
#link
#window_long
};
} else {
return quote! {
#cfg
#link
#window_long
};
}
}

let method = CppMethod::new(self.method, self.namespace);
Expand Down
24 changes: 24 additions & 0 deletions crates/tests/misc/sys_fn_ptrs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "test_sys_fn_ptrs"
version = "0.0.0"
edition = "2021"
publish = false

[lib]
doc = false
doctest = false

[dependencies.windows-result]
workspace = true

[dependencies.windows-strings]
workspace = true

[dependencies.windows-link]
workspace = true

[build-dependencies.windows-bindgen]
workspace = true

[lints]
workspace = true
18 changes: 18 additions & 0 deletions crates/tests/misc/sys_fn_ptrs/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fn main() {
windows_bindgen::bindgen([
"--out",
"src/bindings.rs",
"--filter",
"WebAuthNAuthenticatorMakeCredential",
"WebAuthNAuthenticatorGetAssertion",
"LoadLibraryExA",
"GetProcAddress",
"FreeLibrary",
"LOAD_LIBRARY_SEARCH_DEFAULT_DIRS",
"--sys",
"--sys-fn-ptrs",
"--no-comment",
"--no-deps",
])
.unwrap();
}
Loading