Generate windows-sys bindings

This commit is contained in:
Chris Denton 2023-04-15 11:57:07 +01:00
parent 4a18324a4d
commit f9b3d6a525
No known key found for this signature in database
GPG key ID: 713472F2F45627DE
7 changed files with 98 additions and 0 deletions

View file

@ -1443,6 +1443,13 @@ dependencies = [
"serde_json",
]
[[package]]
name = "generate-windows-sys"
version = "0.1.0"
dependencies = [
"windows-bindgen",
]
[[package]]
name = "generic-array"
version = "0.14.4"
@ -5505,6 +5512,22 @@ dependencies = [
"windows-targets 0.48.0",
]
[[package]]
name = "windows-bindgen"
version = "0.49.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6935fb09b84ee57929ae92518b475f5dfdfbeb87c5334756acc28ee8e202b60"
dependencies = [
"windows-metadata",
"windows-tokens",
]
[[package]]
name = "windows-metadata"
version = "0.49.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f5bca94a32bf1e6a376522b6601275a3b611ee885ec0f1b6a05f17e8cfd3385"
[[package]]
name = "windows-sys"
version = "0.42.0"
@ -5568,6 +5591,12 @@ dependencies = [
"windows_x86_64_msvc 0.48.0",
]
[[package]]
name = "windows-tokens"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b34c9a3b28cb41db7385546f7f9a8179348dffc89923dde66857b1ba5312f6b4"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"

View file

@ -39,6 +39,7 @@ members = [
"src/tools/collect-license-metadata",
"src/tools/generate-copyright",
"src/tools/suggest-tests",
"src/tools/generate-windows-sys",
]
exclude = [

View file

@ -832,6 +832,7 @@ macro_rules! describe {
run::Miri,
run::CollectLicenseMetadata,
run::GenerateCopyright,
run::GenerateWindowsSys,
),
Kind::Setup => describe!(setup::Profile, setup::Hook, setup::Link, setup::Vscode),
Kind::Clean => describe!(clean::CleanAll, clean::Rustc, clean::Std),

View file

@ -253,3 +253,25 @@ fn run(self, builder: &Builder<'_>) -> Self::Output {
dest
}
}
#[derive(Debug, PartialOrd, Ord, Copy, Clone, Hash, PartialEq, Eq)]
pub struct GenerateWindowsSys;
impl Step for GenerateWindowsSys {
type Output = ();
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.path("src/tools/generate-windows-sys")
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(GenerateWindowsSys);
}
fn run(self, builder: &Builder<'_>) {
let mut cmd = builder.tool_cmd(Tool::GenerateWindowsSys);
cmd.arg(&builder.src);
builder.run(&mut cmd);
}
}

View file

@ -301,6 +301,7 @@ fn run(self, builder: &Builder<'_>) -> PathBuf {
CollectLicenseMetadata, "src/tools/collect-license-metadata", "collect-license-metadata";
GenerateCopyright, "src/tools/generate-copyright", "generate-copyright";
SuggestTests, "src/tools/suggest-tests", "suggest-tests";
GenerateWindowsSys, "src/tools/generate-windows-sys", "generate-windows-sys";
);
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]

View file

@ -0,0 +1,7 @@
[package]
name = "generate-windows-sys"
version = "0.1.0"
edition = "2021"
[dependencies.windows-bindgen]
version = "0.49"

View file

@ -0,0 +1,37 @@
use std::fs;
use std::io::{self, Write};
use std::path::PathBuf;
/// This is printed to the file before the rest of the contents.
const PRELUDE: &str = r#"// This file is autogenerated.
//
// To add bindings, edit windows_sys.lst then use `./x run generate-windows-sys` to
// regenerate the bindings.
//
// ignore-tidy-filelength
"#;
fn main() -> io::Result<()> {
let mut path: PathBuf =
std::env::args_os().nth(1).expect("a path to the rust repository is required").into();
path.push("library/std/src/sys/windows/c/windows_sys.lst");
// Load the list of APIs
let buffer = fs::read_to_string(&path)?;
let names: Vec<&str> = buffer
.lines()
.filter_map(|line| {
let line = line.trim();
if line.is_empty() || line.starts_with('#') { None } else { Some(line) }
})
.collect();
// Write the bindings to windows-sys.rs
let bindings = windows_bindgen::standalone_std(&names);
path.set_extension("rs");
let mut f = std::fs::File::create(&path)?;
f.write_all(PRELUDE.as_bytes())?;
f.write_all(bindings.as_bytes())?;
Ok(())
}