Skip to content

Commit 928e773

Browse files
GuillaumeGomezsyphar
authored andcommitted
Add support for additional-targets in docs.rs config
1 parent 1a9c8e8 commit 928e773

File tree

8 files changed

+141
-3
lines changed

8 files changed

+141
-3
lines changed

.sqlx/query-1fc879cc103367a688f9f7bcc079df8f6419c177167ae0bfe8685033b6b8709e.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-bc0b3932dc2f8bd2b8a9f5a312262eafefd3b80b3322116448901aa55f2d89e7.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/metadata/lib.rs

Lines changed: 45 additions & 2 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.
@@ -230,6 +236,9 @@ impl Metadata {
230236
} else {
231237
crate_targets.unwrap_or_default()
232238
};
239+
for additional_target in &self.additional_targets {
240+
targets.insert(additional_target);
241+
}
233242

234243
targets.remove(&default_target);
235244
BuildTargets {
@@ -525,8 +534,6 @@ mod test_targets {
525534

526535
#[test]
527536
fn test_select_targets() {
528-
use super::BuildTargets;
529-
530537
let mut metadata = Metadata::default();
531538

532539
// unchanged default_target, targets not specified
@@ -637,6 +644,42 @@ mod test_targets {
637644
assert_eq!(others, tier_one_targets_no_default);
638645
}
639646

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

src/docbuilder/rustwide_builder.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,4 +2104,58 @@ 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!(
2113+
targets.iter().any(|t| t == target),
2114+
"Not found target {target:?} in {targets:?}"
2115+
);
2116+
}
2117+
2118+
wrapper(|env| {
2119+
let mut builder = RustwideBuilder::init(env)?;
2120+
builder.update_toolchain()?;
2121+
assert!(
2122+
builder
2123+
.build_local_package(Path::new("tests/crates/additional-targets"))?
2124+
.successful
2125+
);
2126+
2127+
let row = env.runtime().block_on(async {
2128+
let mut conn = env.async_db().await.async_conn().await;
2129+
sqlx::query!(
2130+
r#"SELECT
2131+
r.doc_targets
2132+
FROM
2133+
crates as c
2134+
INNER JOIN releases AS r ON c.id = r.crate_id
2135+
WHERE
2136+
c.name = $1 AND
2137+
r.version = $2"#,
2138+
"additional-targets",
2139+
"0.1.0",
2140+
)
2141+
.fetch_one(&mut *conn)
2142+
.await
2143+
})?;
2144+
2145+
let targets: Vec<String> = row
2146+
.doc_targets
2147+
.unwrap()
2148+
.as_array()
2149+
.unwrap()
2150+
.iter()
2151+
.map(|v| v.as_str().unwrap().to_owned())
2152+
.collect();
2153+
2154+
assert_contains(&targets, "x86_64-apple-darwin");
2155+
// Part of the default targets.
2156+
assert_contains(&targets, "aarch64-apple-darwin");
2157+
2158+
Ok(())
2159+
})
2160+
}
21072161
}

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)