Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support registry dependencies for bindeps #12062

Closed
Closed
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
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 @@ -23,7 +23,7 @@ cargo-util = { version = "0.2.4", path = "crates/cargo-util" }
cargo_metadata = "0.14.0"
clap = "4.2.0"
core-foundation = { version = "0.9.0", features = ["mac_os_10_7_support"] }
crates-io = { version = "0.36.1", path = "crates/crates-io" }
crates-io = { version = "0.37.0", path = "crates/crates-io" }
criterion = { version = "0.3.5", features = ["html_reports"] }
curl = "0.4.44"
curl-sys = "0.4.63"
Expand Down
8 changes: 4 additions & 4 deletions benches/benchsuite/benches/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use benchsuite::fixtures;
use cargo::core::compiler::{CompileKind, RustcTargetData};
use cargo::core::compiler::{CompileKind, RustcTargetData, RustcTargetDataBuilder};
use cargo::core::resolver::features::{FeatureOpts, FeatureResolver};
use cargo::core::resolver::{CliFeatures, ForceAllTargets, HasDevUnits, ResolveBehavior};
use cargo::core::{PackageIdSpec, Workspace};
Expand All @@ -11,7 +11,7 @@ use std::path::Path;
struct ResolveInfo<'cfg> {
ws: Workspace<'cfg>,
requested_kinds: [CompileKind; 1],
target_data: RustcTargetData<'cfg>,
target_data: RustcTargetDataBuilder<'cfg>,
cli_features: CliFeatures,
specs: Vec<PackageIdSpec>,
has_dev_units: HasDevUnits,
Expand All @@ -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
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.36.1"
version = "0.37.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-lang/cargo"
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
}

#[derive(Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/build_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::path::PathBuf;

mod target_info;
pub use self::target_info::{
FileFlavor, FileType, RustDocFingerprint, RustcTargetData, TargetInfo,
FileFlavor, FileType, RustDocFingerprint, RustcTargetData, RustcTargetDataBuilder, TargetInfo,
};

/// The build context, containing complete information needed for a build task
Expand Down
88 changes: 43 additions & 45 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::core::compiler::apply_env_config;
use crate::core::compiler::{
BuildOutput, CompileKind, CompileMode, CompileTarget, Context, CrateType,
};
use crate::core::{Dependency, Package, Target, TargetKind, Workspace};
use crate::core::{Dependency, Target, TargetKind, Workspace};
use crate::util::config::{Config, StringList, TargetConfig};
use crate::util::interning::InternedString;
use crate::util::{CargoResult, Rustc};
Expand All @@ -21,6 +21,7 @@ use cargo_util::{paths, ProcessBuilder};
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::collections::hash_map::{Entry, HashMap};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::str::{self, FromStr};

Expand Down Expand Up @@ -850,6 +851,40 @@ fn rustflags_from_build(config: &Config, flag: Flags) -> CargoResult<Option<Vec<
Ok(list.as_ref().map(|l| l.as_slice().to_vec()))
}

pub struct RustcTargetDataBuilder<'cfg> {
data: RustcTargetData<'cfg>,
}

impl<'cfg> RustcTargetDataBuilder<'cfg> {
/// Insert `kind` into our `target_info` and `target_config` members if it isn't present yet.
pub fn merge_compile_kind(&mut self, kind: CompileKind) -> CargoResult<()> {
if let CompileKind::Target(target) = kind {
if !self.data.target_config.contains_key(&target) {
self.data.target_config
.insert(target, self.data.config.target_cfg_triple(target.short_name())?);
}
if !self.data.target_info.contains_key(&target) {
self.data.target_info.insert(
target,
TargetInfo::new(self.data.config, &self.data.requested_kinds, &self.data.rustc, kind)?,
);
}
}
Ok(())
}

pub fn build(self) -> RustcTargetData<'cfg> {
self.data
}
}

impl<'cfg> Deref for RustcTargetDataBuilder<'cfg> {
type Target = RustcTargetData<'cfg>;
fn deref(&self) -> &Self::Target {
&self.data
}
}

/// Collection of information about `rustc` and the host and target.
pub struct RustcTargetData<'cfg> {
/// Information about `rustc` itself.
Expand All @@ -876,7 +911,7 @@ impl<'cfg> RustcTargetData<'cfg> {
pub fn new(
ws: &Workspace<'cfg>,
requested_kinds: &[CompileKind],
) -> CargoResult<RustcTargetData<'cfg>> {
) -> CargoResult<RustcTargetDataBuilder<'cfg>> {
let config = ws.config();
let rustc = config.load_global_rustc(Some(ws))?;
let mut target_config = HashMap::new();
Expand All @@ -900,7 +935,7 @@ impl<'cfg> RustcTargetData<'cfg> {
target_config.insert(ct, config.target_cfg_triple(&rustc.host)?);
};

let mut res = RustcTargetData {
let data = RustcTargetData {
rustc,
config,
requested_kinds: requested_kinds.into(),
Expand All @@ -910,54 +945,17 @@ impl<'cfg> RustcTargetData<'cfg> {
target_info,
};

// Get all kinds we currently know about.
//
// For now, targets can only ever come from the root workspace
// units and artifact dependencies, so this
// correctly represents all the kinds that can happen. When we have
// other ways for targets to appear at places that are not the root units,
// we may have to revisit this.
fn artifact_targets(package: &Package) -> impl Iterator<Item = CompileKind> + '_ {
package
.manifest()
.dependencies()
.iter()
.filter_map(|d| d.artifact()?.target()?.to_compile_kind())
}
let all_kinds = requested_kinds
.iter()
.copied()
.chain(ws.members().flat_map(|p| {
p.manifest()
.default_kind()
.into_iter()
.chain(p.manifest().forced_kind())
.chain(artifact_targets(p))
}));
for kind in all_kinds {
let mut res = RustcTargetDataBuilder {
data
};

for &kind in requested_kinds {
res.merge_compile_kind(kind)?;
}

Ok(res)
}

/// 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<()> {
if let CompileKind::Target(target) = kind {
if !self.target_config.contains_key(&target) {
self.target_config
.insert(target, self.config.target_cfg_triple(target.short_name())?);
}
if !self.target_info.contains_key(&target) {
self.target_info.insert(
target,
TargetInfo::new(self.config, &self.requested_kinds, &self.rustc, kind)?,
);
}
}
Ok(())
}

/// Returns a "short" name for the given kind, suitable for keying off
/// configuration in Cargo or presenting to users.
pub fn short_name<'a>(&'a self, kind: &'a CompileKind) -> &'a str {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use log::{debug, trace};

pub use self::build_config::{BuildConfig, CompileMode, MessageFormat, TimingOutput};
pub use self::build_context::{
BuildContext, FileFlavor, FileType, RustDocFingerprint, RustcTargetData, TargetInfo,
BuildContext, FileFlavor, FileType, RustDocFingerprint, RustcTargetData, RustcTargetDataBuilder, TargetInfo,
};
use self::build_plan::BuildPlan;
pub use self::compilation::{Compilation, Doctest, UnitOutput};
Expand Down
7 changes: 4 additions & 3 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::core::compiler::unit_dependencies::IsArtifact;
use crate::core::compiler::UnitInterner;
use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit};
use crate::core::compiler::{CompileKind, CompileMode, Unit};
use crate::core::profiles::{Profiles, UnitFor};
use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures};
use crate::core::resolver::HasDevUnits;
Expand All @@ -14,6 +14,7 @@ use std::collections::{HashMap, HashSet};
use std::path::PathBuf;

use super::BuildConfig;
use super::build_context::RustcTargetDataBuilder;

/// Parse the `-Zbuild-std` flag.
pub fn parse_unstable_flag(value: Option<&str>) -> Vec<String> {
Expand Down Expand Up @@ -62,7 +63,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 RustcTargetDataBuilder<'cfg>,
build_config: &BuildConfig,
crates: &[String],
) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> {
Expand Down Expand Up @@ -220,7 +221,7 @@ pub fn generate_std_roots(
Ok(ret)
}

fn detect_sysroot_src_path(target_data: &RustcTargetData<'_>) -> CargoResult<PathBuf> {
fn detect_sysroot_src_path(target_data: &RustcTargetDataBuilder<'_>) -> CargoResult<PathBuf> {
if let Some(s) = target_data.config.get_env_os("__CARGO_TESTS_ONLY_SRC_ROOT") {
return Ok(s.into());
}
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