From 739c272f05164ae4ceaea7d6c59c1cf4dd5fb136 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 12 Nov 2018 13:10:54 -0800 Subject: [PATCH] Use "test" profile for `cargo build` benchmarks. --- src/cargo/core/profiles.rs | 3 +- src/cargo/ops/cargo_compile.rs | 20 ++++++------- tests/testsuite/build.rs | 47 ++++++++++++------------------ tests/testsuite/profile_targets.rs | 13 ++------- tests/testsuite/rustc.rs | 4 --- 5 files changed, 30 insertions(+), 57 deletions(-) diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index 6120007c560..6257858cc9b 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -74,7 +74,7 @@ impl Profiles { release: bool, ) -> Profile { let maker = match mode { - CompileMode::Test => { + CompileMode::Test | CompileMode::Bench => { if release { &self.bench } else { @@ -95,7 +95,6 @@ impl Profiles { &self.dev } } - CompileMode::Bench => &self.bench, CompileMode::Doc { .. } => &self.doc, }; let mut profile = maker.get_profile(Some(pkg_id), is_member, unit_for); diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 54f2c41e829..7b188507112 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -532,6 +532,15 @@ fn generate_targets<'a>( TargetKind::Bench => CompileMode::Bench, _ => CompileMode::Build, }, + // CompileMode::Bench is only used to inform filter_default_targets + // which command is being used (`cargo bench`). Afterwards, tests + // and benches are treated identically. 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`. + CompileMode::Bench => CompileMode::Test, _ => target_mode, }; // Plugins or proc-macro should be built for the host. @@ -547,17 +556,6 @@ fn generate_targets<'a>( target_mode, build_config.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, diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index dc72669a48c..1dbae9c117e 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -4147,43 +4147,40 @@ fn build_filter_infer_profile() { p.cargo("build -v") .with_stderr_contains( - "\ - [RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \ + "[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \ --emit=dep-info,link[..]", ).with_stderr_contains( - "\ - [RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \ + "[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \ --emit=dep-info,link[..]", ).run(); p.root().join("target").rm_rf(); p.cargo("build -v --test=t1") .with_stderr_contains( - "\ - [RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \ - --emit=dep-info,link[..]", + "[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \ + --emit=dep-info,link -C debuginfo=2 [..]", ).with_stderr_contains( - "[RUNNING] `rustc --crate-name t1 tests/t1.rs --color never --emit=dep-info,link[..]", + "[RUNNING] `rustc --crate-name t1 tests/t1.rs --color never --emit=dep-info,link \ + -C debuginfo=2 [..]", ).with_stderr_contains( - "\ - [RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \ - --emit=dep-info,link[..]", + "[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \ + --emit=dep-info,link -C debuginfo=2 [..]", ).run(); p.root().join("target").rm_rf(); + // Bench uses test profile without `--release`. p.cargo("build -v --bench=b1") .with_stderr_contains( - "\ - [RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \ - --emit=dep-info,link[..]", + "[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \ + --emit=dep-info,link -C debuginfo=2 [..]", ).with_stderr_contains( - "\ - [RUNNING] `rustc --crate-name b1 benches/b1.rs --color never --emit=dep-info,link \ - -C opt-level=3[..]", - ).with_stderr_contains( - "\ - [RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \ - --emit=dep-info,link[..]", + "[RUNNING] `rustc --crate-name b1 benches/b1.rs --color never --emit=dep-info,link \ + -C debuginfo=2 [..]", + ) + .with_stderr_does_not_contain("opt-level") + .with_stderr_contains( + "[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \ + --emit=dep-info,link -C debuginfo=2 [..]", ).run(); } @@ -4213,10 +4210,6 @@ fn targets_selected_all() { .with_stderr_contains("\ [RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \ --emit=dep-info,link[..]") - // bench - .with_stderr_contains("\ - [RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \ - -C opt-level=3 --test [..]") // unit test .with_stderr_contains("\ [RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \ @@ -4231,10 +4224,6 @@ fn all_targets_no_lib() { .with_stderr_contains("\ [RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \ --emit=dep-info,link[..]") - // bench - .with_stderr_contains("\ - [RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \ - -C opt-level=3 --test [..]") // unit test .with_stderr_contains("\ [RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \ diff --git a/tests/testsuite/profile_targets.rs b/tests/testsuite/profile_targets.rs index cc8a226f5f8..c5598cae6da 100644 --- a/tests/testsuite/profile_targets.rs +++ b/tests/testsuite/profile_targets.rs @@ -137,9 +137,6 @@ fn profile_selection_build_all_targets() { // - bdep `panic` is not set because it thinks `build.rs` is a plugin. // - build_script_build is built without panic because it thinks // `build.rs` is a plugin. - // - build_script_build is being run two times. Once for the `dev` and - // `test` targets, once for the `bench` targets. - // TODO: "PROFILE" says debug both times, though! // - Benchmark dependencies are compiled in `dev` mode, which may be // surprising. See https://github.com/rust-lang/cargo/issues/4929. // @@ -157,11 +154,9 @@ 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 test(bench) // test test test - // bench bench test(bench) + // bench test test // bin test test - // bin bench test(bench) // bin dev build // example dev build p.cargo("build --all-targets -vv").with_stderr_unordered("\ @@ -173,17 +168,13 @@ fn profile_selection_build_all_targets() { [COMPILING] foo [..] [RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..] [RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build` -[RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build` -[foo 0.0.1] foo custom build PROFILE=debug DEBUG=false OPT_LEVEL=3 [foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0 [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C panic=abort -C codegen-units=1 -C debuginfo=2 [..]` [RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C codegen-units=3 -C debuginfo=2 --test [..]` [RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C codegen-units=1 -C debuginfo=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 codegen-units=3 -C debuginfo=2 --test [..]` [RUNNING] `rustc --crate-name test1 tests/test1.rs [..]--emit=dep-info,link -C codegen-units=3 -C debuginfo=2 --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 [..]` +[RUNNING] `rustc --crate-name bench1 benches/bench1.rs [..]--emit=dep-info,link -C codegen-units=3 -C debuginfo=2 --test [..]` [RUNNING] `rustc --crate-name foo src/main.rs [..]--crate-type bin --emit=dep-info,link -C panic=abort -C codegen-units=1 -C debuginfo=2 [..]` [RUNNING] `rustc --crate-name ex1 examples/ex1.rs [..]--crate-type bin --emit=dep-info,link -C panic=abort -C codegen-units=1 -C debuginfo=2 [..]` [FINISHED] dev [unoptimized + debuginfo] [..] diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index 76ff321fb38..44b8a45f174 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -243,10 +243,6 @@ fn targets_selected_all() { .with_stderr_contains("\ [RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \ --emit=dep-info,link[..]") - // bench - .with_stderr_contains("\ - [RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \ - -C opt-level=3 --test [..]") // unit test .with_stderr_contains("\ [RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \