Skip to content

Commit

Permalink
use RustcTargetDataBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed Jun 6, 2023
1 parent 2273009 commit c53917a
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 36 deletions.
4 changes: 2 additions & 2 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 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
60 changes: 41 additions & 19 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,30 +945,17 @@ impl<'cfg> RustcTargetData<'cfg> {
target_info,
};

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.
pub(crate) 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: &mut 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
14 changes: 8 additions & 6 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use log::{debug, warn};
use semver::Version;
use serde::Serialize;

use crate::core::compiler::{CompileKind, RustcTargetData};
use crate::core::compiler::CompileKind;
use crate::core::dependency::DepKind;
use crate::core::resolver::features::ForceAllTargets;
use crate::core::resolver::{HasDevUnits, Resolve};
Expand All @@ -32,6 +32,8 @@ use crate::util::network::retry::{Retry, RetryResult};
use crate::util::network::sleep::SleepTracker;
use crate::util::{self, internal, Config, Progress, ProgressStyle};

use super::compiler::build_context::RustcTargetDataBuilder;

pub const MANIFEST_PREAMBLE: &str = "\
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
Expand Down Expand Up @@ -494,7 +496,7 @@ impl<'cfg> PackageSet<'cfg> {
root_ids: &[PackageId],
has_dev_units: HasDevUnits,
requested_kinds: &[CompileKind],
target_data: &mut RustcTargetData<'cfg>,
target_data: &mut RustcTargetDataBuilder<'cfg>,
force_all_targets: ForceAllTargets,
) -> CargoResult<()> {
fn collect_used_deps(
Expand All @@ -503,7 +505,7 @@ impl<'cfg> PackageSet<'cfg> {
pkg_id: PackageId,
has_dev_units: HasDevUnits,
requested_kinds: &[CompileKind],
target_data: &mut RustcTargetData<'_>,
target_data: &mut RustcTargetDataBuilder<'_>,
force_all_targets: ForceAllTargets,
additional_kinds: Vec<CompileKind>,
) -> CargoResult<()> {
Expand All @@ -515,7 +517,7 @@ impl<'cfg> PackageSet<'cfg> {
resolve,
has_dev_units,
requested_kinds,
&*target_data,
target_data,
force_all_targets,
&additional_kinds,
);
Expand Down Expand Up @@ -576,7 +578,7 @@ impl<'cfg> PackageSet<'cfg> {
root_ids: &[PackageId],
has_dev_units: HasDevUnits,
requested_kinds: &[CompileKind],
target_data: &RustcTargetData<'_>,
target_data: &RustcTargetDataBuilder<'_>,
force_all_targets: ForceAllTargets,
) -> CargoResult<()> {
let no_lib_pkgs: BTreeMap<PackageId, Vec<(&Package, &HashSet<Dependency>)>> = root_ids
Expand Down Expand Up @@ -632,7 +634,7 @@ impl<'cfg> PackageSet<'cfg> {
resolve: &'a Resolve,
has_dev_units: HasDevUnits,
requested_kinds: &'a [CompileKind],
target_data: &'a RustcTargetData<'_>,
target_data: &'a RustcTargetDataBuilder<'_>,
force_all_targets: ForceAllTargets,
additional_kinds: &'a [CompileKind],
) -> impl Iterator<Item = (PackageId, &'a HashSet<Dependency>, Vec<CompileKind>)> + 'a {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ pub fn create_bcx<'a, 'cfg>(
build_config,
profiles,
extra_compiler_args,
target_data,
target_data.build(),
units,
unit_graph,
scrape_units,
Expand Down
7 changes: 4 additions & 3 deletions src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
//! [source implementations]: crate::sources
//! [`Downloads`]: crate::core::package::Downloads
use crate::core::compiler::{CompileKind, RustcTargetData};
use crate::core::compiler::build_context::RustcTargetDataBuilder;
use crate::core::compiler::CompileKind;
use crate::core::registry::{LockedPatchDependency, PackageRegistry};
use crate::core::resolver::features::{
CliFeatures, FeatureOpts, FeatureResolver, ForceAllTargets, RequestedFeatures, ResolvedFeatures,
Expand Down Expand Up @@ -124,7 +125,7 @@ pub fn resolve_ws<'a>(ws: &Workspace<'a>) -> CargoResult<(PackageSet<'a>, Resolv
/// members. In this case, `opts.all_features` must be `true`.
pub fn resolve_ws_with_opts<'cfg>(
ws: &Workspace<'cfg>,
target_data: &mut RustcTargetData<'cfg>,
target_data: &mut RustcTargetDataBuilder<'cfg>,
requested_targets: &[CompileKind],
cli_features: &CliFeatures,
specs: &[PackageIdSpec],
Expand Down Expand Up @@ -198,7 +199,7 @@ pub fn resolve_ws_with_opts<'cfg>(
&member_ids,
has_dev_units,
requested_targets,
&mut *target_data,
target_data,
force_all_targets,
)?;

Expand Down

0 comments on commit c53917a

Please sign in to comment.