Skip to content

Commit

Permalink
Auto merge of #12421 - fee1-dead-contrib:bindeps-registry2, r=ehuss
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Aug 25, 2023
2 parents b2c162c + 43dccc7 commit e2c6a3e
Show file tree
Hide file tree
Showing 20 changed files with 323 additions and 101 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ cargo-util = { version = "0.2.6", path = "crates/cargo-util" }
cargo_metadata = "0.14.0"
clap = "4.3.23"
core-foundation = { version = "0.9.3", features = ["mac_os_10_7_support"] }
crates-io = { version = "0.38.0", path = "crates/crates-io" }
crates-io = { version = "0.39.0", path = "crates/crates-io" }
criterion = { version = "0.5.1", features = ["html_reports"] }
curl = "0.4.44"
curl-sys = "0.4.65"
Expand Down
4 changes: 2 additions & 2 deletions benches/benchsuite/benches/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct ResolveInfo<'cfg> {
fn do_resolve<'cfg>(config: &'cfg Config, ws_root: &Path) -> ResolveInfo<'cfg> {
let requested_kinds = [CompileKind::Host];
let ws = Workspace::new(&ws_root.join("Cargo.toml"), config).unwrap();
let target_data = RustcTargetData::new(&ws, &requested_kinds).unwrap();
let mut target_data = RustcTargetData::new(&ws, &requested_kinds).unwrap();
let cli_features = CliFeatures::from_command_line(&[], false, true).unwrap();
let pkgs = cargo::ops::Packages::Default;
let specs = pkgs.to_package_id_specs(&ws).unwrap();
Expand All @@ -35,7 +35,7 @@ fn do_resolve<'cfg>(config: &'cfg Config, ws_root: &Path) -> ResolveInfo<'cfg> {
// not confuse criterion's warmup.
let ws_resolve = cargo::ops::resolve_ws_with_opts(
&ws,
&target_data,
&mut target_data,
&requested_kinds,
&cli_features,
&specs,
Expand Down
29 changes: 22 additions & 7 deletions crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,9 @@ pub struct Dependency {
name: String,
vers: String,
kind: String,
artifact: Option<(String, Option<String>)>,
artifact: Option<String>,
bindep_target: Option<String>,
lib: bool,
target: Option<String>,
features: Vec<String>,
registry: Option<String>,
Expand Down Expand Up @@ -1409,13 +1411,20 @@ impl Package {
(true, Some("alternative")) => None,
_ => panic!("registry_dep currently only supports `alternative`"),
};
let artifact = if let Some(artifact) = &dep.artifact {
serde_json::json!([artifact])
} else {
serde_json::json!(null)
};
serde_json::json!({
"name": dep.name,
"req": dep.vers,
"features": dep.features,
"default_features": true,
"target": dep.target,
"artifact": dep.artifact,
"artifact": artifact,
"bindep_target": dep.bindep_target,
"lib": dep.lib,
"optional": dep.optional,
"kind": dep.kind,
"registry": registry_url,
Expand Down Expand Up @@ -1536,11 +1545,14 @@ impl Package {
"#,
target, kind, dep.name, dep.vers
));
if let Some((artifact, target)) = &dep.artifact {
if let Some(artifact) = &dep.artifact {
manifest.push_str(&format!("artifact = \"{}\"\n", artifact));
if let Some(target) = &target {
manifest.push_str(&format!("target = \"{}\"\n", target))
}
}
if let Some(target) = &dep.bindep_target {
manifest.push_str(&format!("target = \"{}\"\n", target));
}
if dep.lib {
manifest.push_str("lib = true\n");
}
if let Some(registry) = &dep.registry {
assert_eq!(registry, "alternative");
Expand Down Expand Up @@ -1617,6 +1629,8 @@ impl Dependency {
vers: vers.to_string(),
kind: "normal".to_string(),
artifact: None,
bindep_target: None,
lib: false,
target: None,
features: Vec::new(),
package: None,
Expand Down Expand Up @@ -1646,7 +1660,8 @@ impl Dependency {
/// Change the artifact to be of the given kind, like "bin", or "staticlib",
/// along with a specific target triple if provided.
pub fn artifact(&mut self, kind: &str, target: Option<String>) -> &mut Self {
self.artifact = Some((kind.to_string(), target));
self.artifact = Some(kind.to_string());
self.bindep_target = target;
self
}

Expand Down
2 changes: 1 addition & 1 deletion crates/crates-io/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "crates-io"
version = "0.38.1"
version = "0.39.0"
rust-version.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
10 changes: 10 additions & 0 deletions crates/crates-io/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ pub struct NewCrateDependency {
pub registry: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub explicit_name_in_toml: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub artifact: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bindep_target: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub lib: bool,
}

fn is_false(x: &bool) -> bool {
*x == false
}

#[derive(Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ impl<'cfg> RustcTargetData<'cfg> {
}

/// Insert `kind` into our `target_info` and `target_config` members if it isn't present yet.
fn merge_compile_kind(&mut self, kind: CompileKind) -> CargoResult<()> {
pub fn merge_compile_kind(&mut self, kind: CompileKind) -> CargoResult<()> {
if let CompileKind::Target(target) = kind {
if !self.target_config.contains_key(&target) {
self.target_config
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub(crate) fn std_crates(config: &Config, units: Option<&[Unit]>) -> Option<Vec<
/// Resolve the standard library dependencies.
pub fn resolve_std<'cfg>(
ws: &Workspace<'cfg>,
target_data: &RustcTargetData<'cfg>,
target_data: &mut RustcTargetData<'cfg>,
build_config: &BuildConfig,
crates: &[String],
) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> {
Expand Down
38 changes: 21 additions & 17 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::core::compiler::{CompileKind, CompileTarget};
use crate::core::{PackageId, SourceId, Summary};
use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;
use crate::util::toml::StringOrVec;
use crate::util::OptVersionReq;

/// Information about a dependency requested by a Cargo manifest.
Expand Down Expand Up @@ -468,25 +467,22 @@ impl ser::Serialize for Artifact {
SerializedArtifact {
kinds: self.kinds(),
lib: self.is_lib,
target: self.target.as_ref().map(|t| match t {
ArtifactTarget::BuildDependencyAssumeTarget => "target",
ArtifactTarget::Force(target) => target.rustc_target().as_str(),
}),
target: self.target.as_ref().map(ArtifactTarget::as_str),
}
.serialize(s)
}
}

impl Artifact {
pub(crate) fn parse(
artifacts: &StringOrVec,
artifacts: &[impl AsRef<str>],
is_lib: bool,
target: Option<&str>,
) -> CargoResult<Self> {
let kinds = ArtifactKind::validate(
artifacts
.iter()
.map(|s| ArtifactKind::parse(s))
.map(|s| ArtifactKind::parse(s.as_ref()))
.collect::<Result<Vec<_>, _>>()?,
)?;
Ok(Artifact {
Expand Down Expand Up @@ -529,6 +525,13 @@ impl ArtifactTarget {
})
}

pub fn as_str(&self) -> &str {
match self {
ArtifactTarget::BuildDependencyAssumeTarget => "target",
ArtifactTarget::Force(target) => target.rustc_target().as_str(),
}
}

pub fn to_compile_kind(&self) -> Option<CompileKind> {
self.to_compile_target().map(CompileKind::Target)
}
Expand All @@ -539,6 +542,7 @@ impl ArtifactTarget {
ArtifactTarget::Force(target) => Some(*target),
}
}

pub(crate) fn to_resolved_compile_kind(
&self,
root_unit_compile_kind: CompileKind,
Expand Down Expand Up @@ -575,20 +579,13 @@ impl ser::Serialize for ArtifactKind {
where
S: ser::Serializer,
{
let out: Cow<'_, str> = match *self {
ArtifactKind::SelectedBinary(name) => format!("bin:{}", name.as_str()).into(),
_ => self.crate_type().into(),
};
out.serialize(s)
self.as_str().serialize(s)
}
}

impl fmt::Display for ArtifactKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
ArtifactKind::SelectedBinary(bin_name) => return write!(f, "bin:{bin_name}"),
_ => self.crate_type(),
})
f.write_str(&self.as_str())
}
}

Expand All @@ -604,7 +601,14 @@ impl ArtifactKind {
}
}

fn parse(kind: &str) -> CargoResult<Self> {
pub fn as_str(&self) -> Cow<'static, str> {
match *self {
ArtifactKind::SelectedBinary(name) => format!("bin:{}", name.as_str()).into(),
_ => self.crate_type().into(),
}
}

pub fn parse(kind: &str) -> CargoResult<Self> {
Ok(match kind {
"bin" => ArtifactKind::AllBinaries,
"cdylib" => ArtifactKind::Cdylib,
Expand Down
Loading

0 comments on commit e2c6a3e

Please sign in to comment.