Skip to content

Commit 90e286d

Browse files
committed
Fix dylib+rlib with LTO.
1 parent 1f6c6bd commit 90e286d

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/cargo/core/compiler/lto.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ fn needs_object(crate_types: &[CrateType]) -> bool {
7373

7474
/// Lto setting to use when this unit needs object code.
7575
fn lto_when_needs_object(crate_types: &[CrateType]) -> Lto {
76-
if crate_types.iter().any(CrateType::can_lto) {
77-
// A mixed rlib/cdylib whose parent is running LTO. This
78-
// needs both, for bitcode in the rlib (for LTO) and the
79-
// cdylib requires object code.
80-
Lto::ObjectAndBitcode
81-
} else {
76+
if crate_types.iter().all(|ct| *ct == CrateType::Dylib) {
8277
// A dylib whose parent is running LTO. rustc currently
8378
// doesn't support LTO with dylibs, so bitcode is not
8479
// needed.
8580
Lto::OnlyObject
81+
} else {
82+
// Mixed rlib with a dylib or cdylib whose parent is running LTO. This
83+
// needs both: bitcode for the rlib (for LTO) and object code for the
84+
// dylib.
85+
Lto::ObjectAndBitcode
8686
}
8787
}
8888

tests/testsuite/lto.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,3 +746,35 @@ fn doctest() {
746746
.with_stderr_contains("[..]`rustdoc [..]-C lto[..]")
747747
.run();
748748
}
749+
750+
#[cargo_test]
751+
fn dylib_rlib_bin() {
752+
// dylib+rlib linked with a binary
753+
let p = project()
754+
.file(
755+
"Cargo.toml",
756+
r#"
757+
[package]
758+
name = "foo"
759+
version = "0.1.0"
760+
761+
[lib]
762+
crate-type = ["dylib", "rlib"]
763+
764+
[profile.release]
765+
lto = true
766+
"#,
767+
)
768+
.file("src/lib.rs", "pub fn foo() { println!(\"hi!\"); }")
769+
.file("src/main.rs", "fn main() { foo::foo(); }")
770+
.build();
771+
772+
let output = p.cargo("build --release -v").exec_with_output().unwrap();
773+
verify_lto(
774+
&output,
775+
"foo",
776+
"--crate-type dylib --crate-type rlib",
777+
Lto::ObjectAndBitcode,
778+
);
779+
verify_lto(&output, "foo", "--crate-type bin", Lto::Run(None));
780+
}

0 commit comments

Comments
 (0)