Skip to content

Commit

Permalink
Warn only when lto is open and add test
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Apr 5, 2022
1 parent ed13840 commit 620d667
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/cargo/core/compiler/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,21 @@ fn calculate(
};
// LTO can only be performed if *all* of the crate types support it.
// For example, a cdylib/rlib combination won't allow LTO.
let can_not_lto_types = crate_types
let not_lto_types = crate_types
.iter()
.filter(|ct| !ct.can_lto())
.map(|ct| ct.as_str())
.collect::<Vec<_>>();
let all_lto_types = can_not_lto_types.len() == 0;
let all_lto_types = not_lto_types.len() == 0;
if !all_lto_types {
bcx.config.shell().warn(format!(
"LTO can only be performed if all of the crate types support it,\
you have some crate types do not support LTO: {}",
can_not_lto_types.join(","),
))?;
if let profiles::Lto::Named(_) | profiles::Lto::Bool(_) = unit.profile.lto {
bcx.config.shell().warn(format!(
"LTO can only be performed if all of the crate types support it, \
package `{}` have some crate types do not support LTO: {}",
unit.pkg.name(),
not_lto_types.join(","),
))?;
}
}
// Compute the LTO based on the profile, and what our parent requires.
let lto = if unit.target.for_host() {
Expand Down
19 changes: 19 additions & 0 deletions tests/testsuite/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,25 @@ fn dylib() {
.run();
}

#[cargo_test]
fn lto_with_unsupported_crate_types() {
let p = project_with_dep("'dylib','rlib'");
p.cargo("build --release -v")
.with_stderr_contains(
"\
[WARNING] LTO can only be performed if all of the crate types support it, \
package `bar` have some crate types do not support LTO: dylib,rlib
[WARNING] LTO can only be performed if all of the crate types support it, \
package `registry` have some crate types do not support LTO: lib
[WARNING] LTO can only be performed if all of the crate types support it, \
package `registry-shared` have some crate types do not support LTO: lib
[WARNING] LTO can only be performed if all of the crate types support it, \
package `registry-shared` have some crate types do not support LTO: lib
",
)
.run();
}

#[cargo_test]
fn test_profile() {
Package::new("bar", "0.0.1")
Expand Down

0 comments on commit 620d667

Please sign in to comment.