Skip to content

Commit a90f342

Browse files
committed
Use -m option instead of looking for a cross-compiling version of dlltool
1 parent 439292b commit a90f342

File tree

9 files changed

+87
-14
lines changed

9 files changed

+87
-14
lines changed

compiler/rustc_codegen_llvm/src/back/archive.rs

+25-14
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {
189189
path.push(lib_name);
190190
path
191191
};
192+
// dlltool target architecture args from:
193+
// https://github.com/llvm/llvm-project-release-prs/blob/llvmorg-15.0.6/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp#L69
194+
let (dlltool_target_arch, dlltool_target_bitness) = match sess.target.arch.as_ref() {
195+
"x86_64" => ("i386:x86-64", "--64"),
196+
"x86" => ("i386", "--32"),
197+
"aarch64" => ("arm64", "--64"),
198+
"arm" => ("arm", "--32"),
199+
_ => panic!("unsupported arch {}", sess.target.arch),
200+
};
192201
let result = std::process::Command::new(dlltool)
193202
.args([
194203
"-d",
@@ -197,6 +206,10 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {
197206
lib_name,
198207
"-l",
199208
output_path.to_str().unwrap(),
209+
"-m",
210+
dlltool_target_arch,
211+
"-f",
212+
dlltool_target_bitness,
200213
"--no-leading-underscore",
201214
"--temp-prefix",
202215
temp_prefix.to_str().unwrap(),
@@ -422,24 +435,22 @@ fn find_binutils_dlltool(sess: &Session) -> OsString {
422435
return dlltool_path.clone().into_os_string();
423436
}
424437

425-
let mut tool_name: OsString = if sess.host.arch != sess.target.arch {
426-
// We are cross-compiling, so we need the tool with the prefix matching our target
427-
if sess.target.arch == "x86" {
428-
"i686-w64-mingw32-dlltool"
429-
} else {
430-
"x86_64-w64-mingw32-dlltool"
431-
}
438+
let tool_name: OsString = if sess.host.options.is_like_windows {
439+
// If we're compiling on Windows, always use "dlltool.exe".
440+
"dlltool.exe"
432441
} else {
433-
// We are not cross-compiling, so we just want `dlltool`
434-
"dlltool"
442+
// On other platforms, use the architecture-specific name.
443+
match sess.target.arch.as_ref() {
444+
"x86_64" => "x86_64-w64-mingw32-dlltool",
445+
"x86" => "i686-w64-mingw32-dlltool",
446+
"aarch64" => "aarch64-w64-mingw32-dlltool",
447+
448+
// For non-standard architectures (e.g., aarch32) fallback to "dlltool".
449+
_ => "dlltool",
450+
}
435451
}
436452
.into();
437453

438-
if sess.host.options.is_like_windows {
439-
// If we're compiling on Windows, add the .exe suffix
440-
tool_name.push(".exe");
441-
}
442-
443454
// NOTE: it's not clear how useful it is to explicitly search PATH.
444455
for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) {
445456
let full_path = dir.join(&tool_name);

src/ci/docker/host-x86_64/i686-gnu/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1616
zlib1g-dev \
1717
lib32z1-dev \
1818
xz-utils \
19+
mingw-w64 \
1920
&& rm -rf /var/lib/apt/lists/*
2021

2122

src/ci/docker/host-x86_64/x86_64-gnu-llvm-14-stage1/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
2222
zlib1g-dev \
2323
xz-utils \
2424
nodejs \
25+
mingw-w64 \
2526
&& rm -rf /var/lib/apt/lists/*
2627

2728
COPY scripts/sccache.sh /scripts/

src/ci/docker/host-x86_64/x86_64-gnu-llvm-14/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
2323
zlib1g-dev \
2424
xz-utils \
2525
nodejs \
26+
mingw-w64 \
2627
&& rm -rf /var/lib/apt/lists/*
2728

2829
# Install powershell (universal package) so we can test x.ps1 on Linux

src/ci/docker/host-x86_64/x86_64-gnu-llvm-15/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
2525
zlib1g-dev \
2626
xz-utils \
2727
nodejs \
28+
mingw-w64 \
2829
&& rm -rf /var/lib/apt/lists/*
2930

3031
# Install powershell (universal package) so we can test x.ps1 on Linux

src/ci/docker/host-x86_64/x86_64-gnu/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1616
libssl-dev \
1717
pkg-config \
1818
xz-utils \
19+
mingw-w64 \
1920
&& rm -rf /var/lib/apt/lists/*
2021

2122
COPY scripts/sccache.sh /scripts/

src/tools/compiletest/src/header.rs

+15
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,19 @@ pub fn make_test_description<R: Read>(
964964
.join(if config.host.contains("windows") { "rust-lld.exe" } else { "rust-lld" })
965965
.exists();
966966

967+
fn is_on_path(file: &'static str) -> impl Fn() -> bool {
968+
move || env::split_paths(&env::var_os("PATH").unwrap()).any(|dir| dir.join(file).is_file())
969+
}
970+
971+
// On Windows, dlltool.exe is used for all architectures.
972+
#[cfg(windows)]
973+
let (has_i686_dlltool, has_x86_64_dlltool) =
974+
(is_on_path("dlltool.exe"), is_on_path("dlltool.exe"));
975+
// For non-Windows, there are architecture specific dlltool binaries.
976+
#[cfg(not(windows))]
977+
let (has_i686_dlltool, has_x86_64_dlltool) =
978+
(is_on_path("i686-w64-mingw32-dlltool"), is_on_path("x86_64-w64-mingw32-dlltool"));
979+
967980
iter_header(path, src, &mut |revision, ln| {
968981
if revision.is_some() && revision != cfg {
969982
return;
@@ -1031,6 +1044,8 @@ pub fn make_test_description<R: Read>(
10311044
reason!(config.debugger == Some(Debugger::Gdb) && ignore_gdb(config, ln));
10321045
reason!(config.debugger == Some(Debugger::Lldb) && ignore_lldb(config, ln));
10331046
reason!(!has_rust_lld && config.parse_name_directive(ln, "needs-rust-lld"));
1047+
reason!(config.parse_name_directive(ln, "needs-i686-dlltool") && !has_i686_dlltool());
1048+
reason!(config.parse_name_directive(ln, "needs-x86_64-dlltool") && !has_x86_64_dlltool());
10341049
should_fail |= config.parse_name_directive(ln, "should-fail");
10351050
});
10361051

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Tests that raw-dylib cross compilation works correctly
2+
3+
# only-gnu
4+
# needs-i686-dlltool
5+
# needs-x86_64-dlltool
6+
7+
# i686 dlltool.exe can't product x64 binaries.
8+
# ignore-i686-pc-windows-gnu
9+
10+
include ../../run-make-fulldeps/tools.mk
11+
12+
all:
13+
# Build as x86 and make sure that we have x86 objects only.
14+
$(RUSTC) --crate-type lib --crate-name i686_raw_dylib_test --target i686-pc-windows-gnu lib.rs
15+
"$(LLVM_BIN_DIR)"/llvm-objdump -a $(TMPDIR)/libi686_raw_dylib_test.rlib > $(TMPDIR)/i686.objdump.txt
16+
$(CGREP) "file format coff-i386" < $(TMPDIR)/i686.objdump.txt
17+
$(CGREP) -v "file format coff-x86-64" < $(TMPDIR)/i686.objdump.txt
18+
# Build as x64 and make sure that we have x64 objects only.
19+
$(RUSTC) --crate-type lib --crate-name x64_raw_dylib_test --target x86_64-pc-windows-gnu lib.rs
20+
"$(LLVM_BIN_DIR)"/llvm-objdump -a $(TMPDIR)/libx64_raw_dylib_test.rlib > $(TMPDIR)/x64.objdump.txt
21+
$(CGREP) "file format coff-x86-64" < $(TMPDIR)/x64.objdump.txt
22+
$(CGREP) -v "file format coff-i386" < $(TMPDIR)/x64.objdump.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(raw_dylib)]
2+
#![feature(no_core, lang_items)]
3+
#![no_std]
4+
#![no_core]
5+
#![crate_type = "lib"]
6+
7+
// This is needed because of #![no_core]:
8+
#[lang = "sized"]
9+
trait Sized {}
10+
11+
#[link(name = "extern_1", kind = "raw-dylib")]
12+
extern {
13+
fn extern_fn();
14+
}
15+
16+
pub fn extern_fn_caller() {
17+
unsafe {
18+
extern_fn();
19+
}
20+
}

0 commit comments

Comments
 (0)