Skip to content

Commit 9bb16f9

Browse files
committed
Test wholearchive on rust staticlib
1 parent 6de928d commit 9bb16f9

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

tests/run-make/msvc-wholearchive/c.c

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This page is intentionally left blank
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
LIBRARY dll
2+
EXPORTS
3+
hello
4+
number
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! This is a regression test for #129020
2+
//! It ensures we can use `/WHOLEARCHIVE` to link a rust staticlib into DLL
3+
//! using the MSVC linker
4+
5+
//@ only-msvc
6+
// Reason: this is testing the MSVC linker
7+
8+
use std::ffi::OsStr;
9+
use std::path::PathBuf;
10+
11+
use run_make_support::{cc, cmd, env_var, extra_c_flags, rustc};
12+
13+
fn main() {
14+
// Build the staticlib
15+
rustc().crate_type("staticlib").input("static.rs").output("static.lib").run();
16+
// Build an empty object to pass to the linker.
17+
cc().input("c.c").output("c.obj").args(["-c"]).run();
18+
19+
// Find the C toolchain's linker.
20+
let mut linker = PathBuf::from(env_var("CC"));
21+
let linker_flavour = if linker.file_stem() == Some(OsStr::new("cl")) {
22+
linker.set_file_name("link.exe");
23+
"msvc"
24+
} else if linker.file_stem() == Some(OsStr::new("clang-cl")) {
25+
linker.set_file_name("lld-link.exe");
26+
"llvm"
27+
} else {
28+
panic!("unknown C toolchain");
29+
};
30+
31+
// As a sanity check, make sure this works without /WHOLEARCHIVE.
32+
// Otherwise the actual test failure may be caused by something else.
33+
cmd(&linker)
34+
.args(["c.obj", "./static.lib", "-dll", "-def:dll.def", "-out:dll.dll"])
35+
.args(extra_c_flags())
36+
.run();
37+
38+
// FIXME(@ChrisDenton): this doesn't currently work with llvm's lld-link for other reasons.
39+
// May need LLVM patches.
40+
if linker_flavour == "msvc" {
41+
// Link in the staticlib using `/WHOLEARCHIVE` and produce a DLL.
42+
cmd(&linker)
43+
.args([
44+
"c.obj",
45+
"-WHOLEARCHIVE:./static.lib",
46+
"-dll",
47+
"-def:dll.def",
48+
"-out:dll_whole_archive.dll",
49+
])
50+
.args(extra_c_flags())
51+
.run();
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[no_mangle]
2+
pub extern "C" fn hello() {
3+
println!("Hello world!");
4+
}
5+
6+
#[no_mangle]
7+
pub extern "C" fn number() -> u32 {
8+
42
9+
}

0 commit comments

Comments
 (0)