]> git.proxmox.com Git - cargo.git/blob - src/cargo/core/compiler/unit_dependencies.rs
bcx reorg
[cargo.git] / src / cargo / core / compiler / unit_dependencies.rs
1 //! Constructs the dependency graph for compilation.
2 //!
3 //! Rust code is typically organized as a set of Cargo packages. The
4 //! dependencies between the packages themselves are stored in the
5 //! `Resolve` struct. However, we can't use that information as is for
6 //! compilation! A package typically contains several targets, or crates,
7 //! and these targets has inter-dependencies. For example, you need to
8 //! compile the `lib` target before the `bin` one, and you need to compile
9 //! `build.rs` before either of those.
10 //!
11 //! So, we need to lower the `Resolve`, which specifies dependencies between
12 //! *packages*, to a graph of dependencies between their *targets*, and this
13 //! is exactly what this module is doing! Well, almost exactly: another
14 //! complication is that we might want to compile the same target several times
15 //! (for example, with and without tests), so we actually build a dependency
16 //! graph of `Unit`s, which capture these properties.
17
18 use crate::core::compiler::unit_graph::{UnitDep, UnitGraph};
19 use crate::core::compiler::UnitInterner;
20 use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit};
21 use crate::core::dependency::DepKind;
22 use crate::core::profiles::{Profile, Profiles, UnitFor};
23 use crate::core::resolver::features::{FeaturesFor, ResolvedFeatures};
24 use crate::core::resolver::Resolve;
25 use crate::core::{InternedString, Package, PackageId, PackageSet, Target, Workspace};
26 use crate::ops::resolve_all_features;
27 use crate::util::Config;
28 use crate::CargoResult;
29 use log::trace;
30 use std::collections::{HashMap, HashSet};
31 use std::rc::Rc;
32
33 /// Collection of stuff used while creating the `UnitGraph`.
34 struct State<'a, 'unit, 'cfg> {
35 ws: &'a Workspace<'cfg>,
36 config: &'cfg Config,
37 unit_dependencies: UnitGraph<'unit>,
38 package_set: &'a PackageSet<'cfg>,
39 usr_resolve: &'a Resolve,
40 usr_features: &'a ResolvedFeatures,
41 std_resolve: Option<&'a Resolve>,
42 std_features: Option<&'a ResolvedFeatures>,
43 /// This flag is `true` while generating the dependencies for the standard
44 /// library.
45 is_std: bool,
46 global_mode: CompileMode,
47 target_data: &'a RustcTargetData,
48 profiles: &'a Profiles,
49 interner: &'unit UnitInterner,
50 }
51
52 pub fn build_unit_dependencies<'a, 'unit, 'cfg>(
53 ws: &'a Workspace<'cfg>,
54 package_set: &'a PackageSet<'cfg>,
55 resolve: &'a Resolve,
56 features: &'a ResolvedFeatures,
57 std_resolve: Option<&'a (Resolve, ResolvedFeatures)>,
58 roots: &[Unit<'unit>],
59 std_roots: &[Unit<'unit>],
60 global_mode: CompileMode,
61 target_data: &'a RustcTargetData,
62 profiles: &'a Profiles,
63 interner: &'unit UnitInterner,
64 ) -> CargoResult<UnitGraph<'unit>> {
65 let (std_resolve, std_features) = match std_resolve {
66 Some((r, f)) => (Some(r), Some(f)),
67 None => (None, None),
68 };
69 let mut state = State {
70 ws,
71 config: ws.config(),
72 unit_dependencies: HashMap::new(),
73 package_set,
74 usr_resolve: resolve,
75 usr_features: features,
76 std_resolve,
77 std_features,
78 is_std: false,
79 global_mode,
80 target_data,
81 profiles,
82 interner,
83 };
84
85 let std_unit_deps = calc_deps_of_std(&mut state, std_roots)?;
86
87 deps_of_roots(roots, &mut state)?;
88 super::links::validate_links(state.resolve(), &state.unit_dependencies)?;
89 // Hopefully there aren't any links conflicts with the standard library?
90
91 if let Some(std_unit_deps) = std_unit_deps {
92 attach_std_deps(&mut state, std_roots, std_unit_deps);
93 }
94
95 connect_run_custom_build_deps(&mut state.unit_dependencies);
96
97 // Dependencies are used in tons of places throughout the backend, many of
98 // which affect the determinism of the build itself. As a result be sure
99 // that dependency lists are always sorted to ensure we've always got a
100 // deterministic output.
101 for list in state.unit_dependencies.values_mut() {
102 list.sort();
103 }
104 trace!("ALL UNIT DEPENDENCIES {:#?}", state.unit_dependencies);
105
106 Ok(state.unit_dependencies)
107 }
108
109 /// Compute all the dependencies for the standard library.
110 fn calc_deps_of_std<'unit>(
111 mut state: &mut State<'_, 'unit, '_>,
112 std_roots: &[Unit<'unit>],
113 ) -> CargoResult<Option<UnitGraph<'unit>>> {
114 if std_roots.is_empty() {
115 return Ok(None);
116 }
117 // Compute dependencies for the standard library.
118 state.is_std = true;
119 deps_of_roots(std_roots, &mut state)?;
120 state.is_std = false;
121 Ok(Some(std::mem::replace(
122 &mut state.unit_dependencies,
123 HashMap::new(),
124 )))
125 }
126
127 /// Add the standard library units to the `unit_dependencies`.
128 fn attach_std_deps<'unit>(
129 state: &mut State<'_, 'unit, '_>,
130 std_roots: &[Unit<'unit>],
131 std_unit_deps: UnitGraph<'unit>,
132 ) {
133 // Attach the standard library as a dependency of every target unit.
134 for (unit, deps) in state.unit_dependencies.iter_mut() {
135 if !unit.kind.is_host() && !unit.mode.is_run_custom_build() {
136 deps.extend(std_roots.iter().map(|unit| UnitDep {
137 unit: *unit,
138 unit_for: UnitFor::new_normal(),
139 extern_crate_name: unit.pkg.name(),
140 // TODO: Does this `public` make sense?
141 public: true,
142 noprelude: true,
143 }));
144 }
145 }
146 // And also include the dependencies of the standard library itself.
147 for (unit, deps) in std_unit_deps.into_iter() {
148 if let Some(other_unit) = state.unit_dependencies.insert(unit, deps) {
149 panic!("std unit collision with existing unit: {:?}", other_unit);
150 }
151 }
152 }
153
154 /// Compute all the dependencies of the given root units.
155 /// The result is stored in state.unit_dependencies.
156 fn deps_of_roots<'unit>(
157 roots: &[Unit<'unit>],
158 mut state: &mut State<'_, 'unit, '_>,
159 ) -> CargoResult<()> {
160 for unit in roots.iter() {
161 // Dependencies of tests/benches should not have `panic` set.
162 // We check the global test mode to see if we are running in `cargo
163 // test` in which case we ensure all dependencies have `panic`
164 // cleared, and avoid building the lib thrice (once with `panic`, once
165 // without, once for `--test`). In particular, the lib included for
166 // Doc tests and examples are `Build` mode here.
167 let unit_for = if unit.mode.is_any_test() || state.global_mode.is_rustc_test() {
168 UnitFor::new_test(state.config)
169 } else if unit.target.is_custom_build() {
170 // This normally doesn't happen, except `clean` aggressively
171 // generates all units.
172 UnitFor::new_host(false)
173 } else if unit.target.proc_macro() {
174 UnitFor::new_host(true)
175 } else if unit.target.for_host() {
176 // Plugin should never have panic set.
177 UnitFor::new_compiler()
178 } else {
179 UnitFor::new_normal()
180 };
181 deps_of(unit, &mut state, unit_for)?;
182 }
183
184 Ok(())
185 }
186
187 /// Compute the dependencies of a single unit.
188 fn deps_of<'unit>(
189 unit: &Unit<'unit>,
190 state: &mut State<'_, 'unit, '_>,
191 unit_for: UnitFor,
192 ) -> CargoResult<()> {
193 // Currently the `unit_dependencies` map does not include `unit_for`. This should
194 // be safe for now. `TestDependency` only exists to clear the `panic`
195 // flag, and you'll never ask for a `unit` with `panic` set as a
196 // `TestDependency`. `CustomBuild` should also be fine since if the
197 // requested unit's settings are the same as `Any`, `CustomBuild` can't
198 // affect anything else in the hierarchy.
199 if !state.unit_dependencies.contains_key(unit) {
200 let unit_deps = compute_deps(unit, state, unit_for)?;
201 state.unit_dependencies.insert(*unit, unit_deps.clone());
202 for unit_dep in unit_deps {
203 deps_of(&unit_dep.unit, state, unit_dep.unit_for)?;
204 }
205 }
206 Ok(())
207 }
208
209 /// For a package, returns all targets that are registered as dependencies
210 /// for that package.
211 /// This returns a `Vec` of `(Unit, UnitFor)` pairs. The `UnitFor`
212 /// is the profile type that should be used for dependencies of the unit.
213 fn compute_deps<'unit>(
214 unit: &Unit<'unit>,
215 state: &mut State<'_, 'unit, '_>,
216 unit_for: UnitFor,
217 ) -> CargoResult<Vec<UnitDep<'unit>>> {
218 if unit.mode.is_run_custom_build() {
219 return compute_deps_custom_build(unit, unit_for, state);
220 } else if unit.mode.is_doc() {
221 // Note: this does not include doc test.
222 return compute_deps_doc(unit, state);
223 }
224
225 let id = unit.pkg.package_id();
226 let filtered_deps = state.resolve().deps(id).filter(|&(_id, deps)| {
227 assert!(!deps.is_empty());
228 deps.iter().any(|dep| {
229 // If this target is a build command, then we only want build
230 // dependencies, otherwise we want everything *other than* build
231 // dependencies.
232 if unit.target.is_custom_build() != dep.is_build() {
233 return false;
234 }
235
236 // If this dependency is **not** a transitive dependency, then it
237 // only applies to test/example targets.
238 if !dep.is_transitive()
239 && !unit.target.is_test()
240 && !unit.target.is_example()
241 && !unit.mode.is_any_test()
242 {
243 return false;
244 }
245
246 // If this dependency is only available for certain platforms,
247 // make sure we're only enabling it for that platform.
248 if !state.target_data.dep_platform_activated(dep, unit.kind) {
249 return false;
250 }
251
252 // If this is an optional dependency, and the new feature resolver
253 // did not enable it, don't include it.
254 if dep.is_optional() {
255 let features_for = unit_for.map_to_features_for();
256
257 let feats = state.activated_features(id, features_for);
258 if !feats.contains(&dep.name_in_toml()) {
259 return false;
260 }
261 }
262
263 // If we've gotten past all that, then this dependency is
264 // actually used!
265 true
266 })
267 });
268 // Separate line to avoid rustfmt indentation. Must collect due to `state` capture.
269 let filtered_deps: Vec<_> = filtered_deps.collect();
270
271 let mut ret = Vec::new();
272 for (id, _) in filtered_deps {
273 let pkg = state.get(id);
274 let lib = match pkg.targets().iter().find(|t| t.is_lib()) {
275 Some(t) => t,
276 None => continue,
277 };
278 let mode = check_or_build_mode(unit.mode, lib);
279 let dep_unit_for = unit_for
280 .with_for_host(lib.for_host())
281 // If it is a custom build script, then it *only* has build dependencies.
282 .with_host_features(unit.target.is_custom_build() || lib.proc_macro());
283
284 if state.config.cli_unstable().dual_proc_macros && lib.proc_macro() && !unit.kind.is_host()
285 {
286 let unit_dep = new_unit_dep(state, unit, pkg, lib, dep_unit_for, unit.kind, mode)?;
287 ret.push(unit_dep);
288 let unit_dep =
289 new_unit_dep(state, unit, pkg, lib, dep_unit_for, CompileKind::Host, mode)?;
290 ret.push(unit_dep);
291 } else {
292 let unit_dep = new_unit_dep(
293 state,
294 unit,
295 pkg,
296 lib,
297 dep_unit_for,
298 unit.kind.for_target(lib),
299 mode,
300 )?;
301 ret.push(unit_dep);
302 }
303 }
304
305 // If this target is a build script, then what we've collected so far is
306 // all we need. If this isn't a build script, then it depends on the
307 // build script if there is one.
308 if unit.target.is_custom_build() {
309 return Ok(ret);
310 }
311 ret.extend(dep_build_script(unit, unit_for, state)?);
312
313 // If this target is a binary, test, example, etc, then it depends on
314 // the library of the same package. The call to `resolve.deps` above
315 // didn't include `pkg` in the return values, so we need to special case
316 // it here and see if we need to push `(pkg, pkg_lib_target)`.
317 if unit.target.is_lib() && unit.mode != CompileMode::Doctest {
318 return Ok(ret);
319 }
320 ret.extend(maybe_lib(unit, state, unit_for)?);
321
322 // If any integration tests/benches are being run, make sure that
323 // binaries are built as well.
324 if !unit.mode.is_check()
325 && unit.mode.is_any_test()
326 && (unit.target.is_test() || unit.target.is_bench())
327 {
328 ret.extend(
329 unit.pkg
330 .targets()
331 .iter()
332 .filter(|t| {
333 // Skip binaries with required features that have not been selected.
334 match t.required_features() {
335 Some(rf) if t.is_bin() => {
336 let features = resolve_all_features(
337 state.resolve(),
338 state.features(),
339 state.package_set,
340 id,
341 );
342 rf.iter().all(|f| features.contains(f))
343 }
344 None if t.is_bin() => true,
345 _ => false,
346 }
347 })
348 .map(|t| {
349 new_unit_dep(
350 state,
351 unit,
352 &unit.pkg,
353 t,
354 UnitFor::new_normal(),
355 unit.kind.for_target(t),
356 CompileMode::Build,
357 )
358 })
359 .collect::<CargoResult<Vec<UnitDep<'unit>>>>()?,
360 );
361 }
362
363 Ok(ret)
364 }
365
366 /// Returns the dependencies needed to run a build script.
367 ///
368 /// The `unit` provided must represent an execution of a build script, and
369 /// the returned set of units must all be run before `unit` is run.
370 fn compute_deps_custom_build<'unit>(
371 unit: &Unit<'unit>,
372 unit_for: UnitFor,
373 state: &mut State<'_, 'unit, '_>,
374 ) -> CargoResult<Vec<UnitDep<'unit>>> {
375 if let Some(links) = unit.pkg.manifest().links() {
376 if state
377 .target_data
378 .script_override(links, unit.kind)
379 .is_some()
380 {
381 // Overridden build scripts don't have any dependencies.
382 return Ok(Vec::new());
383 }
384 }
385 // All dependencies of this unit should use profiles for custom builds.
386 // If this is a build script of a proc macro, make sure it uses host
387 // features.
388 let script_unit_for = UnitFor::new_host(unit_for.is_for_host_features());
389 // When not overridden, then the dependencies to run a build script are:
390 //
391 // 1. Compiling the build script itself.
392 // 2. For each immediate dependency of our package which has a `links`
393 // key, the execution of that build script.
394 //
395 // We don't have a great way of handling (2) here right now so this is
396 // deferred until after the graph of all unit dependencies has been
397 // constructed.
398 let unit_dep = new_unit_dep(
399 state,
400 unit,
401 &unit.pkg,
402 &unit.target,
403 script_unit_for,
404 // Build scripts always compiled for the host.
405 CompileKind::Host,
406 CompileMode::Build,
407 )?;
408 Ok(vec![unit_dep])
409 }
410
411 /// Returns the dependencies necessary to document a package.
412 fn compute_deps_doc<'unit>(
413 unit: &Unit<'unit>,
414 state: &mut State<'_, 'unit, '_>,
415 ) -> CargoResult<Vec<UnitDep<'unit>>> {
416 let target_data = state.target_data;
417 let deps = state
418 .resolve()
419 .deps(unit.pkg.package_id())
420 .filter(|&(_id, deps)| {
421 deps.iter().any(|dep| match dep.kind() {
422 DepKind::Normal => target_data.dep_platform_activated(dep, unit.kind),
423 _ => false,
424 })
425 });
426
427 // To document a library, we depend on dependencies actually being
428 // built. If we're documenting *all* libraries, then we also depend on
429 // the documentation of the library being built.
430 let mut ret = Vec::new();
431 for (id, _deps) in deps {
432 let dep = state.get(id);
433 let lib = match dep.targets().iter().find(|t| t.is_lib()) {
434 Some(lib) => lib,
435 None => continue,
436 };
437 // Rustdoc only needs rmeta files for regular dependencies.
438 // However, for plugins/proc macros, deps should be built like normal.
439 let mode = check_or_build_mode(unit.mode, lib);
440 let dep_unit_for = UnitFor::new_normal()
441 .with_for_host(lib.for_host())
442 .with_host_features(lib.proc_macro());
443 let lib_unit_dep = new_unit_dep(
444 state,
445 unit,
446 dep,
447 lib,
448 dep_unit_for,
449 unit.kind.for_target(lib),
450 mode,
451 )?;
452 ret.push(lib_unit_dep);
453 if let CompileMode::Doc { deps: true } = unit.mode {
454 // Document this lib as well.
455 let doc_unit_dep = new_unit_dep(
456 state,
457 unit,
458 dep,
459 lib,
460 dep_unit_for,
461 unit.kind.for_target(lib),
462 unit.mode,
463 )?;
464 ret.push(doc_unit_dep);
465 }
466 }
467
468 // Be sure to build/run the build script for documented libraries.
469 ret.extend(dep_build_script(unit, UnitFor::new_normal(), state)?);
470
471 // If we document a binary/example, we need the library available.
472 if unit.target.is_bin() || unit.target.is_example() {
473 ret.extend(maybe_lib(unit, state, UnitFor::new_normal())?);
474 }
475 Ok(ret)
476 }
477
478 fn maybe_lib<'unit>(
479 unit: &Unit<'unit>,
480 state: &mut State<'_, 'unit, '_>,
481 unit_for: UnitFor,
482 ) -> CargoResult<Option<UnitDep<'unit>>> {
483 unit.pkg
484 .targets()
485 .iter()
486 .find(|t| t.linkable())
487 .map(|t| {
488 let mode = check_or_build_mode(unit.mode, t);
489 new_unit_dep(
490 state,
491 unit,
492 &unit.pkg,
493 t,
494 unit_for,
495 unit.kind.for_target(t),
496 mode,
497 )
498 })
499 .transpose()
500 }
501
502 /// If a build script is scheduled to be run for the package specified by
503 /// `unit`, this function will return the unit to run that build script.
504 ///
505 /// Overriding a build script simply means that the running of the build
506 /// script itself doesn't have any dependencies, so even in that case a unit
507 /// of work is still returned. `None` is only returned if the package has no
508 /// build script.
509 fn dep_build_script<'unit>(
510 unit: &Unit<'unit>,
511 unit_for: UnitFor,
512 state: &State<'_, 'unit, '_>,
513 ) -> CargoResult<Option<UnitDep<'unit>>> {
514 unit.pkg
515 .targets()
516 .iter()
517 .find(|t| t.is_custom_build())
518 .map(|t| {
519 // The profile stored in the Unit is the profile for the thing
520 // the custom build script is running for.
521 let profile = state.profiles.get_profile_run_custom_build(&unit.profile);
522 // UnitFor::new_host is used because we want the `host` flag set
523 // for all of our build dependencies (so they all get
524 // build-override profiles), including compiling the build.rs
525 // script itself.
526 //
527 // If `is_for_host_features` here is `false`, that means we are a
528 // build.rs script for a normal dependency and we want to set the
529 // CARGO_FEATURE_* environment variables to the features as a
530 // normal dep.
531 //
532 // If `is_for_host_features` here is `true`, that means that this
533 // package is being used as a build dependency or proc-macro, and
534 // so we only want to set CARGO_FEATURE_* variables for the host
535 // side of the graph.
536 //
537 // Keep in mind that the RunCustomBuild unit and the Compile
538 // build.rs unit use the same features. This is because some
539 // people use `cfg!` and `#[cfg]` expressions to check for enabled
540 // features instead of just checking `CARGO_FEATURE_*` at runtime.
541 // In the case with `-Zfeatures=host_dep`, and a shared
542 // dependency has different features enabled for normal vs. build,
543 // then the build.rs script will get compiled twice. I believe it
544 // is not feasible to only build it once because it would break a
545 // large number of scripts (they would think they have the wrong
546 // set of features enabled).
547 let script_unit_for = UnitFor::new_host(unit_for.is_for_host_features());
548 new_unit_dep_with_profile(
549 state,
550 unit,
551 &unit.pkg,
552 t,
553 script_unit_for,
554 unit.kind,
555 CompileMode::RunCustomBuild,
556 profile,
557 )
558 })
559 .transpose()
560 }
561
562 /// Choose the correct mode for dependencies.
563 fn check_or_build_mode(mode: CompileMode, target: &Target) -> CompileMode {
564 match mode {
565 CompileMode::Check { .. } | CompileMode::Doc { .. } => {
566 if target.for_host() {
567 // Plugin and proc macro targets should be compiled like
568 // normal.
569 CompileMode::Build
570 } else {
571 // Regular dependencies should not be checked with --test.
572 // Regular dependencies of doc targets should emit rmeta only.
573 CompileMode::Check { test: false }
574 }
575 }
576 _ => CompileMode::Build,
577 }
578 }
579
580 /// Create a new Unit for a dependency from `parent` to `pkg` and `target`.
581 fn new_unit_dep<'unit>(
582 state: &State<'_, 'unit, '_>,
583 parent: &Unit<'unit>,
584 pkg: &Rc<Package>,
585 target: &Rc<Target>,
586 unit_for: UnitFor,
587 kind: CompileKind,
588 mode: CompileMode,
589 ) -> CargoResult<UnitDep<'unit>> {
590 let profile =
591 state
592 .profiles
593 .get_profile(pkg.package_id(), state.ws.is_member(pkg), unit_for, mode);
594 new_unit_dep_with_profile(state, parent, pkg, target, unit_for, kind, mode, profile)
595 }
596
597 fn new_unit_dep_with_profile<'unit>(
598 state: &State<'_, 'unit, '_>,
599 parent: &Unit<'unit>,
600 pkg: &Rc<Package>,
601 target: &Rc<Target>,
602 unit_for: UnitFor,
603 kind: CompileKind,
604 mode: CompileMode,
605 profile: Profile,
606 ) -> CargoResult<UnitDep<'unit>> {
607 // TODO: consider making extern_crate_name return InternedString?
608 let extern_crate_name = InternedString::new(&state.resolve().extern_crate_name(
609 parent.pkg.package_id(),
610 pkg.package_id(),
611 target,
612 )?);
613 let public = state
614 .resolve()
615 .is_public_dep(parent.pkg.package_id(), pkg.package_id());
616 let features_for = unit_for.map_to_features_for();
617 let features = state.activated_features(pkg.package_id(), features_for);
618 let unit = state
619 .interner
620 .intern(pkg, target, profile, kind, mode, features, state.is_std);
621 Ok(UnitDep {
622 unit,
623 unit_for,
624 extern_crate_name,
625 public,
626 noprelude: false,
627 })
628 }
629
630 /// Fill in missing dependencies for units of the `RunCustomBuild`
631 ///
632 /// As mentioned above in `compute_deps_custom_build` each build script
633 /// execution has two dependencies. The first is compiling the build script
634 /// itself (already added) and the second is that all crates the package of the
635 /// build script depends on with `links` keys, their build script execution. (a
636 /// bit confusing eh?)
637 ///
638 /// Here we take the entire `deps` map and add more dependencies from execution
639 /// of one build script to execution of another build script.
640 fn connect_run_custom_build_deps(unit_dependencies: &mut UnitGraph<'_>) {
641 let mut new_deps = Vec::new();
642
643 {
644 // First up build a reverse dependency map. This is a mapping of all
645 // `RunCustomBuild` known steps to the unit which depends on them. For
646 // example a library might depend on a build script, so this map will
647 // have the build script as the key and the library would be in the
648 // value's set.
649 let mut reverse_deps_map = HashMap::new();
650 for (unit, deps) in unit_dependencies.iter() {
651 for dep in deps {
652 if dep.unit.mode == CompileMode::RunCustomBuild {
653 reverse_deps_map
654 .entry(dep.unit)
655 .or_insert_with(HashSet::new)
656 .insert(unit);
657 }
658 }
659 }
660
661 // Next, we take a look at all build scripts executions listed in the
662 // dependency map. Our job here is to take everything that depends on
663 // this build script (from our reverse map above) and look at the other
664 // package dependencies of these parents.
665 //
666 // If we depend on a linkable target and the build script mentions
667 // `links`, then we depend on that package's build script! Here we use
668 // `dep_build_script` to manufacture an appropriate build script unit to
669 // depend on.
670 for unit in unit_dependencies
671 .keys()
672 .filter(|k| k.mode == CompileMode::RunCustomBuild)
673 {
674 // This is the lib that runs this custom build.
675 let reverse_deps = match reverse_deps_map.get(unit) {
676 Some(set) => set,
677 None => continue,
678 };
679
680 let to_add = reverse_deps
681 .iter()
682 // Get all deps for lib.
683 .flat_map(|reverse_dep| unit_dependencies[reverse_dep].iter())
684 // Only deps with `links`.
685 .filter(|other| {
686 other.unit.pkg != unit.pkg
687 && other.unit.target.linkable()
688 && other.unit.pkg.manifest().links().is_some()
689 })
690 // Get the RunCustomBuild for other lib.
691 .filter_map(|other| {
692 unit_dependencies[&other.unit]
693 .iter()
694 .find(|other_dep| other_dep.unit.mode == CompileMode::RunCustomBuild)
695 .cloned()
696 })
697 .collect::<HashSet<_>>();
698
699 if !to_add.is_empty() {
700 // (RunCustomBuild, set(other RunCustomBuild))
701 new_deps.push((*unit, to_add));
702 }
703 }
704 }
705
706 // And finally, add in all the missing dependencies!
707 for (unit, new_deps) in new_deps {
708 unit_dependencies.get_mut(&unit).unwrap().extend(new_deps);
709 }
710 }
711
712 impl<'a, 'unit, 'cfg> State<'a, 'unit, 'cfg> {
713 fn resolve(&self) -> &'a Resolve {
714 if self.is_std {
715 self.std_resolve.unwrap()
716 } else {
717 self.usr_resolve
718 }
719 }
720
721 fn features(&self) -> &'a ResolvedFeatures {
722 if self.is_std {
723 self.std_features.unwrap()
724 } else {
725 self.usr_features
726 }
727 }
728
729 fn activated_features(
730 &self,
731 pkg_id: PackageId,
732 features_for: FeaturesFor,
733 ) -> Vec<InternedString> {
734 let features = self.features();
735 features.activated_features(pkg_id, features_for)
736 }
737
738 fn get(&self, id: PackageId) -> &'a Rc<Package> {
739 self.package_set
740 .get_one(id)
741 .unwrap_or_else(|_| panic!("expected {} to be downloaded", id))
742 }
743 }