|
1 | 1 | #![warn(missing_docs)] |
| 2 | + |
2 | 3 | //! Collect information that allows you to build a crate the same way that docs.rs would. |
3 | 4 | //! |
4 | 5 | //! This library is intended for use in docs.rs and crater, but might be helpful to others. |
@@ -138,6 +139,10 @@ pub struct Metadata { |
138 | 139 | /// These cannot be a subcommand, they may only be options. |
139 | 140 | #[serde(default)] |
140 | 141 | cargo_args: Vec<String>, |
| 142 | + |
| 143 | + /// List of additional targets to be generated. See [`BuildTargets`]. |
| 144 | + #[serde(default)] |
| 145 | + additional_targets: Vec<String>, |
141 | 146 | } |
142 | 147 |
|
143 | 148 | /// The targets that should be built for a crate. |
@@ -220,13 +225,22 @@ impl Metadata { |
220 | 225 | }) |
221 | 226 | .unwrap_or(HOST_TARGET); |
222 | 227 |
|
223 | | - let crate_targets = self |
224 | | - .targets |
225 | | - .as_ref() |
226 | | - .map(|targets| targets.iter().map(String::as_str).collect()); |
| 228 | + let crate_targets = self.targets.as_ref().map(|targets| { |
| 229 | + targets |
| 230 | + .iter() |
| 231 | + .chain(self.additional_targets.iter()) |
| 232 | + .map(String::as_str) |
| 233 | + .collect() |
| 234 | + }); |
227 | 235 | // Let people opt-in to only having specific targets |
228 | 236 | let mut targets: HashSet<_> = if include_default_targets { |
229 | | - crate_targets.unwrap_or_else(|| DEFAULT_TARGETS.iter().copied().collect()) |
| 237 | + crate_targets.unwrap_or_else(|| { |
| 238 | + DEFAULT_TARGETS |
| 239 | + .iter() |
| 240 | + .copied() |
| 241 | + .chain(self.additional_targets.iter().map(String::as_str)) |
| 242 | + .collect() |
| 243 | + }) |
230 | 244 | } else { |
231 | 245 | crate_targets.unwrap_or_default() |
232 | 246 | }; |
@@ -525,8 +539,6 @@ mod test_targets { |
525 | 539 |
|
526 | 540 | #[test] |
527 | 541 | fn test_select_targets() { |
528 | | - use super::BuildTargets; |
529 | | - |
530 | 542 | let mut metadata = Metadata::default(); |
531 | 543 |
|
532 | 544 | // unchanged default_target, targets not specified |
@@ -637,6 +649,42 @@ mod test_targets { |
637 | 649 | assert_eq!(others, tier_one_targets_no_default); |
638 | 650 | } |
639 | 651 |
|
| 652 | + #[test] |
| 653 | + fn test_additional_targets() { |
| 654 | + let mut metadata = Metadata { |
| 655 | + targets: Some( |
| 656 | + DEFAULT_TARGETS |
| 657 | + .iter() |
| 658 | + .map(|s| s.to_string()) |
| 659 | + .collect::<Vec<_>>(), |
| 660 | + ), |
| 661 | + ..Default::default() |
| 662 | + }; |
| 663 | + |
| 664 | + let additional_target = "i686-apple-darwin"; |
| 665 | + metadata.additional_targets = vec![additional_target.to_string()]; |
| 666 | + let BuildTargets { |
| 667 | + other_targets: others, |
| 668 | + .. |
| 669 | + } = metadata.targets(true); |
| 670 | + |
| 671 | + assert!(others.contains(additional_target), "no additional target"); |
| 672 | + for target in DEFAULT_TARGETS.iter().skip(1) { |
| 673 | + assert!(others.contains(target), "missing {target}"); |
| 674 | + } |
| 675 | + |
| 676 | + // Now we check that `additional_targets` also works if `targets` is set. |
| 677 | + let target = "i686-pc-windows-msvc"; |
| 678 | + metadata.targets = Some(vec![target.to_string()]); |
| 679 | + let BuildTargets { |
| 680 | + other_targets: others, |
| 681 | + default_target: default, |
| 682 | + } = metadata.targets(true); |
| 683 | + assert_eq!(others.len(), 1); |
| 684 | + assert!(others.contains(additional_target)); |
| 685 | + assert_eq!(default, target); |
| 686 | + } |
| 687 | + |
640 | 688 | #[test] |
641 | 689 | fn no_default_targets() { |
642 | 690 | // if `targets` is unset, `other_targets` should be empty |
|
0 commit comments