Skip to content

Commit a62a805

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

File tree

6 files changed

+126
-7
lines changed

6 files changed

+126
-7
lines changed

crates/metadata/lib.rs

Lines changed: 57 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.
@@ -93,6 +94,7 @@ pub enum MetadataError {
9394
/// no-default-features = true
9495
/// default-target = "x86_64-unknown-linux-gnu"
9596
/// targets = [ "aarch64-apple-darwin", "x86_64-pc-windows-msvc" ]
97+
/// additional-targets = [ "i686-apple-darwin" ]
9698
/// rustc-args = [ "--example-rustc-arg" ]
9799
/// rustdoc-args = [ "--example-rustdoc-arg" ]
98100
/// ```
@@ -138,6 +140,10 @@ pub struct Metadata {
138140
/// These cannot be a subcommand, they may only be options.
139141
#[serde(default)]
140142
cargo_args: Vec<String>,
143+
144+
/// List of additional targets to be generated. See [`BuildTargets`].
145+
#[serde(default)]
146+
additional_targets: Vec<String>,
141147
}
142148

143149
/// The targets that should be built for a crate.
@@ -220,16 +226,26 @@ impl Metadata {
220226
})
221227
.unwrap_or(HOST_TARGET);
222228

223-
let crate_targets = self
224-
.targets
225-
.as_ref()
226-
.map(|targets| targets.iter().map(String::as_str).collect());
229+
let crate_targets = self.targets.as_ref().map(|targets| {
230+
targets
231+
.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+
.collect()
242+
})
230243
} else {
231244
crate_targets.unwrap_or_default()
232245
};
246+
for additional_target in &self.additional_targets {
247+
targets.insert(additional_target);
248+
}
233249

234250
targets.remove(&default_target);
235251
BuildTargets {
@@ -525,8 +541,6 @@ mod test_targets {
525541

526542
#[test]
527543
fn test_select_targets() {
528-
use super::BuildTargets;
529-
530544
let mut metadata = Metadata::default();
531545

532546
// unchanged default_target, targets not specified
@@ -637,6 +651,42 @@ mod test_targets {
637651
assert_eq!(others, tier_one_targets_no_default);
638652
}
639653

654+
#[test]
655+
fn test_additional_targets() {
656+
let mut metadata = Metadata {
657+
targets: Some(
658+
DEFAULT_TARGETS
659+
.iter()
660+
.map(|s| s.to_string())
661+
.collect::<Vec<_>>(),
662+
),
663+
..Default::default()
664+
};
665+
666+
let additional_target = "i686-apple-darwin";
667+
metadata.additional_targets = vec![additional_target.to_string()];
668+
let BuildTargets {
669+
other_targets: others,
670+
..
671+
} = metadata.targets(true);
672+
673+
assert!(others.contains(additional_target), "no additional target");
674+
for target in DEFAULT_TARGETS.iter().skip(1) {
675+
assert!(others.contains(target), "missing {target}");
676+
}
677+
678+
// Now we check that `additional_targets` also works if `targets` is set.
679+
let target = "i686-pc-windows-msvc";
680+
metadata.targets = Some(vec![target.to_string()]);
681+
let BuildTargets {
682+
other_targets: others,
683+
default_target: default,
684+
} = metadata.targets(true);
685+
assert_eq!(others.len(), 1);
686+
assert!(others.contains(additional_target));
687+
assert_eq!(default, target);
688+
}
689+
640690
#[test]
641691
fn no_default_targets() {
642692
// if `targets` is unset, `other_targets` should be empty

src/docbuilder/rustwide_builder.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,4 +2104,55 @@ mod tests {
21042104

21052105
Ok(())
21062106
}
2107+
2108+
#[test]
2109+
#[ignore]
2110+
fn test_additional_targets() {
2111+
fn assert_contains(targets: &[String], target: &str) {
2112+
assert!(targets.iter().any(|t| t == target), "Not found target {target:?} in {targets:?}");
2113+
}
2114+
2115+
wrapper(|env| {
2116+
let mut builder = RustwideBuilder::init(env)?;
2117+
builder.update_toolchain()?;
2118+
assert!(
2119+
builder
2120+
.build_local_package(Path::new("tests/crates/additional-targets"))?
2121+
.successful
2122+
);
2123+
2124+
let row = env.runtime().block_on(async {
2125+
let mut conn = env.async_db().await.async_conn().await;
2126+
sqlx::query!(
2127+
r#"SELECT
2128+
r.doc_targets
2129+
FROM
2130+
crates as c
2131+
INNER JOIN releases AS r ON c.id = r.crate_id
2132+
WHERE
2133+
c.name = $1 AND
2134+
r.version = $2"#,
2135+
"additional-targets",
2136+
"0.1.0",
2137+
)
2138+
.fetch_one(&mut *conn)
2139+
.await
2140+
})?;
2141+
2142+
let targets: Vec<String> = row
2143+
.doc_targets
2144+
.unwrap()
2145+
.as_array()
2146+
.unwrap()
2147+
.iter()
2148+
.map(|v| v.as_str().unwrap().to_owned())
2149+
.collect();
2150+
2151+
assert_contains(&targets, "x86_64-apple-darwin");
2152+
// Part of the default targets.
2153+
assert_contains(&targets, "aarch64-apple-darwin");
2154+
2155+
Ok(())
2156+
})
2157+
}
21072158
}

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

tests/crates/additional-targets/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "additional-targets"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[package.metadata.docs.rs]
7+
additional-targets = ["x86_64-apple-darwin"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn boo() {}

0 commit comments

Comments
 (0)