From 90e286da0716dc928f1cc2526fbe76438bb6f635 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 5 Oct 2020 08:15:32 -0700 Subject: [PATCH] Fix dylib+rlib with LTO. --- src/cargo/core/compiler/lto.rs | 12 ++++++------ tests/testsuite/lto.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/cargo/core/compiler/lto.rs b/src/cargo/core/compiler/lto.rs index 5c401e82a1d..222578918e9 100644 --- a/src/cargo/core/compiler/lto.rs +++ b/src/cargo/core/compiler/lto.rs @@ -73,16 +73,16 @@ fn needs_object(crate_types: &[CrateType]) -> bool { /// Lto setting to use when this unit needs object code. fn lto_when_needs_object(crate_types: &[CrateType]) -> Lto { - if crate_types.iter().any(CrateType::can_lto) { - // A mixed rlib/cdylib whose parent is running LTO. This - // needs both, for bitcode in the rlib (for LTO) and the - // cdylib requires object code. - Lto::ObjectAndBitcode - } else { + if crate_types.iter().all(|ct| *ct == CrateType::Dylib) { // A dylib whose parent is running LTO. rustc currently // doesn't support LTO with dylibs, so bitcode is not // needed. Lto::OnlyObject + } else { + // Mixed rlib with a dylib or cdylib whose parent is running LTO. This + // needs both: bitcode for the rlib (for LTO) and object code for the + // dylib. + Lto::ObjectAndBitcode } } diff --git a/tests/testsuite/lto.rs b/tests/testsuite/lto.rs index cd69ce42809..9e2f9a42835 100644 --- a/tests/testsuite/lto.rs +++ b/tests/testsuite/lto.rs @@ -746,3 +746,35 @@ fn doctest() { .with_stderr_contains("[..]`rustdoc [..]-C lto[..]") .run(); } + +#[cargo_test] +fn dylib_rlib_bin() { + // dylib+rlib linked with a binary + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [lib] + crate-type = ["dylib", "rlib"] + + [profile.release] + lto = true + "#, + ) + .file("src/lib.rs", "pub fn foo() { println!(\"hi!\"); }") + .file("src/main.rs", "fn main() { foo::foo(); }") + .build(); + + let output = p.cargo("build --release -v").exec_with_output().unwrap(); + verify_lto( + &output, + "foo", + "--crate-type dylib --crate-type rlib", + Lto::ObjectAndBitcode, + ); + verify_lto(&output, "foo", "--crate-type bin", Lto::Run(None)); +}