Skip to content

Commit 04f2fce

Browse files
committed
Auto merge of #129257 - ChrisDenton:rename-null-descriptor, r=<try>
Allow rust staticlib to work with MSVC's /WHOLEARCHIVE This renames the `__NULL_IMPORT_DESCRIPTOR` to prevent conflicts. try-job: x86_64-msvc try-job: i686-msvc r? ghost
2 parents 6de928d + f64ecf1 commit 04f2fce

File tree

8 files changed

+75
-6
lines changed

8 files changed

+75
-6
lines changed

Diff for: Cargo.lock

+2-3
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,8 @@ dependencies = [
205205

206206
[[package]]
207207
name = "ar_archive_writer"
208-
version = "0.4.0"
209-
source = "registry+https://github.com/rust-lang/crates.io-index"
210-
checksum = "de11a9d32db3327f981143bdf699ade4d637c6887b13b97e6e91a9154666963c"
208+
version = "0.3.3"
209+
source = "git+https://github.com/ChrisDenton/ar_archive_writer.git?branch=rename2#a5b424a6ef9d6416b299ac27c2dba224e2470253"
211210
dependencies = [
212211
"object 0.36.2",
213212
]

Diff for: compiler/rustc_codegen_ssa/Cargo.toml

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
# tidy-alphabetical-start
8-
ar_archive_writer = "0.4.0"
8+
ar_archive_writer = { git = 'https://github.com/ChrisDenton/ar_archive_writer.git', branch = "rename2" }
99
arrayvec = { version = "0.7", default-features = false }
1010
bitflags = "2.4.1"
1111
cc = "1.0.90"
@@ -52,7 +52,17 @@ libc = "0.2.50"
5252
[dependencies.object]
5353
version = "0.36.2"
5454
default-features = false
55-
features = ["read_core", "elf", "macho", "pe", "xcoff", "unaligned", "archive", "write", "wasm"]
55+
features = [
56+
"read_core",
57+
"elf",
58+
"macho",
59+
"pe",
60+
"xcoff",
61+
"unaligned",
62+
"archive",
63+
"write",
64+
"wasm",
65+
]
5666

5767
[target.'cfg(windows)'.dependencies.windows]
5868
version = "0.52.0"

Diff for: compiler/rustc_codegen_ssa/src/back/archive.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ pub trait ArchiveBuilderBuilder {
108108
&exports,
109109
machine,
110110
!sess.target.is_like_msvc,
111-
/*comdat=*/ false,
111+
// Tell the import library writer to make `.idata$3` a COMDAT section.
112+
// This prevents duplicate symbol errors when using /WHOLEARCHIVE
113+
// to link a staticlib with the MSVC linker.
114+
// See #129020
115+
true,
112116
) {
113117
sess.dcx()
114118
.emit_fatal(ErrorCreatingImportLibrary { lib_name, error: error.to_string() });

Diff for: src/tools/tidy/src/extdeps.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const ALLOWED_SOURCES: &[&str] = &[
88
r#""registry+https://github.com/rust-lang/crates.io-index""#,
99
// This is `rust_team_data` used by `site` in src/tools/rustc-perf,
1010
r#""git+https://github.com/rust-lang/team#a5260e76d3aa894c64c56e6ddc8545b9a98043ec""#,
11+
r#""git+https://github.com/ChrisDenton/ar_archive_writer.git?branch=rename2#a5b424a6ef9d6416b299ac27c2dba224e2470253""#,
1112
];
1213

1314
/// Checks for external package sources. `root` is the path to the directory that contains the

Diff for: 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

Diff for: tests/run-make/msvc-wholearchive/dll.def

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
LIBRARY dll
2+
EXPORTS
3+
hello
4+
number

Diff for: tests/run-make/msvc-wholearchive/rmake.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//@ only-msvc
2+
// Reason: this is testing the MSVC linker
3+
4+
// This is a regression test for #129020
5+
// It ensures we can link a rust staticlib into DLL using the MSVC linker
6+
7+
use std::path::Path;
8+
9+
use run_make_support::{cc, env_var, extra_c_flags, rustc};
10+
11+
fn main() {
12+
// FIXME: Make this test either work with or ignore LLVM.
13+
if false {
14+
return;
15+
}
16+
// Build the staticlib
17+
rustc().crate_type("staticlib").input("static.rs").output("static.lib").run();
18+
// Create an empty obj file (using the C compiler)
19+
// Then use it to link in the staticlib using `/WHOLEARCHIVE` and produce a DLL.
20+
// We test for `cl.exe` to ensure we're using MSVC's tools and not the LLVM equivalents.
21+
22+
if env_var("CC").ends_with("cl.exe") {
23+
cc().input("c.c")
24+
.args([
25+
"-MT",
26+
"-link",
27+
"-WHOLEARCHIVE:./static.lib",
28+
"-dll",
29+
"-def:dll.def",
30+
"-out:dll.dll",
31+
])
32+
.args(extra_c_flags())
33+
.run();
34+
}
35+
36+
// As a sanity check, make sure it works without /WHOLEARCHIVE
37+
cc().input("c.c")
38+
.args(["-MT", "-link", "./static.lib", "-dll", "-def:dll.def", "-out:dll2.dll"])
39+
.args(extra_c_flags())
40+
.run();
41+
}

Diff for: tests/run-make/msvc-wholearchive/static.rs

+9
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)