Skip to content

Commit 86266a6

Browse files
Add support for additional-targets in docs.rs config
1 parent 1a9c8e8 commit 86266a6

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

crates/metadata/lib.rs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(missing_docs)]
2+
23
//! Collect information that allows you to build a crate the same way that docs.rs would.
34
//!
45
//! This library is intended for use in docs.rs and crater, but might be helpful to others.
@@ -138,6 +139,10 @@ pub struct Metadata {
138139
/// These cannot be a subcommand, they may only be options.
139140
#[serde(default)]
140141
cargo_args: Vec<String>,
142+
143+
/// List of additional targets to be generated. See [`BuildTargets`].
144+
#[serde(default)]
145+
additional_targets: Vec<String>,
141146
}
142147

143148
/// The targets that should be built for a crate.
@@ -220,13 +225,22 @@ impl Metadata {
220225
})
221226
.unwrap_or(HOST_TARGET);
222227

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+
});
227235
// Let people opt-in to only having specific targets
228236
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+
})
230244
} else {
231245
crate_targets.unwrap_or_default()
232246
};
@@ -525,8 +539,6 @@ mod test_targets {
525539

526540
#[test]
527541
fn test_select_targets() {
528-
use super::BuildTargets;
529-
530542
let mut metadata = Metadata::default();
531543

532544
// unchanged default_target, targets not specified
@@ -637,6 +649,42 @@ mod test_targets {
637649
assert_eq!(others, tier_one_targets_no_default);
638650
}
639651

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+
640688
#[test]
641689
fn no_default_targets() {
642690
// if `targets` is unset, `other_targets` should be empty

templates/core/Cargo.toml.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ default-target = "x86_64-unknown-linux-gnu"
3636
# all tier-one targets will be built and `x86_64-unknown-linux-gnu` will be used as the default target.
3737
targets = ["aarch64-apple-darwin", "x86_64-pc-windows-msvc"]
3838

39+
# If you want to instead add another target without overwriting the default ones:
40+
additional-targets = ["i686-apple-darwin"]
41+
3942
# Additional `RUSTFLAGS` to set (default: [])
4043
rustc-args = ["--example-rustc-arg"]
4144

0 commit comments

Comments
 (0)