Skip to content

Commit ac2192c

Browse files
authored
[win] Search the Windows SDK for tools as well (#1553)
1 parent 10f7638 commit ac2192c

File tree

2 files changed

+59
-30
lines changed

2 files changed

+59
-30
lines changed

dev-tools/cc-test/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ fn main() {
173173
!has_spectre(&target),
174174
"LIB should not use spectre-mitigated libs when VSCMD_ARG_VCVARS_SPECTRE is not set"
175175
);
176+
177+
// Test that we can find tools in the Windows SDK too.
178+
cc::windows_registry::find_tool(&target, "rc.exe").unwrap();
176179
}
177180

178181
// This tests whether we can build a library but not link it to the main

find-msvc-tools/src/find_tools.rs

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,13 @@ mod impl_ {
300300
include: Vec<PathBuf>,
301301
}
302302

303+
#[derive(Default)]
304+
struct SdkInfo {
305+
libs: Vec<PathBuf>,
306+
path: Vec<PathBuf>,
307+
include: Vec<PathBuf>,
308+
}
309+
303310
struct LibraryHandle(HMODULE);
304311

305312
impl LibraryHandle {
@@ -373,6 +380,12 @@ mod impl_ {
373380
}
374381
}
375382

383+
fn add_sdk(&mut self, sdk_info: SdkInfo) {
384+
self.libs.extend(sdk_info.libs);
385+
self.path.extend(sdk_info.path);
386+
self.include.extend(sdk_info.include);
387+
}
388+
376389
fn into_tool(self, env_getter: &dyn EnvGetter) -> Tool {
377390
let MsvcTool {
378391
tool,
@@ -392,6 +405,12 @@ mod impl_ {
392405
}
393406
}
394407

408+
impl SdkInfo {
409+
fn find_tool(&self, tool: &str) -> Option<PathBuf> {
410+
self.path.iter().map(|p| p.join(tool)).find(|p| p.exists())
411+
}
412+
}
413+
395414
/// Checks to see if the target's arch matches the VS environment. Returns `None` if the
396415
/// environment is unknown.
397416
fn is_vscmd_target(target: TargetArch, env_getter: &dyn EnvGetter) -> Option<bool> {
@@ -738,9 +757,10 @@ mod impl_ {
738757
) -> Option<Tool> {
739758
let (root_path, bin_path, host_dylib_path, lib_path, alt_lib_path, include_path) =
740759
vs15plus_vc_paths(target, instance_path, env_getter)?;
741-
let tool_path = bin_path.join(tool);
760+
let sdk_info = get_sdks(target, env_getter)?;
761+
let mut tool_path = bin_path.join(tool);
742762
if !tool_path.exists() {
743-
return None;
763+
tool_path = sdk_info.find_tool(tool)?;
744764
};
745765

746766
let mut tool = MsvcTool::new(tool_path);
@@ -757,7 +777,7 @@ mod impl_ {
757777
tool.include.push(atl_include_path);
758778
}
759779

760-
add_sdks(&mut tool, target, env_getter)?;
780+
tool.add_sdk(sdk_info);
761781

762782
Some(tool.into_tool(env_getter))
763783
}
@@ -900,12 +920,13 @@ mod impl_ {
900920
env_getter: &dyn EnvGetter,
901921
) -> Option<Tool> {
902922
let vcdir = get_vc_dir("14.0")?;
903-
let mut tool = get_tool(tool, &vcdir, target)?;
904-
add_sdks(&mut tool, target, env_getter)?;
923+
let sdk_info = get_sdks(target, env_getter)?;
924+
let mut tool = get_tool(tool, &vcdir, target, &sdk_info)?;
925+
tool.add_sdk(sdk_info);
905926
Some(tool.into_tool(env_getter))
906927
}
907928

908-
fn add_sdks(tool: &mut MsvcTool, target: TargetArch, env_getter: &dyn EnvGetter) -> Option<()> {
929+
fn get_sdks(target: TargetArch, env_getter: &dyn EnvGetter) -> Option<SdkInfo> {
909930
let sub = target.as_vs_arch();
910931
let (ucrt, ucrt_version) = get_ucrt_dir()?;
911932

@@ -916,35 +937,37 @@ mod impl_ {
916937
_ => return None,
917938
};
918939

919-
tool.path
940+
let mut info = SdkInfo::default();
941+
942+
info.path
920943
.push(ucrt.join("bin").join(&ucrt_version).join(host));
921944

922945
let ucrt_include = ucrt.join("include").join(&ucrt_version);
923-
tool.include.push(ucrt_include.join("ucrt"));
946+
info.include.push(ucrt_include.join("ucrt"));
924947

925948
let ucrt_lib = ucrt.join("lib").join(&ucrt_version);
926-
tool.libs.push(ucrt_lib.join("ucrt").join(sub));
949+
info.libs.push(ucrt_lib.join("ucrt").join(sub));
927950

928951
if let Some((sdk, version)) = get_sdk10_dir(env_getter) {
929-
tool.path.push(sdk.join("bin").join(host));
952+
info.path.push(sdk.join("bin").join(host));
930953
let sdk_lib = sdk.join("lib").join(&version);
931-
tool.libs.push(sdk_lib.join("um").join(sub));
954+
info.libs.push(sdk_lib.join("um").join(sub));
932955
let sdk_include = sdk.join("include").join(&version);
933-
tool.include.push(sdk_include.join("um"));
934-
tool.include.push(sdk_include.join("cppwinrt"));
935-
tool.include.push(sdk_include.join("winrt"));
936-
tool.include.push(sdk_include.join("shared"));
956+
info.include.push(sdk_include.join("um"));
957+
info.include.push(sdk_include.join("cppwinrt"));
958+
info.include.push(sdk_include.join("winrt"));
959+
info.include.push(sdk_include.join("shared"));
937960
} else if let Some(sdk) = get_sdk81_dir() {
938-
tool.path.push(sdk.join("bin").join(host));
961+
info.path.push(sdk.join("bin").join(host));
939962
let sdk_lib = sdk.join("lib").join("winv6.3");
940-
tool.libs.push(sdk_lib.join("um").join(sub));
963+
info.libs.push(sdk_lib.join("um").join(sub));
941964
let sdk_include = sdk.join("include");
942-
tool.include.push(sdk_include.join("um"));
943-
tool.include.push(sdk_include.join("winrt"));
944-
tool.include.push(sdk_include.join("shared"));
965+
info.include.push(sdk_include.join("um"));
966+
info.include.push(sdk_include.join("winrt"));
967+
info.include.push(sdk_include.join("shared"));
945968
}
946969

947-
Some(())
970+
Some(info)
948971
}
949972

950973
fn add_env(
@@ -963,22 +986,25 @@ mod impl_ {
963986

964987
// Given a possible MSVC installation directory, we look for the linker and
965988
// then add the MSVC library path.
966-
fn get_tool(tool: &str, path: &Path, target: TargetArch) -> Option<MsvcTool> {
989+
fn get_tool(
990+
tool: &str,
991+
path: &Path,
992+
target: TargetArch,
993+
sdk_info: &SdkInfo,
994+
) -> Option<MsvcTool> {
967995
bin_subdir(target)
968996
.into_iter()
969997
.map(|(sub, host)| {
970998
(
971999
path.join("bin").join(sub).join(tool),
972-
path.join("bin").join(host),
1000+
Some(path.join("bin").join(host)),
9731001
)
9741002
})
9751003
.filter(|(path, _)| path.is_file())
976-
.map(|(path, host)| {
977-
let mut tool = MsvcTool::new(path);
978-
tool.path.push(host);
979-
tool
980-
})
981-
.filter_map(|mut tool| {
1004+
.chain(iter::once_with(|| Some((sdk_info.find_tool(tool)?, None))).flatten())
1005+
.map(|(tool_path, host)| {
1006+
let mut tool = MsvcTool::new(tool_path);
1007+
tool.path.extend(host);
9821008
let sub = vc_lib_subdir(target);
9831009
tool.libs.push(path.join("lib").join(sub));
9841010
tool.include.push(path.join("include"));
@@ -987,7 +1013,7 @@ mod impl_ {
9871013
tool.libs.push(atlmfc_path.join("lib").join(sub));
9881014
tool.include.push(atlmfc_path.join("include"));
9891015
}
990-
Some(tool)
1016+
tool
9911017
})
9921018
.next()
9931019
}

0 commit comments

Comments
 (0)