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

Fix dylib+rlib with LTO. #8754

Merged
merged 1 commit into from
Oct 5, 2020
Merged
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
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));
}