Skip to content

Commit

Permalink
Auto merge of #129257 - ChrisDenton:rename-null-descriptor, r=<try>
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Aug 18, 2024
2 parents 6de928d + f64ecf1 commit 04f2fce
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 6 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,8 @@ dependencies = [

[[package]]
name = "ar_archive_writer"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de11a9d32db3327f981143bdf699ade4d637c6887b13b97e6e91a9154666963c"
version = "0.3.3"
source = "git+https://github.com/ChrisDenton/ar_archive_writer.git?branch=rename2#a5b424a6ef9d6416b299ac27c2dba224e2470253"
dependencies = [
"object 0.36.2",
]
Expand Down
14 changes: 12 additions & 2 deletions compiler/rustc_codegen_ssa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
# tidy-alphabetical-start
ar_archive_writer = "0.4.0"
ar_archive_writer = { git = 'https://github.com/ChrisDenton/ar_archive_writer.git', branch = "rename2" }
arrayvec = { version = "0.7", default-features = false }
bitflags = "2.4.1"
cc = "1.0.90"
Expand Down Expand Up @@ -52,7 +52,17 @@ libc = "0.2.50"
[dependencies.object]
version = "0.36.2"
default-features = false
features = ["read_core", "elf", "macho", "pe", "xcoff", "unaligned", "archive", "write", "wasm"]
features = [
"read_core",
"elf",
"macho",
"pe",
"xcoff",
"unaligned",
"archive",
"write",
"wasm",
]

[target.'cfg(windows)'.dependencies.windows]
version = "0.52.0"
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ pub trait ArchiveBuilderBuilder {
&exports,
machine,
!sess.target.is_like_msvc,
/*comdat=*/ false,
// Tell the import library writer to make `.idata$3` a COMDAT section.
// This prevents duplicate symbol errors when using /WHOLEARCHIVE
// to link a staticlib with the MSVC linker.
// See #129020
true,
) {
sess.dcx()
.emit_fatal(ErrorCreatingImportLibrary { lib_name, error: error.to_string() });
Expand Down
1 change: 1 addition & 0 deletions src/tools/tidy/src/extdeps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const ALLOWED_SOURCES: &[&str] = &[
r#""registry+https://github.com/rust-lang/crates.io-index""#,
// This is `rust_team_data` used by `site` in src/tools/rustc-perf,
r#""git+https://github.com/rust-lang/team#a5260e76d3aa894c64c56e6ddc8545b9a98043ec""#,
r#""git+https://github.com/ChrisDenton/ar_archive_writer.git?branch=rename2#a5b424a6ef9d6416b299ac27c2dba224e2470253""#,
];

/// Checks for external package sources. `root` is the path to the directory that contains the
Expand Down
1 change: 1 addition & 0 deletions tests/run-make/msvc-wholearchive/c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This page is intentionally left blank
4 changes: 4 additions & 0 deletions tests/run-make/msvc-wholearchive/dll.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
LIBRARY dll
EXPORTS
hello
number
41 changes: 41 additions & 0 deletions tests/run-make/msvc-wholearchive/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//@ only-msvc
// Reason: this is testing the MSVC linker

// This is a regression test for #129020
// It ensures we can link a rust staticlib into DLL using the MSVC linker

use std::path::Path;

use run_make_support::{cc, env_var, extra_c_flags, rustc};

fn main() {
// FIXME: Make this test either work with or ignore LLVM.
if false {
return;
}
// Build the staticlib
rustc().crate_type("staticlib").input("static.rs").output("static.lib").run();
// Create an empty obj file (using the C compiler)
// Then use it to link in the staticlib using `/WHOLEARCHIVE` and produce a DLL.
// We test for `cl.exe` to ensure we're using MSVC's tools and not the LLVM equivalents.

if env_var("CC").ends_with("cl.exe") {
cc().input("c.c")
.args([
"-MT",
"-link",
"-WHOLEARCHIVE:./static.lib",
"-dll",
"-def:dll.def",
"-out:dll.dll",
])
.args(extra_c_flags())
.run();
}

// As a sanity check, make sure it works without /WHOLEARCHIVE
cc().input("c.c")
.args(["-MT", "-link", "./static.lib", "-dll", "-def:dll.def", "-out:dll2.dll"])
.args(extra_c_flags())
.run();
}
9 changes: 9 additions & 0 deletions tests/run-make/msvc-wholearchive/static.rs
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
}

0 comments on commit 04f2fce

Please sign in to comment.