Skip to content

Commit 005e1a4

Browse files
committed
Fix O0 build scripts by default without [profile.release]
This fixes an issue where #8500 didn't quite work as expected, since it only worked if a crate had a `[profile.release]` section.
1 parent 0d79ba6 commit 005e1a4

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

src/cargo/core/profiles.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,31 @@ impl ProfileMaker {
470470
unit_for: UnitFor,
471471
) -> Profile {
472472
let mut profile = self.default;
473+
474+
// First apply profile-specific settings, things like
475+
// `[profile.release]`
473476
if let Some(toml) = &self.toml {
474477
merge_profile(&mut profile, toml);
478+
}
479+
480+
// Next start overriding those settings. First comes build dependencies
481+
// which default to opt-level 0...
482+
if unit_for.is_for_host() {
483+
// For-host units are things like procedural macros, build scripts, and
484+
// their dependencies. For these units most projects simply want them
485+
// to compile quickly and the runtime doesn't matter too much since
486+
// they tend to process very little data. For this reason we default
487+
// them to a "compile as quickly as possible" mode which for now means
488+
// basically turning down the optimization level and avoid limiting
489+
// codegen units. This ensures that we spend little time optimizing as
490+
// well as enabling parallelism by not constraining codegen units.
491+
profile.opt_level = InternedString::new("0");
492+
profile.codegen_units = None;
493+
}
494+
// ... and next comes any other sorts of overrides specified in
495+
// profiles, such as `[profile.release.build-override]` or
496+
// `[profile.release.package.foo]`
497+
if let Some(toml) = &self.toml {
475498
merge_toml_overrides(pkg_id, is_member, unit_for, &mut profile, toml);
476499
}
477500
profile
@@ -487,17 +510,6 @@ fn merge_toml_overrides(
487510
toml: &TomlProfile,
488511
) {
489512
if unit_for.is_for_host() {
490-
// For-host units are things like procedural macros, build scripts, and
491-
// their dependencies. For these units most projects simply want them
492-
// to compile quickly and the runtime doesn't matter too much since
493-
// they tend to process very little data. For this reason we default
494-
// them to a "compile as quickly as possible" mode which for now means
495-
// basically turning down the optimization level and avoid limiting
496-
// codegen units. This ensures that we spend little time optimizing as
497-
// well as enabling parallelism by not constraining codegen units.
498-
profile.opt_level = InternedString::new("0");
499-
profile.codegen_units = None;
500-
501513
if let Some(build_override) = &toml.build_override {
502514
merge_profile(profile, build_override);
503515
}

src/doc/src/reference/profiles.md

+18
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,24 @@ codegen-units = 16
282282
rpath = false
283283
```
284284

285+
#### Build Dependencies
286+
287+
All profiles, by default, do not optimize build dependencies (build scripts,
288+
proc macros, and their dependencies). The default settings for build overrides
289+
are:
290+
291+
```toml
292+
[profile.dev.build-override]
293+
opt-level = 0
294+
codegen-units = 256
295+
296+
[profile.release.build-override]
297+
opt-level = 0
298+
codegen-units = 256
299+
```
300+
301+
Build dependencies otherwise inherit settings from the active profile in use, as
302+
described below.
285303

286304
### Profile selection
287305

tests/testsuite/build.rs

+35
Original file line numberDiff line numberDiff line change
@@ -5127,3 +5127,38 @@ fn simple_terminal_width() {
51275127
.with_stderr_contains("3 | ..._: () = 42;")
51285128
.run();
51295129
}
5130+
5131+
#[cargo_test]
5132+
fn build_script_o0_default() {
5133+
let p = project()
5134+
.file("src/lib.rs", "")
5135+
.file("build.rs", "fn main() {}")
5136+
.build();
5137+
5138+
p.cargo("build -v --release")
5139+
.with_stderr_does_not_contain("[..]build_script_build[..]opt-level[..]")
5140+
.run();
5141+
}
5142+
5143+
#[cargo_test]
5144+
fn build_script_o0_default_even_with_release() {
5145+
let p = project()
5146+
.file(
5147+
"Cargo.toml",
5148+
r#"
5149+
[package]
5150+
name = "foo"
5151+
version = "0.1.0"
5152+
5153+
[profile.release]
5154+
opt-level = 1
5155+
"#,
5156+
)
5157+
.file("src/lib.rs", "")
5158+
.file("build.rs", "fn main() {}")
5159+
.build();
5160+
5161+
p.cargo("build -v --release")
5162+
.with_stderr_does_not_contain("[..]build_script_build[..]opt-level[..]")
5163+
.run();
5164+
}

0 commit comments

Comments
 (0)