#
cargo/ops/cargo_compile/
compile_filter.rs

    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

    //! Filters and their rules to select which Cargo targets will be built.

    use crate::core::compiler::CompileMode;

    use crate::core::{Target, TargetKind};
    use crate::util::restricted_names::is_glob_pattern;

    #[derive(Debug, PartialEq, Eq, Clone)]
    /// Indicates whether or not the library target gets included.
    pub enum LibRule {
        /// Include the library, fail if not present
        True,
        /// Include the library if present
        Default,
        /// Exclude the library
        False,
    }

    #[derive(Debug, Clone)]
    /// Indicates which Cargo targets will be selected to be built.
    pub enum FilterRule {
        /// All included.
        All,
        /// Just a subset of Cargo targets based on names given.
        Just(Vec<String>),
    }

    /// Filter to apply to the root package to select which Cargo targets will be built.
    /// (examples, bins, benches, tests, ...)
    ///
    /// The actual filter process happens inside [`generate_root_units`].
    ///
    /// Not to be confused with [`Packages`], which opts in packages to be built.
    ///
    /// [`generate_root_units`]: super::UnitGenerator::generate_root_units
    /// [`Packages`]: crate::ops::Packages
    #[derive(Debug, Clone)]
    pub enum CompileFilter {
        /// The default set of Cargo targets.
        Default {
            /// Flag whether targets can be safely skipped when required-features are not satisfied.
            required_features_filterable: bool,
        },
        /// Only includes a subset of all Cargo targets.
        Only {
            /// Include all Cargo targets.
            all_targets: bool,
            lib: LibRule,
            bins: FilterRule,
            examples: FilterRule,
            tests: FilterRule,
            benches: FilterRule,
        },
    }

    impl FilterRule {
        pub fn new(targets: Vec<String>, all: bool) -> FilterRule {
            if all {
                FilterRule::All
            } else {
                FilterRule::Just(targets)
            }
        }

        /// Creates a filter with no rule.
        ///
        /// In the current Cargo implementation, filter without a rule implies
        /// Cargo will follows the default behaviour to filter targets.
        pub fn none() -> FilterRule {
            FilterRule::Just(Vec::new())
        }

        /// Checks if a target definition matches this filter rule.
        fn matches(&self, target: &Target) -> bool {
            match *self {
                FilterRule::All => true,
                FilterRule::Just(ref targets) => targets.iter().any(|x| *x == target.name()),
            }
        }

        /// Check if a filter is specific.
        ///
        /// Only filters without rules are considered as not specific.
        fn is_specific(&self) -> bool {
            match *self {
                FilterRule::All => true,
                FilterRule::Just(ref targets) => !targets.is_empty(),
            }
        }

        /// Checks if any specified target name contains glob patterns.
        pub(crate) fn contains_glob_patterns(&self) -> bool {
            match self {
                FilterRule::All => false,
                FilterRule::Just(targets) => targets.iter().any(is_glob_pattern),
            }
        }
    }

    impl CompileFilter {
        /// Constructs a filter from raw command line arguments.
        pub fn from_raw_arguments(
            lib_only: bool,
            bins: Vec<String>,
            all_bins: bool,
            tsts: Vec<String>,
            all_tsts: bool,
            exms: Vec<String>,
            all_exms: bool,
            bens: Vec<String>,
            all_bens: bool,
            all_targets: bool,
        ) -> CompileFilter {
            if all_targets {
                return CompileFilter::new_all_targets();
            }
            let rule_lib = if lib_only {
                LibRule::True
            } else {
                LibRule::False
            };
            let rule_bins = FilterRule::new(bins, all_bins);
            let rule_tsts = FilterRule::new(tsts, all_tsts);
            let rule_exms = FilterRule::new(exms, all_exms);
            let rule_bens = FilterRule::new(bens, all_bens);

            CompileFilter::new(rule_lib, rule_bins, rule_tsts, rule_exms, rule_bens)
        }

        /// Constructs a filter from underlying primitives.
        pub fn new(
            rule_lib: LibRule,
            rule_bins: FilterRule,
            rule_tsts: FilterRule,
            rule_exms: FilterRule,
            rule_bens: FilterRule,
        ) -> CompileFilter {
            if rule_lib == LibRule::True
                || rule_bins.is_specific()
                || rule_tsts.is_specific()
                || rule_exms.is_specific()
                || rule_bens.is_specific()
            {
                CompileFilter::Only {
                    all_targets: false,
                    lib: rule_lib,
                    bins: rule_bins,
                    examples: rule_exms,
                    benches: rule_bens,
                    tests: rule_tsts,
                }
            } else {
                CompileFilter::Default {
                    required_features_filterable: true,
                }
            }
        }

        /// Constructs a filter that includes all targets.
        pub fn new_all_targets() -> CompileFilter {
            CompileFilter::Only {
                all_targets: true,
                lib: LibRule::Default,
                bins: FilterRule::All,
                examples: FilterRule::All,
                benches: FilterRule::All,
                tests: FilterRule::All,
            }
        }

        /// Constructs a filter that includes all test targets.
        ///
        /// Being different from the behavior of [`CompileFilter::Default`], this
        /// function only recognizes test targets, which means cargo might compile
        /// all targets with `tested` flag on, whereas [`CompileFilter::Default`]
        /// may include additional example targets to ensure they can be compiled.
        ///
        /// Note that the actual behavior is subject to [`filter_default_targets`]
        /// and [`generate_root_units`] though.
        ///
        /// [`generate_root_units`]: super::UnitGenerator::generate_root_units
        /// [`filter_default_targets`]: super::UnitGenerator::filter_default_targets
        pub fn all_test_targets() -> Self {
            Self::Only {
                all_targets: false,
                lib: LibRule::Default,
                bins: FilterRule::none(),
                examples: FilterRule::none(),
                tests: FilterRule::All,
                benches: FilterRule::none(),
            }
        }

        /// Constructs a filter that includes lib target only.
        pub fn lib_only() -> Self {
            Self::Only {
                all_targets: false,
                lib: LibRule::True,
                bins: FilterRule::none(),
                examples: FilterRule::none(),
                tests: FilterRule::none(),
                benches: FilterRule::none(),
            }
        }

        /// Constructs a filter that includes the given binary. No more. No less.
        pub fn single_bin(bin: String) -> Self {
            Self::Only {
                all_targets: false,
                lib: LibRule::False,
                bins: FilterRule::new(vec![bin], false),
                examples: FilterRule::none(),
                tests: FilterRule::none(),
                benches: FilterRule::none(),
            }
        }

        /// Indicates if Cargo needs to build any dev dependency.
        pub fn need_dev_deps(&self, mode: CompileMode) -> bool {
            match mode {
                CompileMode::Test | CompileMode::Doctest | CompileMode::Bench => true,
                CompileMode::Check { test: true } => true,
                CompileMode::Build
                | CompileMode::Doc { .. }
                | CompileMode::Docscrape
                | CompileMode::Check { test: false } => match *self {
                    CompileFilter::Default { .. } => false,
                    CompileFilter::Only {
                        ref examples,
                        ref tests,
                        ref benches,
                        ..
                    } => examples.is_specific() || tests.is_specific() || benches.is_specific(),
                },
                CompileMode::RunCustomBuild => panic!("Invalid mode"),
            }
        }

        /// Selects targets for "cargo run". for logic to select targets for other
        /// subcommands, see [`generate_root_units`] and [`filter_default_targets`].
        ///
        /// [`generate_root_units`]: super::UnitGenerator::generate_root_units
        /// [`filter_default_targets`]: super::UnitGenerator::filter_default_targets
        pub fn target_run(&self, target: &Target) -> bool {
            match *self {
                CompileFilter::Default { .. } => true,
                CompileFilter::Only {
                    ref lib,
                    ref bins,
                    ref examples,
                    ref tests,
                    ref benches,
                    ..
                } => {
                    let rule = match *target.kind() {
                        TargetKind::Bin => bins,
                        TargetKind::Test => tests,
                        TargetKind::Bench => benches,
                        TargetKind::ExampleBin | TargetKind::ExampleLib(..) => examples,
                        TargetKind::Lib(..) => {
                            return match *lib {
                                LibRule::True => true,
                                LibRule::Default => true,
                                LibRule::False => false,
                            };
                        }
                        TargetKind::CustomBuild => return false,
                    };
                    rule.matches(target)
                }
            }
        }

        pub fn is_specific(&self) -> bool {
            match *self {
                CompileFilter::Default { .. } => false,
                CompileFilter::Only { .. } => true,
            }
        }

        pub fn is_all_targets(&self) -> bool {
            matches!(
                *self,
                CompileFilter::Only {
                    all_targets: true,
                    ..
                }
            )
        }

        /// Checks if any specified target name contains glob patterns.
        pub(crate) fn contains_glob_patterns(&self) -> bool {
            match self {
                CompileFilter::Default { .. } => false,
                CompileFilter::Only {
                    bins,
                    examples,
                    tests,
                    benches,
                    ..
                } => {
                    bins.contains_glob_patterns()
                        || examples.contains_glob_patterns()
                        || tests.contains_glob_patterns()
                        || benches.contains_glob_patterns()
                }
            }
        }
    }