[check|build|rustc] Add --all-targets option
authorStepan Koltsov <stepan.koltsov@gmail.com>
Fri, 11 Aug 2017 01:46:07 +0000 (18:46 -0700)
committerBehnam Esfahbod <behnam@zwnj.org>
Fri, 11 Aug 2017 18:58:01 +0000 (11:58 -0700)
`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 <https://github.com/rust-lang/cargo/pull/4157>

13 files changed:
src/bin/bench.rs
src/bin/build.rs
src/bin/check.rs
src/bin/doc.rs
src/bin/install.rs
src/bin/run.rs
src/bin/rustc.rs
src/bin/rustdoc.rs
src/bin/test.rs
src/cargo/ops/cargo_compile.rs
src/cargo/ops/cargo_install.rs
src/cargo/ops/cargo_package.rs
tests/check.rs

index d164c7a81b686243ae690b772d17f5d0f7247b11..dcee0049e23f372949e914844b92fe11247ced87 100644 (file)
@@ -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,
@@ -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
@@ -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,
index 04a108a40304557c779bd1e77ee60cce70b0191a..b0ee43b4ba459e6693b503614a2bc628ba28a262 100644 (file)
@@ -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,
@@ -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
@@ -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,
index ed4942de61cd0b72ff4656183fe64f969dcb996b..262e4aaf859d8ac3a50a8e3a2ae70ada25a18bb5 100644 (file)
@@ -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
@@ -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,
@@ -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,
index 25aa0ec8a494677916362ca9b45fdbca2fd3cf4b..517d2b6da153c438a53f4564fefc78d53946fe78 100644 (file)
@@ -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 {
index 12518926ca9158741989f0b08380dfa6d11742e5..10ea2d1259cf59d558a3df6aa752d537c927f799 100644 (file)
@@ -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,
index 3ff26fc621200e2bac1fc7487f4064216b81e9b4..58012cb3469e68722d3be870a8b25e35a89615a2 100644 (file)
@@ -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,
index 0616e6f5e0869d1afb582e579faa183c78a297ba..95f6d173b86fea7c8dab9e886b777734bf6cb322 100644 (file)
@@ -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,
@@ -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
@@ -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[..]),
index 55ce9c89d61f106fe43941d1de0d9ea1d37d0eac..82de14c00f528b188a098b021ab0dc285951d2cf 100644 (file)
@@ -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,
 }
@@ -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
@@ -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),
index 7010fbad441eb90206f14c0ec2f9edadbd03e1cc..39322feb5f912bcba6a13f80bd7d09283ab4d9fd 100644 (file)
@@ -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>,
@@ -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
@@ -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(),
index 6d2ba3bd7a065c12b5458e0f83a66dcab4badadd..af1eb0f2e9d89c2dd6a86f4e3846360180af3820 100644 (file)
@@ -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,
@@ -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,
     },
@@ -371,13 +371,20 @@ 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,
@@ -385,7 +392,7 @@ impl<'a> CompileFilter<'a> {
                 tests: rule_tsts,
             }
         } else {
-            CompileFilter::Everything {
+            CompileFilter::Default {
                 required_features_filterable: true,
             }
         }
@@ -393,7 +400,7 @@ impl<'a> CompileFilter<'a> {
 
     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,
@@ -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,
         }
     }
@@ -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 {
index 3ce46881b61fa03efcbc370020246896df246fe1..b6b87a82bb1cc42a0884edadedbbdc1a981fd6ae 100644 (file)
@@ -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()))
index c46c7b2eeeb220c41e454c3f3408b31297f0648f..3bccb7c1fa2280bf9264345df551619accf9a846 100644 (file)
@@ -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,
index d0a0e48fff37b2134f003f6e59009306ecc55618..84ff66b5f3312703ac9df46cd36d6d9decf903d7 100644 (file)
@@ -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 [..]")
+        );
+}