-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Allow rust staticlib to work with MSVC's /WHOLEARCHIVE #129257
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// This page is intentionally left blank |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
LIBRARY dll | ||
EXPORTS | ||
hello | ||
number |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//! This is a regression test for #129020 | ||
//! It ensures we can use `/WHOLEARCHIVE` to link a rust staticlib into DLL | ||
//! using the MSVC linker | ||
|
||
//@ only-msvc | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice (but not required) to also cover There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've not tried gnullvm specifically but the llvm linker has another issue which I think needs solving on their end first (which is why even the msvc test skips lld-link).
LLVM seems to not properly recognise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably should open a new issue to track this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe? How do we usually track upstream issues? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a special label for waiting on LLVM: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ld.bfd used by |
||
// Reason: this is testing the MSVC linker | ||
|
||
use std::path::PathBuf; | ||
|
||
use run_make_support::{cc, cmd, env_var, extra_c_flags, rustc}; | ||
|
||
fn main() { | ||
// Build the staticlib | ||
rustc().crate_type("staticlib").input("static.rs").output("static.lib").run(); | ||
// Build an empty object to pass to the linker. | ||
cc().input("c.c").output("c.obj").args(["-c"]).run(); | ||
|
||
// Find the C toolchain's linker. | ||
let mut linker = PathBuf::from(env_var("CC")); | ||
let linker_flavour = if linker.file_stem().is_some_and(|s| s == "cl") { | ||
linker.set_file_name("link.exe"); | ||
"msvc" | ||
} else if linker.file_stem().is_some_and(|s| s == "clang-cl") { | ||
linker.set_file_name("lld-link.exe"); | ||
"llvm" | ||
} else { | ||
panic!("unknown C toolchain"); | ||
}; | ||
|
||
// As a sanity check, make sure this works without /WHOLEARCHIVE. | ||
// Otherwise the actual test failure may be caused by something else. | ||
cmd(&linker) | ||
.args(["c.obj", "./static.lib", "-dll", "-def:dll.def", "-out:dll.dll"]) | ||
.args(extra_c_flags()) | ||
.run(); | ||
|
||
// FIXME(@ChrisDenton): this doesn't currently work with llvm's lld-link for other reasons. | ||
// May need LLVM patches. | ||
if linker_flavour == "msvc" { | ||
// Link in the staticlib using `/WHOLEARCHIVE` and produce a DLL. | ||
cmd(&linker) | ||
.args([ | ||
"c.obj", | ||
"-WHOLEARCHIVE:./static.lib", | ||
"-dll", | ||
"-def:dll.def", | ||
"-out:dll_whole_archive.dll", | ||
]) | ||
.args(extra_c_flags()) | ||
.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#[no_mangle] | ||
pub extern "C" fn hello() { | ||
println!("Hello world!"); | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn number() -> u32 { | ||
42 | ||
} |
Uh oh!
There was an error while loading. Please reload this page.