diff --git a/benches/benchsuite/benches/resolve.rs b/benches/benchsuite/benches/resolve.rs index e235441e1e2..b2a80b0b6fb 100644 --- a/benches/benchsuite/benches/resolve.rs +++ b/benches/benchsuite/benches/resolve.rs @@ -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}; @@ -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, has_dev_units: HasDevUnits, diff --git a/src/cargo/core/compiler/build_context/mod.rs b/src/cargo/core/compiler/build_context/mod.rs index f35084e2b7f..89ee6de9ef4 100644 --- a/src/cargo/core/compiler/build_context/mod.rs +++ b/src/cargo/core/compiler/build_context/mod.rs @@ -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 diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 46285cdf080..2e83c9d5fa2 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -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}; @@ -850,6 +851,40 @@ fn rustflags_from_build(config: &Config, flag: Flags) -> CargoResult { + 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. @@ -876,7 +911,7 @@ impl<'cfg> RustcTargetData<'cfg> { pub fn new( ws: &Workspace<'cfg>, requested_kinds: &[CompileKind], - ) -> CargoResult> { + ) -> CargoResult> { let config = ws.config(); let rustc = config.load_global_rustc(Some(ws))?; let mut target_config = HashMap::new(); @@ -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(), @@ -910,6 +945,10 @@ impl<'cfg> RustcTargetData<'cfg> { target_info, }; + let mut res = RustcTargetDataBuilder { + data + }; + for &kind in requested_kinds { res.merge_compile_kind(kind)?; } @@ -917,23 +956,6 @@ impl<'cfg> RustcTargetData<'cfg> { 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 { diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 31e63c226b7..d771ee940a3 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -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}; diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index b76c395b87e..6fa531cf3d1 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -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; @@ -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 { @@ -62,7 +63,7 @@ pub(crate) fn std_crates(config: &Config, units: Option<&[Unit]>) -> Option( ws: &Workspace<'cfg>, - target_data: &mut RustcTargetData<'cfg>, + target_data: &mut RustcTargetDataBuilder<'cfg>, build_config: &BuildConfig, crates: &[String], ) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> { @@ -220,7 +221,7 @@ pub fn generate_std_roots( Ok(ret) } -fn detect_sysroot_src_path(target_data: &RustcTargetData<'_>) -> CargoResult { +fn detect_sysroot_src_path(target_data: &RustcTargetDataBuilder<'_>) -> CargoResult { if let Some(s) = target_data.config.get_env_os("__CARGO_TESTS_ONLY_SRC_ROOT") { return Ok(s.into()); } diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index 02dc6310747..bb8db51b720 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -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}; @@ -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 # @@ -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( @@ -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, ) -> CargoResult<()> { @@ -515,7 +517,7 @@ impl<'cfg> PackageSet<'cfg> { resolve, has_dev_units, requested_kinds, - &*target_data, + target_data, force_all_targets, &additional_kinds, ); @@ -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)>> = root_ids @@ -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, Vec)> + 'a { diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs index 0a16d665450..7340d397a98 100644 --- a/src/cargo/ops/cargo_compile/mod.rs +++ b/src/cargo/ops/cargo_compile/mod.rs @@ -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, diff --git a/src/cargo/ops/resolve.rs b/src/cargo/ops/resolve.rs index 650ef0ff4ba..bacdf9be6c3 100644 --- a/src/cargo/ops/resolve.rs +++ b/src/cargo/ops/resolve.rs @@ -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, @@ -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], @@ -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, )?;