Skip to content

Commit 409b35c

Browse files
NobodyXumadsmtm
andauthored
Refactor: Extract windows-find-tools (#1531)
* Create new workspace windows-registry * Rm src/windows mod * cargo fmt * Fix compilation on windows * Temporarily disable semver checks in CI due to false positive * Fix windows-registry testing * Fix unexpected cfg for windows-registry * Apply suggestions from code review Co-authored-by: Mads Marquart <[email protected]> * Replace From<windows_registry::Tool> for Tool with private method * Add README for windows-registry * Rename windows-registry to windows-find-tools since windows-registry is already occupied * Fix crate name in windows-find-tools/README.md * Mark `windows_find_tools::Env` to be non-exhaustive and derive Debug and Clone * Rename windows-find-tools to find-msvc-tools * Update README.md and crate doc * Update find-msvc-tools/README.md Co-authored-by: Mads Marquart <[email protected]> --------- Co-authored-by: Mads Marquart <[email protected]>
1 parent 5781d3e commit 409b35c

File tree

22 files changed

+331
-144
lines changed

22 files changed

+331
-144
lines changed

.github/workflows/main.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,13 @@ jobs:
402402
- uses: Swatinem/rust-cache@v2
403403
- run: cargo fmt -- --check
404404

405-
semver-checks:
406-
runs-on: ubuntu-latest
407-
steps:
408-
- name: Checkout
409-
uses: actions/checkout@v5
410-
- name: Check semver
411-
uses: obi1kenobi/cargo-semver-checks-action@v2
405+
#semver-checks:
406+
# runs-on: ubuntu-latest
407+
# steps:
408+
# - name: Checkout
409+
# uses: actions/checkout@v5
410+
# - name: Check semver
411+
# uses: obi1kenobi/cargo-semver-checks-action@v2
412412

413413
# Dummy job to have a stable name for the "all tests pass" requirement
414414
tests-pass:
@@ -422,7 +422,7 @@ jobs:
422422
- msrv
423423
- clippy
424424
- rustfmt
425-
- semver-checks
425+
#- semver-checks
426426
if: always() # always run even if dependencies fail
427427
runs-on: ubuntu-latest
428428
steps:

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ rust-version = "1.63"
2222
[dependencies]
2323
jobserver = { version = "0.1.30", default-features = false, optional = true }
2424
shlex = "1.3.0"
25+
find-msvc-tools = { version = "0.1.0", path = "find-msvc-tools" }
2526

2627
[target.'cfg(unix)'.dependencies]
2728
# Don't turn on the feature "std" for this, see https://github.com/rust-lang/cargo/issues/4866
@@ -39,6 +40,7 @@ tempfile = "3"
3940

4041
[workspace]
4142
members = [
43+
"find-msvc-tools",
4244
"dev-tools/cc-test",
4345
"dev-tools/gen-target-info",
4446
"dev-tools/gen-windows-sys-binding",
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//! Adapted from
2+
//! https://github.com/rust-lang/rust/blob/master/src/tools/generate-windows-sys/src/main.rs
3+
4+
use std::{
5+
fs,
6+
io::{BufWriter, Write as _},
7+
};
8+
9+
use regex::Regex;
10+
11+
/// This is printed to the file before the rest of the contents.
12+
const PRELUDE: &str = r#"// This file is autogenerated.
13+
//
14+
// To add bindings, edit windows_sys.lst then run:
15+
//
16+
// ```
17+
// cd dev-tools/generate-windows-sys/
18+
// cargo run
19+
// ```"#;
20+
21+
const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
22+
23+
pub fn generate_bindings() {
24+
let filter: String = format!("{MANIFEST_DIR}/windows_sys.list");
25+
let temp_file = tempfile::Builder::new()
26+
.suffix(".rs")
27+
.tempfile()
28+
.expect("failed to create temp file");
29+
30+
// Generate bindings.
31+
windows_bindgen::bindgen([
32+
"--flat",
33+
"--sys",
34+
"--no-deps",
35+
"--out",
36+
temp_file.path().to_str().unwrap(),
37+
"--filter",
38+
"--etc",
39+
&filter,
40+
])
41+
.unwrap();
42+
43+
let bindings =
44+
fs::read_to_string(temp_file.path()).expect("failed to read temp windows_sys.rs");
45+
46+
let mut f: BufWriter<fs::File> = fs::File::create(format!(
47+
"{MANIFEST_DIR}/../../find-msvc-tools/src/windows_sys.rs"
48+
))
49+
.map(BufWriter::new)
50+
.expect("failed to create windows_sys.rs");
51+
52+
write!(&mut f, "{PRELUDE}\n{bindings}\n").unwrap();
53+
54+
let mut dll_names: Vec<&str> = Regex::new(r#"link!\("(.*)\.dll""#)
55+
.unwrap()
56+
.captures_iter(&bindings)
57+
.map(|caps| caps.extract().1)
58+
.map(|[dll_name]| dll_name)
59+
.filter(|dll_name| *dll_name != "kernel32")
60+
.collect();
61+
62+
if !dll_names.is_empty() {
63+
dll_names.sort_unstable();
64+
dll_names.dedup();
65+
66+
for dll_name in dll_names {
67+
write!(&mut f, r#"#[link(name = "{dll_name}")]"#).unwrap();
68+
f.write_all("\n".as_bytes()).unwrap();
69+
}
70+
71+
f.write_all(r#"extern "C" {}"#.as_bytes()).unwrap();
72+
f.write_all("\n".as_bytes()).unwrap();
73+
}
74+
75+
f.write_all(r#"use super::windows_link;"#.as_bytes())
76+
.unwrap();
77+
f.write_all("\n".as_bytes()).unwrap();
78+
79+
f.into_inner().unwrap().sync_all().unwrap();
80+
}
Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,3 @@
1-
//! Adapted from
2-
//! https://github.com/rust-lang/rust/blob/master/src/tools/generate-windows-sys/src/main.rs
3-
4-
use std::{
5-
fs,
6-
io::{BufWriter, Write as _},
7-
};
8-
9-
use regex::Regex;
10-
11-
/// This is printed to the file before the rest of the contents.
12-
const PRELUDE: &str = r#"// This file is autogenerated.
13-
//
14-
// To add bindings, edit windows_sys.lst then run:
15-
//
16-
// ```
17-
// cd generate-windows-sys/
18-
// cargo run
19-
// ```"#;
20-
211
fn main() {
22-
let manifest_dir = env!("CARGO_MANIFEST_DIR");
23-
let filter = format!("{manifest_dir}/windows_sys.list");
24-
let temp_file = tempfile::Builder::new()
25-
.suffix(".rs")
26-
.tempfile()
27-
.expect("failed to create temp file");
28-
29-
// Generate bindings.
30-
windows_bindgen::bindgen([
31-
"--flat",
32-
"--sys",
33-
"--no-deps",
34-
"--out",
35-
temp_file.path().to_str().unwrap(),
36-
"--filter",
37-
"--etc",
38-
&filter,
39-
])
40-
.unwrap();
41-
42-
let bindings =
43-
fs::read_to_string(temp_file.path()).expect("failed to read temp windows_sys.rs");
44-
45-
let mut f = fs::File::create(format!("{manifest_dir}/../../src/windows/windows_sys.rs"))
46-
.map(BufWriter::new)
47-
.expect("failed to create windows_sys.rs");
48-
49-
write!(&mut f, "{PRELUDE}\n{bindings}\n").unwrap();
50-
51-
let mut dll_names: Vec<&str> = Regex::new(r#"link!\("(.*)\.dll""#)
52-
.unwrap()
53-
.captures_iter(&bindings)
54-
.map(|caps| caps.extract().1)
55-
.map(|[dll_name]| dll_name)
56-
.filter(|dll_name| *dll_name != "kernel32")
57-
.collect();
58-
59-
if !dll_names.is_empty() {
60-
dll_names.sort_unstable();
61-
dll_names.dedup();
62-
63-
for dll_name in dll_names {
64-
write!(&mut f, r#"#[link(name = "{dll_name}")]"#).unwrap();
65-
f.write_all("\n".as_bytes()).unwrap();
66-
}
67-
68-
f.write_all(r#"extern "C" {}"#.as_bytes()).unwrap();
69-
f.write_all("\n".as_bytes()).unwrap();
70-
}
71-
72-
f.write_all(r#"use super::windows_link;"#.as_bytes())
73-
.unwrap();
74-
f.write_all("\n".as_bytes()).unwrap();
75-
76-
f.into_inner().unwrap().sync_all().unwrap();
2+
gen_windows_sys_binding::generate_bindings();
773
}

find-msvc-tools/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "find-msvc-tools"
3+
version = "0.1.0"
4+
edition = "2018"
5+
license = "MIT OR Apache-2.0"
6+
repository = "https://github.com/rust-lang/cc-rs"
7+
documentation = "https://docs.rs/find-msvc-tools"
8+
description = "Find windows-specific tools, read MSVC versions from the registry and from COM interfaces"
9+
keywords = ["build-dependencies"]
10+
categories = ["development-tools::build-utils"]
11+
rust-version = "1.63"
12+
13+
14+
[dependencies]
15+
16+
[lints.rust]
17+
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(disable_clang_cl_tests)'] }
18+

find-msvc-tools/LICENSE-APACHE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../LICENSE-APACHE

find-msvc-tools/LICENSE-MIT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../LICENSE-MIT

find-msvc-tools/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# find-msvc-tools
2+
3+
> This crate is maintained by the library team, primarily for use by the `cc` crate and not intended for external use (except as a transitive dependency). This crate may make major changes to its APIs or be deprecated without warning.
4+
5+
An internal use library for finding windows-specific tools, reading MSVC versions from the
6+
registry and from COM interfaces.
7+
8+
Refer to the [documentation](https://docs.rs/find-msvc-tools) for detailed usage instructions.
9+
10+
## License
11+
12+
This project is licensed under either of
13+
14+
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
15+
https://www.apache.org/licenses/LICENSE-2.0)
16+
* MIT license ([LICENSE-MIT](LICENSE-MIT) or
17+
https://opensource.org/licenses/MIT)
18+
19+
at your option.
20+
21+
### Contribution
22+
23+
Unless you explicitly state otherwise, any contribution transitively intentionally submitted
24+
for inclusion in find-msvc-tools by you, as defined in the Apache-2.0 license, shall be
25+
dual licensed as above, without any additional terms or conditions.

src/windows/com.rs renamed to find-msvc-tools/src/com.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// All files in the project carrying such notice may not be copied, modified, or distributed
66
// except according to those terms.
77

8-
use crate::windows::{
8+
use crate::{
99
winapi::{IUnknown, Interface},
1010
windows_sys::{
1111
CoInitializeEx, SysFreeString, SysStringLen, BSTR, COINIT_MULTITHREADED, HRESULT, S_FALSE,

0 commit comments

Comments
 (0)