Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix O0 build scripts by default without [profile.release] #8560

Merged
merged 1 commit into from
Jul 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
alexcrichton committed Jul 29, 2020

Verified

This commit was signed with the committer’s verified signature.
lamweili Lam Wei Li
commit 005e1a442a4cbdf4512ec940b7a02bf32d4d65fd
34 changes: 23 additions & 11 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
@@ -470,8 +470,31 @@ impl ProfileMaker {
unit_for: UnitFor,
) -> Profile {
let mut profile = self.default;

// First apply profile-specific settings, things like
// `[profile.release]`
if let Some(toml) = &self.toml {
merge_profile(&mut profile, toml);
}

// Next start overriding those settings. First comes build dependencies
// which default to opt-level 0...
if unit_for.is_for_host() {
// For-host units are things like procedural macros, build scripts, and
// their dependencies. For these units most projects simply want them
// to compile quickly and the runtime doesn't matter too much since
// they tend to process very little data. For this reason we default
// them to a "compile as quickly as possible" mode which for now means
// basically turning down the optimization level and avoid limiting
// codegen units. This ensures that we spend little time optimizing as
// well as enabling parallelism by not constraining codegen units.
profile.opt_level = InternedString::new("0");
profile.codegen_units = None;
alexcrichton marked this conversation as resolved.
Show resolved Hide resolved
}
// ... and next comes any other sorts of overrides specified in
// profiles, such as `[profile.release.build-override]` or
// `[profile.release.package.foo]`
if let Some(toml) = &self.toml {
merge_toml_overrides(pkg_id, is_member, unit_for, &mut profile, toml);
}
profile
@@ -487,17 +510,6 @@ fn merge_toml_overrides(
toml: &TomlProfile,
) {
if unit_for.is_for_host() {
// For-host units are things like procedural macros, build scripts, and
// their dependencies. For these units most projects simply want them
// to compile quickly and the runtime doesn't matter too much since
// they tend to process very little data. For this reason we default
// them to a "compile as quickly as possible" mode which for now means
// basically turning down the optimization level and avoid limiting
// codegen units. This ensures that we spend little time optimizing as
// well as enabling parallelism by not constraining codegen units.
profile.opt_level = InternedString::new("0");
profile.codegen_units = None;

if let Some(build_override) = &toml.build_override {
merge_profile(profile, build_override);
}
18 changes: 18 additions & 0 deletions src/doc/src/reference/profiles.md
Original file line number Diff line number Diff line change
@@ -282,6 +282,24 @@ codegen-units = 16
rpath = false
```

#### Build Dependencies

All profiles, by default, do not optimize build dependencies (build scripts,
proc macros, and their dependencies). The default settings for build overrides
are:

```toml
[profile.dev.build-override]
opt-level = 0
codegen-units = 256

[profile.release.build-override]
opt-level = 0
codegen-units = 256
```

Build dependencies otherwise inherit settings from the active profile in use, as
described below.

### Profile selection

35 changes: 35 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
@@ -5127,3 +5127,38 @@ fn simple_terminal_width() {
.with_stderr_contains("3 | ..._: () = 42;")
.run();
}

#[cargo_test]
fn build_script_o0_default() {
let p = project()
.file("src/lib.rs", "")
.file("build.rs", "fn main() {}")
.build();

p.cargo("build -v --release")
.with_stderr_does_not_contain("[..]build_script_build[..]opt-level[..]")
.run();
}

#[cargo_test]
fn build_script_o0_default_even_with_release() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"

[profile.release]
opt-level = 1
"#,
)
.file("src/lib.rs", "")
.file("build.rs", "fn main() {}")
.build();

p.cargo("build -v --release")
.with_stderr_does_not_contain("[..]build_script_build[..]opt-level[..]")
.run();
}