diff --git a/.sqlx/query-1fc879cc103367a688f9f7bcc079df8f6419c177167ae0bfe8685033b6b8709e.json b/.sqlx/query-1fc879cc103367a688f9f7bcc079df8f6419c177167ae0bfe8685033b6b8709e.json new file mode 100644 index 000000000..d5ee6c7da --- /dev/null +++ b/.sqlx/query-1fc879cc103367a688f9f7bcc079df8f6419c177167ae0bfe8685033b6b8709e.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT\n r.doc_targets\n FROM\n crates as c\n INNER JOIN releases AS r ON c.id = r.crate_id\n WHERE\n c.name = $1 AND\n r.version = $2", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "doc_targets", + "type_info": "Json" + } + ], + "parameters": { + "Left": [ + "Text", + "Text" + ] + }, + "nullable": [ + true + ] + }, + "hash": "1fc879cc103367a688f9f7bcc079df8f6419c177167ae0bfe8685033b6b8709e" +} diff --git a/.sqlx/query-bc0b3932dc2f8bd2b8a9f5a312262eafefd3b80b3322116448901aa55f2d89e7.json b/.sqlx/query-bc0b3932dc2f8bd2b8a9f5a312262eafefd3b80b3322116448901aa55f2d89e7.json index d9cbc56c1..847813ccf 100644 --- a/.sqlx/query-bc0b3932dc2f8bd2b8a9f5a312262eafefd3b80b3322116448901aa55f2d89e7.json +++ b/.sqlx/query-bc0b3932dc2f8bd2b8a9f5a312262eafefd3b80b3322116448901aa55f2d89e7.json @@ -56,7 +56,7 @@ true, true, true, - false, + true, null ] }, diff --git a/crates/metadata/lib.rs b/crates/metadata/lib.rs index edc5ba838..90f0e2fb9 100644 --- a/crates/metadata/lib.rs +++ b/crates/metadata/lib.rs @@ -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. @@ -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" ] /// ``` @@ -138,6 +140,10 @@ pub struct Metadata { /// These cannot be a subcommand, they may only be options. #[serde(default)] cargo_args: Vec, + + /// List of additional targets to be generated. See [`BuildTargets`]. + #[serde(default)] + additional_targets: Vec, } /// The targets that should be built for a crate. @@ -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 { @@ -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 @@ -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::>(), + ), + ..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 diff --git a/src/docbuilder/rustwide_builder.rs b/src/docbuilder/rustwide_builder.rs index 6a475dbaf..c69e2c463 100644 --- a/src/docbuilder/rustwide_builder.rs +++ b/src/docbuilder/rustwide_builder.rs @@ -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 = 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(()) + }) + } } diff --git a/templates/core/Cargo.toml.example b/templates/core/Cargo.toml.example index db7dd4cdf..9bc4706d9 100644 --- a/templates/core/Cargo.toml.example +++ b/templates/core/Cargo.toml.example @@ -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"] diff --git a/tests/crates/additional-targets/Cargo.lock b/tests/crates/additional-targets/Cargo.lock new file mode 100644 index 000000000..70058222d --- /dev/null +++ b/tests/crates/additional-targets/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "additional-targets" +version = "0.1.0" diff --git a/tests/crates/additional-targets/Cargo.toml b/tests/crates/additional-targets/Cargo.toml new file mode 100644 index 000000000..8236fe927 --- /dev/null +++ b/tests/crates/additional-targets/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "additional-targets" +version = "0.1.0" +edition = "2024" + +[package.metadata.docs.rs] +additional-targets = ["x86_64-apple-darwin"] diff --git a/tests/crates/additional-targets/src/lib.rs b/tests/crates/additional-targets/src/lib.rs new file mode 100644 index 000000000..84014ad4e --- /dev/null +++ b/tests/crates/additional-targets/src/lib.rs @@ -0,0 +1 @@ +pub fn boo() {}