Skip to content

Commit

Permalink
[check|build|rustc] Add --all-targets option
Browse files Browse the repository at this point in the history
`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>
  • Loading branch information
stepancheg authored and behnam committed Aug 11, 2017
1 parent 373c5d8 commit 6344db0
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/bin/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Options {
flag_tests: bool,
flag_bench: Vec<String>,
flag_benches: bool,
flag_all_targets: bool,
flag_no_fail_fast: bool,
flag_frozen: bool,
flag_locked: bool,
Expand All @@ -53,6 +54,7 @@ Options:
--tests Benchmark all tests
--bench NAME Benchmark only the specified bench target
--benches Benchmark all benches
--all-targets Benchmark all targets (default)
--no-run Compile, but don't run benchmarks
-p SPEC, --package SPEC ... Package to run benchmarks for
--all Benchmark all packages in the workspace
Expand Down Expand Up @@ -125,7 +127,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
&options.flag_bin, options.flag_bins,
&options.flag_test, options.flag_tests,
&options.flag_example, options.flag_examples,
&options.flag_bench, options.flag_benches,),
&options.flag_bench, options.flag_benches,
options.flag_all_targets),
message_format: options.flag_message_format,
target_rustdoc_args: None,
target_rustc_args: None,
Expand Down
5 changes: 4 additions & 1 deletion src/bin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Options {
flag_tests: bool,
flag_bench: Vec<String>,
flag_benches: bool,
flag_all_targets: bool,
flag_locked: bool,
flag_frozen: bool,
flag_all: bool,
Expand Down Expand Up @@ -55,6 +56,7 @@ Options:
--tests Build all tests
--bench NAME Build only the specified bench target
--benches Build all benches
--all-targets Build all targets (lib and bin targets by default)
--release Build artifacts in release mode, with optimizations
--features FEATURES Space-separated list of features to also build
--all-features Build all available features
Expand Down Expand Up @@ -113,7 +115,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
&options.flag_bin, options.flag_bins,
&options.flag_test, options.flag_tests,
&options.flag_example, options.flag_examples,
&options.flag_bench, options.flag_benches,),
&options.flag_bench, options.flag_benches,
options.flag_all_targets),
message_format: options.flag_message_format,
target_rustdoc_args: None,
target_rustc_args: None,
Expand Down
5 changes: 4 additions & 1 deletion src/bin/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Options:
--tests Check all tests
--bench NAME Check only the specified bench target
--benches Check all benches
--all-targets Check all targets (lib and bin targets by default)
--release Check artifacts in release mode, with optimizations
--features FEATURES Space-separated list of features to also check
--all-features Check all available features
Expand Down Expand Up @@ -72,6 +73,7 @@ pub struct Options {
flag_tests: bool,
flag_bench: Vec<String>,
flag_benches: bool,
flag_all_targets: bool,
flag_locked: bool,
flag_frozen: bool,
flag_all: bool,
Expand Down Expand Up @@ -110,7 +112,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
&options.flag_bin, options.flag_bins,
&options.flag_test, options.flag_tests,
&options.flag_example, options.flag_examples,
&options.flag_bench, options.flag_benches,),
&options.flag_bench, options.flag_benches,
options.flag_all_targets),
message_format: options.flag_message_format,
target_rustdoc_args: None,
target_rustc_args: None,
Expand Down
3 changes: 2 additions & 1 deletion src/bin/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
&options.flag_bin, options.flag_bins,
&empty, false,
&empty, false,
&empty, false),
&empty, false,
false),
message_format: options.flag_message_format,
release: options.flag_release,
mode: ops::CompileMode::Doc {
Expand Down
3 changes: 2 additions & 1 deletion src/bin/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
&options.flag_bin, options.flag_bins,
&[], false,
&options.flag_example, options.flag_examples,
&[], false),
&[], false,
false),
message_format: ops::MessageFormat::Human,
target_rustc_args: None,
target_rustdoc_args: None,
Expand Down
5 changes: 3 additions & 2 deletions src/bin/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
release: options.flag_release,
mode: ops::CompileMode::Build,
filter: if examples.is_empty() && bins.is_empty() {
ops::CompileFilter::Everything { required_features_filterable: false, }
ops::CompileFilter::Default { required_features_filterable: false, }
} else {
ops::CompileFilter::new(false,
&bins, false,
&[], false,
&examples, false,
&[], false)
&[], false,
false)
},
message_format: options.flag_message_format,
target_rustdoc_args: None,
Expand Down
5 changes: 4 additions & 1 deletion src/bin/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct Options {
flag_tests: bool,
flag_bench: Vec<String>,
flag_benches: bool,
flag_all_targets: bool,
flag_profile: Option<String>,
flag_frozen: bool,
flag_locked: bool,
Expand All @@ -53,6 +54,7 @@ Options:
--tests Build all tests
--bench NAME Build only the specified bench target
--benches Build all benches
--all-targets Build all targets (lib and bin targets by default)
--release Build artifacts in release mode, with optimizations
--profile PROFILE Profile to build the selected target for
--features FEATURES Features to compile for the package
Expand Down Expand Up @@ -120,7 +122,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
&options.flag_bin, options.flag_bins,
&options.flag_test, options.flag_tests,
&options.flag_example, options.flag_examples,
&options.flag_bench, options.flag_benches,),
&options.flag_bench, options.flag_benches,
options.flag_all_targets),
message_format: options.flag_message_format,
target_rustdoc_args: None,
target_rustc_args: options.arg_opts.as_ref().map(|a| &a[..]),
Expand Down
5 changes: 4 additions & 1 deletion src/bin/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Options {
flag_tests: bool,
flag_bench: Vec<String>,
flag_benches: bool,
flag_all_targets: bool,
flag_frozen: bool,
flag_locked: bool,
}
Expand All @@ -52,6 +53,7 @@ Options:
--tests Build all tests
--bench NAME Build only the specified bench target
--benches Build all benches
--all-targets Build all targets (default)
--release Build artifacts in release mode, with optimizations
--features FEATURES Space-separated list of features to also build
--all-features Build all available features
Expand Down Expand Up @@ -105,7 +107,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
&options.flag_bin, options.flag_bins,
&options.flag_test, options.flag_tests,
&options.flag_example, options.flag_examples,
&options.flag_bench, options.flag_benches,),
&options.flag_bench, options.flag_benches,
options.flag_all_targets),
message_format: options.flag_message_format,
mode: ops::CompileMode::Doc { deps: false },
target_rustdoc_args: Some(&options.arg_opts),
Expand Down
8 changes: 6 additions & 2 deletions src/bin/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Options {
flag_tests: bool,
flag_bench: Vec<String>,
flag_benches: bool,
flag_all_targets: bool,
flag_verbose: u32,
flag_quiet: Option<bool>,
flag_color: Option<String>,
Expand Down Expand Up @@ -56,6 +57,7 @@ Options:
--tests Test all tests
--bench NAME ... Test only the specified bench target
--benches Test all benches
--all-targets Test all targets (default)
--no-run Compile, but don't run tests
-p SPEC, --package SPEC ... Package to run tests for
--all Test all packages in the workspace
Expand Down Expand Up @@ -128,14 +130,16 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
if options.flag_doc {
mode = ops::CompileMode::Doctest;
filter = ops::CompileFilter::new(true, &empty, false, &empty, false,
&empty, false, &empty, false);
&empty, false, &empty, false,
false);
} else {
mode = ops::CompileMode::Test;
filter = ops::CompileFilter::new(options.flag_lib,
&options.flag_bin, options.flag_bins,
&options.flag_test, options.flag_tests,
&options.flag_example, options.flag_examples,
&options.flag_bench, options.flag_benches);
&options.flag_bench, options.flag_benches,
options.flag_all_targets);
}

let spec = Packages::from_flags(ws.is_virtual(),
Expand Down
23 changes: 15 additions & 8 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'a> CompileOptions<'a> {
spec: ops::Packages::Packages(&[]),
mode: mode,
release: false,
filter: CompileFilter::Everything { required_features_filterable: false },
filter: CompileFilter::Default { required_features_filterable: false },
message_format: MessageFormat::Human,
target_rustdoc_args: None,
target_rustc_args: None,
Expand Down Expand Up @@ -158,7 +158,7 @@ pub enum FilterRule<'a> {

#[derive(Debug)]
pub enum CompileFilter<'a> {
Everything {
Default {
/// Flag whether targets can be safely skipped when required-features are not satisfied.
required_features_filterable: bool,
},
Expand Down Expand Up @@ -371,29 +371,36 @@ impl<'a> CompileFilter<'a> {
bins: &'a [String], all_bins: bool,
tsts: &'a [String], all_tsts: bool,
exms: &'a [String], all_exms: bool,
bens: &'a [String], all_bens: bool) -> CompileFilter<'a> {
bens: &'a [String], all_bens: bool,
all_targets: bool) -> CompileFilter<'a> {
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);

if lib_only || rule_bins.is_specific() || rule_tsts.is_specific()
if all_targets {
CompileFilter::Only {
lib: true, bins: FilterRule::All,
examples: FilterRule::All, benches: FilterRule::All,
tests: FilterRule::All,
}
} else if lib_only || rule_bins.is_specific() || rule_tsts.is_specific()
|| rule_exms.is_specific() || rule_bens.is_specific() {
CompileFilter::Only {
lib: lib_only, bins: rule_bins,
examples: rule_exms, benches: rule_bens,
tests: rule_tsts,
}
} else {
CompileFilter::Everything {
CompileFilter::Default {
required_features_filterable: true,
}
}
}

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

pub fn is_specific(&self) -> bool {
match *self {
CompileFilter::Everything { .. } => false,
CompileFilter::Default { .. } => false,
CompileFilter::Only { .. } => true,
}
}
Expand Down Expand Up @@ -593,7 +600,7 @@ fn generate_targets<'a>(pkg: &'a Package,
};

let targets = match *filter {
CompileFilter::Everything { required_features_filterable } => {
CompileFilter::Default { required_features_filterable } => {
let deps = if release {
&profiles.bench_deps
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ fn find_duplicates(dst: &Path,
}
};
match *filter {
CompileFilter::Everything { .. } => {
CompileFilter::Default { .. } => {
pkg.targets().iter()
.filter(|t| t.is_bin())
.filter_map(|t| check(t.name().to_string()))
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ fn run_verify(ws: &Workspace, tar: &File, opts: &PackageOpts) -> CargoResult<()>
no_default_features: false,
all_features: false,
spec: ops::Packages::Packages(&[]),
filter: ops::CompileFilter::Everything { required_features_filterable: true },
filter: ops::CompileFilter::Default { required_features_filterable: true },
release: false,
message_format: ops::MessageFormat::Human,
mode: ops::CompileMode::Build,
Expand Down
26 changes: 26 additions & 0 deletions tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,29 @@ fn check_virtual_all_implied() {
.with_stderr_contains("[..] --crate-name bar bar[/]src[/]lib.rs [..]")
);
}

#[test]
fn check_all_targets() {
let foo = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#)
.file("src/main.rs", "fn main() {}")
.file("src/lib.rs", "pub fn smth() {}")
.file("examples/example1.rs", "fn main() {}")
.file("tests/test2.rs", "#[test] fn t() {}")
.file("benches/bench3.rs", "")
;

assert_that(foo.cargo_process("check").arg("--all-targets").arg("-v"),
execs().with_status(0)
.with_stderr_contains("[..] --crate-name foo src[/]lib.rs [..]")
.with_stderr_contains("[..] --crate-name foo src[/]main.rs [..]")
.with_stderr_contains("[..] --crate-name example1 examples[/]example1.rs [..]")
.with_stderr_contains("[..] --crate-name test2 tests[/]test2.rs [..]")
.with_stderr_contains("[..] --crate-name bench3 benches[/]bench3.rs [..]")
);
}

0 comments on commit 6344db0

Please sign in to comment.