Skip to content

Commit a4dccc0

Browse files
committed
Take &mut RustcTargetData for resolve_ws_with_opts
And then go through each packages and add their per-pkg-targets and artifact dependency targets to the `RustcTargetData`.
1 parent 88189c5 commit a4dccc0

File tree

10 files changed

+48
-47
lines changed

10 files changed

+48
-47
lines changed

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,

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

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::core::compiler::apply_env_config;
1111
use crate::core::compiler::{
1212
BuildOutput, CompileKind, CompileMode, CompileTarget, Context, CrateType,
1313
};
14-
use crate::core::{Dependency, Package, Target, TargetKind, Workspace};
14+
use crate::core::{Dependency, Target, TargetKind, Workspace};
1515
use crate::util::config::{Config, StringList, TargetConfig};
1616
use crate::util::interning::InternedString;
1717
use crate::util::{CargoResult, Rustc};
@@ -910,39 +910,15 @@ impl<'cfg> RustcTargetData<'cfg> {
910910
target_info,
911911
};
912912

913-
// Get all kinds we currently know about.
914-
//
915-
// For now, targets can only ever come from the root workspace
916-
// units and artifact dependencies, so this
917-
// correctly represents all the kinds that can happen. When we have
918-
// other ways for targets to appear at places that are not the root units,
919-
// we may have to revisit this.
920-
fn artifact_targets(package: &Package) -> impl Iterator<Item = CompileKind> + '_ {
921-
package
922-
.manifest()
923-
.dependencies()
924-
.iter()
925-
.filter_map(|d| d.artifact()?.target()?.to_compile_kind())
926-
}
927-
let all_kinds = requested_kinds
928-
.iter()
929-
.copied()
930-
.chain(ws.members().flat_map(|p| {
931-
p.manifest()
932-
.default_kind()
933-
.into_iter()
934-
.chain(p.manifest().forced_kind())
935-
.chain(artifact_targets(p))
936-
}));
937-
for kind in all_kinds {
913+
for &kind in requested_kinds {
938914
res.merge_compile_kind(kind)?;
939915
}
940916

941917
Ok(res)
942918
}
943919

944920
/// Insert `kind` into our `target_info` and `target_config` members if it isn't present yet.
945-
fn merge_compile_kind(&mut self, kind: CompileKind) -> CargoResult<()> {
921+
pub(crate) fn merge_compile_kind(&mut self, kind: CompileKind) -> CargoResult<()> {
946922
if let CompileKind::Target(target) = kind {
947923
if !self.target_config.contains_key(&target) {
948924
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/package.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,8 @@ impl<'cfg> PackageSet<'cfg> {
651651
Some((
652652
id,
653653
deps,
654-
deps_iter.filter_map(|dep| dep.artifact())
654+
deps_iter
655+
.filter_map(|dep| dep.artifact())
655656
.filter_map(|artifact| artifact.target())
656657
.filter_map(|target| target.to_compile_kind())
657658
.collect(),

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub fn create_bcx<'a, 'cfg>(
239239
}
240240
config.validate_term_config()?;
241241

242-
let target_data = RustcTargetData::new(ws, &build_config.requested_kinds)?;
242+
let mut target_data = RustcTargetData::new(ws, &build_config.requested_kinds)?;
243243

244244
let specs = spec.to_package_id_specs(ws)?;
245245
let has_dev_units = {
@@ -265,7 +265,7 @@ pub fn create_bcx<'a, 'cfg>(
265265
};
266266
let resolve = ops::resolve_ws_with_opts(
267267
ws,
268-
&target_data,
268+
&mut target_data,
269269
&build_config.requested_kinds,
270270
cli_features,
271271
&specs,
@@ -281,7 +281,7 @@ pub fn create_bcx<'a, 'cfg>(
281281

282282
let std_resolve_features = if let Some(crates) = &config.cli_unstable().build_std {
283283
let (std_package_set, std_resolve, std_features) =
284-
standard_lib::resolve_std(ws, &target_data, &build_config, crates)?;
284+
standard_lib::resolve_std(ws, &mut target_data, &build_config, crates)?;
285285
pkg_set.add_set(std_package_set);
286286
Some((std_resolve, std_features))
287287
} else {

src/cargo/ops/cargo_fetch.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn fetch<'a>(
3030
&options.targets,
3131
CompileMode::Build,
3232
)?;
33-
let data = RustcTargetData::new(ws, &build_config.requested_kinds)?;
33+
let mut data = RustcTargetData::new(ws, &build_config.requested_kinds)?;
3434
let mut fetched_packages = HashSet::new();
3535
let mut deps_to_fetch = ws.members().map(|p| p.package_id()).collect::<Vec<_>>();
3636
let mut to_download = Vec::new();
@@ -69,7 +69,8 @@ pub fn fetch<'a>(
6969
// If -Zbuild-std was passed, download dependencies for the standard library.
7070
// We don't know ahead of time what jobs we'll be running, so tell `std_crates` that.
7171
if let Some(crates) = standard_lib::std_crates(config, None) {
72-
let (std_package_set, _, _) = standard_lib::resolve_std(ws, &data, &build_config, &crates)?;
72+
let (std_package_set, _, _) =
73+
standard_lib::resolve_std(ws, &mut data, &build_config, &crates)?;
7374
packages.add_set(std_package_set);
7475
}
7576

src/cargo/ops/cargo_output_metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn build_resolve_graph(
124124
// How should this work?
125125
let requested_kinds =
126126
CompileKind::from_requested_targets(ws.config(), &metadata_opts.filter_platforms)?;
127-
let target_data = RustcTargetData::new(ws, &requested_kinds)?;
127+
let mut target_data = RustcTargetData::new(ws, &requested_kinds)?;
128128
// Resolve entire workspace.
129129
let specs = Packages::All.to_package_id_specs(ws)?;
130130
let force_all = if metadata_opts.filter_platforms.is_empty() {
@@ -137,7 +137,7 @@ fn build_resolve_graph(
137137
// as that is the behavior of download_accessible.
138138
let ws_resolve = ops::resolve_ws_with_opts(
139139
ws,
140-
&target_data,
140+
&mut target_data,
141141
&requested_kinds,
142142
&metadata_opts.cli_features,
143143
&specs,

src/cargo/ops/fix.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,12 @@ fn check_resolver_change(ws: &Workspace<'_>, opts: &FixOptions) -> CargoResult<(
243243
// 2018 without `resolver` set must be V1
244244
assert_eq!(ws.resolve_behavior(), ResolveBehavior::V1);
245245
let specs = opts.compile_opts.spec.to_package_id_specs(ws)?;
246-
let target_data = RustcTargetData::new(ws, &opts.compile_opts.build_config.requested_kinds)?;
247-
let resolve_differences = |has_dev_units| -> CargoResult<(WorkspaceResolve<'_>, DiffMap)> {
246+
let mut target_data =
247+
RustcTargetData::new(ws, &opts.compile_opts.build_config.requested_kinds)?;
248+
let mut resolve_differences = |has_dev_units| -> CargoResult<(WorkspaceResolve<'_>, DiffMap)> {
248249
let ws_resolve = ops::resolve_ws_with_opts(
249250
ws,
250-
&target_data,
251+
&mut target_data,
251252
&opts.compile_opts.build_config.requested_kinds,
252253
&opts.compile_opts.cli_features,
253254
&specs,
@@ -258,7 +259,7 @@ fn check_resolver_change(ws: &Workspace<'_>, opts: &FixOptions) -> CargoResult<(
258259
let feature_opts = FeatureOpts::new_behavior(ResolveBehavior::V2, has_dev_units);
259260
let v2_features = FeatureResolver::resolve(
260261
ws,
261-
&target_data,
262+
&mut target_data,
262263
&ws_resolve.targeted_resolve,
263264
&ws_resolve.pkg_set,
264265
&opts.compile_opts.cli_features,

src/cargo/ops/resolve.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use crate::core::resolver::{
6464
self, HasDevUnits, Resolve, ResolveOpts, ResolveVersion, VersionPreferences,
6565
};
6666
use crate::core::summary::Summary;
67-
use crate::core::Feature;
67+
use crate::core::{Feature, Package};
6868
use crate::core::{GitReference, PackageId, PackageIdSpec, PackageSet, SourceId, Workspace};
6969
use crate::ops;
7070
use crate::sources::PathSource;
@@ -124,7 +124,7 @@ pub fn resolve_ws<'a>(ws: &Workspace<'a>) -> CargoResult<(PackageSet<'a>, Resolv
124124
/// members. In this case, `opts.all_features` must be `true`.
125125
pub fn resolve_ws_with_opts<'cfg>(
126126
ws: &Workspace<'cfg>,
127-
target_data: &RustcTargetData<'cfg>,
127+
target_data: &mut RustcTargetData<'cfg>,
128128
requested_targets: &[CompileKind],
129129
cli_features: &CliFeatures,
130130
specs: &[PackageIdSpec],
@@ -198,10 +198,32 @@ pub fn resolve_ws_with_opts<'cfg>(
198198
&member_ids,
199199
has_dev_units,
200200
requested_targets,
201-
target_data,
201+
&*target_data,
202202
force_all_targets,
203203
)?;
204204

205+
// After we download all the packages we need, we need to change target_data such that any
206+
// additionally enabled targets are registered, since they could have per-pkg-target or
207+
// artifact dependencies.
208+
for pkg in pkg_set.packages() {
209+
fn artifact_targets(package: &Package) -> impl Iterator<Item = CompileKind> + '_ {
210+
package
211+
.manifest()
212+
.dependencies()
213+
.iter()
214+
.filter_map(|d| d.artifact()?.target()?.to_compile_kind())
215+
}
216+
let kinds = pkg
217+
.manifest()
218+
.default_kind()
219+
.into_iter()
220+
.chain(pkg.manifest().forced_kind())
221+
.chain(artifact_targets(pkg));
222+
for kind in kinds {
223+
target_data.merge_compile_kind(kind)?;
224+
}
225+
}
226+
205227
let feature_opts = FeatureOpts::new(ws, has_dev_units, force_all_targets)?;
206228
let resolved_features = FeatureResolver::resolve(
207229
ws,

src/cargo/ops/tree/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
135135
// TODO: Target::All is broken with -Zfeatures=itarget. To handle that properly,
136136
// `FeatureResolver` will need to be taught what "all" means.
137137
let requested_kinds = CompileKind::from_requested_targets(ws.config(), &requested_targets)?;
138-
let target_data = RustcTargetData::new(ws, &requested_kinds)?;
138+
let mut target_data = RustcTargetData::new(ws, &requested_kinds)?;
139139
let specs = opts.packages.to_package_id_specs(ws)?;
140140
let has_dev = if opts
141141
.edge_kinds
@@ -152,7 +152,7 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
152152
};
153153
let ws_resolve = ops::resolve_ws_with_opts(
154154
ws,
155-
&target_data,
155+
&mut target_data,
156156
&requested_kinds,
157157
&opts.cli_features,
158158
&specs,

0 commit comments

Comments
 (0)