Skip to content

Commit

Permalink
Use "test" profile for cargo build benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Nov 12, 2018
1 parent 4e6bbd7 commit 739c272
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 57 deletions.
3 changes: 1 addition & 2 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Profiles {
release: bool,
) -> Profile {
let maker = match mode {
CompileMode::Test => {
CompileMode::Test | CompileMode::Bench => {
if release {
&self.bench
} else {
Expand All @@ -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);
Expand Down
20 changes: 9 additions & 11 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
47 changes: 18 additions & 29 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
13 changes: 2 additions & 11 deletions tests/testsuite/profile_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -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("\
Expand All @@ -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] [..]
Expand Down
4 changes: 0 additions & 4 deletions tests/testsuite/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit 739c272

Please sign in to comment.