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