Skip to content

Commit 620d667

Browse files
committed
Warn only when lto is open and add test
Signed-off-by: hi-rustin <[email protected]>
1 parent ed13840 commit 620d667

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/cargo/core/compiler/lto.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,21 @@ fn calculate(
106106
};
107107
// LTO can only be performed if *all* of the crate types support it.
108108
// For example, a cdylib/rlib combination won't allow LTO.
109-
let can_not_lto_types = crate_types
109+
let not_lto_types = crate_types
110110
.iter()
111111
.filter(|ct| !ct.can_lto())
112112
.map(|ct| ct.as_str())
113113
.collect::<Vec<_>>();
114-
let all_lto_types = can_not_lto_types.len() == 0;
114+
let all_lto_types = not_lto_types.len() == 0;
115115
if !all_lto_types {
116-
bcx.config.shell().warn(format!(
117-
"LTO can only be performed if all of the crate types support it,\
118-
you have some crate types do not support LTO: {}",
119-
can_not_lto_types.join(","),
120-
))?;
116+
if let profiles::Lto::Named(_) | profiles::Lto::Bool(_) = unit.profile.lto {
117+
bcx.config.shell().warn(format!(
118+
"LTO can only be performed if all of the crate types support it, \
119+
package `{}` have some crate types do not support LTO: {}",
120+
unit.pkg.name(),
121+
not_lto_types.join(","),
122+
))?;
123+
}
121124
}
122125
// Compute the LTO based on the profile, and what our parent requires.
123126
let lto = if unit.target.for_host() {

tests/testsuite/lto.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,25 @@ fn dylib() {
624624
.run();
625625
}
626626

627+
#[cargo_test]
628+
fn lto_with_unsupported_crate_types() {
629+
let p = project_with_dep("'dylib','rlib'");
630+
p.cargo("build --release -v")
631+
.with_stderr_contains(
632+
"\
633+
[WARNING] LTO can only be performed if all of the crate types support it, \
634+
package `bar` have some crate types do not support LTO: dylib,rlib
635+
[WARNING] LTO can only be performed if all of the crate types support it, \
636+
package `registry` have some crate types do not support LTO: lib
637+
[WARNING] LTO can only be performed if all of the crate types support it, \
638+
package `registry-shared` have some crate types do not support LTO: lib
639+
[WARNING] LTO can only be performed if all of the crate types support it, \
640+
package `registry-shared` have some crate types do not support LTO: lib
641+
",
642+
)
643+
.run();
644+
}
645+
627646
#[cargo_test]
628647
fn test_profile() {
629648
Package::new("bar", "0.0.1")

0 commit comments

Comments
 (0)