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