Skip to content

Commit 13d3b75

Browse files
committed
Stabilize -Cdlltool
1 parent df7fd99 commit 13d3b75

File tree

17 files changed

+104
-15
lines changed

17 files changed

+104
-15
lines changed

compiler/rustc_codegen_llvm/messages.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ codegen_llvm_error_writing_def_file =
2424
Error writing .DEF file: {$error}
2525
2626
codegen_llvm_error_calling_dlltool =
27-
Error calling dlltool: {$error}
27+
Error calling dlltool '{$dlltool_path}': {$error}
2828
2929
codegen_llvm_dlltool_fail_import_library =
30-
Dlltool could not create import library: {$stdout}\n{$stderr}
30+
Dlltool could not create import library: {$error}
3131
3232
codegen_llvm_target_feature_disable_or_enable =
3333
the target features {$features} must all be either enabled or disabled together

compiler/rustc_codegen_llvm/src/back/archive.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {
198198
"arm" => ("arm", "--32"),
199199
_ => panic!("unsupported arch {}", sess.target.arch),
200200
};
201-
let result = std::process::Command::new(dlltool)
201+
let result = std::process::Command::new(&dlltool)
202202
.args([
203203
"-d",
204204
def_file_path.to_str().unwrap(),
@@ -218,12 +218,15 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {
218218

219219
match result {
220220
Err(e) => {
221-
sess.emit_fatal(ErrorCallingDllTool { error: e });
221+
sess.emit_fatal(ErrorCallingDllTool {
222+
dlltool_path: dlltool.to_string_lossy(),
223+
error: e,
224+
});
222225
}
223-
Ok(output) if !output.status.success() => {
226+
// dlltool returns '0' on failure, so check for error output instead.
227+
Ok(output) if !output.stderr.is_empty() => {
224228
sess.emit_fatal(DlltoolFailImportLibrary {
225-
stdout: String::from_utf8_lossy(&output.stdout),
226-
stderr: String::from_utf8_lossy(&output.stderr),
229+
error: String::from_utf8_lossy(&output.stderr),
227230
})
228231
}
229232
_ => {}
@@ -431,7 +434,7 @@ fn string_to_io_error(s: String) -> io::Error {
431434

432435
fn find_binutils_dlltool(sess: &Session) -> OsString {
433436
assert!(sess.target.options.is_like_windows && !sess.target.options.is_like_msvc);
434-
if let Some(dlltool_path) = &sess.opts.unstable_opts.dlltool {
437+
if let Some(dlltool_path) = &sess.opts.cg.dlltool {
435438
return dlltool_path.clone().into_os_string();
436439
}
437440

compiler/rustc_codegen_llvm/src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ pub(crate) struct ErrorWritingDEFFile {
6767

6868
#[derive(Diagnostic)]
6969
#[diag(codegen_llvm_error_calling_dlltool)]
70-
pub(crate) struct ErrorCallingDllTool {
70+
pub(crate) struct ErrorCallingDllTool<'a> {
71+
pub dlltool_path: Cow<'a, str>,
7172
pub error: std::io::Error,
7273
}
7374

7475
#[derive(Diagnostic)]
7576
#[diag(codegen_llvm_dlltool_fail_import_library)]
7677
pub(crate) struct DlltoolFailImportLibrary<'a> {
77-
pub stdout: Cow<'a, str>,
78-
pub stderr: Cow<'a, str>,
78+
pub error: Cow<'a, str>,
7979
}
8080

8181
#[derive(Diagnostic)]

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ fn test_codegen_options_tracking_hash() {
545545
untracked!(ar, String::from("abc"));
546546
untracked!(codegen_units, Some(42));
547547
untracked!(default_linker_libraries, true);
548+
untracked!(dlltool, Some(PathBuf::from("custom_dlltool.exe")));
548549
untracked!(extra_filename, String::from("extra-filename"));
549550
untracked!(incremental, Some(String::from("abc")));
550551
// `link_arg` is omitted because it just forwards to `link_args`.
@@ -649,7 +650,6 @@ fn test_unstable_options_tracking_hash() {
649650
untracked!(assert_incr_state, Some(String::from("loaded")));
650651
untracked!(deduplicate_diagnostics, false);
651652
untracked!(dep_tasks, true);
652-
untracked!(dlltool, Some(PathBuf::from("custom_dlltool.exe")));
653653
untracked!(dont_buffer_diagnostics, true);
654654
untracked!(dump_dep_graph, true);
655655
untracked!(dump_drop_tracking_cfg, Some("cfg.dot".to_string()));

compiler/rustc_session/src/options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,8 @@ options! {
12051205
2 = full debug info with variable and type information; default: 0)"),
12061206
default_linker_libraries: bool = (false, parse_bool, [UNTRACKED],
12071207
"allow the linker to link its default libraries (default: no)"),
1208+
dlltool: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
1209+
"import library generation tool (windows-gnu only)"),
12081210
embed_bitcode: bool = (true, parse_bool, [TRACKED],
12091211
"emit bitcode in rlibs (default: yes)"),
12101212
extra_filename: String = (String::new(), parse_string, [UNTRACKED],
@@ -1361,8 +1363,6 @@ options! {
13611363
(default: no)"),
13621364
diagnostic_width: Option<usize> = (None, parse_opt_number, [UNTRACKED],
13631365
"set the current output width for diagnostic truncation"),
1364-
dlltool: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
1365-
"import library generation tool (windows-gnu only)"),
13661366
dont_buffer_diagnostics: bool = (false, parse_bool, [UNTRACKED],
13671367
"emit diagnostics rather than buffering (breaks NLL error downgrading, sorting) \
13681368
(default: no)"),

src/doc/rustc/src/codegen-options/index.md

+8
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ It takes one of the following values:
8888
For example, for gcc flavor linkers, this issues the `-nodefaultlibs` flag to
8989
the linker.
9090

91+
## dlltool
92+
93+
On `windows-gnu` targets, this flag controls which dlltool `rustc` invokes to
94+
generate import libraries when using the [`raw-dylib` link kind](../../reference/items/external-blocks.md#the-link-attribute).
95+
It takes a path to [the dlltool executable](https://sourceware.org/binutils/docs/binutils/dlltool.html).
96+
If this flag is not specified, a dlltool executable will be inferred based on
97+
the host environment and target.
98+
9199
## embed-bitcode
92100

93101
This flag controls whether or not the compiler embeds LLVM bitcode into object

src/tools/compiletest/src/header.rs

+10
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,15 @@ pub fn make_test_description<R: Read>(
976976
#[cfg(not(windows))]
977977
let (has_i686_dlltool, has_x86_64_dlltool) =
978978
(is_on_path("i686-w64-mingw32-dlltool"), is_on_path("x86_64-w64-mingw32-dlltool"));
979+
let has_dlltool = || {
980+
if config.matches_arch("x86") {
981+
has_i686_dlltool()
982+
} else if config.matches_arch("x86_64") {
983+
has_x86_64_dlltool()
984+
} else {
985+
false
986+
}
987+
};
979988

980989
iter_header(path, src, &mut |revision, ln| {
981990
if revision.is_some() && revision != cfg {
@@ -1046,6 +1055,7 @@ pub fn make_test_description<R: Read>(
10461055
reason!(!has_rust_lld && config.parse_name_directive(ln, "needs-rust-lld"));
10471056
reason!(config.parse_name_directive(ln, "needs-i686-dlltool") && !has_i686_dlltool());
10481057
reason!(config.parse_name_directive(ln, "needs-x86_64-dlltool") && !has_x86_64_dlltool());
1058+
reason!(config.parse_name_directive(ln, "needs-dlltool") && !has_dlltool());
10491059
should_fail |= config.parse_name_directive(ln, "should-fail");
10501060
});
10511061

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Test using -Cdlltool to change where raw-dylib looks for the dlltool binary.
2+
3+
# only-windows
4+
# only-gnu
5+
# needs-dlltool
6+
7+
include ../../run-make-fulldeps/tools.mk
8+
9+
all:
10+
$(RUSTC) --crate-type lib --crate-name raw_dylib_test lib.rs -Cdlltool=$(CURDIR)/script.cmd
11+
$(DIFF) output.txt "$(TMPDIR)"/output.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(raw_dylib)]
2+
3+
#[link(name = "extern_1", kind = "raw-dylib")]
4+
extern {
5+
fn extern_fn_1();
6+
}
7+
8+
pub fn library_function() {
9+
unsafe {
10+
extern_fn_1();
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Called dlltool via script.cmd
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
echo Called dlltool via script.cmd> %TMPDIR%\output.txt
2+
dlltool.exe %*

tests/rustdoc-ui/c-help.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
-C debug-assertions=val -- explicitly enable the `cfg(debug_assertions)` directive
66
-C debuginfo=val -- debug info emission level (0 = no debug info, 1 = line tables only, 2 = full debug info with variable and type information; default: 0)
77
-C default-linker-libraries=val -- allow the linker to link its default libraries (default: no)
8+
-C dlltool=val -- import library generation tool (windows-gnu only)
89
-C embed-bitcode=val -- emit bitcode in rlibs (default: yes)
910
-C extra-filename=val -- extra data to put in each output filename
1011
-C force-frame-pointers=val -- force use of the frame pointers

tests/rustdoc-ui/z-help.stdout

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
-Z dep-info-omit-d-target=val -- in dep-info output, omit targets for tracking dependencies of the dep-info files themselves (default: no)
1818
-Z dep-tasks=val -- print tasks that execute and the color their dep node gets (requires debug build) (default: no)
1919
-Z diagnostic-width=val -- set the current output width for diagnostic truncation
20-
-Z dlltool=val -- import library generation tool (windows-gnu only)
2120
-Z dont-buffer-diagnostics=val -- emit diagnostics rather than buffering (breaks NLL error downgrading, sorting) (default: no)
2221
-Z drop-tracking=val -- enables drop tracking in generators (default: no)
2322
-Z drop-tracking-mir=val -- enables drop tracking on MIR in generators (default: no)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Tests that dlltool failing to generate an import library will raise an error.
2+
3+
// only-gnu
4+
// only-windows
5+
// needs-dlltool
6+
// compile-flags: --crate-type lib --emit link
7+
// normalize-stderr-test: "[^ ']*/dlltool.exe" -> "$$DLLTOOL"
8+
// normalize-stderr-test: "[^ ]*/foo.def" -> "$$DEF_FILE"
9+
#![feature(raw_dylib)]
10+
#[link(name = "foo", kind = "raw-dylib")]
11+
extern "C" {
12+
// `@1` is an invalid name to export, as it usually indicates that something
13+
// is being exported via ordinal.
14+
#[link_name = "@1"]
15+
fn f(x: i32);
16+
}
17+
18+
pub fn lib_main() {
19+
unsafe { f(42); }
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: Dlltool could not create import library: $DLLTOOL: Syntax error in def file $DEF_FILE:1
2+
3+
error: aborting due to previous error
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Tests that failing to run dlltool will raise an error.
2+
3+
// only-gnu
4+
// only-windows
5+
// compile-flags: --crate-type lib --emit link -Cdlltool=does_not_exit.exe
6+
#![feature(raw_dylib)]
7+
#[link(name = "foo", kind = "raw-dylib")]
8+
extern "C" {
9+
fn f(x: i32);
10+
}
11+
12+
pub fn lib_main() {
13+
unsafe { f(42); }
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: Error calling dlltool 'does_not_exit.exe': program not found
2+
3+
error: aborting due to previous error
4+

0 commit comments

Comments
 (0)