Skip to content

Commit 4e78b29

Browse files
authored
Improve windows-bindgen reference usability and default reference support (#3492)
1 parent 1237ec8 commit 4e78b29

24 files changed

Lines changed: 321 additions & 73 deletions

File tree

.github/workflows/clippy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ jobs:
241241
run: cargo clippy -p test_reference
242242
- name: Clippy test_reference_client
243243
run: cargo clippy -p test_reference_client
244+
- name: Clippy test_reference_default
245+
run: cargo clippy -p test_reference_default
244246
- name: Clippy test_reference_float
245247
run: cargo clippy -p test_reference_float
246248
- name: Clippy test_registry

.github/workflows/raw-dylib.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ jobs:
272272
run: cargo test -p test_reference --target ${{ matrix.target }} ${{ matrix.etc }}
273273
- name: Test test_reference_client
274274
run: cargo test -p test_reference_client --target ${{ matrix.target }} ${{ matrix.etc }}
275+
- name: Test test_reference_default
276+
run: cargo test -p test_reference_default --target ${{ matrix.target }} ${{ matrix.etc }}
275277
- name: Test test_reference_float
276278
run: cargo test -p test_reference_float --target ${{ matrix.target }} ${{ matrix.etc }}
277279
- name: Test test_registry
@@ -362,10 +364,10 @@ jobs:
362364
run: cargo test -p windows-targets --target ${{ matrix.target }} ${{ matrix.etc }}
363365
- name: Test windows-version
364366
run: cargo test -p windows-version --target ${{ matrix.target }} ${{ matrix.etc }}
365-
- name: Test windows_aarch64_gnullvm
366-
run: cargo test -p windows_aarch64_gnullvm --target ${{ matrix.target }} ${{ matrix.etc }}
367367
- name: Clean
368368
run: cargo clean
369+
- name: Test windows_aarch64_gnullvm
370+
run: cargo test -p windows_aarch64_gnullvm --target ${{ matrix.target }} ${{ matrix.etc }}
369371
- name: Test windows_aarch64_msvc
370372
run: cargo test -p windows_aarch64_msvc --target ${{ matrix.target }} ${{ matrix.etc }}
371373
- name: Test windows_i686_gnu

.github/workflows/test.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ jobs:
269269
run: cargo test -p test_reference --target ${{ matrix.target }} ${{ matrix.etc }}
270270
- name: Test test_reference_client
271271
run: cargo test -p test_reference_client --target ${{ matrix.target }} ${{ matrix.etc }}
272+
- name: Test test_reference_default
273+
run: cargo test -p test_reference_default --target ${{ matrix.target }} ${{ matrix.etc }}
272274
- name: Test test_reference_float
273275
run: cargo test -p test_reference_float --target ${{ matrix.target }} ${{ matrix.etc }}
274276
- name: Test test_registry
@@ -359,10 +361,10 @@ jobs:
359361
run: cargo test -p windows-targets --target ${{ matrix.target }} ${{ matrix.etc }}
360362
- name: Test windows-version
361363
run: cargo test -p windows-version --target ${{ matrix.target }} ${{ matrix.etc }}
362-
- name: Test windows_aarch64_gnullvm
363-
run: cargo test -p windows_aarch64_gnullvm --target ${{ matrix.target }} ${{ matrix.etc }}
364364
- name: Clean
365365
run: cargo clean
366+
- name: Test windows_aarch64_gnullvm
367+
run: cargo test -p windows_aarch64_gnullvm --target ${{ matrix.target }} ${{ matrix.etc }}
366368
- name: Test windows_aarch64_msvc
367369
run: cargo test -p windows_aarch64_msvc --target ${{ matrix.target }} ${{ matrix.etc }}
368370
- name: Test windows_i686_gnu

crates/libs/bindgen/src/filter.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ fn push_filter(reader: &Reader, rules: &mut Vec<(String, bool)>, filter: &str, i
8181
rules.push((filter.to_string(), include));
8282
return;
8383
}
84+
85+
if let Some(starts_with) = name.strip_suffix('*') {
86+
if let Some(types) = reader.get(namespace) {
87+
let prev_len = rules.len();
88+
89+
for name in types.keys() {
90+
if name.starts_with(starts_with) {
91+
rules.push((format!("{namespace}.{name}"), include));
92+
}
93+
}
94+
95+
if prev_len != rules.len() {
96+
return;
97+
}
98+
}
99+
}
84100
}
85101

86102
let mut pushed = false;

crates/libs/bindgen/src/lib.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct Config {
5656
pub flat: bool,
5757
pub no_allow: bool,
5858
pub no_comment: bool,
59-
pub no_core: bool,
59+
pub no_deps: bool,
6060
pub no_toml: bool,
6161
pub package: bool,
6262
pub rustfmt: String,
@@ -84,7 +84,7 @@ where
8484
let mut flat = false;
8585
let mut no_allow = false;
8686
let mut no_comment = false;
87-
let mut no_core = false;
87+
let mut no_deps = false;
8888
let mut no_toml = false;
8989
let mut package = false;
9090
let mut implement = false;
@@ -109,7 +109,7 @@ where
109109
"--flat" => flat = true,
110110
"--no-allow" => no_allow = true,
111111
"--no-comment" => no_comment = true,
112-
"--no-core" => no_core = true,
112+
"--no-deps" => no_deps = true,
113113
"--no-toml" => no_toml = true,
114114
"--package" => package = true,
115115
"--sys" => sys = true,
@@ -143,10 +143,6 @@ where
143143
}
144144
}
145145

146-
if !sys && no_core {
147-
panic!("`--no-core` requires `--sys`");
148-
}
149-
150146
if package && flat {
151147
panic!("cannot combine `--package` and `--flat`");
152148
}
@@ -159,6 +155,25 @@ where
159155
panic!("exactly one `--out` is required");
160156
};
161157

158+
if !sys && !no_deps {
159+
references.insert(
160+
0,
161+
ReferenceStage::parse("windows_collections,flat,Windows.Foundation.Collections"),
162+
);
163+
references.insert(
164+
0,
165+
ReferenceStage::parse("windows_numerics,flat,Windows.Foundation.Numerics"),
166+
);
167+
references.insert(
168+
0,
169+
ReferenceStage::parse("windows_future,flat,Windows.Foundation.Async*"),
170+
);
171+
references.insert(
172+
0,
173+
ReferenceStage::parse("windows_future,flat,Windows.Foundation.IAsync*"),
174+
);
175+
}
176+
162177
// This isn't strictly necessary but avoids a common newbie pitfall where all metadata
163178
// would be generated when building a component for a specific API.
164179
if include.is_empty() {
@@ -178,7 +193,7 @@ where
178193
derive,
179194
no_allow,
180195
no_comment,
181-
no_core,
196+
no_deps,
182197
no_toml,
183198
package,
184199
rustfmt,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ impl Type {
790790
}
791791

792792
fn write_no_deps(&self, writer: &Writer) -> TokenStream {
793-
if !writer.config.no_core {
793+
if !writer.config.no_deps || !writer.config.sys {
794794
return quote! {};
795795
}
796796

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::*;
33
impl Writer {
44
pub fn write_core(&self) -> TokenStream {
55
if self.config.sys {
6-
if self.config.package || !self.config.no_core {
6+
if self.config.package || !self.config.no_deps {
77
quote! { windows_sys::core:: }
88
} else if self.config.flat {
99
quote! {}

crates/tests/bindgen/tests/panic.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ fn invalid_option() {
3939
bindgen("--invalid");
4040
}
4141

42-
#[test]
43-
#[should_panic(expected = "`--no-core` requires `--sys`")]
44-
fn no_core() {
45-
bindgen("--in default --no-core");
46-
}
47-
4842
#[test]
4943
#[should_panic(expected = "cannot combine `--package` and `--flat`")]
5044
fn flat_package() {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "test_reference_default"
3+
version = "0.0.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[lib]
8+
doc = false
9+
doctest = false
10+
11+
[dependencies.windows-core]
12+
workspace = true
13+
14+
[dependencies.windows-numerics]
15+
workspace = true
16+
17+
[dependencies.windows-collections]
18+
workspace = true
19+
20+
[dependencies.windows-future]
21+
workspace = true
22+
23+
[dependencies.windows]
24+
workspace = true
25+
features = [
26+
"Foundation"
27+
]
28+
29+
[build-dependencies.windows-bindgen]
30+
workspace = true
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
fn main() {
2+
println!("cargo:rerun-if-changed=src/test.idl");
3+
let mut command = std::process::Command::new("midlrt.exe");
4+
5+
command.args([
6+
"/winrt",
7+
"/nomidl",
8+
"/h",
9+
"nul",
10+
"/metadata_dir",
11+
"../../../libs/bindgen/default",
12+
"/reference",
13+
"../../../libs/bindgen/default/Windows.winmd",
14+
"/winmd",
15+
"test.winmd",
16+
"src/test.idl",
17+
]);
18+
19+
if !command.status().unwrap().success() {
20+
panic!("Failed to run midlrt");
21+
}
22+
23+
windows_bindgen::bindgen([
24+
"--in",
25+
"default",
26+
"test.winmd",
27+
"--out",
28+
"src/bindings.rs",
29+
"--filter",
30+
"Test",
31+
"--implement",
32+
"--flat",
33+
"--reference",
34+
"windows,skip-root,Windows.Foundation",
35+
]);
36+
}

0 commit comments

Comments
 (0)