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 {
@@ -411,7 +418,7 @@ mod test_targets {
411
418
let BuildTargets {
412
419
default_target : default,
413
420
other_targets : tier_one,
414
- } = metadata. targets ( ) ;
421
+ } = metadata. targets ( true ) ;
415
422
assert_eq ! ( default , HOST_TARGET ) ;
416
423
417
424
// should be equal to TARGETS \ {HOST_TARGET}
@@ -433,7 +440,7 @@ mod test_targets {
433
440
let BuildTargets {
434
441
default_target : default,
435
442
other_targets : others,
436
- } = metadata. targets ( ) ;
443
+ } = metadata. targets ( true ) ;
437
444
438
445
assert_eq ! ( default , HOST_TARGET ) ;
439
446
assert ! ( others. is_empty( ) ) ;
@@ -447,7 +454,7 @@ mod test_targets {
447
454
let BuildTargets {
448
455
default_target : default,
449
456
other_targets : others,
450
- } = metadata. targets ( ) ;
457
+ } = metadata. targets ( true ) ;
451
458
452
459
assert_eq ! ( default , "i686-pc-windows-msvc" ) ;
453
460
assert_eq ! ( others. len( ) , 1 ) ;
@@ -458,7 +465,7 @@ mod test_targets {
458
465
let BuildTargets {
459
466
default_target : default,
460
467
other_targets : others,
461
- } = metadata. targets ( ) ;
468
+ } = metadata. targets ( true ) ;
462
469
463
470
assert_eq ! ( default , HOST_TARGET ) ;
464
471
assert ! ( others. is_empty( ) ) ;
@@ -472,7 +479,7 @@ mod test_targets {
472
479
let BuildTargets {
473
480
default_target : default,
474
481
other_targets : others,
475
- } = metadata. targets ( ) ;
482
+ } = metadata. targets ( true ) ;
476
483
477
484
assert_eq ! ( default , "i686-pc-windows-msvc" ) ;
478
485
assert ! ( others. is_empty( ) ) ;
@@ -482,7 +489,7 @@ mod test_targets {
482
489
let BuildTargets {
483
490
default_target : default,
484
491
other_targets : others,
485
- } = metadata. targets ( ) ;
492
+ } = metadata. targets ( true ) ;
486
493
487
494
assert_eq ! ( default , "i686-apple-darwin" ) ;
488
495
assert_eq ! ( others. len( ) , 1 ) ;
@@ -493,7 +500,7 @@ mod test_targets {
493
500
let BuildTargets {
494
501
default_target : default,
495
502
other_targets : others,
496
- } = metadata. targets ( ) ;
503
+ } = metadata. targets ( true ) ;
497
504
498
505
assert_eq ! ( default , "i686-apple-darwin" ) ;
499
506
assert ! ( others. is_empty( ) ) ;
@@ -503,7 +510,7 @@ mod test_targets {
503
510
let BuildTargets {
504
511
default_target : default,
505
512
other_targets : others,
506
- } = metadata. targets ( ) ;
513
+ } = metadata. targets ( true ) ;
507
514
508
515
assert_eq ! ( default , "i686-apple-darwin" ) ;
509
516
let tier_one_targets_no_default = DEFAULT_TARGETS
@@ -514,6 +521,17 @@ mod test_targets {
514
521
515
522
assert_eq ! ( others, tier_one_targets_no_default) ;
516
523
}
524
+
525
+ #[ test]
526
+ fn no_default_targets ( ) {
527
+ // if `targets` is unset, `other_targets` should be empty
528
+ let metadata = Metadata :: default ( ) ;
529
+ let BuildTargets {
530
+ other_targets : others,
531
+ ..
532
+ } = metadata. targets ( false ) ;
533
+ assert ! ( others. is_empty( ) , "{:?}" , others) ;
534
+ }
517
535
}
518
536
519
537
#[ cfg( test) ]
0 commit comments