Skip to content

Commit d336633

Browse files
committed
Auto merge of #4400 - behnam:pr4157, r=alexcrichton
[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>
2 parents 64f4dfd + 6344db0 commit d336633

13 files changed

+76
-21
lines changed

src/bin/bench.rs

Lines changed: 4 additions & 1 deletion
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<'a> CompileOptions<'a> {
8080
spec: ops::Packages::Packages(&[]),
8181
mode: mode,
8282
release: false,
83-
filter: CompileFilter::Everything { required_features_filterable: false },
83+
filter: CompileFilter::Default { required_features_filterable: false },
8484
message_format: MessageFormat::Human,
8585
target_rustdoc_args: None,
8686
target_rustc_args: None,
@@ -159,7 +159,7 @@ pub enum FilterRule<'a> {
159159

160160
#[derive(Debug)]
161161
pub enum CompileFilter<'a> {
162-
Everything {
162+
Default {
163163
/// Flag whether targets can be safely skipped when required-features are not satisfied.
164164
required_features_filterable: bool,
165165
},
@@ -379,29 +379,36 @@ impl<'a> CompileFilter<'a> {
379379
bins: &'a [String], all_bins: bool,
380380
tsts: &'a [String], all_tsts: bool,
381381
exms: &'a [String], all_exms: bool,
382-
bens: &'a [String], all_bens: bool) -> CompileFilter<'a> {
382+
bens: &'a [String], all_bens: bool,
383+
all_targets: bool) -> CompileFilter<'a> {
383384
let rule_bins = FilterRule::new(bins, all_bins);
384385
let rule_tsts = FilterRule::new(tsts, all_tsts);
385386
let rule_exms = FilterRule::new(exms, all_exms);
386387
let rule_bens = FilterRule::new(bens, all_bens);
387388

388-
if lib_only || rule_bins.is_specific() || rule_tsts.is_specific()
389+
if all_targets {
390+
CompileFilter::Only {
391+
lib: true, bins: FilterRule::All,
392+
examples: FilterRule::All, benches: FilterRule::All,
393+
tests: FilterRule::All,
394+
}
395+
} else if lib_only || rule_bins.is_specific() || rule_tsts.is_specific()
389396
|| rule_exms.is_specific() || rule_bens.is_specific() {
390397
CompileFilter::Only {
391398
lib: lib_only, bins: rule_bins,
392399
examples: rule_exms, benches: rule_bens,
393400
tests: rule_tsts,
394401
}
395402
} else {
396-
CompileFilter::Everything {
403+
CompileFilter::Default {
397404
required_features_filterable: true,
398405
}
399406
}
400407
}
401408

402409
pub fn matches(&self, target: &Target) -> bool {
403410
match *self {
404-
CompileFilter::Everything { .. } => true,
411+
CompileFilter::Default { .. } => true,
405412
CompileFilter::Only { lib, bins, examples, tests, benches } => {
406413
let rule = match *target.kind() {
407414
TargetKind::Bin => bins,
@@ -419,7 +426,7 @@ impl<'a> CompileFilter<'a> {
419426

420427
pub fn is_specific(&self) -> bool {
421428
match *self {
422-
CompileFilter::Everything { .. } => false,
429+
CompileFilter::Default { .. } => false,
423430
CompileFilter::Only { .. } => true,
424431
}
425432
}
@@ -601,7 +608,7 @@ fn generate_targets<'a>(pkg: &'a Package,
601608
};
602609

603610
let targets = match *filter {
604-
CompileFilter::Everything { required_features_filterable } => {
611+
CompileFilter::Default { required_features_filterable } => {
605612
let deps = if release {
606613
&profiles.bench_deps
607614
} else {

src/cargo/ops/cargo_install.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 26 additions & 0 deletions
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)