Skip to content

Commit 797c4b1

Browse files
committed
use find-msvc-tools crate directly instead of cc
1 parent 095fa86 commit 797c4b1

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

Cargo.lock

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,12 @@ dependencies = [
13251325
"windows-sys 0.59.0",
13261326
]
13271327

1328+
[[package]]
1329+
name = "find-msvc-tools"
1330+
version = "0.1.1"
1331+
source = "registry+https://github.com/rust-lang/crates.io-index"
1332+
checksum = "7fd99930f64d146689264c637b5af2f0233a933bef0d8570e2526bf9e083192d"
1333+
13281334
[[package]]
13291335
name = "flate2"
13301336
version = "1.1.2"
@@ -4724,7 +4730,7 @@ dependencies = [
47244730
name = "rustc_windows_rc"
47254731
version = "0.0.0"
47264732
dependencies = [
4727-
"cc",
4733+
"find-msvc-tools",
47284734
]
47294735

47304736
[[package]]

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ exclude = [
6464
bitflags = "2.9.3"
6565
derive-where = "1.6.0"
6666
either = "1.15.0"
67+
find-msvc-tools = "0.1"
6768
indexmap = "2.10.0"
6869
itertools = "0.12.1"
6970
# FIXME: Remove this pin once this rustix issue is resolved

compiler/rustc_windows_rc/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ edition = "2024"
55

66
[dependencies]
77
#tidy-alphabetical-start
8-
# `cc` updates often break things, so we pin it here. Cargo enforces "max 1 semver-compat version
9-
# per crate", so if you change this, you need to also change it in `rustc_llvm` and `rustc_codegen_ssa`.
10-
cc = "=1.2.16"
8+
find-msvc-tools.workspace = true
119
#tidy-alphabetical-end

compiler/rustc_windows_rc/src/lib.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
//!
33
//! Uses values from the `CFG_VERSION` and `CFG_RELEASE` environment variables
44
//! to set the product and file version information in the Windows resource file.
5-
use std::{env, ffi, fs, path, process};
6-
7-
use cc::windows_registry;
5+
use std::{env, fs, path, process};
86

97
/// The template for the Windows resource file.
108
const RESOURCE_TEMPLATE: &str = include_str!("../rustc.rc.in");
@@ -35,8 +33,7 @@ pub fn compile_windows_resource_file(
3533
resources_dir.push("resources");
3634
fs::create_dir_all(&resources_dir).unwrap();
3735

38-
let resource_compiler =
39-
find_resource_compiler(&env::var("CARGO_CFG_TARGET_ARCH").unwrap()).expect("found rc.exe");
36+
let resource_compiler = find_resource_compiler().expect("found rc.exe");
4037

4138
let rc_path = resources_dir.join(file_stem.with_extension("rc"));
4239

@@ -134,25 +131,26 @@ fn parse_version(version: &str) -> Option<ResourceVersion> {
134131

135132
/// Find the Windows SDK resource compiler `rc.exe` for the given architecture or target triple.
136133
/// Returns `None` if the tool could not be found.
137-
fn find_resource_compiler(arch_or_target: &str) -> Option<path::PathBuf> {
138-
find_windows_sdk_tool(arch_or_target, "rc.exe")
134+
fn find_resource_compiler() -> Option<path::PathBuf> {
135+
find_windows_sdk_tool("rc.exe")
139136
}
140137

141138
/// Find a Windows SDK tool for the given architecture or target triple.
142139
/// Returns `None` if the tool could not be found.
143-
fn find_windows_sdk_tool(arch_or_target: &str, tool_name: &str) -> Option<path::PathBuf> {
144-
// windows_registry::find_tool can only find MSVC tools, not Windows SDK tools, but
145-
// cc does include the Windows SDK tools in the PATH environment of MSVC tools.
140+
fn find_windows_sdk_tool(tool_name: &str) -> Option<path::PathBuf> {
141+
let (ucrt_dir, ucrt_version) = find_msvc_tools::get_ucrt_dir()?;
146142

147-
let msvc_linker = windows_registry::find_tool(arch_or_target, "link.exe")?;
148-
let path = &msvc_linker.env().iter().find(|(k, _)| k == "PATH")?.1;
149-
find_tool_in_path(tool_name, path)
150-
}
143+
// Determine the host architecture to find the right subdirectory in the Windows SDK
144+
let host = match env::var("HOST").ok()?.split('-').next()? {
145+
"x86" => "x86",
146+
"x86_64" => "x64",
147+
"aarch64" => "arm64",
148+
_ => return None,
149+
};
150+
151+
let mut tool_path = ucrt_dir.join("bin").join(&ucrt_version).join(host);
152+
153+
tool_path.push(tool_name);
151154

152-
/// Find a tool in the directories in a given PATH-like string.
153-
fn find_tool_in_path<P: AsRef<ffi::OsStr>>(tool_name: &str, path: P) -> Option<path::PathBuf> {
154-
env::split_paths(path.as_ref()).find_map(|p| {
155-
let tool_path = p.join(tool_name);
156-
if tool_path.try_exists().unwrap_or(false) { Some(tool_path) } else { None }
157-
})
155+
Some(tool_path)
158156
}

0 commit comments

Comments
 (0)