Skip to content

Commit 9b1c5d4

Browse files
committed
Try to find the windows resource compiler on the PATH first
In some situations `cc::windows_registry::find_msvc_tool("link.exe")` returns a `Tool` with an empty environment (without the Windows SDK tools in the PATH). This happens if link.exe itself was found on the PATH, for example if you're in a VS Developer Command Prompt. Update the `find_resource_compiler` to more closely follow how `cc::windows_registry` does it: first try to find "rc.exe" on the PATH and only if that fails (and also if "link.exe" isn't on the PATH), try to locate it indirectly via `find_msvc_tool("link.exe")`
1 parent 5d1b897 commit 9b1c5d4

File tree

1 file changed

+12
-2
lines changed
  • compiler/rustc_windows_rc/src

1 file changed

+12
-2
lines changed

compiler/rustc_windows_rc/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,30 @@ fn parse_version(version: &str) -> Option<ResourceVersion> {
135135
/// Find the Windows SDK resource compiler `rc.exe` for the given architecture or target triple.
136136
/// Returns `None` if the tool could not be found.
137137
fn find_resource_compiler(arch_or_target: &str) -> Option<path::PathBuf> {
138-
find_windows_sdk_tool(arch_or_target, "rc.exe")
138+
// If "rc.exe" is on the PATH, use it. Otherwise, try to find the tool in a Windows SDK
139+
find_tool_in_env("rc.exe").or_else(|| find_windows_sdk_tool(arch_or_target, "rc.exe"))
139140
}
140141

141142
/// Find a Windows SDK tool for the given architecture or target triple.
142143
/// Returns `None` if the tool could not be found.
143144
fn find_windows_sdk_tool(arch_or_target: &str, tool_name: &str) -> Option<path::PathBuf> {
144145
// 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+
// cc does include the Windows SDK tools in the PATH environment of an MSVC tool, if
147+
// the MSVC tool itself was not found via the PATH.
148+
if find_tool_in_env("link.exe").is_some() {
149+
return None;
150+
}
146151

147152
let msvc_linker = windows_registry::find_tool(arch_or_target, "link.exe")?;
148153
let path = &msvc_linker.env().iter().find(|(k, _)| k == "PATH")?.1;
149154
find_tool_in_path(tool_name, path)
150155
}
151156

157+
/// Find a tool in the directories of the PATH environment variable
158+
fn find_tool_in_env(tool_name: &str) -> Option<path::PathBuf> {
159+
env::var_os("PATH").and_then(|p| find_tool_in_path(tool_name, p))
160+
}
161+
152162
/// Find a tool in the directories in a given PATH-like string.
153163
fn find_tool_in_path<P: AsRef<ffi::OsStr>>(tool_name: &str, path: P) -> Option<path::PathBuf> {
154164
env::split_paths(path.as_ref()).find_map(|p| {

0 commit comments

Comments
 (0)