From f9b3d6a5252e043b9a0f0347e8481a61d6b83b8b Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Sat, 15 Apr 2023 11:57:07 +0100 Subject: [PATCH] Generate windows-sys bindings --- Cargo.lock | 29 +++++++++++++++++ Cargo.toml | 1 + src/bootstrap/builder.rs | 1 + src/bootstrap/run.rs | 22 +++++++++++++ src/bootstrap/tool.rs | 1 + src/tools/generate-windows-sys/Cargo.toml | 7 ++++ src/tools/generate-windows-sys/src/main.rs | 37 ++++++++++++++++++++++ 7 files changed, 98 insertions(+) create mode 100644 src/tools/generate-windows-sys/Cargo.toml create mode 100644 src/tools/generate-windows-sys/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 8e71d8ceac8..bd5af7b409a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 7aaa34a68e6..53331e2869f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ members = [ "src/tools/collect-license-metadata", "src/tools/generate-copyright", "src/tools/suggest-tests", + "src/tools/generate-windows-sys", ] exclude = [ diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index d9d4685dfc7..2cc89117ae5 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -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), diff --git a/src/bootstrap/run.rs b/src/bootstrap/run.rs index cb15d9a6325..57f3119e322 100644 --- a/src/bootstrap/run.rs +++ b/src/bootstrap/run.rs @@ -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); + } +} diff --git a/src/bootstrap/tool.rs b/src/bootstrap/tool.rs index 39f6369b4d3..f13d365e375 100644 --- a/src/bootstrap/tool.rs +++ b/src/bootstrap/tool.rs @@ -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)] diff --git a/src/tools/generate-windows-sys/Cargo.toml b/src/tools/generate-windows-sys/Cargo.toml new file mode 100644 index 00000000000..23e88844bd0 --- /dev/null +++ b/src/tools/generate-windows-sys/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "generate-windows-sys" +version = "0.1.0" +edition = "2021" + +[dependencies.windows-bindgen] +version = "0.49" diff --git a/src/tools/generate-windows-sys/src/main.rs b/src/tools/generate-windows-sys/src/main.rs new file mode 100644 index 00000000000..0b091b1ebfa --- /dev/null +++ b/src/tools/generate-windows-sys/src/main.rs @@ -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(()) +}