Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions compiler/rustc_windows_rc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,30 @@ fn parse_version(version: &str) -> Option<ResourceVersion> {
/// 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")
// If "rc.exe" is on the PATH, use it. Otherwise, try to find the tool in a Windows SDK
find_tool_in_env("rc.exe").or_else(|| 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.
// cc does include the Windows SDK tools in the PATH environment of an MSVC tool, if
// the MSVC tool itself was not found via the PATH.
if find_tool_in_env("link.exe").is_some() {
return None;
}

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 of the PATH environment variable
fn find_tool_in_env(tool_name: &str) -> Option<path::PathBuf> {
env::var_os("PATH").and_then(|p| find_tool_in_path(tool_name, p))
}

/// 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| {
Expand Down
Loading