Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 45 additions & 2 deletions crates/metadata/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(missing_docs)]

//! Collect information that allows you to build a crate the same way that docs.rs would.
//!
//! This library is intended for use in docs.rs and crater, but might be helpful to others.
Expand Down Expand Up @@ -93,6 +94,7 @@ pub enum MetadataError {
/// no-default-features = true
/// default-target = "x86_64-unknown-linux-gnu"
/// targets = [ "aarch64-apple-darwin", "x86_64-pc-windows-msvc" ]
/// additional-targets = [ "i686-apple-darwin" ]
/// rustc-args = [ "--example-rustc-arg" ]
/// rustdoc-args = [ "--example-rustdoc-arg" ]
/// ```
Expand Down Expand Up @@ -138,6 +140,10 @@ pub struct Metadata {
/// These cannot be a subcommand, they may only be options.
#[serde(default)]
cargo_args: Vec<String>,

/// List of additional targets to be generated. See [`BuildTargets`].
#[serde(default)]
additional_targets: Vec<String>,
}

/// The targets that should be built for a crate.
Expand Down Expand Up @@ -230,6 +236,9 @@ impl Metadata {
} else {
crate_targets.unwrap_or_default()
};
for additional_target in &self.additional_targets {
targets.insert(additional_target);
}

targets.remove(&default_target);
BuildTargets {
Expand Down Expand Up @@ -525,8 +534,6 @@ mod test_targets {

#[test]
fn test_select_targets() {
use super::BuildTargets;

let mut metadata = Metadata::default();

// unchanged default_target, targets not specified
Expand Down Expand Up @@ -637,6 +644,42 @@ mod test_targets {
assert_eq!(others, tier_one_targets_no_default);
}

#[test]
fn test_additional_targets() {
let mut metadata = Metadata {
targets: Some(
DEFAULT_TARGETS
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>(),
),
..Default::default()
};

let additional_target = "i686-apple-darwin";
metadata.additional_targets = vec![additional_target.to_string()];
let BuildTargets {
other_targets: others,
..
} = metadata.targets(true);

assert!(others.contains(additional_target), "no additional target");
for target in DEFAULT_TARGETS.iter().skip(1) {
assert!(others.contains(target), "missing {target}");
}

// Now we check that `additional_targets` also works if `targets` is set.
let target = "i686-pc-windows-msvc";
metadata.targets = Some(vec![target.to_string()]);
let BuildTargets {
other_targets: others,
default_target: default,
} = metadata.targets(true);
assert_eq!(others.len(), 1);
assert!(others.contains(additional_target));
assert_eq!(default, target);
}

#[test]
fn no_default_targets() {
// if `targets` is unset, `other_targets` should be empty
Expand Down
54 changes: 54 additions & 0 deletions src/docbuilder/rustwide_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,4 +2104,58 @@ mod tests {

Ok(())
}

#[test]
#[ignore]
fn test_additional_targets() {
fn assert_contains(targets: &[String], target: &str) {
assert!(
targets.iter().any(|t| t == target),
"Not found target {target:?} in {targets:?}"
);
}

wrapper(|env| {
let mut builder = RustwideBuilder::init(env)?;
builder.update_toolchain()?;
assert!(
builder
.build_local_package(Path::new("tests/crates/additional-targets"))?
.successful
);

let row = env.runtime().block_on(async {
let mut conn = env.async_db().await.async_conn().await;
sqlx::query!(
r#"SELECT
r.doc_targets
FROM
crates as c
INNER JOIN releases AS r ON c.id = r.crate_id
WHERE
c.name = $1 AND
r.version = $2"#,
"additional-targets",
"0.1.0",
)
.fetch_one(&mut *conn)
.await
})?;

let targets: Vec<String> = row
.doc_targets
.unwrap()
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap().to_owned())
.collect();

assert_contains(&targets, "x86_64-apple-darwin");
// Part of the default targets.
assert_contains(&targets, "aarch64-apple-darwin");

Ok(())
})
}
}
3 changes: 3 additions & 0 deletions templates/core/Cargo.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ default-target = "x86_64-unknown-linux-gnu"
# all tier-one targets will be built and `x86_64-unknown-linux-gnu` will be used as the default target.
targets = ["aarch64-apple-darwin", "x86_64-pc-windows-msvc"]

# If you want to instead add another target without overwriting the default ones:
additional-targets = ["i686-apple-darwin"]

# Additional `RUSTFLAGS` to set (default: [])
rustc-args = ["--example-rustc-arg"]

Expand Down
7 changes: 7 additions & 0 deletions tests/crates/additional-targets/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/crates/additional-targets/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "additional-targets"
version = "0.1.0"
edition = "2024"

[package.metadata.docs.rs]
additional-targets = ["x86_64-apple-darwin"]
1 change: 1 addition & 0 deletions tests/crates/additional-targets/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn boo() {}
Loading