Skip to content

Commit 51d541f

Browse files
committed
Auto merge of #6309 - ehuss:bench-build-profile, r=alexcrichton
Use "test" profile for `cargo build` benchmarks. When using `cargo build` (without `--release`), build benchmarks using the "test" profile. This was causing some confusion where the benchmark is placed in the `target/debug` directory, and also causing some duplicates that may not be expected. It also makes it easier to debug benchmarks (previously you had to edit the `[profile.bench]` profile). Closes #5575, closes #6301, closes #4240, closes #4929.
2 parents 0da2c99 + 739c272 commit 51d541f

File tree

5 files changed

+30
-57
lines changed

5 files changed

+30
-57
lines changed

src/cargo/core/profiles.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Profiles {
7474
release: bool,
7575
) -> Profile {
7676
let maker = match mode {
77-
CompileMode::Test => {
77+
CompileMode::Test | CompileMode::Bench => {
7878
if release {
7979
&self.bench
8080
} else {
@@ -95,7 +95,6 @@ impl Profiles {
9595
&self.dev
9696
}
9797
}
98-
CompileMode::Bench => &self.bench,
9998
CompileMode::Doc { .. } => &self.doc,
10099
};
101100
let mut profile = maker.get_profile(Some(pkg_id), is_member, unit_for);

src/cargo/ops/cargo_compile.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,15 @@ fn generate_targets<'a>(
532532
TargetKind::Bench => CompileMode::Bench,
533533
_ => CompileMode::Build,
534534
},
535+
// CompileMode::Bench is only used to inform filter_default_targets
536+
// which command is being used (`cargo bench`). Afterwards, tests
537+
// and benches are treated identically. Switching the mode allows
538+
// de-duplication of units that are essentially identical. For
539+
// example, `cargo build --all-targets --release` creates the units
540+
// (lib profile:bench, mode:test) and (lib profile:bench, mode:bench)
541+
// and since these are the same, we want them to be de-duped in
542+
// `unit_dependencies`.
543+
CompileMode::Bench => CompileMode::Test,
535544
_ => target_mode,
536545
};
537546
// Plugins or proc-macro should be built for the host.
@@ -547,17 +556,6 @@ fn generate_targets<'a>(
547556
target_mode,
548557
build_config.release,
549558
);
550-
// Once the profile has been selected for benchmarks, we don't need to
551-
// distinguish between benches and tests. Switching the mode allows
552-
// de-duplication of units that are essentially identical. For
553-
// example, `cargo build --all-targets --release` creates the units
554-
// (lib profile:bench, mode:test) and (lib profile:bench, mode:bench)
555-
// and since these are the same, we want them to be de-duped in
556-
// `unit_dependencies`.
557-
let target_mode = match target_mode {
558-
CompileMode::Bench => CompileMode::Test,
559-
_ => target_mode,
560-
};
561559
Unit {
562560
pkg,
563561
target,

tests/testsuite/build.rs

+18-29
Original file line numberDiff line numberDiff line change
@@ -4147,43 +4147,40 @@ fn build_filter_infer_profile() {
41474147

41484148
p.cargo("build -v")
41494149
.with_stderr_contains(
4150-
"\
4151-
[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
4150+
"[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
41524151
--emit=dep-info,link[..]",
41534152
).with_stderr_contains(
4154-
"\
4155-
[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
4153+
"[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
41564154
--emit=dep-info,link[..]",
41574155
).run();
41584156

41594157
p.root().join("target").rm_rf();
41604158
p.cargo("build -v --test=t1")
41614159
.with_stderr_contains(
4162-
"\
4163-
[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
4164-
--emit=dep-info,link[..]",
4160+
"[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
4161+
--emit=dep-info,link -C debuginfo=2 [..]",
41654162
).with_stderr_contains(
4166-
"[RUNNING] `rustc --crate-name t1 tests/t1.rs --color never --emit=dep-info,link[..]",
4163+
"[RUNNING] `rustc --crate-name t1 tests/t1.rs --color never --emit=dep-info,link \
4164+
-C debuginfo=2 [..]",
41674165
).with_stderr_contains(
4168-
"\
4169-
[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
4170-
--emit=dep-info,link[..]",
4166+
"[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
4167+
--emit=dep-info,link -C debuginfo=2 [..]",
41714168
).run();
41724169

41734170
p.root().join("target").rm_rf();
4171+
// Bench uses test profile without `--release`.
41744172
p.cargo("build -v --bench=b1")
41754173
.with_stderr_contains(
4176-
"\
4177-
[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
4178-
--emit=dep-info,link[..]",
4174+
"[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
4175+
--emit=dep-info,link -C debuginfo=2 [..]",
41794176
).with_stderr_contains(
4180-
"\
4181-
[RUNNING] `rustc --crate-name b1 benches/b1.rs --color never --emit=dep-info,link \
4182-
-C opt-level=3[..]",
4183-
).with_stderr_contains(
4184-
"\
4185-
[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
4186-
--emit=dep-info,link[..]",
4177+
"[RUNNING] `rustc --crate-name b1 benches/b1.rs --color never --emit=dep-info,link \
4178+
-C debuginfo=2 [..]",
4179+
)
4180+
.with_stderr_does_not_contain("opt-level")
4181+
.with_stderr_contains(
4182+
"[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
4183+
--emit=dep-info,link -C debuginfo=2 [..]",
41874184
).run();
41884185
}
41894186

@@ -4213,10 +4210,6 @@ fn targets_selected_all() {
42134210
.with_stderr_contains("\
42144211
[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
42154212
--emit=dep-info,link[..]")
4216-
// bench
4217-
.with_stderr_contains("\
4218-
[RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \
4219-
-C opt-level=3 --test [..]")
42204213
// unit test
42214214
.with_stderr_contains("\
42224215
[RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \
@@ -4231,10 +4224,6 @@ fn all_targets_no_lib() {
42314224
.with_stderr_contains("\
42324225
[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
42334226
--emit=dep-info,link[..]")
4234-
// bench
4235-
.with_stderr_contains("\
4236-
[RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \
4237-
-C opt-level=3 --test [..]")
42384227
// unit test
42394228
.with_stderr_contains("\
42404229
[RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \

tests/testsuite/profile_targets.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ fn profile_selection_build_all_targets() {
137137
// - bdep `panic` is not set because it thinks `build.rs` is a plugin.
138138
// - build_script_build is built without panic because it thinks
139139
// `build.rs` is a plugin.
140-
// - build_script_build is being run two times. Once for the `dev` and
141-
// `test` targets, once for the `bench` targets.
142-
// TODO: "PROFILE" says debug both times, though!
143140
// - Benchmark dependencies are compiled in `dev` mode, which may be
144141
// surprising. See https://github.com/rust-lang/cargo/issues/4929.
145142
//
@@ -157,11 +154,9 @@ fn profile_selection_build_all_targets() {
157154
// lib dev+panic build (a normal lib target)
158155
// lib dev-panic build (used by tests/benches)
159156
// lib test test
160-
// lib bench test(bench)
161157
// test test test
162-
// bench bench test(bench)
158+
// bench test test
163159
// bin test test
164-
// bin bench test(bench)
165160
// bin dev build
166161
// example dev build
167162
p.cargo("build --all-targets -vv").with_stderr_unordered("\
@@ -173,17 +168,13 @@ fn profile_selection_build_all_targets() {
173168
[COMPILING] foo [..]
174169
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
175170
[RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build`
176-
[RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build`
177-
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=false OPT_LEVEL=3
178171
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
179172
[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 [..]`
180173
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C codegen-units=3 -C debuginfo=2 --test [..]`
181174
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]`
182-
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]`
183175
[RUNNING] `rustc --crate-name foo src/main.rs [..]--emit=dep-info,link -C codegen-units=3 -C debuginfo=2 --test [..]`
184176
[RUNNING] `rustc --crate-name test1 tests/test1.rs [..]--emit=dep-info,link -C codegen-units=3 -C debuginfo=2 --test [..]`
185-
[RUNNING] `rustc --crate-name bench1 benches/bench1.rs [..]--emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]`
186-
[RUNNING] `rustc --crate-name foo src/main.rs [..]--emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]`
177+
[RUNNING] `rustc --crate-name bench1 benches/bench1.rs [..]--emit=dep-info,link -C codegen-units=3 -C debuginfo=2 --test [..]`
187178
[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 [..]`
188179
[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 [..]`
189180
[FINISHED] dev [unoptimized + debuginfo] [..]

tests/testsuite/rustc.rs

-4
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,6 @@ fn targets_selected_all() {
243243
.with_stderr_contains("\
244244
[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
245245
--emit=dep-info,link[..]")
246-
// bench
247-
.with_stderr_contains("\
248-
[RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \
249-
-C opt-level=3 --test [..]")
250246
// unit test
251247
.with_stderr_contains("\
252248
[RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=dep-info,link \

0 commit comments

Comments
 (0)