guppy 0.18.0

Track and query Cargo dependency graphs.
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// Copyright (c) The cargo-guppy Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::{
    Error, PackageId,
    graph::{
        DependencyDirection, PackageGraph, PackageIx, PackageLink, PackageLinkContext, PackageSet,
        cargo::build::CargoSetBuildState,
        feature::{FeatureGraph, FeatureSet},
    },
    platform::PlatformSpec,
    sorted_set::SortedSet,
};
use petgraph::prelude::*;
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, fmt};

/// Options for queries which simulate what Cargo does.
///
/// This provides control over the resolution algorithm used by `guppy`'s simulation of Cargo.
#[derive(Clone, Debug)]
pub struct CargoOptions<'a> {
    pub(crate) resolver: CargoResolverVersion,
    pub(crate) include_dev: bool,
    pub(crate) initials_platform: InitialsPlatform,
    pub(crate) host_platform: PlatformSpec,
    pub(crate) target_platform: PlatformSpec,
    pub(crate) omitted_packages: HashSet<&'a PackageId>,
}

impl<'a> CargoOptions<'a> {
    /// Creates a new `CargoOptions` with default settings.
    ///
    /// The default settings are similar to what a plain `cargo build` does:
    ///
    /// * use version 3 of the Cargo resolver
    /// * exclude dev-dependencies
    /// * do not build proc macros specified in the query on the target platform
    /// * resolve dependencies assuming any possible host or target platform
    /// * do not omit any packages.
    pub fn new() -> Self {
        Self {
            resolver: CargoResolverVersion::V3,
            include_dev: false,
            initials_platform: InitialsPlatform::Standard,
            host_platform: PlatformSpec::Any,
            target_platform: PlatformSpec::Any,
            omitted_packages: HashSet::new(),
        }
    }

    /// Sets the Cargo feature resolver version.
    ///
    /// For more about feature resolution, see the documentation for `CargoResolverVersion`.
    pub fn set_resolver(&mut self, resolver: CargoResolverVersion) -> &mut Self {
        self.resolver = resolver;
        self
    }

    /// If set to true, causes dev-dependencies of the initial set to be followed.
    ///
    /// This does not affect transitive dependencies -- for example, a build or dev-dependency's
    /// further dev-dependencies are never followed.
    ///
    /// The default is false, which matches what a plain `cargo build` does.
    pub fn set_include_dev(&mut self, include_dev: bool) -> &mut Self {
        self.include_dev = include_dev;
        self
    }

    /// Configures the way initials are treated on the target and the host.
    ///
    /// The default is a "standard" build and this does not usually need to be set, but some
    /// advanced use cases may require it. For more about this option, see the documentation for
    /// [`InitialsPlatform`](InitialsPlatform).
    pub fn set_initials_platform(&mut self, initials_platform: InitialsPlatform) -> &mut Self {
        self.initials_platform = initials_platform;
        self
    }

    /// Sets both the target and host platforms to the provided spec.
    pub fn set_platform(&mut self, platform_spec: impl Into<PlatformSpec>) -> &mut Self {
        let platform_spec = platform_spec.into();
        self.target_platform = platform_spec.clone();
        self.host_platform = platform_spec;
        self
    }

    /// Sets the target platform to the provided spec.
    pub fn set_target_platform(&mut self, target_platform: impl Into<PlatformSpec>) -> &mut Self {
        self.target_platform = target_platform.into();
        self
    }

    /// Sets the host platform to the provided spec.
    pub fn set_host_platform(&mut self, host_platform: impl Into<PlatformSpec>) -> &mut Self {
        self.host_platform = host_platform.into();
        self
    }

    /// Omits edges into the given packages.
    ///
    /// This may be useful in order to figure out what additional dependencies or features a
    /// particular set of packages pulls in.
    ///
    /// This method is additive.
    pub fn add_omitted_packages(
        &mut self,
        package_ids: impl IntoIterator<Item = &'a PackageId>,
    ) -> &mut Self {
        self.omitted_packages.extend(package_ids);
        self
    }

    // Note that the lifetime of the returned value is `'g` rather than being
    // tied to `self`.
    pub(crate) fn resolve_package_ids<'g>(
        &self,
        graph: &'g PackageGraph,
    ) -> Result<CargoOptions<'g>, Error> {
        let omitted_packages = self
            .omitted_packages
            .iter()
            .map(|package_id| graph.metadata(package_id).map(|metadata| metadata.id()))
            .collect::<Result<HashSet<_>, _>>()?;

        Ok(CargoOptions {
            resolver: self.resolver,
            include_dev: self.include_dev,
            initials_platform: self.initials_platform,
            host_platform: self.host_platform.clone(),
            target_platform: self.target_platform.clone(),
            omitted_packages,
        })
    }
}

impl Default for CargoOptions<'_> {
    fn default() -> Self {
        Self::new()
    }
}

/// Represents whether a particular link within a package graph should be
/// followed while building a [`CargoSet`].
///
/// This is similar to [`PackageLinkVisitor`], but is passed a
/// [`CargoLinkContext`] with additional information about the Cargo build.
///
/// [`PackageLinkVisitor`]: crate::graph::PackageLinkVisitor
pub trait CargoLinkVisitor<'g> {
    /// Returns true if this link should be followed.
    ///
    /// Returning false does not prevent the `to` package from being included
    /// if it's reachable through other means.
    fn visit_link(&mut self, cx: &CargoLinkContext<'_, 'g>, link: PackageLink<'g>) -> bool;
}

impl<'g, T> CargoLinkVisitor<'g> for &mut T
where
    T: CargoLinkVisitor<'g>,
{
    fn visit_link(&mut self, cx: &CargoLinkContext<'_, 'g>, link: PackageLink<'g>) -> bool {
        (**self).visit_link(cx, link)
    }
}

impl<'g> CargoLinkVisitor<'g> for Box<dyn CargoLinkVisitor<'g> + '_> {
    fn visit_link(&mut self, cx: &CargoLinkContext<'_, 'g>, link: PackageLink<'g>) -> bool {
        (**self).visit_link(cx, link)
    }
}

impl<'g> CargoLinkVisitor<'g> for &mut dyn CargoLinkVisitor<'g> {
    fn visit_link(&mut self, cx: &CargoLinkContext<'_, 'g>, link: PackageLink<'g>) -> bool {
        (**self).visit_link(cx, link)
    }
}

/// Context passed to a [`CargoLinkVisitor`] for each link visited while
/// building a [`CargoSet`].
#[derive(Clone, Debug)]
pub struct CargoLinkContext<'a, 'g> {
    package_context: &'a PackageLinkContext<'g>,
    build_platform: BuildPlatform,
    platform_spec: &'a PlatformSpec,
    build_dep_platform_spec: &'a PlatformSpec,
    include_dev: bool,
}

impl<'a, 'g> CargoLinkContext<'a, 'g> {
    pub(super) fn new(
        package_context: &'a PackageLinkContext<'g>,
        build_platform: BuildPlatform,
        opts: &'a CargoOptions<'_>,
    ) -> Self {
        let (platform_spec, build_dep_platform_spec) = match build_platform {
            BuildPlatform::Target => (&opts.target_platform, &opts.host_platform),
            BuildPlatform::Host => (&opts.host_platform, &opts.host_platform),
        };
        Self {
            package_context,
            build_platform,
            platform_spec,
            build_dep_platform_spec,
            include_dev: opts.include_dev,
        }
    }

    /// Returns the context for the underlying package graph traversal.
    pub fn package_context(&self) -> &PackageLinkContext<'g> {
        self.package_context
    }

    /// Returns the platform this link is being evaluated for: the target pass
    /// or the host pass.
    pub fn build_platform(&self) -> BuildPlatform {
        self.build_platform
    }

    /// Returns the platform spec that normal and dev dependencies are
    /// evaluated against in this pass.
    pub fn platform_spec(&self) -> &PlatformSpec {
        self.platform_spec
    }

    /// Returns the platform spec that build dependencies are evaluated against
    /// in this pass (always the host platform).
    pub fn build_dep_platform_spec(&self) -> &PlatformSpec {
        self.build_dep_platform_spec
    }

    /// Returns true if dev-dependencies of this link may be followed:
    /// [`CargoOptions::set_include_dev`] is set and the `from` package is an
    /// initial.
    pub fn considers_dev_deps(&self, link: &PackageLink<'g>) -> bool {
        self.include_dev && self.package_context.starts_from_initial(link)
    }

    /// Returns true if build-dependencies of this link may be followed: the
    /// `from` package has a build script.
    pub fn considers_build_deps(&self, link: &PackageLink<'g>) -> bool {
        link.from().has_build_script()
    }
}

/// The version of Cargo's feature resolver to use.
#[derive(Copy, Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[cfg_attr(feature = "proptest1", derive(proptest_derive::Arbitrary))]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum CargoResolverVersion {
    /// The "classic" feature resolver in Rust.
    ///
    /// This feature resolver unifies features across inactive platforms, and also unifies features
    /// across normal, build and dev dependencies for initials. This may produce results that are
    /// surprising at times.
    #[serde(rename = "1", alias = "v1")]
    V1,

    /// The "classic" feature resolver in Rust, as used by commands like `cargo install`.
    ///
    /// This resolver is the same as `V1`, except it doesn't unify features across dev dependencies
    /// for initials. However, if `CargoOptions::set_include_dev` is set to true, it behaves
    /// identically to the V1 resolver.
    ///
    /// For more, see
    /// [avoid-dev-deps](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#avoid-dev-deps)
    /// in the Cargo reference.
    #[serde(rename = "install", alias = "v1-install")]
    V1Install,

    /// [Version 2 of the feature resolver](https://doc.rust-lang.org/cargo/reference/resolver.html#feature-resolver-version-2),
    /// available since Rust 1.51. This feature resolver does not unify features:
    ///
    /// * across host (build) and target (regular) dependencies
    /// * with dev-dependencies for initials, if tests aren't currently being built
    /// * with [platform-specific dependencies](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies) that are currently inactive
    ///
    /// Version 2 of the feature resolver can be enabled by specifying `resolver
    /// = "2"` in the workspace's `Cargo.toml`. It is also [the default resolver
    /// version](https://doc.rust-lang.org/beta/edition-guide/rust-2021/default-cargo-resolver.html)
    /// for [the Rust 2021
    /// edition](https://doc.rust-lang.org/edition-guide/rust-2021/index.html).
    #[serde(rename = "2", alias = "v2")]
    V2,

    /// [Version 3 of the dependency
    /// resolver](https://doc.rust-lang.org/beta/cargo/reference/resolver.html#resolver-versions),
    /// available since Rust 1.84.
    ///
    /// Version 3 of the resolver enables [MSRV-aware dependency
    /// resolution](https://doc.rust-lang.org/beta/cargo/reference/config.html#resolverincompatible-rust-versions).
    /// There are no changes to feature resolution compared to version 2.
    ///
    /// Version 3 of the feature resolver can be enabled by specifying `resolver
    /// = "3"` in the workspace's `Cargo.toml`. It is also [the default resolver
    /// version](https://doc.rust-lang.org/beta/edition-guide/rust-2024/cargo-resolver.html)
    /// for [the Rust 2024
    /// edition](https://doc.rust-lang.org/beta/edition-guide/rust-2024/index.html).
    #[serde(rename = "3", alias = "v3")]
    V3,
}

/// For a given Cargo build simulation, what platform to assume the initials are being built on.
#[derive(Copy, Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[cfg_attr(feature = "proptest1", derive(proptest_derive::Arbitrary))]
#[serde(rename_all = "kebab-case")]
pub enum InitialsPlatform {
    /// Assume that the initials are being built on the host platform.
    ///
    /// This is most useful for "continuing" simulations, where it is already known that some
    /// packages are being built on the host and one wishes to find their dependencies.
    Host,

    /// Assume a standard build.
    ///
    /// In this mode, all initials other than proc-macros are built on the target platform. Proc-
    /// macros, being compiler plugins, are built on the host.
    ///
    /// This is the default for `InitialsPlatform`.
    Standard,

    /// Perform a standard build, and also build proc-macros on the target.
    ///
    /// Proc-macro crates may include tests, which are run on the target platform. This option is
    /// most useful for such situations.
    ProcMacrosOnTarget,
}

/// The default for `InitialsPlatform`: the `Standard` option.
impl Default for InitialsPlatform {
    fn default() -> Self {
        InitialsPlatform::Standard
    }
}

/// The inputs to a `CargoSet` other than the initials.
///
/// The initials are kept separate so that one `CargoSetInputs` can be reused
/// across many `CargoSet`s via [`to_cargo_set`](Self::to_cargo_set).
///
/// Both fields must refer to the same `PackageGraph` as the initials they
/// are used with.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct CargoSetInputs<'g> {
    /// The Cargo options used to build the `CargoSet`.
    pub options: CargoOptions<'g>,

    /// The features-only set: see [`CargoSet::new`].
    pub features_only: FeatureSet<'g>,
}

assert_covariant!(CargoSetInputs);

impl<'g> CargoSetInputs<'g> {
    /// Creates a new `CargoSetInputs` from the given inputs.
    pub fn new(options: CargoOptions<'g>, features_only: FeatureSet<'g>) -> Self {
        Self {
            options,
            features_only,
        }
    }

    /// Simulates a Cargo build of `initials` with these inputs.
    ///
    /// This is a shorthand for [`CargoSet::new`].
    ///
    /// ## Panics
    ///
    /// Panics if `initials` was built from a different package graph than
    /// these inputs.
    pub fn to_cargo_set(&self, initials: FeatureSet<'g>) -> Result<CargoSet<'g>, Error> {
        CargoSet::new(initials, self.features_only.clone(), &self.options)
    }
}

/// A set of packages and features, as would be built by Cargo.
///
/// Cargo implements a set of algorithms to figure out which packages or features are built in
/// a given situation. `guppy` implements those algorithms.
#[derive(Clone, Debug)]
pub struct CargoSet<'g> {
    pub(super) initials: FeatureSet<'g>,
    pub(super) inputs: CargoSetInputs<'g>,
    pub(super) target_features: FeatureSet<'g>,
    pub(super) host_features: FeatureSet<'g>,
    pub(super) target_direct_deps: PackageSet<'g>,
    pub(super) host_direct_deps: PackageSet<'g>,
    pub(super) proc_macro_edge_ixs: SortedSet<EdgeIndex<PackageIx>>,
    pub(super) build_dep_edge_ixs: SortedSet<EdgeIndex<PackageIx>>,
    pub(super) target_edge_ixs: SortedSet<EdgeIndex<PackageIx>>,
    pub(super) host_edge_ixs: SortedSet<EdgeIndex<PackageIx>>,
}

assert_covariant!(CargoSet);

impl<'g> CargoSet<'g> {
    /// Simulates a Cargo build of this feature set, with the given options.
    ///
    /// The feature sets are expected to be entirely within the workspace. Its behavior outside the
    /// workspace isn't defined and may be surprising.
    ///
    /// `CargoSet::new` takes two `FeatureSet` instances:
    /// * `initials`, from which dependencies are followed to build the `CargoSet`.
    /// * `features_only`, which are additional inputs that are only used for feature
    ///   unification. This may be used to simulate, e.g. `cargo build --package foo --package bar`,
    ///   when you only care about the results of `foo` but specifying `bar` influences the build.
    ///
    /// Note that even if a package is in `features_only`, it may be included in the final build set
    /// through other means (for example, if it is also in `initials` or it is a dependency of one
    /// of them).
    ///
    /// In many cases `features_only` is empty -- in that case you may wish to use
    /// `FeatureSet::into_cargo_set()`, and it may be more convenient to use that if the code is
    /// written in a "fluent" style.
    ///
    ///
    pub fn new(
        initials: FeatureSet<'g>,
        features_only: FeatureSet<'g>,
        opts: &CargoOptions<'_>,
    ) -> Result<Self, Error> {
        Self::new_internal(initials, features_only, None, opts)
    }

    /// Like `Cargo.new`, but takes an additional [`CargoLinkVisitor`] which can
    /// be used to filter out some dependency edges, or to collect additional
    /// information.
    ///
    /// [`visitor.visit_link`] is called for both target and host dependencies. It
    /// is called after static filtering through
    /// [`CargoOptions::add_omitted_packages`], but before any other decisions
    /// are made.
    ///
    /// [`visitor.visit_link`]: CargoLinkVisitor::visit_link
    pub fn with_cargo_link_visitor(
        initials: FeatureSet<'g>,
        features_only: FeatureSet<'g>,
        mut visitor: impl CargoLinkVisitor<'g>,
        opts: &CargoOptions<'_>,
    ) -> Result<Self, Error> {
        Self::new_internal(initials, features_only, Some(&mut visitor), opts)
    }

    /// Internal helper to deduplicate code across `CargoSet::new` and
    /// `CargoSet::with_cargo_link_visitor`.
    fn new_internal(
        initials: FeatureSet<'g>,
        features_only: FeatureSet<'g>,
        visitor: Option<&mut dyn CargoLinkVisitor<'g>>,
        opts: &CargoOptions<'_>,
    ) -> Result<Self, Error> {
        let graph = initials.graph().package_graph;
        let build_state = CargoSetBuildState::new(graph, opts.resolve_package_ids(graph)?)?;
        Ok(build_state.build(initials, features_only, visitor))
    }

    /// Creates a new `CargoIntermediateSet` based on the given query and options.
    ///
    /// This set contains an over-estimate of targets and features.
    ///
    /// Not part of the stable API, exposed for testing.
    #[doc(hidden)]
    pub fn new_intermediate(
        initials: &FeatureSet<'g>,
        opts: &CargoOptions<'_>,
    ) -> Result<CargoIntermediateSet<'g>, Error> {
        let graph = initials.graph().package_graph;
        let build_state = CargoSetBuildState::new(graph, opts.resolve_package_ids(graph)?)?;
        Ok(build_state.build_intermediate(initials.to_feature_query(DependencyDirection::Forward)))
    }

    /// Returns the feature graph for this `CargoSet` instance.
    pub fn feature_graph(&self) -> &FeatureGraph<'g> {
        self.initials.graph()
    }

    /// Returns the package graph for this `CargoSet` instance.
    pub fn package_graph(&self) -> &'g PackageGraph {
        self.feature_graph().package_graph
    }

    /// Returns the initial packages and features from which the `CargoSet` instance was
    /// constructed.
    pub fn initials(&self) -> &FeatureSet<'g> {
        &self.initials
    }

    /// Returns the inputs, other than the initials, from which this `CargoSet`
    /// instance was constructed.
    pub fn inputs(&self) -> &CargoSetInputs<'g> {
        &self.inputs
    }

    /// Returns the feature set enabled on the target platform.
    ///
    /// This represents the packages and features that are included as code in the final build
    /// artifacts. This is relevant for both cross-compilation and auditing.
    pub fn target_features(&self) -> &FeatureSet<'g> {
        &self.target_features
    }

    /// Returns the feature set enabled on the host platform.
    ///
    /// This represents the packages and features that influence the final build artifacts, but
    /// whose code is generally not directly included.
    ///
    /// This includes all procedural macros, including those specified in the initial query.
    pub fn host_features(&self) -> &FeatureSet<'g> {
        &self.host_features
    }

    /// Returns the feature set enabled on the specified build platform.
    pub fn platform_features(&self, build_platform: BuildPlatform) -> &FeatureSet<'g> {
        match build_platform {
            BuildPlatform::Target => self.target_features(),
            BuildPlatform::Host => self.host_features(),
        }
    }

    /// Returns the feature sets across the target and host build platforms.
    pub fn all_features(&self) -> [(BuildPlatform, &FeatureSet<'g>); 2] {
        [
            (BuildPlatform::Target, self.target_features()),
            (BuildPlatform::Host, self.host_features()),
        ]
    }

    /// Returns the set of workspace and direct dependency packages on the target platform.
    ///
    /// The packages in this set are a subset of the packages in `target_features`.
    pub fn target_direct_deps(&self) -> &PackageSet<'g> {
        &self.target_direct_deps
    }

    /// Returns the set of workspace and direct dependency packages on the host platform.
    ///
    /// The packages in this set are a subset of the packages in `host_features`.
    pub fn host_direct_deps(&self) -> &PackageSet<'g> {
        &self.host_direct_deps
    }

    /// Returns the set of workspace and direct dependency packages on the specified build platform.
    pub fn platform_direct_deps(&self, build_platform: BuildPlatform) -> &PackageSet<'g> {
        match build_platform {
            BuildPlatform::Target => self.target_direct_deps(),
            BuildPlatform::Host => self.host_direct_deps(),
        }
    }

    /// Returns the set of workspace and direct dependency packages across the target and host
    /// build platforms.
    pub fn all_direct_deps(&self) -> [(BuildPlatform, &PackageSet<'g>); 2] {
        [
            (BuildPlatform::Target, self.target_direct_deps()),
            (BuildPlatform::Host, self.host_direct_deps()),
        ]
    }

    /// Returns `PackageLink` instances for procedural macro dependencies from target packages.
    ///
    /// Procedural macros straddle the line between target and host: they're built for the host
    /// but generate code that is compiled for the target platform.
    ///
    /// ## Notes
    ///
    /// Procedural macro packages will be included in the *host* feature set.
    /// See also [`Self::host_features`].
    ///
    /// The returned iterator will include proc macros that are depended on normally or in dev
    /// builds from initials (if `include_dev` is set), but not the ones in the
    /// `[build-dependencies]` section.
    pub fn proc_macro_links<'a>(&'a self) -> impl ExactSizeIterator<Item = PackageLink<'g>> + 'a {
        let package_graph = self.target_features.graph().package_graph;
        self.proc_macro_edge_ixs
            .iter()
            .map(move |edge_ix| package_graph.edge_ix_to_link(*edge_ix))
    }

    /// Returns `PackageLink` instances for build dependencies from target packages.
    ///
    /// ## Notes
    ///
    /// For each link, the `from` is built on the target while the `to` is built on the host.
    /// It is possible (though rare) that a build dependency is also included as a normal
    /// dependency, or as a dev dependency in which case it will also be built on the target.
    ///
    /// The returned iterators will not include build dependencies of host packages -- those are
    /// also built on the host.
    pub fn build_dep_links<'a>(&'a self) -> impl ExactSizeIterator<Item = PackageLink<'g>> + 'a {
        let package_graph = self.target_features.graph().package_graph;
        self.build_dep_edge_ixs
            .iter()
            .map(move |edge_ix| package_graph.edge_ix_to_link(*edge_ix))
    }

    /// Returns `PackageLink` instances for normal dependencies between target packages.
    ///
    /// ## Notes
    ///
    /// For each link, both the `from` and the `to` package are built on the target.
    ///
    /// Target packages will be included in the *target* feature set.
    /// See also [`Self::target_features`].
    pub fn target_links<'a>(&'a self) -> impl ExactSizeIterator<Item = PackageLink<'g>> + 'a {
        let package_graph = self.target_features.graph().package_graph;
        self.target_edge_ixs
            .iter()
            .map(move |edge_ix| package_graph.edge_ix_to_link(*edge_ix))
    }

    /// Returns `PackageLink` instances for dependencies between host packages.
    ///
    /// ## Notes
    ///
    /// For each link, both the `from` and the `to` package are built on the host.
    /// Typically most links are normal dependencies, but it is possible to have
    /// build dependencies as well (e.g. dependencies of a build script used
    /// in a proc-macro package).
    ///
    /// Host packages will be included in the *host* feature set.
    /// See also [`Self::host_features`].
    pub fn host_links<'a>(&'a self) -> impl ExactSizeIterator<Item = PackageLink<'g>> + 'a {
        let package_graph = self.target_features.graph().package_graph;
        self.host_edge_ixs
            .iter()
            .map(move |edge_ix| package_graph.edge_ix_to_link(*edge_ix))
    }
}

/// Either the target or the host platform.
///
/// When Cargo computes the platforms it is building on, it computes two separate build graphs: one
/// for the target platform and one for the host. This is most useful in cross-compilation
/// situations where the target is different from the host, but the separate graphs are computed
/// whether or not a build cross-compiles.
///
/// A `cargo check` can be looked at as a kind of cross-compilation as well--machine code is
/// generated and run for the host platform but not the target platform. This is why `cargo check`
/// output usually has some lines that say `Compiling` (for the host platform) and some that say
/// `Checking` (for the target platform).
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum BuildPlatform {
    /// The target platform.
    ///
    /// This represents the packages and features that are included as code in the final build
    /// artifacts.
    Target,

    /// The host platform.
    ///
    /// This represents build scripts, proc macros and other code that is run on the machine doing
    /// the compiling.
    Host,
}

impl BuildPlatform {
    /// A list of all possible variants of `BuildPlatform`.
    pub const VALUES: &'static [Self; 2] = &[BuildPlatform::Target, BuildPlatform::Host];

    /// Returns the build platform that's not `self`.
    pub fn flip(self) -> Self {
        match self {
            BuildPlatform::Host => BuildPlatform::Target,
            BuildPlatform::Target => BuildPlatform::Host,
        }
    }
}

impl fmt::Display for BuildPlatform {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BuildPlatform::Target => write!(f, "target"),
            BuildPlatform::Host => write!(f, "host"),
        }
    }
}

/// An intermediate set representing an overestimate of what packages are built, but an accurate
/// summary of what features are built given a particular package.
///
/// Not part of the stable API, exposed for cargo-compare.
#[doc(hidden)]
#[derive(Debug)]
pub enum CargoIntermediateSet<'g> {
    Unified(FeatureSet<'g>),
    TargetHost {
        target: FeatureSet<'g>,
        host: FeatureSet<'g>,
    },
}

impl<'g> CargoIntermediateSet<'g> {
    #[doc(hidden)]
    pub fn target_host_sets(&self) -> (&FeatureSet<'g>, &FeatureSet<'g>) {
        match self {
            CargoIntermediateSet::Unified(set) => (set, set),
            CargoIntermediateSet::TargetHost { target, host } => (target, host),
        }
    }
}