Skip to content

Commit 8f11c4d

Browse files
authored
Rollup merge of #146784 - dpaoliello:findmsvc, r=wesleywiser
[win] Use find-msvc-tools instead of cc to find the linker and rc on Windows `find-msvc-tools` was factored out from `cc` to allow updating the use in `rustc_codegen_ssa` (finding the linker when running the Rust compiler) and `rustc_windows_rc` (finding the Windows Resource Compiler when running the Rust compiler) to be separate from the use in `rustc_llvm` (building LLVM as part of building the Rust compiler).
2 parents 8905ad9 + 4da5935 commit 8f11c4d

File tree

8 files changed

+22
-45
lines changed

8 files changed

+22
-45
lines changed

Cargo.lock

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

1335+
[[package]]
1336+
name = "find-msvc-tools"
1337+
version = "0.1.2"
1338+
source = "registry+https://github.com/rust-lang/crates.io-index"
1339+
checksum = "1ced73b1dacfc750a6db6c0a0c3a3853c8b41997e2e2c563dc90804ae6867959"
1340+
13351341
[[package]]
13361342
name = "flate2"
13371343
version = "1.1.2"
@@ -3556,7 +3562,7 @@ dependencies = [
35563562
"ar_archive_writer",
35573563
"bitflags",
35583564
"bstr",
3559-
"cc",
3565+
"find-msvc-tools",
35603566
"itertools",
35613567
"libc",
35623568
"object 0.37.3",
@@ -4760,7 +4766,7 @@ dependencies = [
47604766
name = "rustc_windows_rc"
47614767
version = "0.0.0"
47624768
dependencies = [
4763-
"cc",
4769+
"find-msvc-tools",
47644770
]
47654771

47664772
[[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: 5 additions & 29 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");
@@ -38,7 +36,10 @@ pub fn compile_windows_resource_file(
3836
let resource_compiler = if let Ok(path) = env::var("RUSTC_WINDOWS_RC") {
3937
path.into()
4038
} else {
41-
find_resource_compiler(&env::var("CARGO_CFG_TARGET_ARCH").unwrap()).expect("found rc.exe")
39+
find_msvc_tools::find_tool(&env::var("CARGO_CFG_TARGET_ARCH").unwrap(), "rc.exe")
40+
.expect("found rc.exe")
41+
.path()
42+
.to_owned()
4243
};
4344

4445
let rc_path = resources_dir.join(file_stem.with_extension("rc"));
@@ -134,28 +135,3 @@ fn parse_version(version: &str) -> Option<ResourceVersion> {
134135
Some(ResourceVersion { major, minor, patch, build: 0 })
135136
}
136137
}
137-
138-
/// Find the Windows SDK resource compiler `rc.exe` for the given architecture or target triple.
139-
/// Returns `None` if the tool could not be found.
140-
fn find_resource_compiler(arch_or_target: &str) -> Option<path::PathBuf> {
141-
find_windows_sdk_tool(arch_or_target, "rc.exe")
142-
}
143-
144-
/// Find a Windows SDK tool for the given architecture or target triple.
145-
/// Returns `None` if the tool could not be found.
146-
fn find_windows_sdk_tool(arch_or_target: &str, tool_name: &str) -> Option<path::PathBuf> {
147-
// windows_registry::find_tool can only find MSVC tools, not Windows SDK tools, but
148-
// cc does include the Windows SDK tools in the PATH environment of MSVC tools.
149-
150-
let msvc_linker = windows_registry::find_tool(arch_or_target, "link.exe")?;
151-
let path = &msvc_linker.env().iter().find(|(k, _)| k == "PATH")?.1;
152-
find_tool_in_path(tool_name, path)
153-
}
154-
155-
/// Find a tool in the directories in a given PATH-like string.
156-
fn find_tool_in_path<P: AsRef<ffi::OsStr>>(tool_name: &str, path: P) -> Option<path::PathBuf> {
157-
env::split_paths(path.as_ref()).find_map(|p| {
158-
let tool_path = p.join(tool_name);
159-
if tool_path.try_exists().unwrap_or(false) { Some(tool_path) } else { None }
160-
})
161-
}

src/tools/tidy/src/deps.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
367367
"expect-test",
368368
"fallible-iterator", // dependency of `thorin`
369369
"fastrand",
370+
"find-msvc-tools",
370371
"flate2",
371372
"fluent-bundle",
372373
"fluent-langneg",

0 commit comments

Comments
 (0)