Skip to content

Commit 6344db0

Browse files
stepanchegbehnam
authored andcommitted
[check|build|rustc] Add --all-targets option
`cargo check` does not check all targets by default, and to check all, you need to call it `cargo check --tests --examples --bins --benches`. Here, we implement `--all-targets` For `check`, `build`, and `rustc`. For consitency, `--all-targets` is also added to other commands like `test` although "all targets" is the default behavior. This is a rebase of <#4157>
1 parent 373c5d8 commit 6344db0

13 files changed

+76
-21
lines changed

src/bin/bench.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct Options {
2828
flag_tests: bool,
2929
flag_bench: Vec<String>,
3030
flag_benches: bool,
31+
flag_all_targets: bool,
3132
flag_no_fail_fast: bool,
3233
flag_frozen: bool,
3334
flag_locked: bool,
@@ -53,6 +54,7 @@ Options:
5354
--tests Benchmark all tests
5455
--bench NAME Benchmark only the specified bench target
5556
--benches Benchmark all benches
57+
--all-targets Benchmark all targets (default)
5658
--no-run Compile, but don't run benchmarks
5759
-p SPEC, --package SPEC ... Package to run benchmarks for
5860
--all Benchmark all packages in the workspace
@@ -125,7 +127,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
125127
&options.flag_bin, options.flag_bins,
126128
&options.flag_test, options.flag_tests,
127129
&options.flag_example, options.flag_examples,
128-
&options.flag_bench, options.flag_benches,),
130+
&options.flag_bench, options.flag_benches,
131+
options.flag_all_targets),
129132
message_format: options.flag_message_format,
130133
target_rustdoc_args: None,
131134
target_rustc_args: None,

src/bin/build.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct Options {
2828
flag_tests: bool,
2929
flag_bench: Vec<String>,
3030
flag_benches: bool,
31+
flag_all_targets: bool,
3132
flag_locked: bool,
3233
flag_frozen: bool,
3334
flag_all: bool,
@@ -55,6 +56,7 @@ Options:
5556
--tests Build all tests
5657
--bench NAME Build only the specified bench target
5758
--benches Build all benches
59+
--all-targets Build all targets (lib and bin targets by default)
5860
--release Build artifacts in release mode, with optimizations
5961
--features FEATURES Space-separated list of features to also build
6062
--all-features Build all available features
@@ -113,7 +115,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
113115
&options.flag_bin, options.flag_bins,
114116
&options.flag_test, options.flag_tests,
115117
&options.flag_example, options.flag_examples,
116-
&options.flag_bench, options.flag_benches,),
118+
&options.flag_bench, options.flag_benches,
119+
options.flag_all_targets),
117120
message_format: options.flag_message_format,
118121
target_rustdoc_args: None,
119122
target_rustc_args: None,

src/bin/check.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Options:
2626
--tests Check all tests
2727
--bench NAME Check only the specified bench target
2828
--benches Check all benches
29+
--all-targets Check all targets (lib and bin targets by default)
2930
--release Check artifacts in release mode, with optimizations
3031
--features FEATURES Space-separated list of features to also check
3132
--all-features Check all available features
@@ -72,6 +73,7 @@ pub struct Options {
7273
flag_tests: bool,
7374
flag_bench: Vec<String>,
7475
flag_benches: bool,
76+
flag_all_targets: bool,
7577
flag_locked: bool,
7678
flag_frozen: bool,
7779
flag_all: bool,
@@ -110,7 +112,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
110112
&options.flag_bin, options.flag_bins,
111113
&options.flag_test, options.flag_tests,
112114
&options.flag_example, options.flag_examples,
113-
&options.flag_bench, options.flag_benches,),
115+
&options.flag_bench, options.flag_benches,
116+
options.flag_all_targets),
114117
message_format: options.flag_message_format,
115118
target_rustdoc_args: None,
116119
target_rustc_args: None,

src/bin/doc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
104104
&options.flag_bin, options.flag_bins,
105105
&empty, false,
106106
&empty, false,
107-
&empty, false),
107+
&empty, false,
108+
false),
108109
message_format: options.flag_message_format,
109110
release: options.flag_release,
110111
mode: ops::CompileMode::Doc {

src/bin/install.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
119119
&options.flag_bin, options.flag_bins,
120120
&[], false,
121121
&options.flag_example, options.flag_examples,
122-
&[], false),
122+
&[], false,
123+
false),
123124
message_format: ops::MessageFormat::Human,
124125
target_rustc_args: None,
125126
target_rustdoc_args: None,

src/bin/run.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
9292
release: options.flag_release,
9393
mode: ops::CompileMode::Build,
9494
filter: if examples.is_empty() && bins.is_empty() {
95-
ops::CompileFilter::Everything { required_features_filterable: false, }
95+
ops::CompileFilter::Default { required_features_filterable: false, }
9696
} else {
9797
ops::CompileFilter::new(false,
9898
&bins, false,
9999
&[], false,
100100
&examples, false,
101-
&[], false)
101+
&[], false,
102+
false)
102103
},
103104
message_format: options.flag_message_format,
104105
target_rustdoc_args: None,

src/bin/rustc.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct Options {
2929
flag_tests: bool,
3030
flag_bench: Vec<String>,
3131
flag_benches: bool,
32+
flag_all_targets: bool,
3233
flag_profile: Option<String>,
3334
flag_frozen: bool,
3435
flag_locked: bool,
@@ -53,6 +54,7 @@ Options:
5354
--tests Build all tests
5455
--bench NAME Build only the specified bench target
5556
--benches Build all benches
57+
--all-targets Build all targets (lib and bin targets by default)
5658
--release Build artifacts in release mode, with optimizations
5759
--profile PROFILE Profile to build the selected target for
5860
--features FEATURES Features to compile for the package
@@ -120,7 +122,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
120122
&options.flag_bin, options.flag_bins,
121123
&options.flag_test, options.flag_tests,
122124
&options.flag_example, options.flag_examples,
123-
&options.flag_bench, options.flag_benches,),
125+
&options.flag_bench, options.flag_benches,
126+
options.flag_all_targets),
124127
message_format: options.flag_message_format,
125128
target_rustdoc_args: None,
126129
target_rustc_args: options.arg_opts.as_ref().map(|a| &a[..]),

src/bin/rustdoc.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct Options {
2828
flag_tests: bool,
2929
flag_bench: Vec<String>,
3030
flag_benches: bool,
31+
flag_all_targets: bool,
3132
flag_frozen: bool,
3233
flag_locked: bool,
3334
}
@@ -52,6 +53,7 @@ Options:
5253
--tests Build all tests
5354
--bench NAME Build only the specified bench target
5455
--benches Build all benches
56+
--all-targets Build all targets (default)
5557
--release Build artifacts in release mode, with optimizations
5658
--features FEATURES Space-separated list of features to also build
5759
--all-features Build all available features
@@ -105,7 +107,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
105107
&options.flag_bin, options.flag_bins,
106108
&options.flag_test, options.flag_tests,
107109
&options.flag_example, options.flag_examples,
108-
&options.flag_bench, options.flag_benches,),
110+
&options.flag_bench, options.flag_benches,
111+
options.flag_all_targets),
109112
message_format: options.flag_message_format,
110113
mode: ops::CompileMode::Doc { deps: false },
111114
target_rustdoc_args: Some(&options.arg_opts),

src/bin/test.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct Options {
2626
flag_tests: bool,
2727
flag_bench: Vec<String>,
2828
flag_benches: bool,
29+
flag_all_targets: bool,
2930
flag_verbose: u32,
3031
flag_quiet: Option<bool>,
3132
flag_color: Option<String>,
@@ -56,6 +57,7 @@ Options:
5657
--tests Test all tests
5758
--bench NAME ... Test only the specified bench target
5859
--benches Test all benches
60+
--all-targets Test all targets (default)
5961
--no-run Compile, but don't run tests
6062
-p SPEC, --package SPEC ... Package to run tests for
6163
--all Test all packages in the workspace
@@ -128,14 +130,16 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
128130
if options.flag_doc {
129131
mode = ops::CompileMode::Doctest;
130132
filter = ops::CompileFilter::new(true, &empty, false, &empty, false,
131-
&empty, false, &empty, false);
133+
&empty, false, &empty, false,
134+
false);
132135
} else {
133136
mode = ops::CompileMode::Test;
134137
filter = ops::CompileFilter::new(options.flag_lib,
135138
&options.flag_bin, options.flag_bins,
136139
&options.flag_test, options.flag_tests,
137140
&options.flag_example, options.flag_examples,
138-
&options.flag_bench, options.flag_benches);
141+
&options.flag_bench, options.flag_benches,
142+
options.flag_all_targets);
139143
}
140144

141145
let spec = Packages::from_flags(ws.is_virtual(),

src/cargo/ops/cargo_compile.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'a> CompileOptions<'a> {
7979
spec: ops::Packages::Packages(&[]),
8080
mode: mode,
8181
release: false,
82-
filter: CompileFilter::Everything { required_features_filterable: false },
82+
filter: CompileFilter::Default { required_features_filterable: false },
8383
message_format: MessageFormat::Human,
8484
target_rustdoc_args: None,
8585
target_rustc_args: None,
@@ -158,7 +158,7 @@ pub enum FilterRule<'a> {
158158

159159
#[derive(Debug)]
160160
pub enum CompileFilter<'a> {
161-
Everything {
161+
Default {
162162
/// Flag whether targets can be safely skipped when required-features are not satisfied.
163163
required_features_filterable: bool,
164164
},
@@ -371,29 +371,36 @@ impl<'a> CompileFilter<'a> {
371371
bins: &'a [String], all_bins: bool,
372372
tsts: &'a [String], all_tsts: bool,
373373
exms: &'a [String], all_exms: bool,
374-
bens: &'a [String], all_bens: bool) -> CompileFilter<'a> {
374+
bens: &'a [String], all_bens: bool,
375+
all_targets: bool) -> CompileFilter<'a> {
375376
let rule_bins = FilterRule::new(bins, all_bins);
376377
let rule_tsts = FilterRule::new(tsts, all_tsts);
377378
let rule_exms = FilterRule::new(exms, all_exms);
378379
let rule_bens = FilterRule::new(bens, all_bens);
379380

380-
if lib_only || rule_bins.is_specific() || rule_tsts.is_specific()
381+
if all_targets {
382+
CompileFilter::Only {
383+
lib: true, bins: FilterRule::All,
384+
examples: FilterRule::All, benches: FilterRule::All,
385+
tests: FilterRule::All,
386+
}
387+
} else if lib_only || rule_bins.is_specific() || rule_tsts.is_specific()
381388
|| rule_exms.is_specific() || rule_bens.is_specific() {
382389
CompileFilter::Only {
383390
lib: lib_only, bins: rule_bins,
384391
examples: rule_exms, benches: rule_bens,
385392
tests: rule_tsts,
386393
}
387394
} else {
388-
CompileFilter::Everything {
395+
CompileFilter::Default {
389396
required_features_filterable: true,
390397
}
391398
}
392399
}
393400

394401
pub fn matches(&self, target: &Target) -> bool {
395402
match *self {
396-
CompileFilter::Everything { .. } => true,
403+
CompileFilter::Default { .. } => true,
397404
CompileFilter::Only { lib, bins, examples, tests, benches } => {
398405
let rule = match *target.kind() {
399406
TargetKind::Bin => bins,
@@ -411,7 +418,7 @@ impl<'a> CompileFilter<'a> {
411418

412419
pub fn is_specific(&self) -> bool {
413420
match *self {
414-
CompileFilter::Everything { .. } => false,
421+
CompileFilter::Default { .. } => false,
415422
CompileFilter::Only { .. } => true,
416423
}
417424
}
@@ -593,7 +600,7 @@ fn generate_targets<'a>(pkg: &'a Package,
593600
};
594601

595602
let targets = match *filter {
596-
CompileFilter::Everything { required_features_filterable } => {
603+
CompileFilter::Default { required_features_filterable } => {
597604
let deps = if release {
598605
&profiles.bench_deps
599606
} else {

src/cargo/ops/cargo_install.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ fn find_duplicates(dst: &Path,
490490
}
491491
};
492492
match *filter {
493-
CompileFilter::Everything { .. } => {
493+
CompileFilter::Default { .. } => {
494494
pkg.targets().iter()
495495
.filter(|t| t.is_bin())
496496
.filter_map(|t| check(t.name().to_string()))

src/cargo/ops/cargo_package.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ fn run_verify(ws: &Workspace, tar: &File, opts: &PackageOpts) -> CargoResult<()>
299299
no_default_features: false,
300300
all_features: false,
301301
spec: ops::Packages::Packages(&[]),
302-
filter: ops::CompileFilter::Everything { required_features_filterable: true },
302+
filter: ops::CompileFilter::Default { required_features_filterable: true },
303303
release: false,
304304
message_format: ops::MessageFormat::Human,
305305
mode: ops::CompileMode::Build,

tests/check.rs

+26
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,29 @@ fn check_virtual_all_implied() {
424424
.with_stderr_contains("[..] --crate-name bar bar[/]src[/]lib.rs [..]")
425425
);
426426
}
427+
428+
#[test]
429+
fn check_all_targets() {
430+
let foo = project("foo")
431+
.file("Cargo.toml", r#"
432+
[package]
433+
name = "foo"
434+
version = "0.0.1"
435+
authors = []
436+
"#)
437+
.file("src/main.rs", "fn main() {}")
438+
.file("src/lib.rs", "pub fn smth() {}")
439+
.file("examples/example1.rs", "fn main() {}")
440+
.file("tests/test2.rs", "#[test] fn t() {}")
441+
.file("benches/bench3.rs", "")
442+
;
443+
444+
assert_that(foo.cargo_process("check").arg("--all-targets").arg("-v"),
445+
execs().with_status(0)
446+
.with_stderr_contains("[..] --crate-name foo src[/]lib.rs [..]")
447+
.with_stderr_contains("[..] --crate-name foo src[/]main.rs [..]")
448+
.with_stderr_contains("[..] --crate-name example1 examples[/]example1.rs [..]")
449+
.with_stderr_contains("[..] --crate-name test2 tests[/]test2.rs [..]")
450+
.with_stderr_contains("[..] --crate-name bench3 benches[/]bench3.rs [..]")
451+
);
452+
}

0 commit comments

Comments
 (0)