Skip to content

Commit 3d91fd8

Browse files
authored
Unrolled build for rust-lang#118749
Rollup merge of rust-lang#118749 - ChrisDenton:winsys, r=cuviper Make contributing to windows bindings easier This PR does three things: - Automatically sorts bindings so contributors don't have to. I should have done this to begin with but was lazy. - Renames `windows_sys.lst` to `bindings.txt`. This [matches the windows-rs repository](https://github.com/microsoft/windows-rs/blob/8e71051ea8a57594478e585d2740126893f9dbb7/crates/tools/sys/bindings.txt) (and repos that copy it). I believe consistency with other projects helps get people orientated. - Adds a `README.md` file explaining what this is about and how to add bindings. This has the benefit of being directly editable and it's rendered when viewed online. Also people are understandably jumping right into the `windows_sys.rs` file via ripgrep or github search and so missing that it's generated. A `README.md` alongside it is at least slightly more obvious in that case. There is still a small note at the top of `windows_sys` in case people do read from the beginning. None of this has any impact on the actual code generated. It's purely to make the new contributors workflow a bit nicer.
2 parents bd6b336 + 846315d commit 3d91fd8

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The `windows_sys.rs` file is autogenerated from `bindings.txt` and must not
2+
be edited manually.
3+
4+
To add bindings, edit `bindings.txt` then regenerate using the following command:
5+
6+
./x run generate-windows-sys && ./x fmt library/std
7+
8+
If you need to override generated functions or types then add them to
9+
`library/std/src/sys/pal/windows/c.rs`.

library/std/src/sys/pal/windows/c/windows_sys.lst library/std/src/sys/pal/windows/c/bindings.txt

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
--out windows_sys.rs
22
--config flatten std
33
--filter
4-
// tidy-alphabetical-start
54
!Windows.Win32.Foundation.INVALID_HANDLE_VALUE
65
Windows.Wdk.Storage.FileSystem.FILE_COMPLETE_IF_OPLOCKED
76
Windows.Wdk.Storage.FileSystem.FILE_CONTAINS_EXTENDED_CREATE_INFORMATION
@@ -2592,5 +2591,3 @@ Windows.Win32.System.Threading.WakeAllConditionVariable
25922591
Windows.Win32.System.Threading.WakeConditionVariable
25932592
Windows.Win32.System.WindowsProgramming.PROGRESS_CONTINUE
25942593
Windows.Win32.UI.Shell.GetUserProfileDirectoryW
2595-
// tidy-alphabetical-end
2596-

library/std/src/sys/pal/windows/c/windows_sys.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
// This file is autogenerated.
2-
//
3-
// To add bindings, edit windows_sys.lst then use `./x run generate-windows-sys` to
4-
// regenerate the bindings.
5-
//
6-
// ignore-tidy-filelength
71
// Bindings generated by `windows-bindgen` 0.52.0
82

93
#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]
@@ -4351,3 +4345,4 @@ impl ::core::clone::Clone for XSAVE_FORMAT {
43514345
*self
43524346
}
43534347
}
4348+
// ignore-tidy-filelength
+31-16
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,49 @@
11
use std::env;
22
use std::error::Error;
33
use std::fs;
4-
use std::io::{self, Read, Seek, Write};
4+
use std::io::{Read, Seek, SeekFrom, Write};
55
use std::path::PathBuf;
66

7-
/// This is printed to the file before the rest of the contents.
8-
const PRELUDE: &str = r#"// This file is autogenerated.
9-
//
10-
// To add bindings, edit windows_sys.lst then use `./x run generate-windows-sys` to
11-
// regenerate the bindings.
12-
//
13-
// ignore-tidy-filelength
14-
"#;
15-
167
fn main() -> Result<(), Box<dyn Error>> {
178
let mut path: PathBuf =
189
env::args_os().nth(1).expect("a path to the rust repository is required").into();
1910
path.push("library/std/src/sys/pal/windows/c");
2011
env::set_current_dir(&path)?;
2112

22-
let info = windows_bindgen::bindgen(["--etc", "windows_sys.lst"])?;
13+
sort_bindings("bindings.txt")?;
14+
15+
let info = windows_bindgen::bindgen(["--etc", "bindings.txt"])?;
2316
println!("{info}");
2417

25-
// add some gunk to the output file.
26-
let mut f = fs::File::options().read(true).write(true).open("windows_sys.rs")?;
18+
let mut f = std::fs::File::options().append(true).open("windows_sys.rs")?;
19+
writeln!(&mut f, "// ignore-tidy-filelength")?;
20+
21+
Ok(())
22+
}
23+
24+
fn sort_bindings(file_name: &str) -> Result<(), Box<dyn Error>> {
25+
let mut f = fs::File::options().read(true).write(true).open(file_name)?;
2726
let mut bindings = String::new();
2827
f.read_to_string(&mut bindings)?;
29-
f.seek(io::SeekFrom::Start(0))?;
30-
f.write_all(PRELUDE.as_bytes())?;
31-
f.write_all(bindings.as_bytes())?;
28+
f.set_len(0)?;
29+
f.seek(SeekFrom::Start(0))?;
3230

31+
let mut lines = bindings.split_inclusive('\n');
32+
for line in &mut lines {
33+
f.write(line.as_bytes())?;
34+
if line.contains("--filter") {
35+
break;
36+
}
37+
}
38+
let mut bindings = Vec::new();
39+
for line in &mut lines {
40+
if !line.trim().is_empty() {
41+
bindings.push(line);
42+
}
43+
}
44+
bindings.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase()));
45+
for line in bindings {
46+
f.write(line.as_bytes())?;
47+
}
3348
Ok(())
3449
}

0 commit comments

Comments
 (0)