[win] Use find-msvc-tools instead of cc to find the linker and rc on Windows

This commit is contained in:
Daniel Paoliello
2025-09-19 09:16:26 -07:00
parent 59043567a5
commit 4da59355fd
8 changed files with 22 additions and 45 deletions

View File

@@ -1332,6 +1332,12 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ced73b1dacfc750a6db6c0a0c3a3853c8b41997e2e2c563dc90804ae6867959"
[[package]]
name = "flate2"
version = "1.1.2"
@@ -3556,7 +3562,7 @@ dependencies = [
"ar_archive_writer",
"bitflags",
"bstr",
"cc",
"find-msvc-tools",
"itertools",
"libc",
"object 0.37.3",
@@ -4760,7 +4766,7 @@ dependencies = [
name = "rustc_windows_rc"
version = "0.0.0"
dependencies = [
"cc",
"find-msvc-tools",
]
[[package]]

View File

@@ -8,9 +8,7 @@ edition = "2024"
ar_archive_writer = "0.5"
bitflags = "2.4.1"
bstr = "1.11.3"
# `cc` updates often break things, so we pin it here. Cargo enforces "max 1 semver-compat version
# per crate", so if you change this, you need to also change it in `rustc_llvm` and `rustc_windows_rc`.
cc = "=1.2.16"
find-msvc-tools = "0.1.2"
itertools = "0.12"
pathdiff = "0.2.0"
regex = "1.4"

View File

@@ -9,7 +9,7 @@ use std::path::{Path, PathBuf};
use std::process::{Output, Stdio};
use std::{env, fmt, fs, io, mem, str};
use cc::windows_registry;
use find_msvc_tools;
use itertools::Itertools;
use regex::Regex;
use rustc_arena::TypedArena;
@@ -877,9 +877,9 @@ fn link_natively(
// All Microsoft `link.exe` linking ror codes are
// four digit numbers in the range 1000 to 9999 inclusive
if is_msvc_link_exe && (code < 1000 || code > 9999) {
let is_vs_installed = windows_registry::find_vs_version().is_ok();
let is_vs_installed = find_msvc_tools::find_vs_version().is_ok();
let has_linker =
windows_registry::find_tool(&sess.target.arch, "link.exe").is_some();
find_msvc_tools::find_tool(&sess.target.arch, "link.exe").is_some();
sess.dcx().emit_note(errors::LinkExeUnexpectedError);

View File

@@ -4,7 +4,7 @@ use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::{env, io, iter, mem, str};
use cc::windows_registry;
use find_msvc_tools;
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
use rustc_metadata::{
find_native_static_library, try_find_native_dynamic_library, try_find_native_static_library,
@@ -53,7 +53,7 @@ pub(crate) fn get_linker<'a>(
self_contained: bool,
target_cpu: &'a str,
) -> Box<dyn Linker + 'a> {
let msvc_tool = windows_registry::find_tool(&sess.target.arch, "link.exe");
let msvc_tool = find_msvc_tools::find_tool(&sess.target.arch, "link.exe");
// If our linker looks like a batch script on Windows then to execute this
// we'll need to spawn `cmd` explicitly. This is primarily done to handle
@@ -117,7 +117,6 @@ pub(crate) fn get_linker<'a>(
if sess.target.is_like_msvc
&& let Some(ref tool) = msvc_tool
{
cmd.args(tool.args());
for (k, v) in tool.env() {
if k == "PATH" {
new_path.extend(env::split_paths(v));

View File

@@ -10,8 +10,7 @@ libc = "0.2.73"
[build-dependencies]
# tidy-alphabetical-start
# `cc` updates often break things, so we pin it here. Cargo enforces "max 1 semver-compat version
# per crate", so if you change this, you need to also change it in `rustc_codegen_ssa` and `rustc_windows_rc`.
# `cc` updates often break things, so we pin it here.
cc = "=1.2.16"
# tidy-alphabetical-end

View File

@@ -5,7 +5,5 @@ edition = "2024"
[dependencies]
#tidy-alphabetical-start
# `cc` updates often break things, so we pin it here. Cargo enforces "max 1 semver-compat version
# per crate", so if you change this, you need to also change it in `rustc_llvm` and `rustc_codegen_ssa`.
cc = "=1.2.16"
find-msvc-tools = "0.1.2"
#tidy-alphabetical-end

View File

@@ -2,9 +2,7 @@
//!
//! Uses values from the `CFG_VERSION` and `CFG_RELEASE` environment variables
//! to set the product and file version information in the Windows resource file.
use std::{env, ffi, fs, path, process};
use cc::windows_registry;
use std::{env, fs, path, process};
/// The template for the Windows resource file.
const RESOURCE_TEMPLATE: &str = include_str!("../rustc.rc.in");
@@ -38,7 +36,10 @@ pub fn compile_windows_resource_file(
let resource_compiler = if let Ok(path) = env::var("RUSTC_WINDOWS_RC") {
path.into()
} else {
find_resource_compiler(&env::var("CARGO_CFG_TARGET_ARCH").unwrap()).expect("found rc.exe")
find_msvc_tools::find_tool(&env::var("CARGO_CFG_TARGET_ARCH").unwrap(), "rc.exe")
.expect("found rc.exe")
.path()
.to_owned()
};
let rc_path = resources_dir.join(file_stem.with_extension("rc"));
@@ -134,28 +135,3 @@ fn parse_version(version: &str) -> Option<ResourceVersion> {
Some(ResourceVersion { major, minor, patch, build: 0 })
}
}
/// Find the Windows SDK resource compiler `rc.exe` for the given architecture or target triple.
/// Returns `None` if the tool could not be found.
fn find_resource_compiler(arch_or_target: &str) -> Option<path::PathBuf> {
find_windows_sdk_tool(arch_or_target, "rc.exe")
}
/// Find a Windows SDK tool for the given architecture or target triple.
/// Returns `None` if the tool could not be found.
fn find_windows_sdk_tool(arch_or_target: &str, tool_name: &str) -> Option<path::PathBuf> {
// windows_registry::find_tool can only find MSVC tools, not Windows SDK tools, but
// cc does include the Windows SDK tools in the PATH environment of MSVC tools.
let msvc_linker = windows_registry::find_tool(arch_or_target, "link.exe")?;
let path = &msvc_linker.env().iter().find(|(k, _)| k == "PATH")?.1;
find_tool_in_path(tool_name, path)
}
/// Find a tool in the directories in a given PATH-like string.
fn find_tool_in_path<P: AsRef<ffi::OsStr>>(tool_name: &str, path: P) -> Option<path::PathBuf> {
env::split_paths(path.as_ref()).find_map(|p| {
let tool_path = p.join(tool_name);
if tool_path.try_exists().unwrap_or(false) { Some(tool_path) } else { None }
})
}

View File

@@ -365,6 +365,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"expect-test",
"fallible-iterator", // dependency of `thorin`
"fastrand",
"find-msvc-tools",
"flate2",
"fluent-bundle",
"fluent-langneg",