Skip to content

Commit e2c6a3e

Browse files
committed
Auto merge of #12421 - fee1-dead-contrib:bindeps-registry2, r=ehuss
Support dependencies from registries for artifact dependencies, take 2 This is a continuation of #12062, and closes #10405. r? `@ehuss` Here are things this PR changes: 1. Add information about artifact dependencies to the index. This bumps index_v to 3 for people using bindeps. 2. Make `RustcTargetData` mutable for the feature resolver. This is so that we can query rustc for target info as late as resolving features, as that is when we really know if a package is really going to be used.
2 parents b2c162c + 43dccc7 commit e2c6a3e

File tree

20 files changed

+323
-101
lines changed

20 files changed

+323
-101
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cargo-util = { version = "0.2.6", path = "crates/cargo-util" }
3131
cargo_metadata = "0.14.0"
3232
clap = "4.3.23"
3333
core-foundation = { version = "0.9.3", features = ["mac_os_10_7_support"] }
34-
crates-io = { version = "0.38.0", path = "crates/crates-io" }
34+
crates-io = { version = "0.39.0", path = "crates/crates-io" }
3535
criterion = { version = "0.5.1", features = ["html_reports"] }
3636
curl = "0.4.44"
3737
curl-sys = "0.4.65"

benches/benchsuite/benches/resolve.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct ResolveInfo<'cfg> {
2525
fn do_resolve<'cfg>(config: &'cfg Config, ws_root: &Path) -> ResolveInfo<'cfg> {
2626
let requested_kinds = [CompileKind::Host];
2727
let ws = Workspace::new(&ws_root.join("Cargo.toml"), config).unwrap();
28-
let target_data = RustcTargetData::new(&ws, &requested_kinds).unwrap();
28+
let mut target_data = RustcTargetData::new(&ws, &requested_kinds).unwrap();
2929
let cli_features = CliFeatures::from_command_line(&[], false, true).unwrap();
3030
let pkgs = cargo::ops::Packages::Default;
3131
let specs = pkgs.to_package_id_specs(&ws).unwrap();
@@ -35,7 +35,7 @@ fn do_resolve<'cfg>(config: &'cfg Config, ws_root: &Path) -> ResolveInfo<'cfg> {
3535
// not confuse criterion's warmup.
3636
let ws_resolve = cargo::ops::resolve_ws_with_opts(
3737
&ws,
38-
&target_data,
38+
&mut target_data,
3939
&requested_kinds,
4040
&cli_features,
4141
&specs,

crates/cargo-test-support/src/registry.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,9 @@ pub struct Dependency {
549549
name: String,
550550
vers: String,
551551
kind: String,
552-
artifact: Option<(String, Option<String>)>,
552+
artifact: Option<String>,
553+
bindep_target: Option<String>,
554+
lib: bool,
553555
target: Option<String>,
554556
features: Vec<String>,
555557
registry: Option<String>,
@@ -1409,13 +1411,20 @@ impl Package {
14091411
(true, Some("alternative")) => None,
14101412
_ => panic!("registry_dep currently only supports `alternative`"),
14111413
};
1414+
let artifact = if let Some(artifact) = &dep.artifact {
1415+
serde_json::json!([artifact])
1416+
} else {
1417+
serde_json::json!(null)
1418+
};
14121419
serde_json::json!({
14131420
"name": dep.name,
14141421
"req": dep.vers,
14151422
"features": dep.features,
14161423
"default_features": true,
14171424
"target": dep.target,
1418-
"artifact": dep.artifact,
1425+
"artifact": artifact,
1426+
"bindep_target": dep.bindep_target,
1427+
"lib": dep.lib,
14191428
"optional": dep.optional,
14201429
"kind": dep.kind,
14211430
"registry": registry_url,
@@ -1536,11 +1545,14 @@ impl Package {
15361545
"#,
15371546
target, kind, dep.name, dep.vers
15381547
));
1539-
if let Some((artifact, target)) = &dep.artifact {
1548+
if let Some(artifact) = &dep.artifact {
15401549
manifest.push_str(&format!("artifact = \"{}\"\n", artifact));
1541-
if let Some(target) = &target {
1542-
manifest.push_str(&format!("target = \"{}\"\n", target))
1543-
}
1550+
}
1551+
if let Some(target) = &dep.bindep_target {
1552+
manifest.push_str(&format!("target = \"{}\"\n", target));
1553+
}
1554+
if dep.lib {
1555+
manifest.push_str("lib = true\n");
15441556
}
15451557
if let Some(registry) = &dep.registry {
15461558
assert_eq!(registry, "alternative");
@@ -1617,6 +1629,8 @@ impl Dependency {
16171629
vers: vers.to_string(),
16181630
kind: "normal".to_string(),
16191631
artifact: None,
1632+
bindep_target: None,
1633+
lib: false,
16201634
target: None,
16211635
features: Vec::new(),
16221636
package: None,
@@ -1646,7 +1660,8 @@ impl Dependency {
16461660
/// Change the artifact to be of the given kind, like "bin", or "staticlib",
16471661
/// along with a specific target triple if provided.
16481662
pub fn artifact(&mut self, kind: &str, target: Option<String>) -> &mut Self {
1649-
self.artifact = Some((kind.to_string(), target));
1663+
self.artifact = Some(kind.to_string());
1664+
self.bindep_target = target;
16501665
self
16511666
}
16521667

crates/crates-io/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "crates-io"
3-
version = "0.38.1"
3+
version = "0.39.0"
44
rust-version.workspace = true
55
edition.workspace = true
66
license.workspace = true

crates/crates-io/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ pub struct NewCrateDependency {
7373
pub registry: Option<String>,
7474
#[serde(skip_serializing_if = "Option::is_none")]
7575
pub explicit_name_in_toml: Option<String>,
76+
#[serde(skip_serializing_if = "Option::is_none")]
77+
pub artifact: Option<Vec<String>>,
78+
#[serde(skip_serializing_if = "Option::is_none")]
79+
pub bindep_target: Option<String>,
80+
#[serde(default, skip_serializing_if = "is_false")]
81+
pub lib: bool,
82+
}
83+
84+
fn is_false(x: &bool) -> bool {
85+
*x == false
7686
}
7787

7888
#[derive(Deserialize)]

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ impl<'cfg> RustcTargetData<'cfg> {
948948
}
949949

950950
/// Insert `kind` into our `target_info` and `target_config` members if it isn't present yet.
951-
fn merge_compile_kind(&mut self, kind: CompileKind) -> CargoResult<()> {
951+
pub fn merge_compile_kind(&mut self, kind: CompileKind) -> CargoResult<()> {
952952
if let CompileKind::Target(target) = kind {
953953
if !self.target_config.contains_key(&target) {
954954
self.target_config

src/cargo/core/compiler/standard_lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(crate) fn std_crates(config: &Config, units: Option<&[Unit]>) -> Option<Vec<
6262
/// Resolve the standard library dependencies.
6363
pub fn resolve_std<'cfg>(
6464
ws: &Workspace<'cfg>,
65-
target_data: &RustcTargetData<'cfg>,
65+
target_data: &mut RustcTargetData<'cfg>,
6666
build_config: &BuildConfig,
6767
crates: &[String],
6868
) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> {

src/cargo/core/dependency.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::core::compiler::{CompileKind, CompileTarget};
1212
use crate::core::{PackageId, SourceId, Summary};
1313
use crate::util::errors::CargoResult;
1414
use crate::util::interning::InternedString;
15-
use crate::util::toml::StringOrVec;
1615
use crate::util::OptVersionReq;
1716

1817
/// Information about a dependency requested by a Cargo manifest.
@@ -468,25 +467,22 @@ impl ser::Serialize for Artifact {
468467
SerializedArtifact {
469468
kinds: self.kinds(),
470469
lib: self.is_lib,
471-
target: self.target.as_ref().map(|t| match t {
472-
ArtifactTarget::BuildDependencyAssumeTarget => "target",
473-
ArtifactTarget::Force(target) => target.rustc_target().as_str(),
474-
}),
470+
target: self.target.as_ref().map(ArtifactTarget::as_str),
475471
}
476472
.serialize(s)
477473
}
478474
}
479475

480476
impl Artifact {
481477
pub(crate) fn parse(
482-
artifacts: &StringOrVec,
478+
artifacts: &[impl AsRef<str>],
483479
is_lib: bool,
484480
target: Option<&str>,
485481
) -> CargoResult<Self> {
486482
let kinds = ArtifactKind::validate(
487483
artifacts
488484
.iter()
489-
.map(|s| ArtifactKind::parse(s))
485+
.map(|s| ArtifactKind::parse(s.as_ref()))
490486
.collect::<Result<Vec<_>, _>>()?,
491487
)?;
492488
Ok(Artifact {
@@ -529,6 +525,13 @@ impl ArtifactTarget {
529525
})
530526
}
531527

528+
pub fn as_str(&self) -> &str {
529+
match self {
530+
ArtifactTarget::BuildDependencyAssumeTarget => "target",
531+
ArtifactTarget::Force(target) => target.rustc_target().as_str(),
532+
}
533+
}
534+
532535
pub fn to_compile_kind(&self) -> Option<CompileKind> {
533536
self.to_compile_target().map(CompileKind::Target)
534537
}
@@ -539,6 +542,7 @@ impl ArtifactTarget {
539542
ArtifactTarget::Force(target) => Some(*target),
540543
}
541544
}
545+
542546
pub(crate) fn to_resolved_compile_kind(
543547
&self,
544548
root_unit_compile_kind: CompileKind,
@@ -575,20 +579,13 @@ impl ser::Serialize for ArtifactKind {
575579
where
576580
S: ser::Serializer,
577581
{
578-
let out: Cow<'_, str> = match *self {
579-
ArtifactKind::SelectedBinary(name) => format!("bin:{}", name.as_str()).into(),
580-
_ => self.crate_type().into(),
581-
};
582-
out.serialize(s)
582+
self.as_str().serialize(s)
583583
}
584584
}
585585

586586
impl fmt::Display for ArtifactKind {
587587
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
588-
f.write_str(match self {
589-
ArtifactKind::SelectedBinary(bin_name) => return write!(f, "bin:{bin_name}"),
590-
_ => self.crate_type(),
591-
})
588+
f.write_str(&self.as_str())
592589
}
593590
}
594591

@@ -604,7 +601,14 @@ impl ArtifactKind {
604601
}
605602
}
606603

607-
fn parse(kind: &str) -> CargoResult<Self> {
604+
pub fn as_str(&self) -> Cow<'static, str> {
605+
match *self {
606+
ArtifactKind::SelectedBinary(name) => format!("bin:{}", name.as_str()).into(),
607+
_ => self.crate_type().into(),
608+
}
609+
}
610+
611+
pub fn parse(kind: &str) -> CargoResult<Self> {
608612
Ok(match kind {
609613
"bin" => ArtifactKind::AllBinaries,
610614
"cdylib" => ArtifactKind::Cdylib,

0 commit comments

Comments
 (0)