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

Warning about unsupported LTO crete types #10537

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
22 changes: 19 additions & 3 deletions src/cargo/core/compiler/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn generate(bcx: &BuildContext<'_, '_>) -> CargoResult<HashMap<Unit, Lto>> {
}
}
};
calculate(bcx, &mut map, unit, root_lto)?;
calculate(bcx, &mut map, unit, root_lto, false)?;
}
Ok(map)
}
Expand Down Expand Up @@ -91,6 +91,7 @@ fn calculate(
map: &mut HashMap<Unit, Lto>,
unit: &Unit,
parent_lto: Lto,
is_dep: bool,
) -> CargoResult<()> {
let crate_types = match unit.mode {
// Note: Doctest ignores LTO, but for now we'll compute it as-if it is
Expand All @@ -106,7 +107,22 @@ 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 all_lto_types = crate_types.iter().all(CrateType::can_lto);
let not_lto_types = crate_types
.iter()
.filter(|ct| !ct.can_lto())
.map(|ct| ct.as_str())
.collect::<Vec<_>>();
let all_lto_types = not_lto_types.len() == 0;
if !all_lto_types && !is_dep {
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() {
// Disable LTO for host builds since we only really want to perform LTO
Expand Down Expand Up @@ -186,7 +202,7 @@ fn calculate(
};

for dep in &bcx.unit_graph[unit] {
calculate(bcx, map, &dep.unit, merged_lto)?;
calculate(bcx, map, &dep.unit, merged_lto, true)?;
}
Ok(())
}
37 changes: 37 additions & 0 deletions tests/testsuite/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,43 @@ fn dylib() {
.run();
}

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

[lib]
crate-type = ['dylib','rlib']

[profile.release]
lto = true
"#,
)
.file(
"src/lib.rs",
r#"
pub fn foo() {
println!("foo");
}
"#,
)
.build();

p.cargo("build --release -v")
.with_stderr_contains(
"\
[WARNING] LTO can only be performed if all of the crate types support it, \
package `foo` have some crate types do not support LTO: dylib,rlib
",
)
.run();
}

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