Skip to content

Commit f9b3d6a

Browse files
committed
Generate windows-sys bindings
1 parent 4a18324 commit f9b3d6a

File tree

7 files changed

+98
-0
lines changed

7 files changed

+98
-0
lines changed

Cargo.lock

+29
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,13 @@ dependencies = [
14431443
"serde_json",
14441444
]
14451445

1446+
[[package]]
1447+
name = "generate-windows-sys"
1448+
version = "0.1.0"
1449+
dependencies = [
1450+
"windows-bindgen",
1451+
]
1452+
14461453
[[package]]
14471454
name = "generic-array"
14481455
version = "0.14.4"
@@ -5505,6 +5512,22 @@ dependencies = [
55055512
"windows-targets 0.48.0",
55065513
]
55075514

5515+
[[package]]
5516+
name = "windows-bindgen"
5517+
version = "0.49.0"
5518+
source = "registry+https://github.com/rust-lang/crates.io-index"
5519+
checksum = "b6935fb09b84ee57929ae92518b475f5dfdfbeb87c5334756acc28ee8e202b60"
5520+
dependencies = [
5521+
"windows-metadata",
5522+
"windows-tokens",
5523+
]
5524+
5525+
[[package]]
5526+
name = "windows-metadata"
5527+
version = "0.49.0"
5528+
source = "registry+https://github.com/rust-lang/crates.io-index"
5529+
checksum = "2f5bca94a32bf1e6a376522b6601275a3b611ee885ec0f1b6a05f17e8cfd3385"
5530+
55085531
[[package]]
55095532
name = "windows-sys"
55105533
version = "0.42.0"
@@ -5568,6 +5591,12 @@ dependencies = [
55685591
"windows_x86_64_msvc 0.48.0",
55695592
]
55705593

5594+
[[package]]
5595+
name = "windows-tokens"
5596+
version = "0.48.0"
5597+
source = "registry+https://github.com/rust-lang/crates.io-index"
5598+
checksum = "b34c9a3b28cb41db7385546f7f9a8179348dffc89923dde66857b1ba5312f6b4"
5599+
55715600
[[package]]
55725601
name = "windows_aarch64_gnullvm"
55735602
version = "0.42.2"

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ members = [
3939
"src/tools/collect-license-metadata",
4040
"src/tools/generate-copyright",
4141
"src/tools/suggest-tests",
42+
"src/tools/generate-windows-sys",
4243
]
4344

4445
exclude = [

src/bootstrap/builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ impl<'a> Builder<'a> {
832832
run::Miri,
833833
run::CollectLicenseMetadata,
834834
run::GenerateCopyright,
835+
run::GenerateWindowsSys,
835836
),
836837
Kind::Setup => describe!(setup::Profile, setup::Hook, setup::Link, setup::Vscode),
837838
Kind::Clean => describe!(clean::CleanAll, clean::Rustc, clean::Std),

src/bootstrap/run.rs

+22
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,25 @@ impl Step for GenerateCopyright {
253253
dest
254254
}
255255
}
256+
257+
#[derive(Debug, PartialOrd, Ord, Copy, Clone, Hash, PartialEq, Eq)]
258+
pub struct GenerateWindowsSys;
259+
260+
impl Step for GenerateWindowsSys {
261+
type Output = ();
262+
const ONLY_HOSTS: bool = true;
263+
264+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
265+
run.path("src/tools/generate-windows-sys")
266+
}
267+
268+
fn make_run(run: RunConfig<'_>) {
269+
run.builder.ensure(GenerateWindowsSys);
270+
}
271+
272+
fn run(self, builder: &Builder<'_>) {
273+
let mut cmd = builder.tool_cmd(Tool::GenerateWindowsSys);
274+
cmd.arg(&builder.src);
275+
builder.run(&mut cmd);
276+
}
277+
}

src/bootstrap/tool.rs

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ bootstrap_tool!(
301301
CollectLicenseMetadata, "src/tools/collect-license-metadata", "collect-license-metadata";
302302
GenerateCopyright, "src/tools/generate-copyright", "generate-copyright";
303303
SuggestTests, "src/tools/suggest-tests", "suggest-tests";
304+
GenerateWindowsSys, "src/tools/generate-windows-sys", "generate-windows-sys";
304305
);
305306

306307
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "generate-windows-sys"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies.windows-bindgen]
7+
version = "0.49"
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::fs;
2+
use std::io::{self, Write};
3+
use std::path::PathBuf;
4+
5+
/// This is printed to the file before the rest of the contents.
6+
const PRELUDE: &str = r#"// This file is autogenerated.
7+
//
8+
// To add bindings, edit windows_sys.lst then use `./x run generate-windows-sys` to
9+
// regenerate the bindings.
10+
//
11+
// ignore-tidy-filelength
12+
"#;
13+
14+
fn main() -> io::Result<()> {
15+
let mut path: PathBuf =
16+
std::env::args_os().nth(1).expect("a path to the rust repository is required").into();
17+
path.push("library/std/src/sys/windows/c/windows_sys.lst");
18+
19+
// Load the list of APIs
20+
let buffer = fs::read_to_string(&path)?;
21+
let names: Vec<&str> = buffer
22+
.lines()
23+
.filter_map(|line| {
24+
let line = line.trim();
25+
if line.is_empty() || line.starts_with('#') { None } else { Some(line) }
26+
})
27+
.collect();
28+
29+
// Write the bindings to windows-sys.rs
30+
let bindings = windows_bindgen::standalone_std(&names);
31+
path.set_extension("rs");
32+
let mut f = std::fs::File::create(&path)?;
33+
f.write_all(PRELUDE.as_bytes())?;
34+
f.write_all(bindings.as_bytes())?;
35+
36+
Ok(())
37+
}

0 commit comments

Comments
 (0)