20
20
//! let metadata = Metadata::from_crate_root(&source_root)?;
21
21
//!
22
22
//! // Next, learn what arguments we need to pass to `cargo`.
23
- //! let targets = metadata.targets();
23
+ //! let targets = metadata.targets(/* include_default_targets: */ true );
24
24
//! let mut cargo_args = metadata.cargo_args(&[], &[]);
25
25
//! cargo_args.push(targets.default_target.into());
26
26
//!
@@ -190,7 +190,10 @@ impl Metadata {
190
190
/// Return the targets that should be built.
191
191
///
192
192
/// The `default_target` will never be one of the `other_targets`.
193
- pub fn targets ( & self ) -> BuildTargets < ' _ > {
193
+ /// If `include_default_targets` is `true` and `targets` is unset, this also includes
194
+ /// [`DEFAULT_TARGETS`]. Otherwise, if `include_default_targets` is `false` and `targets`
195
+ /// is unset, `other_targets` will be empty.
196
+ pub fn targets ( & self , include_default_targets : bool ) -> BuildTargets < ' _ > {
194
197
let default_target = self
195
198
. default_target
196
199
. as_deref ( )
@@ -202,12 +205,16 @@ impl Metadata {
202
205
} )
203
206
. unwrap_or ( HOST_TARGET ) ;
204
207
205
- // Let people opt-in to only having specific targets
206
- let mut targets: HashSet < _ > = self
208
+ let crate_targets = self
207
209
. targets
208
210
. as_ref ( )
209
- . map ( |targets| targets. iter ( ) . map ( String :: as_str) . collect ( ) )
210
- . unwrap_or_else ( || DEFAULT_TARGETS . iter ( ) . copied ( ) . collect ( ) ) ;
211
+ . map ( |targets| targets. iter ( ) . map ( String :: as_str) . collect ( ) ) ;
212
+ // Let people opt-in to only having specific targets
213
+ let mut targets: HashSet < _ > = if include_default_targets {
214
+ crate_targets. unwrap_or_else ( || DEFAULT_TARGETS . iter ( ) . copied ( ) . collect ( ) )
215
+ } else {
216
+ crate_targets. unwrap_or_default ( )
217
+ } ;
211
218
212
219
targets. remove ( & default_target) ;
213
220
BuildTargets {
@@ -412,7 +419,7 @@ mod test_targets {
412
419
let BuildTargets {
413
420
default_target : default,
414
421
other_targets : tier_one,
415
- } = metadata. targets ( ) ;
422
+ } = metadata. targets ( true ) ;
416
423
assert_eq ! ( default , HOST_TARGET ) ;
417
424
418
425
// should be equal to TARGETS \ {HOST_TARGET}
@@ -434,7 +441,7 @@ mod test_targets {
434
441
let BuildTargets {
435
442
default_target : default,
436
443
other_targets : others,
437
- } = metadata. targets ( ) ;
444
+ } = metadata. targets ( true ) ;
438
445
439
446
assert_eq ! ( default , HOST_TARGET ) ;
440
447
assert ! ( others. is_empty( ) ) ;
@@ -448,7 +455,7 @@ mod test_targets {
448
455
let BuildTargets {
449
456
default_target : default,
450
457
other_targets : others,
451
- } = metadata. targets ( ) ;
458
+ } = metadata. targets ( true ) ;
452
459
453
460
assert_eq ! ( default , "i686-pc-windows-msvc" ) ;
454
461
assert_eq ! ( others. len( ) , 1 ) ;
@@ -459,7 +466,7 @@ mod test_targets {
459
466
let BuildTargets {
460
467
default_target : default,
461
468
other_targets : others,
462
- } = metadata. targets ( ) ;
469
+ } = metadata. targets ( true ) ;
463
470
464
471
assert_eq ! ( default , HOST_TARGET ) ;
465
472
assert ! ( others. is_empty( ) ) ;
@@ -473,7 +480,7 @@ mod test_targets {
473
480
let BuildTargets {
474
481
default_target : default,
475
482
other_targets : others,
476
- } = metadata. targets ( ) ;
483
+ } = metadata. targets ( true ) ;
477
484
478
485
assert_eq ! ( default , "i686-pc-windows-msvc" ) ;
479
486
assert ! ( others. is_empty( ) ) ;
@@ -483,7 +490,7 @@ mod test_targets {
483
490
let BuildTargets {
484
491
default_target : default,
485
492
other_targets : others,
486
- } = metadata. targets ( ) ;
493
+ } = metadata. targets ( true ) ;
487
494
488
495
assert_eq ! ( default , "i686-apple-darwin" ) ;
489
496
assert_eq ! ( others. len( ) , 1 ) ;
@@ -494,7 +501,7 @@ mod test_targets {
494
501
let BuildTargets {
495
502
default_target : default,
496
503
other_targets : others,
497
- } = metadata. targets ( ) ;
504
+ } = metadata. targets ( true ) ;
498
505
499
506
assert_eq ! ( default , "i686-apple-darwin" ) ;
500
507
assert ! ( others. is_empty( ) ) ;
@@ -504,7 +511,7 @@ mod test_targets {
504
511
let BuildTargets {
505
512
default_target : default,
506
513
other_targets : others,
507
- } = metadata. targets ( ) ;
514
+ } = metadata. targets ( true ) ;
508
515
509
516
assert_eq ! ( default , "i686-apple-darwin" ) ;
510
517
let tier_one_targets_no_default = DEFAULT_TARGETS
@@ -515,6 +522,17 @@ mod test_targets {
515
522
516
523
assert_eq ! ( others, tier_one_targets_no_default) ;
517
524
}
525
+
526
+ #[ test]
527
+ fn no_default_targets ( ) {
528
+ // if `targets` is unset, `other_targets` should be empty
529
+ let metadata = Metadata :: default ( ) ;
530
+ let BuildTargets {
531
+ other_targets : others,
532
+ ..
533
+ } = metadata. targets ( false ) ;
534
+ assert ! ( others. is_empty( ) , "{:?}" , others) ;
535
+ }
518
536
}
519
537
520
538
#[ cfg( test) ]
0 commit comments