Skip to content

Commit

Permalink
Fix dylib+rlib with LTO.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Oct 5, 2020
1 parent 1f6c6bd commit 90e286d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/cargo/core/compiler/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/testsuite/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

0 comments on commit 90e286d

Please sign in to comment.