Avoid building libs and bins twice in `cargo build --all-targets --release`.
authorEric Huss <eric@huss.org>
Sun, 22 Apr 2018 20:57:38 +0000 (13:57 -0700)
committerEric Huss <eric@huss.org>
Fri, 27 Apr 2018 20:42:30 +0000 (13:42 -0700)
This changes it so that `Build` mode is not set in the Unit, since there is
no difference between Bench and Test once the profile has been selected.

src/cargo/ops/cargo_compile.rs
tests/testsuite/profile_targets.rs

index e0c4e0b1e40df305a06d6acfdef71f8cd8ed0496..624659784717d8bb92c535a8953926489e80b26a 100644 (file)
@@ -112,7 +112,10 @@ pub enum CompileMode {
     /// `test` is true, then it is also compiled with `--test` to check it like
     /// a test.
     Check { test: bool },
-    /// A target being built for a benchmark.
+    /// Used to indicate benchmarks should be built.  This is not used in
+    /// `Target` because it is essentially the same as `Test` (indicating
+    /// `--test` should be passed to rustc) and by using `Test` instead it
+    /// allows some de-duping of Units to occur.
     Bench,
     /// A target that will be documented with `rustdoc`.
     /// If `deps` is true, then it will also document all dependencies.
@@ -629,6 +632,17 @@ fn generate_targets<'a>(
             target_mode,
             release,
         );
+        // Once the profile has been selected for benchmarks, we don't need to
+        // distinguish between benches and tests. Switching the mode allows
+        // de-duplication of units that are essentially identical.  For
+        // example, `cargo build --all-targets --release` creates the units
+        // (lib profile:bench, mode:test) and (lib profile:bench, mode:bench)
+        // and since these are the same, we want them to be de-duped in
+        // `unit_dependencies`.
+        let target_mode = match target_mode {
+            CompileMode::Bench => CompileMode::Test,
+            _ => target_mode,
+        };
         Unit {
             pkg,
             target,
index 91450742e625fa57acb7f4f6320d3fa6a5348c5b..7a245fe8a959374ba56d150e96d3b494a6d40b87 100644 (file)
@@ -178,11 +178,11 @@ fn profile_selection_build_all_targets() {
     //   lib      dev+panic  build  (a normal lib target)
     //   lib      dev-panic  build  (used by tests/benches)
     //   lib      test       test
-    //   lib      bench      bench
+    //   lib      bench      test(bench)
     //   test     test       test
-    //   bench    bench      bench
+    //   bench    bench      test(bench)
     //   bin      test       test
-    //   bin      bench      bench
+    //   bin      bench      test(bench)
     //   bin      dev        build
     //   example  dev        build
     assert_that(p.cargo("build --all-targets -vv"),
@@ -253,12 +253,10 @@ fn profile_selection_build_all_targets_release() {
     //   ------   -------        ----
     //   lib      release+panic  build  (a normal lib target)
     //   lib      release-panic  build  (used by tests/benches)
-    //   lib      bench          test
-    //   lib      bench          bench
+    //   lib      bench          test   (bench/test de-duped)
     //   test     bench          test
-    //   bench    bench          bench
-    //   bin      bench          test
-    //   bin      bench          bench
+    //   bench    bench          test
+    //   bin      bench          test   (bench/test de-duped)
     //   bin      release        build
     //   example  release        build
     assert_that(p.cargo("build --all-targets --release -vv"),
@@ -275,8 +273,6 @@ foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
 [RUNNING] `rustc --crate-name foo src[/]lib.rs --crate-type lib --emit=dep-info,link -C opt-level=3 -C panic=abort -C codegen-units=2 [..]`
 [RUNNING] `rustc --crate-name foo src[/]lib.rs --emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]`
 [RUNNING] `rustc --crate-name foo src[/]lib.rs --crate-type lib --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]`
-[RUNNING] `rustc --crate-name foo src[/]lib.rs --emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]`
-[RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]`
 [RUNNING] `rustc --crate-name test1 tests[/]test1.rs --emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]`
 [RUNNING] `rustc --crate-name bench1 benches[/]bench1.rs --emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]`
 [RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]`
@@ -449,9 +445,9 @@ fn profile_selection_bench() {
     //   Target   Profile        Mode
     //   ------   -------        ----
     //   lib      release-panic  build
-    //   lib      bench          bench
-    //   bench    bench          bench
-    //   bin      bench          bench
+    //   lib      bench          test(bench)
+    //   bench    bench          test(bench)
+    //   bin      bench          test(bench)
     //   bin      release-panic  build
     //
     assert_that(p.cargo("bench -vv"), execs().with_status(0).with_stderr_unordered("\