Skip to content

Commit b3547dd

Browse files
authored
Improve windows-bindgen dependency targeting consistency and flexibility (#3763)
1 parent 3af8eb9 commit b3547dd

File tree

273 files changed

+21235
-20971
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

273 files changed

+21235
-20971
lines changed

.github/workflows/clippy.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ jobs:
216216
run: cargo clippy -p test_interface_core --tests
217217
- name: Check test_interop
218218
run: cargo clippy -p test_interop --tests
219+
- name: Check test_just_core
220+
run: cargo clippy -p test_just_core --tests
219221
- name: Check test_lib
220222
run: cargo clippy -p test_lib --tests
221223
- name: Check test_link
@@ -232,6 +234,8 @@ jobs:
232234
run: cargo clippy -p test_metadata --tests
233235
- name: Check test_msrv
234236
run: cargo clippy -p test_msrv --tests
237+
- name: Check test_no_core
238+
run: cargo clippy -p test_no_core --tests
235239
- name: Check test_no_std
236240
run: cargo clippy -p test_no_std --tests
237241
- name: Check test_no_use

.github/workflows/test.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ jobs:
245245
run: cargo test -p test_interface_core --target ${{ matrix.target }}
246246
- name: Test test_interop
247247
run: cargo test -p test_interop --target ${{ matrix.target }}
248+
- name: Test test_just_core
249+
run: cargo test -p test_just_core --target ${{ matrix.target }}
248250
- name: Test test_lib
249251
run: cargo test -p test_lib --target ${{ matrix.target }}
250252
- name: Test test_link
@@ -257,12 +259,14 @@ jobs:
257259
run: cargo test -p test_marshal --target ${{ matrix.target }}
258260
- name: Test test_match
259261
run: cargo test -p test_match --target ${{ matrix.target }}
260-
- name: Test test_metadata
261-
run: cargo test -p test_metadata --target ${{ matrix.target }}
262262
- name: Clean
263263
run: cargo clean
264+
- name: Test test_metadata
265+
run: cargo test -p test_metadata --target ${{ matrix.target }}
264266
- name: Test test_msrv
265267
run: cargo test -p test_msrv --target ${{ matrix.target }}
268+
- name: Test test_no_core
269+
run: cargo test -p test_no_core --target ${{ matrix.target }}
266270
- name: Test test_no_std
267271
run: cargo test -p test_no_std --target ${{ matrix.target }}
268272
- name: Test test_no_use
@@ -357,12 +361,12 @@ jobs:
357361
run: cargo test -p tool_bindings --target ${{ matrix.target }}
358362
- name: Test tool_gnu
359363
run: cargo test -p tool_gnu --target ${{ matrix.target }}
364+
- name: Clean
365+
run: cargo clean
360366
- name: Test tool_license
361367
run: cargo test -p tool_license --target ${{ matrix.target }}
362368
- name: Test tool_merge
363369
run: cargo test -p tool_merge --target ${{ matrix.target }}
364-
- name: Clean
365-
run: cargo clean
366370
- name: Test tool_msvc
367371
run: cargo test -p tool_msvc --target ${{ matrix.target }}
368372
- name: Test tool_standalone

crates/libs/bindgen/src/config/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct Config<'a> {
2121
pub rustfmt: &'a str,
2222
pub sys: bool,
2323
pub implement: bool,
24+
pub specific_deps: bool,
2425
pub derive: &'a Derive,
2526
pub link: &'a str,
2627
pub warnings: &'a WarningBuilder,

crates/libs/bindgen/src/config/names.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ use super::*;
22

33
impl Config<'_> {
44
pub fn write_core(&self) -> TokenStream {
5+
self.write_specific("windows_core")
6+
}
7+
8+
pub fn write_result(&self) -> TokenStream {
9+
self.write_specific("windows_result")
10+
}
11+
12+
pub fn write_strings(&self) -> TokenStream {
13+
self.write_specific("windows_strings")
14+
}
15+
16+
fn write_specific(&self, specific: &str) -> TokenStream {
517
if self.sys {
618
if self.package || !self.no_deps {
719
quote! { windows_sys::core:: }
@@ -16,8 +28,10 @@ impl Config<'_> {
1628

1729
tokens
1830
}
19-
} else {
31+
} else if !self.specific_deps {
2032
quote! { windows_core:: }
33+
} else {
34+
format!("{specific}::").into()
2135
}
2236
}
2337

crates/libs/bindgen/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,11 @@ where
274274
let mut no_toml = false;
275275
let mut package = false;
276276
let mut implement = false;
277+
let mut specific_deps = false;
277278
let mut rustfmt = String::new();
278279
let mut output = String::new();
279280
let mut sys = false;
280-
let mut link = "windows_link".to_string();
281+
let mut link = String::new();
281282
let mut index = false;
282283

283284
for arg in &args {
@@ -301,6 +302,7 @@ where
301302
"--package" => package = true,
302303
"--sys" => sys = true,
303304
"--implement" => implement = true,
305+
"--specific-deps" => specific_deps = true,
304306
"--link" => kind = ArgKind::Link,
305307
"--index" => index = true,
306308
_ => panic!("invalid option `{arg}`"),
@@ -331,6 +333,14 @@ where
331333
}
332334
}
333335

336+
if link.is_empty() {
337+
if sys || specific_deps {
338+
link = "windows_link".to_string();
339+
} else {
340+
link = "windows_core".to_string();
341+
}
342+
}
343+
334344
if package && flat {
335345
panic!("cannot combine `--package` and `--flat`");
336346
}
@@ -389,6 +399,7 @@ where
389399
output: &output,
390400
sys,
391401
implement,
402+
specific_deps,
392403
link: &link,
393404
warnings: &warnings,
394405
namespace: "",

crates/libs/bindgen/src/types/class.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,16 @@ impl Class {
6565
}
6666
}
6767

68+
let result = config.write_result();
69+
6870
let new = self.has_default_constructor().then(||
6971
quote! {
70-
pub fn new() -> windows_core::Result<Self> {
72+
pub fn new() -> #result Result<Self> {
7173
Self::IActivationFactory(|f| f.ActivateInstance::<Self>())
7274
}
73-
fn IActivationFactory<R, F: FnOnce(&windows_core::imp::IGenericFactory) -> windows_core::Result<R>>(
75+
fn IActivationFactory<R, F: FnOnce(&windows_core::imp::IGenericFactory) -> #result Result<R>>(
7476
callback: F,
75-
) -> windows_core::Result<R> {
77+
) -> #result Result<R> {
7678
static SHARED: windows_core::imp::FactoryCache<#name, windows_core::imp::IGenericFactory> =
7779
windows_core::imp::FactoryCache::new();
7880
SHARED.call(callback)
@@ -96,9 +98,9 @@ impl Class {
9698

9799
Some(quote! {
98100
#cfg
99-
fn #method_name<R, F: FnOnce(&#interface_type) -> windows_core::Result<R>>(
101+
fn #method_name<R, F: FnOnce(&#interface_type) -> #result Result<R>>(
100102
callback: F,
101-
) -> windows_core::Result<R> {
103+
) -> #result Result<R> {
102104
static SHARED: windows_core::imp::FactoryCache<#name, #interface_type> =
103105
windows_core::imp::FactoryCache::new();
104106
SHARED.call(callback)

crates/libs/bindgen/src/types/cpp_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl CppConst {
5050

5151
if field_ty == constant_ty {
5252
if field_ty == Type::String {
53-
let crate_name = config.write_core();
53+
let crate_name = config.write_strings();
5454
let value = constant.value().write();
5555

5656
// TODO: if config.no_core then write these literals out as byte strings?

crates/libs/bindgen/src/types/cpp_fn.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ impl CppFn {
9090
let params = method.write_params(config);
9191
let generics = method.write_generics();
9292
let abi_return_type = method.write_return(config);
93+
let result = config.write_result();
9394

9495
let wrapper = match method.return_hint {
9596
ReturnHint::Query(..) => {
@@ -98,7 +99,7 @@ impl CppFn {
9899
quote! {
99100
#cfg
100101
#[inline]
101-
pub unsafe fn #name<#generics T>(#params) -> windows_core::Result<T> #where_clause {
102+
pub unsafe fn #name<#generics T>(#params) -> #result Result<T> #where_clause {
102103
#link
103104
let mut result__ = core::ptr::null_mut();
104105
unsafe { #name(#args).and_then(||windows_core::Type::from_abi(result__)) }
@@ -111,7 +112,7 @@ impl CppFn {
111112
quote! {
112113
#cfg
113114
#[inline]
114-
pub unsafe fn #name<#generics T>(#params result__: *mut Option<T>) -> windows_core::Result<()> #where_clause {
115+
pub unsafe fn #name<#generics T>(#params result__: *mut Option<T>) -> #result Result<()> #where_clause {
115116
#link
116117
unsafe { #name(#args).ok() }
117118
}
@@ -126,7 +127,7 @@ impl CppFn {
126127
quote! {
127128
#cfg
128129
#[inline]
129-
pub unsafe fn #name<#generics>(#params) -> windows_core::Result<#return_type> #where_clause {
130+
pub unsafe fn #name<#generics>(#params) -> #result Result<#return_type> #where_clause {
130131
#link
131132
unsafe {
132133
let mut result__ = core::mem::zeroed();
@@ -141,7 +142,7 @@ impl CppFn {
141142
quote! {
142143
#cfg
143144
#[inline]
144-
pub unsafe fn #name<#generics>(#params) -> windows_core::Result<()> #where_clause {
145+
pub unsafe fn #name<#generics>(#params) -> #result Result<()> #where_clause {
145146
#link
146147
unsafe { #name(#args).ok() }
147148
}
@@ -159,7 +160,7 @@ impl CppFn {
159160
quote! {
160161
#cfg
161162
#[inline]
162-
pub unsafe fn #name<#generics>(#params) -> windows_core::Result<#return_type> #where_clause {
163+
pub unsafe fn #name<#generics>(#params) -> #result Result<#return_type> #where_clause {
163164
#link
164165
unsafe {
165166
let mut result__ = core::mem::zeroed();
@@ -201,7 +202,7 @@ impl CppFn {
201202
quote! {
202203
#cfg
203204
#[inline]
204-
pub unsafe fn #name<#generics>(#params) -> windows_core::Result<#return_type> #where_clause {
205+
pub unsafe fn #name<#generics>(#params) -> #result Result<#return_type> #where_clause {
205206
#link
206207
let result__ = unsafe { #name(#args) };
207208
(!result__.is_invalid()).then_some(result__).ok_or_else(windows_core::Error::from_thread)

crates/libs/bindgen/src/types/cpp_method.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,14 @@ impl CppMethod {
262262
let params = self.write_params(config);
263263
let generics = self.write_generics();
264264
let abi_return_type = self.write_return(config);
265+
let result = config.write_result();
265266

266267
match self.return_hint {
267268
ReturnHint::Query(..) => {
268269
let where_clause = self.write_where(config, true);
269270

270271
quote! {
271-
pub unsafe fn #name<#generics T>(&self, #params) -> windows_core::Result<T> #where_clause {
272+
pub unsafe fn #name<#generics T>(&self, #params) -> #result Result<T> #where_clause {
272273
let mut result__ = core::ptr::null_mut();
273274
unsafe { (windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).and_then(||windows_core::Type::from_abi(result__)) }
274275
}
@@ -278,7 +279,7 @@ impl CppMethod {
278279
let where_clause = self.write_where(config, true);
279280

280281
quote! {
281-
pub unsafe fn #name<#generics T>(&self, #params result__: *mut Option<T>) -> windows_core::Result<()> #where_clause {
282+
pub unsafe fn #name<#generics T>(&self, #params result__: *mut Option<T>) -> #result Result<()> #where_clause {
282283
unsafe { (windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).ok() }
283284
}
284285
}
@@ -292,7 +293,7 @@ impl CppMethod {
292293
let return_type = return_type.write_name(config);
293294

294295
quote! {
295-
pub unsafe fn #name<#generics>(&self, #params) -> windows_core::Result<#return_type> #where_clause {
296+
pub unsafe fn #name<#generics>(&self, #params) -> #result Result<#return_type> #where_clause {
296297
unsafe {
297298
let mut result__ = core::mem::zeroed();
298299
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).#map
@@ -304,7 +305,7 @@ impl CppMethod {
304305
let where_clause = self.write_where(config, false);
305306

306307
quote! {
307-
pub unsafe fn #name<#generics>(&self, #params) -> windows_core::Result<()> #where_clause {
308+
pub unsafe fn #name<#generics>(&self, #params) -> #result Result<()> #where_clause {
308309
unsafe { (windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self),#args).ok() }
309310
}
310311
}
@@ -318,7 +319,7 @@ impl CppMethod {
318319
let return_type = return_type.write_name(config);
319320

320321
quote! {
321-
pub unsafe fn #name<#generics>(&self, #params) -> windows_core::Result<#return_type> #where_clause {
322+
pub unsafe fn #name<#generics>(&self, #params) -> #result Result<#return_type> #where_clause {
322323
unsafe {
323324
let mut result__ = core::mem::zeroed();
324325
(windows_core::Interface::vtable(self).#vname)(windows_core::Interface::as_raw(self), #args);
@@ -441,15 +442,17 @@ impl CppMethod {
441442
}
442443
}
443444

445+
let result = config.write_result();
446+
444447
let return_type = match self.return_hint {
445448
ReturnHint::Query(..) | ReturnHint::QueryOptional(..) | ReturnHint::ResultVoid => {
446-
quote! { -> windows_core::Result<()> }
449+
quote! { -> #result Result<()> }
447450
}
448451
ReturnHint::ResultValue => {
449452
let return_type = self.signature.params[self.signature.params.len() - 1].deref();
450453
let return_type = return_type.write_name(config);
451454

452-
quote! { -> windows_core::Result<#return_type> }
455+
quote! { -> #result Result<#return_type> }
453456
}
454457
_ => self.write_return(config),
455458
};

crates/libs/bindgen/src/types/interface.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl Interface {
9696

9797
let vtbl = {
9898
let virtual_names = &mut MethodNames::new();
99-
let core = config.write_core();
99+
let result = config.write_result();
100100

101101
let vtbl_methods = methods.iter().map(|method| match method {
102102
MethodOrName::Method(method) => {
@@ -108,14 +108,14 @@ impl Interface {
108108

109109
if yes.is_empty() {
110110
quote! {
111-
pub #name: unsafe extern "system" fn(#vtbl) -> #core HRESULT,
111+
pub #name: unsafe extern "system" fn(#vtbl) -> #result HRESULT,
112112
}
113113
} else {
114114
let no = method_cfg.write(config, true);
115115

116116
quote! {
117117
#yes
118-
pub #name: unsafe extern "system" fn(#vtbl) -> #core HRESULT,
118+
pub #name: unsafe extern "system" fn(#vtbl) -> #result HRESULT,
119119
#no
120120
#name: usize,
121121
}
@@ -133,6 +133,8 @@ impl Interface {
133133
quote! { #[doc(hidden)] }
134134
};
135135

136+
let core = config.write_core();
137+
136138
quote! {
137139
#cfg
138140
#[repr(C)]

0 commit comments

Comments
 (0)