Skip to content

Commit dc18d08

Browse files
authored
Rollup merge of rust-lang#109677 - dpaoliello:rawdylib, r=michaelwoerister,wesleywiser
Stabilize raw-dylib, link_ordinal, import_name_type and -Cdlltool This stabilizes the `raw-dylib` feature (rust-lang#58713) for all architectures (i.e., `x86` as it is already stable for all other architectures). Changes: * Permit the use of the `raw-dylib` link kind for x86, the `link_ordinal` attribute and the `import_name_type` key for the `link` attribute. * Mark the `raw_dylib` feature as stable. * Stabilized the `-Zdlltool` argument as `-Cdlltool`. * Note the path to `dlltool` if invoking it failed (we don't need to do this if `dlltool` returns an error since it prints its path in the error message). * Adds tests for `-Cdlltool`. * Adds tests for being unable to find the dlltool executable, and dlltool failing. * Fixes a bug where we were checking the exit code of dlltool to see if it failed, but dlltool always returns 0 (indicating success), so instead we need to check if anything was written to `stderr`. NOTE: As previously noted (rust-lang#104218 (comment)) using dlltool within rustc is temporary, but this is not the first time that Rust has added a temporary tool use and argument: rust-lang#104218 (comment) Big thanks to ```@tbu-``` for the first version of this PR (rust-lang#104218)
2 parents 02d8fa7 + 1ece1ea commit dc18d08

File tree

62 files changed

+146
-223
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+146
-223
lines changed

compiler/rustc_codegen_llvm/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ 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 =
3030
Dlltool could not create import library: {$stdout}

compiler/rustc_codegen_llvm/src/back/archive.rs

+8-4
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,9 +218,13 @@ 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 {
225229
stdout: String::from_utf8_lossy(&output.stdout),
226230
stderr: String::from_utf8_lossy(&output.stderr),
@@ -431,7 +435,7 @@ fn string_to_io_error(s: String) -> io::Error {
431435

432436
fn find_binutils_dlltool(sess: &Session) -> OsString {
433437
assert!(sess.target.options.is_like_windows && !sess.target.options.is_like_msvc);
434-
if let Some(dlltool_path) = &sess.opts.unstable_opts.dlltool {
438+
if let Some(dlltool_path) = &sess.opts.cg.dlltool {
435439
return dlltool_path.clone().into_os_string();
436440
}
437441

compiler/rustc_codegen_llvm/src/errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ 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

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

-9
Original file line numberDiff line numberDiff line change
@@ -592,15 +592,6 @@ fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
592592

593593
fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<u16> {
594594
use rustc_ast::{LitIntType, LitKind, MetaItemLit};
595-
if !tcx.features().raw_dylib && tcx.sess.target.arch == "x86" {
596-
feature_err(
597-
&tcx.sess.parse_sess,
598-
sym::raw_dylib,
599-
attr.span,
600-
"`#[link_ordinal]` is unstable on x86",
601-
)
602-
.emit();
603-
}
604595
let meta_item_list = attr.meta_item_list();
605596
let meta_item_list = meta_item_list.as_deref();
606597
let sole_meta_list = match meta_item_list {

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ declare_features! (
280280
(accepted, pub_restricted, "1.18.0", Some(32409), None),
281281
/// Allows use of the postfix `?` operator in expressions.
282282
(accepted, question_mark, "1.13.0", Some(31436), None),
283+
/// Allows the use of raw-dylibs (RFC 2627).
284+
(accepted, raw_dylib, "CURRENT_RUSTC_VERSION", Some(58713), None),
283285
/// Allows keywords to be escaped for use as identifiers.
284286
(accepted, raw_identifiers, "1.30.0", Some(48589), None),
285287
/// Allows relaxing the coherence rules such that

compiler/rustc_feature/src/active.rs

-2
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,6 @@ declare_features! (
483483
(active, precise_pointer_size_matching, "1.32.0", Some(56354), None),
484484
/// Allows macro attributes on expressions, statements and non-inline modules.
485485
(active, proc_macro_hygiene, "1.30.0", Some(54727), None),
486-
/// Allows the use of raw-dylibs (RFC 2627).
487-
(active, raw_dylib, "1.65.0", Some(58713), None),
488486
/// Allows `&raw const $place_expr` and `&raw mut $place_expr` expressions.
489487
(active, raw_ref_op, "1.41.0", Some(64490), None),
490488
/// Allows using the `#[register_tool]` attribute.

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ fn test_codegen_options_tracking_hash() {
547547
untracked!(ar, String::from("abc"));
548548
untracked!(codegen_units, Some(42));
549549
untracked!(default_linker_libraries, true);
550+
untracked!(dlltool, Some(PathBuf::from("custom_dlltool.exe")));
550551
untracked!(extra_filename, String::from("extra-filename"));
551552
untracked!(incremental, Some(String::from("abc")));
552553
// `link_arg` is omitted because it just forwards to `link_args`.
@@ -651,7 +652,6 @@ fn test_unstable_options_tracking_hash() {
651652
untracked!(assert_incr_state, Some(String::from("loaded")));
652653
untracked!(deduplicate_diagnostics, false);
653654
untracked!(dep_tasks, true);
654-
untracked!(dlltool, Some(PathBuf::from("custom_dlltool.exe")));
655655
untracked!(dont_buffer_diagnostics, true);
656656
untracked!(dump_dep_graph, true);
657657
untracked!(dump_drop_tracking_cfg, Some("cfg.dot".to_string()));

compiler/rustc_metadata/src/native_libs.rs

-18
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,6 @@ impl<'tcx> Collector<'tcx> {
161161
"raw-dylib" => {
162162
if !sess.target.is_like_windows {
163163
sess.emit_err(errors::FrameworkOnlyWindows { span });
164-
} else if !features.raw_dylib && sess.target.arch == "x86" {
165-
feature_err(
166-
&sess.parse_sess,
167-
sym::raw_dylib,
168-
span,
169-
"link kind `raw-dylib` is unstable on x86",
170-
)
171-
.emit();
172164
}
173165
NativeLibKind::RawDylib
174166
}
@@ -251,16 +243,6 @@ impl<'tcx> Collector<'tcx> {
251243
continue;
252244
}
253245
};
254-
if !features.raw_dylib {
255-
let span = item.name_value_literal_span().unwrap();
256-
feature_err(
257-
&sess.parse_sess,
258-
sym::raw_dylib,
259-
span,
260-
"import name type is unstable",
261-
)
262-
.emit();
263-
}
264246
import_name_type = Some((link_import_name_type, item.span()));
265247
}
266248
_ => {

compiler/rustc_session/src/options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,8 @@ options! {
12351235
line-tables-only, limited, or full; default: 0)"),
12361236
default_linker_libraries: bool = (false, parse_bool, [UNTRACKED],
12371237
"allow the linker to link its default libraries (default: no)"),
1238+
dlltool: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
1239+
"import library generation tool (ignored except when targeting windows-gnu)"),
12381240
embed_bitcode: bool = (true, parse_bool, [TRACKED],
12391241
"emit bitcode in rlibs (default: yes)"),
12401242
extra_filename: String = (String::new(), parse_string, [UNTRACKED],
@@ -1391,8 +1393,6 @@ options! {
13911393
(default: no)"),
13921394
diagnostic_width: Option<usize> = (None, parse_opt_number, [UNTRACKED],
13931395
"set the current output width for diagnostic truncation"),
1394-
dlltool: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
1395-
"import library generation tool (windows-gnu only)"),
13961396
dont_buffer_diagnostics: bool = (false, parse_bool, [UNTRACKED],
13971397
"emit diagnostics rather than buffering (breaks NLL error downgrading, sorting) \
13981398
(default: no)"),

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

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

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

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

src/doc/unstable-book/src/language-features/raw-dylib.md

-34
This file was deleted.

src/tools/compiletest/src/header/needs.rs

+31-5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ pub(super) fn handle_needs(
115115
condition: cache.x86_64_dlltool,
116116
ignore_reason: "ignored when dlltool for x86_64 is not present",
117117
},
118+
Need {
119+
name: "needs-dlltool",
120+
condition: cache.dlltool,
121+
ignore_reason: "ignored when dlltool for the current architecture is not present",
122+
},
118123
Need {
119124
name: "needs-git-hash",
120125
condition: config.git_hash,
@@ -183,13 +188,25 @@ pub(super) struct CachedNeedsConditions {
183188
rust_lld: bool,
184189
i686_dlltool: bool,
185190
x86_64_dlltool: bool,
191+
dlltool: bool,
186192
}
187193

188194
impl CachedNeedsConditions {
189195
pub(super) fn load(config: &Config) -> Self {
190196
let path = std::env::var_os("PATH").expect("missing PATH environment variable");
191197
let path = std::env::split_paths(&path).collect::<Vec<_>>();
192198

199+
// On Windows, dlltool.exe is used for all architectures.
200+
#[cfg(windows)]
201+
let dlltool = path.iter().any(|dir| dir.join("dlltool.exe").is_file());
202+
203+
// For non-Windows, there are architecture specific dlltool binaries.
204+
#[cfg(not(windows))]
205+
let i686_dlltool = path.iter().any(|dir| dir.join("i686-w64-mingw32-dlltool").is_file());
206+
#[cfg(not(windows))]
207+
let x86_64_dlltool =
208+
path.iter().any(|dir| dir.join("x86_64-w64-mingw32-dlltool").is_file());
209+
193210
let target = &&*config.target;
194211
Self {
195212
sanitizer_support: std::env::var_os("RUSTC_SANITIZER_SUPPORT").is_some(),
@@ -225,17 +242,26 @@ impl CachedNeedsConditions {
225242
.join(if config.host.contains("windows") { "rust-lld.exe" } else { "rust-lld" })
226243
.exists(),
227244

228-
// On Windows, dlltool.exe is used for all architectures.
229245
#[cfg(windows)]
230-
i686_dlltool: path.iter().any(|dir| dir.join("dlltool.exe").is_file()),
246+
i686_dlltool: dlltool,
231247
#[cfg(windows)]
232-
x86_64_dlltool: path.iter().any(|dir| dir.join("dlltool.exe").is_file()),
248+
x86_64_dlltool: dlltool,
249+
#[cfg(windows)]
250+
dlltool,
233251

234252
// For non-Windows, there are architecture specific dlltool binaries.
235253
#[cfg(not(windows))]
236-
i686_dlltool: path.iter().any(|dir| dir.join("i686-w64-mingw32-dlltool").is_file()),
254+
i686_dlltool,
255+
#[cfg(not(windows))]
256+
x86_64_dlltool,
237257
#[cfg(not(windows))]
238-
x86_64_dlltool: path.iter().any(|dir| dir.join("x86_64-w64-mingw32-dlltool").is_file()),
258+
dlltool: if config.matches_arch("x86") {
259+
i686_dlltool
260+
} else if config.matches_arch("x86_64") {
261+
x86_64_dlltool
262+
} else {
263+
false
264+
},
239265
}
240266
}
241267
}

tests/run-make/raw-dylib-alt-calling-convention/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![feature(abi_vectorcall)]
2-
#![cfg_attr(target_arch = "x86", feature(raw_dylib))]
32

43
#[repr(C)]
54
#[derive(Clone)]

tests/run-make/raw-dylib-c/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(raw_dylib)]
2-
31
#[link(name = "extern_1.dll", kind = "raw-dylib", modifiers = "+verbatim")]
42
extern {
53
fn extern_fn_1();

tests/run-make/raw-dylib-cross-compilation/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(raw_dylib)]
21
#![feature(no_core, lang_items)]
32
#![no_std]
43
#![no_core]
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 ../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,10 @@
1+
#[link(name = "extern_1", kind = "raw-dylib")]
2+
extern {
3+
fn extern_fn_1();
4+
}
5+
6+
pub fn library_function() {
7+
unsafe {
8+
extern_fn_1();
9+
}
10+
}
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/run-make/raw-dylib-import-name-type/driver.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(raw_dylib)]
21
#![feature(abi_vectorcall)]
32

43
#[link(name = "extern", kind = "raw-dylib", import_name_type = "undecorated")]

tests/run-make/raw-dylib-inline-cross-dylib/driver.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(raw_dylib)]
2-
31
extern crate raw_dylib_test;
42
extern crate raw_dylib_test_wrapper;
53

tests/run-make/raw-dylib-inline-cross-dylib/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(raw_dylib)]
2-
31
#[link(name = "extern_1", kind = "raw-dylib")]
42
extern {
53
fn extern_fn_1();

tests/run-make/raw-dylib-link-ordinal/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![cfg_attr(target_arch = "x86", feature(raw_dylib))]
2-
31
#[link(name = "exporter", kind = "raw-dylib")]
42
extern {
53
#[link_ordinal(13)]

tests/run-make/raw-dylib-stdcall-ordinal/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![cfg_attr(target_arch = "x86", feature(raw_dylib))]
2-
31
#[link(name = "exporter", kind = "raw-dylib")]
42
extern "stdcall" {
53
#[link_ordinal(15)]

tests/ui/feature-gates/feature-gate-raw-dylib-2.rs

-12
This file was deleted.

tests/ui/feature-gates/feature-gate-raw-dylib-2.stderr

-21
This file was deleted.

tests/ui/feature-gates/feature-gate-raw-dylib-import-name-type.rs

-8
This file was deleted.

0 commit comments

Comments
 (0)