1313
1414use std:: process:: Command ;
1515
16- use Tool ;
17-
18- #[ cfg( windows) ]
19- macro_rules! otry {
20- ( $expr: expr) => {
21- match $expr {
22- Some ( val) => val,
23- None => return None ,
24- }
25- } ;
26- }
16+ use crate :: Tool ;
2717
2818/// Attempts to find a tool within an MSVC installation using the Windows
2919/// registry as a point to search from.
@@ -173,9 +163,9 @@ pub fn find_vs_version() -> Result<VsVers, String> {
173163
174164#[ cfg( windows) ]
175165mod impl_ {
176- use com;
177- use registry:: { RegistryKey , LOCAL_MACHINE } ;
178- use setup_config:: { EnumSetupInstances , SetupConfiguration , SetupInstance } ;
166+ use crate :: com;
167+ use crate :: registry:: { RegistryKey , LOCAL_MACHINE } ;
168+ use crate :: setup_config:: { EnumSetupInstances , SetupConfiguration , SetupInstance } ;
179169 use std:: env;
180170 use std:: ffi:: OsString ;
181171 use std:: fs:: File ;
@@ -184,7 +174,7 @@ mod impl_ {
184174 use std:: mem;
185175 use std:: path:: { Path , PathBuf } ;
186176
187- use Tool ;
177+ use crate :: Tool ;
188178
189179 struct MsvcTool {
190180 tool : PathBuf ,
@@ -226,10 +216,10 @@ mod impl_ {
226216 return Box :: new ( iter:: empty ( ) ) ;
227217 } ;
228218 Box :: new ( instances. filter_map ( |instance| {
229- let instance = otry ! ( instance. ok( ) ) ;
230- let installation_name = otry ! ( instance. installation_name( ) . ok( ) ) ;
231- if otry ! ( installation_name. to_str( ) ) . starts_with ( "VisualStudio/16." ) {
232- Some ( PathBuf :: from ( otry ! ( instance. installation_path( ) . ok( ) ) ) )
219+ let instance = instance. ok ( ) ? ;
220+ let installation_name = instance. installation_name ( ) . ok ( ) ? ;
221+ if installation_name. to_str ( ) ? . starts_with ( "VisualStudio/16." ) {
222+ Some ( PathBuf :: from ( instance. installation_path ( ) . ok ( ) ? ) )
233223 } else {
234224 None
235225 }
@@ -264,16 +254,16 @@ mod impl_ {
264254 //
265255 // [online]: https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/
266256 fn vs15_instances ( ) -> Option < EnumSetupInstances > {
267- otry ! ( com:: initialize( ) . ok( ) ) ;
257+ com:: initialize ( ) . ok ( ) ? ;
268258
269- let config = otry ! ( SetupConfiguration :: new( ) . ok( ) ) ;
259+ let config = SetupConfiguration :: new ( ) . ok ( ) ? ;
270260 config. enum_all_instances ( ) . ok ( )
271261 }
272262
273263 pub fn find_msvc_15 ( tool : & str , target : & str ) -> Option < Tool > {
274- let iter = otry ! ( vs15_instances( ) ) ;
264+ let iter = vs15_instances ( ) ? ;
275265 for instance in iter {
276- let instance = otry ! ( instance. ok( ) ) ;
266+ let instance = instance. ok ( ) ? ;
277267 let tool = tool_from_vs15_instance ( tool, target, & instance) ;
278268 if tool. is_some ( ) {
279269 return tool;
@@ -324,7 +314,7 @@ mod impl_ {
324314
325315 fn tool_from_vs15_instance ( tool : & str , target : & str , instance : & SetupInstance ) -> Option < Tool > {
326316 let ( bin_path, host_dylib_path, lib_path, include_path) =
327- otry ! ( vs15_vc_paths( target, instance) ) ;
317+ vs15_vc_paths ( target, instance) ? ;
328318 let tool_path = bin_path. join ( tool) ;
329319 if !tool_path. exists ( ) {
330320 return None ;
@@ -340,7 +330,7 @@ mod impl_ {
340330 tool. include . push ( atl_include_path) ;
341331 }
342332
343- otry ! ( add_sdks( & mut tool, target) ) ;
333+ add_sdks ( & mut tool, target) ? ;
344334
345335 Some ( tool. into_tool ( ) )
346336 }
@@ -349,19 +339,19 @@ mod impl_ {
349339 target : & str ,
350340 instance : & SetupInstance ,
351341 ) -> Option < ( PathBuf , PathBuf , PathBuf , PathBuf ) > {
352- let instance_path: PathBuf = otry ! ( instance. installation_path( ) . ok( ) ) . into ( ) ;
342+ let instance_path: PathBuf = instance. installation_path ( ) . ok ( ) ? . into ( ) ;
353343 let version_path =
354344 instance_path. join ( r"VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt" ) ;
355- let mut version_file = otry ! ( File :: open( version_path) . ok( ) ) ;
345+ let mut version_file = File :: open ( version_path) . ok ( ) ? ;
356346 let mut version = String :: new ( ) ;
357- otry ! ( version_file. read_to_string( & mut version) . ok( ) ) ;
347+ version_file. read_to_string ( & mut version) . ok ( ) ? ;
358348 let version = version. trim ( ) ;
359349 let host = match host_arch ( ) {
360350 X86 => "X86" ,
361351 X86_64 => "X64" ,
362352 _ => return None ,
363353 } ;
364- let target = otry ! ( lib_subdir( target) ) ;
354+ let target = lib_subdir ( target) ? ;
365355 // The directory layout here is MSVC/bin/Host$host/$target/
366356 let path = instance_path. join ( r"VC\Tools\MSVC" ) . join ( version) ;
367357 // This is the path to the toolchain for a particular target, running
@@ -384,7 +374,7 @@ mod impl_ {
384374
385375 fn atl_paths ( target : & str , path : & Path ) -> Option < ( PathBuf , PathBuf ) > {
386376 let atl_path = path. join ( "atlfmc" ) ;
387- let sub = otry ! ( lib_subdir( target) ) ;
377+ let sub = lib_subdir ( target) ? ;
388378 if atl_path. exists ( ) {
389379 Some ( ( atl_path. join ( "lib" ) . join ( sub) , atl_path. join ( "include" ) ) )
390380 } else {
@@ -395,15 +385,15 @@ mod impl_ {
395385 // For MSVC 14 we need to find the Universal CRT as well as either
396386 // the Windows 10 SDK or Windows 8.1 SDK.
397387 pub fn find_msvc_14 ( tool : & str , target : & str ) -> Option < Tool > {
398- let vcdir = otry ! ( get_vc_dir( "14.0" ) ) ;
399- let mut tool = otry ! ( get_tool( tool, & vcdir, target) ) ;
400- otry ! ( add_sdks( & mut tool, target) ) ;
388+ let vcdir = get_vc_dir ( "14.0" ) ? ;
389+ let mut tool = get_tool ( tool, & vcdir, target) ? ;
390+ add_sdks ( & mut tool, target) ? ;
401391 Some ( tool. into_tool ( ) )
402392 }
403393
404394 fn add_sdks ( tool : & mut MsvcTool , target : & str ) -> Option < ( ) > {
405- let sub = otry ! ( lib_subdir( target) ) ;
406- let ( ucrt, ucrt_version) = otry ! ( get_ucrt_dir( ) ) ;
395+ let sub = lib_subdir ( target) ? ;
396+ let ( ucrt, ucrt_version) = get_ucrt_dir ( ) ? ;
407397
408398 tool. path
409399 . push ( ucrt. join ( "bin" ) . join ( & ucrt_version) . join ( sub) ) ;
@@ -438,10 +428,10 @@ mod impl_ {
438428
439429 // For MSVC 12 we need to find the Windows 8.1 SDK.
440430 pub fn find_msvc_12 ( tool : & str , target : & str ) -> Option < Tool > {
441- let vcdir = otry ! ( get_vc_dir( "12.0" ) ) ;
442- let mut tool = otry ! ( get_tool( tool, & vcdir, target) ) ;
443- let sub = otry ! ( lib_subdir( target) ) ;
444- let sdk81 = otry ! ( get_sdk81_dir( ) ) ;
431+ let vcdir = get_vc_dir ( "12.0" ) ? ;
432+ let mut tool = get_tool ( tool, & vcdir, target) ? ;
433+ let sub = lib_subdir ( target) ? ;
434+ let sdk81 = get_sdk81_dir ( ) ? ;
445435 tool. path . push ( sdk81. join ( "bin" ) . join ( sub) ) ;
446436 let sdk_lib = sdk81. join ( "lib" ) . join ( "winv6.3" ) ;
447437 tool. libs . push ( sdk_lib. join ( "um" ) . join ( sub) ) ;
@@ -454,10 +444,10 @@ mod impl_ {
454444
455445 // For MSVC 11 we need to find the Windows 8 SDK.
456446 pub fn find_msvc_11 ( tool : & str , target : & str ) -> Option < Tool > {
457- let vcdir = otry ! ( get_vc_dir( "11.0" ) ) ;
458- let mut tool = otry ! ( get_tool( tool, & vcdir, target) ) ;
459- let sub = otry ! ( lib_subdir( target) ) ;
460- let sdk8 = otry ! ( get_sdk8_dir( ) ) ;
447+ let vcdir = get_vc_dir ( "11.0" ) ? ;
448+ let mut tool = get_tool ( tool, & vcdir, target) ? ;
449+ let sub = lib_subdir ( target) ? ;
450+ let sdk8 = get_sdk8_dir ( ) ? ;
461451 tool. path . push ( sdk8. join ( "bin" ) . join ( sub) ) ;
462452 let sdk_lib = sdk8. join ( "lib" ) . join ( "win8" ) ;
463453 tool. libs . push ( sdk_lib. join ( "um" ) . join ( sub) ) ;
@@ -494,7 +484,7 @@ mod impl_ {
494484 tool
495485 } )
496486 . filter_map ( |mut tool| {
497- let sub = otry ! ( vc_lib_subdir( target) ) ;
487+ let sub = vc_lib_subdir ( target) ? ;
498488 tool. libs . push ( path. join ( "lib" ) . join ( sub) ) ;
499489 tool. include . push ( path. join ( "include" ) ) ;
500490 let atlmfc_path = path. join ( "atlmfc" ) ;
@@ -511,8 +501,8 @@ mod impl_ {
511501 // trying to find.
512502 fn get_vc_dir ( ver : & str ) -> Option < PathBuf > {
513503 let key = r"SOFTWARE\Microsoft\VisualStudio\SxS\VC7" ;
514- let key = otry ! ( LOCAL_MACHINE . open( key. as_ref( ) ) . ok( ) ) ;
515- let path = otry ! ( key. query_str( ver) . ok( ) ) ;
504+ let key = LOCAL_MACHINE . open ( key. as_ref ( ) ) . ok ( ) ? ;
505+ let path = key. query_str ( ver) . ok ( ) ? ;
516506 Some ( path. into ( ) )
517507 }
518508
@@ -524,10 +514,10 @@ mod impl_ {
524514 // Returns a pair of (root, version) for the ucrt dir if found
525515 fn get_ucrt_dir ( ) -> Option < ( PathBuf , String ) > {
526516 let key = r"SOFTWARE\Microsoft\Windows Kits\Installed Roots" ;
527- let key = otry ! ( LOCAL_MACHINE . open( key. as_ref( ) ) . ok( ) ) ;
528- let root = otry ! ( key. query_str( "KitsRoot10" ) . ok( ) ) ;
529- let readdir = otry ! ( Path :: new( & root) . join( "lib" ) . read_dir( ) . ok( ) ) ;
530- let max_libdir = otry ! ( readdir
517+ let key = LOCAL_MACHINE . open ( key. as_ref ( ) ) . ok ( ) ? ;
518+ let root = key. query_str ( "KitsRoot10" ) . ok ( ) ? ;
519+ let readdir = Path :: new ( & root) . join ( "lib" ) . read_dir ( ) . ok ( ) ? ;
520+ let max_libdir = readdir
531521 . filter_map ( |dir| dir. ok ( ) )
532522 . map ( |dir| dir. path ( ) )
533523 . filter ( |dir| dir
@@ -536,7 +526,7 @@ mod impl_ {
536526 . and_then ( |c| c. as_os_str ( ) . to_str ( ) )
537527 . map ( |c| c. starts_with ( "10." ) && dir. join ( "ucrt" ) . is_dir ( ) )
538528 . unwrap_or ( false ) )
539- . max( ) ) ;
529+ . max ( ) ? ;
540530 let version = max_libdir. components ( ) . last ( ) . unwrap ( ) ;
541531 let version = version. as_os_str ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
542532 Some ( ( root. into ( ) , version) )
@@ -552,19 +542,19 @@ mod impl_ {
552542 // asciibetically to find the newest one as that is what vcvars does.
553543 fn get_sdk10_dir ( ) -> Option < ( PathBuf , String ) > {
554544 let key = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v10.0" ;
555- let key = otry ! ( LOCAL_MACHINE . open( key. as_ref( ) ) . ok( ) ) ;
556- let root = otry ! ( key. query_str( "InstallationFolder" ) . ok( ) ) ;
557- let readdir = otry ! ( Path :: new( & root) . join( "lib" ) . read_dir( ) . ok( ) ) ;
545+ let key = LOCAL_MACHINE . open ( key. as_ref ( ) ) . ok ( ) ? ;
546+ let root = key. query_str ( "InstallationFolder" ) . ok ( ) ? ;
547+ let readdir = Path :: new ( & root) . join ( "lib" ) . read_dir ( ) . ok ( ) ? ;
558548 let mut dirs = readdir
559549 . filter_map ( |dir| dir. ok ( ) )
560550 . map ( |dir| dir. path ( ) )
561551 . collect :: < Vec < _ > > ( ) ;
562552 dirs. sort ( ) ;
563- let dir = otry ! ( dirs
553+ let dir = dirs
564554 . into_iter ( )
565555 . rev ( )
566556 . filter ( |dir| dir. join ( "um" ) . join ( "x64" ) . join ( "kernel32.lib" ) . is_file ( ) )
567- . next( ) ) ;
557+ . next ( ) ? ;
568558 let version = dir. components ( ) . last ( ) . unwrap ( ) ;
569559 let version = version. as_os_str ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
570560 Some ( ( root. into ( ) , version) )
@@ -576,15 +566,15 @@ mod impl_ {
576566 // instead of user mode applications, we would care.
577567 fn get_sdk81_dir ( ) -> Option < PathBuf > {
578568 let key = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v8.1" ;
579- let key = otry ! ( LOCAL_MACHINE . open( key. as_ref( ) ) . ok( ) ) ;
580- let root = otry ! ( key. query_str( "InstallationFolder" ) . ok( ) ) ;
569+ let key = LOCAL_MACHINE . open ( key. as_ref ( ) ) . ok ( ) ? ;
570+ let root = key. query_str ( "InstallationFolder" ) . ok ( ) ? ;
581571 Some ( root. into ( ) )
582572 }
583573
584574 fn get_sdk8_dir ( ) -> Option < PathBuf > {
585575 let key = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v8.0" ;
586- let key = otry ! ( LOCAL_MACHINE . open( key. as_ref( ) ) . ok( ) ) ;
587- let root = otry ! ( key. query_str( "InstallationFolder" ) . ok( ) ) ;
576+ let key = LOCAL_MACHINE . open ( key. as_ref ( ) ) . ok ( ) ? ;
577+ let root = key. query_str ( "InstallationFolder" ) . ok ( ) ? ;
588578 Some ( root. into ( ) )
589579 }
590580
0 commit comments