Skip to content

Commit c3e7cb5

Browse files
committed
[win] Use find-msvc-tools instead of cc to find the linker and rc on Windows
1 parent e10aa88 commit c3e7cb5

File tree

8 files changed

+21
-46
lines changed

8 files changed

+21
-46
lines changed

Cargo.lock

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,12 @@ dependencies = [
13311331
"windows-sys 0.59.0",
13321332
]
13331333

1334+
[[package]]
1335+
name = "find-msvc-tools"
1336+
version = "0.1.2"
1337+
source = "registry+https://github.com/rust-lang/crates.io-index"
1338+
checksum = "1ced73b1dacfc750a6db6c0a0c3a3853c8b41997e2e2c563dc90804ae6867959"
1339+
13341340
[[package]]
13351341
name = "flate2"
13361342
version = "1.1.2"
@@ -3549,7 +3555,7 @@ dependencies = [
35493555
"ar_archive_writer",
35503556
"bitflags",
35513557
"bstr",
3552-
"cc",
3558+
"find-msvc-tools",
35533559
"itertools",
35543560
"libc",
35553561
"object 0.37.3",
@@ -4753,7 +4759,7 @@ dependencies = [
47534759
name = "rustc_windows_rc"
47544760
version = "0.0.0"
47554761
dependencies = [
4756-
"cc",
4762+
"find-msvc-tools",
47574763
]
47584764

47594765
[[package]]

compiler/rustc_codegen_ssa/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ edition = "2024"
88
ar_archive_writer = "0.5"
99
bitflags = "2.4.1"
1010
bstr = "1.11.3"
11-
# `cc` updates often break things, so we pin it here. Cargo enforces "max 1 semver-compat version
12-
# per crate", so if you change this, you need to also change it in `rustc_llvm` and `rustc_windows_rc`.
13-
cc = "=1.2.16"
11+
find-msvc-tools = "0.1.2"
1412
itertools = "0.12"
1513
pathdiff = "0.2.0"
1614
regex = "1.4"

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::path::{Path, PathBuf};
99
use std::process::{Output, Stdio};
1010
use std::{env, fmt, fs, io, mem, str};
1111

12-
use cc::windows_registry;
12+
use find_msvc_tools;
1313
use itertools::Itertools;
1414
use regex::Regex;
1515
use rustc_arena::TypedArena;
@@ -877,9 +877,9 @@ fn link_natively(
877877
// All Microsoft `link.exe` linking ror codes are
878878
// four digit numbers in the range 1000 to 9999 inclusive
879879
if is_msvc_link_exe && (code < 1000 || code > 9999) {
880-
let is_vs_installed = windows_registry::find_vs_version().is_ok();
880+
let is_vs_installed = find_msvc_tools::find_vs_version().is_ok();
881881
let has_linker =
882-
windows_registry::find_tool(&sess.target.arch, "link.exe").is_some();
882+
find_msvc_tools::find_tool(&sess.target.arch, "link.exe").is_some();
883883

884884
sess.dcx().emit_note(errors::LinkExeUnexpectedError);
885885

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::prelude::*;
44
use std::path::{Path, PathBuf};
55
use std::{env, io, iter, mem, str};
66

7-
use cc::windows_registry;
7+
use find_msvc_tools;
88
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
99
use rustc_metadata::{
1010
find_native_static_library, try_find_native_dynamic_library, try_find_native_static_library,
@@ -53,7 +53,7 @@ pub(crate) fn get_linker<'a>(
5353
self_contained: bool,
5454
target_cpu: &'a str,
5555
) -> Box<dyn Linker + 'a> {
56-
let msvc_tool = windows_registry::find_tool(&sess.target.arch, "link.exe");
56+
let msvc_tool = find_msvc_tools::find_tool(&sess.target.arch, "link.exe");
5757

5858
// If our linker looks like a batch script on Windows then to execute this
5959
// we'll need to spawn `cmd` explicitly. This is primarily done to handle
@@ -117,7 +117,6 @@ pub(crate) fn get_linker<'a>(
117117
if sess.target.is_like_msvc
118118
&& let Some(ref tool) = msvc_tool
119119
{
120-
cmd.args(tool.args());
121120
for (k, v) in tool.env() {
122121
if k == "PATH" {
123122
new_path.extend(env::split_paths(v));

compiler/rustc_llvm/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ libc = "0.2.73"
1010

1111
[build-dependencies]
1212
# tidy-alphabetical-start
13-
# `cc` updates often break things, so we pin it here. Cargo enforces "max 1 semver-compat version
14-
# per crate", so if you change this, you need to also change it in `rustc_codegen_ssa` and `rustc_windows_rc`.
13+
# `cc` updates often break things, so we pin it here.
1514
cc = "=1.2.16"
1615
# tidy-alphabetical-end
1716

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 = "0.1.2"
119
#tidy-alphabetical-end

compiler/rustc_windows_rc/src/lib.rs

Lines changed: 4 additions & 30 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");
@@ -36,15 +34,16 @@ pub fn compile_windows_resource_file(
3634
fs::create_dir_all(&resources_dir).unwrap();
3735

3836
let resource_compiler =
39-
find_resource_compiler(&env::var("CARGO_CFG_TARGET_ARCH").unwrap()).expect("found rc.exe");
37+
find_msvc_tools::find_tool(&env::var("CARGO_CFG_TARGET_ARCH").unwrap(), "rc.exe")
38+
.expect("found rc.exe");
4039

4140
let rc_path = resources_dir.join(file_stem.with_extension("rc"));
4241

4342
write_resource_script_file(&rc_path, file_description, filetype);
4443

4544
let res_path = resources_dir.join(file_stem.with_extension("res"));
4645

47-
let status = process::Command::new(resource_compiler)
46+
let status = process::Command::new(resource_compiler.path())
4847
.arg("/fo")
4948
.arg(&res_path)
5049
.arg(&rc_path)
@@ -131,28 +130,3 @@ fn parse_version(version: &str) -> Option<ResourceVersion> {
131130
Some(ResourceVersion { major, minor, patch, build: 0 })
132131
}
133132
}
134-
135-
/// Find the Windows SDK resource compiler `rc.exe` for the given architecture or target triple.
136-
/// 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")
139-
}
140-
141-
/// Find a Windows SDK tool for the given architecture or target triple.
142-
/// 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.
146-
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-
}
151-
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-
})
158-
}

src/tools/tidy/src/deps.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
365365
"expect-test",
366366
"fallible-iterator", // dependency of `thorin`
367367
"fastrand",
368+
"find-msvc-tools",
368369
"flate2",
369370
"fluent-bundle",
370371
"fluent-langneg",

0 commit comments

Comments
 (0)