1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Mitts Build — feature graph propagation
* Copyright (c) 2026 Alexander Panasenko
*
* Contact: alex@prod.codes
* Author: https://prod.codes/about/
* Project: https://github.com/alex09x/mitts
* SPDX-License-Identifier: MIT OR Apache-2.0
*/
use super::*;
use crate::adapter::rust::manifest::TomlDependency;
use crate::adapter::rust::types::RustAdapterError;
use std::collections::{HashMap, HashSet};
#[allow(clippy::too_many_arguments)]
pub(super) fn propagate_package<'a>(
pkg_idx: usize,
ctx: &'a PackageFeatureContext<'a>,
index: &mut FeatureGraphIndex<'a>,
is_test: bool,
is_host_ctx: bool,
active_target: &mut HashSet<String>,
active_host: &mut HashSet<String>,
target_req: &mut HashMap<String, HashSet<String>>,
host_req: &mut HashMap<String, HashSet<String>>,
changed: &mut bool,
) -> Result<(), RustAdapterError> {
let id = index.pkg_ids[pkg_idx].clone();
let mut runtime_deps: Vec<(&String, &TomlDependency)> = ctx.toml.dependencies.iter().collect();
let mut build_deps: Vec<(&String, &TomlDependency)> =
ctx.toml.build_dependencies.iter().collect();
let is_pm = ctx.is_proc_macro();
let include_dev_deps =
is_test && ctx.is_root && (if is_pm { is_host_ctx } else { !is_host_ctx });
if include_dev_deps {
runtime_deps.extend(&ctx.toml.dev_dependencies);
}
for (spec, t) in &ctx.toml.target {
if !index.is_target_incompat(spec, is_host_ctx)? {
runtime_deps.extend(&t.dependencies);
if include_dev_deps {
runtime_deps.extend(&t.dev_dependencies);
}
}
if !index.is_target_incompat(spec, true)? {
build_deps.extend(&t.build_dependencies);
}
}
// (A) Activate non-optional dependencies
for (dep_key, dep) in runtime_deps {
let is_opt = matches!(dep, TomlDependency::Detailed(d) if d.optional.unwrap_or(false));
if !is_opt {
let to_host =
is_host_ctx || index.is_dep_proc_macro_with_hint(pkg_idx, dep_key, Some(dep))?;
let (act, req) = if to_host {
(&mut *active_host, &mut *host_req)
} else {
(&mut *active_target, &mut *target_req)
};
index.activate_dep_instance(pkg_idx, dep_key, dep, act, req, changed)?;
}
}
for (dep_key, dep) in build_deps {
let is_opt = matches!(dep, TomlDependency::Detailed(d) if d.optional.unwrap_or(false));
if !is_opt {
index.activate_dep_instance(pkg_idx, dep_key, dep, active_host, host_req, changed)?;
}
}
// (B) Expand active features
let current_feats = if is_host_ctx {
&*host_req
} else {
&*target_req
}
.get(&id)
.cloned()
.unwrap_or_default();
for feat in ¤t_feats {
let deps =
index.collect_manifest_dependencies_with_dev(ctx.toml, feat, include_dev_deps)?;
if is_optional_dep(&deps) {
let is_pm_dep =
index.is_dep_proc_macro_with_hint(pkg_idx, feat, deps.first().copied())?;
let (to_target, to_host) = optional_dep_sides(ctx.toml, feat, is_host_ctx, is_pm_dep);
if to_target {
index.activate_manifest_dep(
pkg_idx,
feat,
&deps,
active_target,
target_req,
changed,
)?;
}
if to_host {
index.activate_manifest_dep(
pkg_idx,
feat,
&deps,
active_host,
host_req,
changed,
)?;
}
}
if let Some(sub_feats) = ctx.toml.features.get(feat) {
for sub in sub_feats {
if let Some((dep, dep_feat)) = sub.split_once('/') {
let is_weak = dep.ends_with('?');
let dep_unweak = dep.strip_suffix('?').unwrap_or(dep);
let actual_dep = dep_unweak.strip_prefix("dep:").unwrap_or(dep_unweak);
let real_dep: String = index.aliases_by_idx[pkg_idx]
.alias_to_real
.get(actual_dep)
.cloned()
.unwrap_or_else(|| actual_dep.to_string());
let mut dep_decls = index.collect_manifest_dependencies_with_dev(
ctx.toml,
actual_dep,
include_dev_deps,
)?;
if dep_decls.is_empty() {
dep_decls = index.collect_manifest_dependencies_with_dev(
ctx.toml,
&real_dep,
include_dev_deps,
)?;
}
if dep_decls.is_empty()
&& !include_dev_deps
&& (ctx.toml.dev_dependencies.contains_key(actual_dep)
|| ctx.toml.dev_dependencies.contains_key(&real_dep)
|| ctx.toml.target.values().any(|target| {
target.dev_dependencies.contains_key(actual_dep)
|| target.dev_dependencies.contains_key(&real_dep)
}))
{
continue;
}
if !is_weak
&& (ctx.toml.features.contains_key(actual_dep)
|| is_optional_dep(&dep_decls))
{
let this_req = if is_host_ctx {
&mut *host_req
} else {
&mut *target_req
};
if this_req
.entry(id.clone())
.or_default()
.insert(actual_dep.to_string())
{
*changed = true;
}
}
if dep_decls.is_empty()
&& ctx.toml.target.values().any(|t| {
t.dependencies.contains_key(actual_dep)
|| t.build_dependencies.contains_key(actual_dep)
})
{
continue;
}
if is_weak {
let dep_prefix = format!("dep:{}", actual_dep);
let real_prefix = format!("dep:{}", real_dep);
let cur_req = if is_host_ctx {
&*host_req
} else {
&*target_req
};
let is_dep_active = !dep_decls.is_empty()
&& (!is_optional_dep(&dep_decls)
|| cur_req.get(&id).is_some_and(|s| {
s.contains(actual_dep)
|| s.contains(&dep_prefix)
|| s.contains(&real_dep)
|| s.contains(&real_prefix)
}));
if !is_dep_active {
continue;
}
}
let dep_hint = dep_decls.first().copied();
let is_pm_dep =
index.is_dep_proc_macro_with_hint(pkg_idx, &real_dep, dep_hint)?;
let (t1, h1) = optional_dep_sides(ctx.toml, actual_dep, is_host_ctx, is_pm_dep);
let (t2, h2) = optional_dep_sides(ctx.toml, &real_dep, is_host_ctx, is_pm_dep);
let (to_target, to_host) = (t1 || t2, h1 || h2);
if !is_weak && !dep_decls.is_empty() {
if to_target {
index.activate_manifest_dep(
pkg_idx,
actual_dep,
&dep_decls,
active_target,
target_req,
changed,
)?;
}
if to_host {
index.activate_manifest_dep(
pkg_idx,
actual_dep,
&dep_decls,
active_host,
host_req,
changed,
)?;
}
}
if let Some(target_id) =
index.resolve_dep_id_with_hint(pkg_idx, &real_dep, dep_hint)?
{
if to_target
&& target_req
.entry(target_id.clone())
.or_default()
.insert(dep_feat.to_string())
{
*changed = true;
}
if to_host
&& host_req
.entry(target_id)
.or_default()
.insert(dep_feat.to_string())
{
*changed = true;
}
}
} else {
let this_req = if is_host_ctx {
&mut *host_req
} else {
&mut *target_req
};
if this_req.entry(id.clone()).or_default().insert(sub.clone()) {
*changed = true;
}
let (dep_name, explicit_dep) = match sub.strip_prefix("dep:") {
Some(d) => {
let norm = format!("dep:{}", d.replace('-', "_"));
if norm != *sub {
this_req.entry(id.clone()).or_default().insert(norm);
}
(d, true)
}
None => (sub.as_str(), false),
};
let deps = index.collect_manifest_dependencies_with_dev(
ctx.toml,
dep_name,
include_dev_deps,
)?;
if explicit_dep || is_optional_dep(&deps) {
let hint = deps.first().copied();
let is_pm_dep =
index.is_dep_proc_macro_with_hint(pkg_idx, dep_name, hint)?;
let (to_target, to_host) =
optional_dep_sides(ctx.toml, dep_name, is_host_ctx, is_pm_dep);
if to_target {
index.activate_manifest_dep(
pkg_idx,
dep_name,
&deps,
active_target,
target_req,
changed,
)?;
}
if to_host {
index.activate_manifest_dep(
pkg_idx,
dep_name,
&deps,
active_host,
host_req,
changed,
)?;
}
}
}
}
}
}
Ok(())
}