13
13
14
14
use std:: process:: Command ;
15
15
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 ;
27
17
28
18
/// Attempts to find a tool within an MSVC installation using the Windows
29
19
/// registry as a point to search from.
@@ -173,9 +163,9 @@ pub fn find_vs_version() -> Result<VsVers, String> {
173
163
174
164
#[ cfg( windows) ]
175
165
mod 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 } ;
179
169
use std:: env;
180
170
use std:: ffi:: OsString ;
181
171
use std:: fs:: File ;
@@ -184,7 +174,7 @@ mod impl_ {
184
174
use std:: mem;
185
175
use std:: path:: { Path , PathBuf } ;
186
176
187
- use Tool ;
177
+ use crate :: Tool ;
188
178
189
179
struct MsvcTool {
190
180
tool : PathBuf ,
@@ -226,10 +216,10 @@ mod impl_ {
226
216
return Box :: new ( iter:: empty ( ) ) ;
227
217
} ;
228
218
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 ( ) ? ) )
233
223
} else {
234
224
None
235
225
}
@@ -264,16 +254,16 @@ mod impl_ {
264
254
//
265
255
// [online]: https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/
266
256
fn vs15_instances ( ) -> Option < EnumSetupInstances > {
267
- otry ! ( com:: initialize( ) . ok( ) ) ;
257
+ com:: initialize ( ) . ok ( ) ? ;
268
258
269
- let config = otry ! ( SetupConfiguration :: new( ) . ok( ) ) ;
259
+ let config = SetupConfiguration :: new ( ) . ok ( ) ? ;
270
260
config. enum_all_instances ( ) . ok ( )
271
261
}
272
262
273
263
pub fn find_msvc_15 ( tool : & str , target : & str ) -> Option < Tool > {
274
- let iter = otry ! ( vs15_instances( ) ) ;
264
+ let iter = vs15_instances ( ) ? ;
275
265
for instance in iter {
276
- let instance = otry ! ( instance. ok( ) ) ;
266
+ let instance = instance. ok ( ) ? ;
277
267
let tool = tool_from_vs15_instance ( tool, target, & instance) ;
278
268
if tool. is_some ( ) {
279
269
return tool;
@@ -324,7 +314,7 @@ mod impl_ {
324
314
325
315
fn tool_from_vs15_instance ( tool : & str , target : & str , instance : & SetupInstance ) -> Option < Tool > {
326
316
let ( bin_path, host_dylib_path, lib_path, include_path) =
327
- otry ! ( vs15_vc_paths( target, instance) ) ;
317
+ vs15_vc_paths ( target, instance) ? ;
328
318
let tool_path = bin_path. join ( tool) ;
329
319
if !tool_path. exists ( ) {
330
320
return None ;
@@ -340,7 +330,7 @@ mod impl_ {
340
330
tool. include . push ( atl_include_path) ;
341
331
}
342
332
343
- otry ! ( add_sdks( & mut tool, target) ) ;
333
+ add_sdks ( & mut tool, target) ? ;
344
334
345
335
Some ( tool. into_tool ( ) )
346
336
}
@@ -349,19 +339,19 @@ mod impl_ {
349
339
target : & str ,
350
340
instance : & SetupInstance ,
351
341
) -> 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 ( ) ;
353
343
let version_path =
354
344
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 ( ) ? ;
356
346
let mut version = String :: new ( ) ;
357
- otry ! ( version_file. read_to_string( & mut version) . ok( ) ) ;
347
+ version_file. read_to_string ( & mut version) . ok ( ) ? ;
358
348
let version = version. trim ( ) ;
359
349
let host = match host_arch ( ) {
360
350
X86 => "X86" ,
361
351
X86_64 => "X64" ,
362
352
_ => return None ,
363
353
} ;
364
- let target = otry ! ( lib_subdir( target) ) ;
354
+ let target = lib_subdir ( target) ? ;
365
355
// The directory layout here is MSVC/bin/Host$host/$target/
366
356
let path = instance_path. join ( r"VC\Tools\MSVC" ) . join ( version) ;
367
357
// This is the path to the toolchain for a particular target, running
@@ -384,7 +374,7 @@ mod impl_ {
384
374
385
375
fn atl_paths ( target : & str , path : & Path ) -> Option < ( PathBuf , PathBuf ) > {
386
376
let atl_path = path. join ( "atlfmc" ) ;
387
- let sub = otry ! ( lib_subdir( target) ) ;
377
+ let sub = lib_subdir ( target) ? ;
388
378
if atl_path. exists ( ) {
389
379
Some ( ( atl_path. join ( "lib" ) . join ( sub) , atl_path. join ( "include" ) ) )
390
380
} else {
@@ -395,15 +385,15 @@ mod impl_ {
395
385
// For MSVC 14 we need to find the Universal CRT as well as either
396
386
// the Windows 10 SDK or Windows 8.1 SDK.
397
387
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) ? ;
401
391
Some ( tool. into_tool ( ) )
402
392
}
403
393
404
394
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 ( ) ? ;
407
397
408
398
tool. path
409
399
. push ( ucrt. join ( "bin" ) . join ( & ucrt_version) . join ( sub) ) ;
@@ -438,10 +428,10 @@ mod impl_ {
438
428
439
429
// For MSVC 12 we need to find the Windows 8.1 SDK.
440
430
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 ( ) ? ;
445
435
tool. path . push ( sdk81. join ( "bin" ) . join ( sub) ) ;
446
436
let sdk_lib = sdk81. join ( "lib" ) . join ( "winv6.3" ) ;
447
437
tool. libs . push ( sdk_lib. join ( "um" ) . join ( sub) ) ;
@@ -454,10 +444,10 @@ mod impl_ {
454
444
455
445
// For MSVC 11 we need to find the Windows 8 SDK.
456
446
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 ( ) ? ;
461
451
tool. path . push ( sdk8. join ( "bin" ) . join ( sub) ) ;
462
452
let sdk_lib = sdk8. join ( "lib" ) . join ( "win8" ) ;
463
453
tool. libs . push ( sdk_lib. join ( "um" ) . join ( sub) ) ;
@@ -494,7 +484,7 @@ mod impl_ {
494
484
tool
495
485
} )
496
486
. filter_map ( |mut tool| {
497
- let sub = otry ! ( vc_lib_subdir( target) ) ;
487
+ let sub = vc_lib_subdir ( target) ? ;
498
488
tool. libs . push ( path. join ( "lib" ) . join ( sub) ) ;
499
489
tool. include . push ( path. join ( "include" ) ) ;
500
490
let atlmfc_path = path. join ( "atlmfc" ) ;
@@ -511,8 +501,8 @@ mod impl_ {
511
501
// trying to find.
512
502
fn get_vc_dir ( ver : & str ) -> Option < PathBuf > {
513
503
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 ( ) ? ;
516
506
Some ( path. into ( ) )
517
507
}
518
508
@@ -524,10 +514,10 @@ mod impl_ {
524
514
// Returns a pair of (root, version) for the ucrt dir if found
525
515
fn get_ucrt_dir ( ) -> Option < ( PathBuf , String ) > {
526
516
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
531
521
. filter_map ( |dir| dir. ok ( ) )
532
522
. map ( |dir| dir. path ( ) )
533
523
. filter ( |dir| dir
@@ -536,7 +526,7 @@ mod impl_ {
536
526
. and_then ( |c| c. as_os_str ( ) . to_str ( ) )
537
527
. map ( |c| c. starts_with ( "10." ) && dir. join ( "ucrt" ) . is_dir ( ) )
538
528
. unwrap_or ( false ) )
539
- . max( ) ) ;
529
+ . max ( ) ? ;
540
530
let version = max_libdir. components ( ) . last ( ) . unwrap ( ) ;
541
531
let version = version. as_os_str ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
542
532
Some ( ( root. into ( ) , version) )
@@ -552,19 +542,19 @@ mod impl_ {
552
542
// asciibetically to find the newest one as that is what vcvars does.
553
543
fn get_sdk10_dir ( ) -> Option < ( PathBuf , String ) > {
554
544
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 ( ) ? ;
558
548
let mut dirs = readdir
559
549
. filter_map ( |dir| dir. ok ( ) )
560
550
. map ( |dir| dir. path ( ) )
561
551
. collect :: < Vec < _ > > ( ) ;
562
552
dirs. sort ( ) ;
563
- let dir = otry ! ( dirs
553
+ let dir = dirs
564
554
. into_iter ( )
565
555
. rev ( )
566
556
. filter ( |dir| dir. join ( "um" ) . join ( "x64" ) . join ( "kernel32.lib" ) . is_file ( ) )
567
- . next( ) ) ;
557
+ . next ( ) ? ;
568
558
let version = dir. components ( ) . last ( ) . unwrap ( ) ;
569
559
let version = version. as_os_str ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
570
560
Some ( ( root. into ( ) , version) )
@@ -576,15 +566,15 @@ mod impl_ {
576
566
// instead of user mode applications, we would care.
577
567
fn get_sdk81_dir ( ) -> Option < PathBuf > {
578
568
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 ( ) ? ;
581
571
Some ( root. into ( ) )
582
572
}
583
573
584
574
fn get_sdk8_dir ( ) -> Option < PathBuf > {
585
575
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 ( ) ? ;
588
578
Some ( root. into ( ) )
589
579
}
590
580
0 commit comments