Skip to content

Commit f33d409

Browse files
authored
Rollup merge of rust-lang#104360 - petrochenkov:stabverb, r=TaKO8Ki
Stabilize native library modifier `verbatim` Stabilization report - rust-lang#104360 (comment). cc rust-lang#81490 Closes rust-lang#99425
2 parents dd12cd6 + 5b0e80e commit f33d409

File tree

17 files changed

+73
-95
lines changed

17 files changed

+73
-95
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,8 @@ fn link_rlib<'a>(
377377
find_native_static_library(name.as_str(), lib.verbatim, &lib_search_paths, sess);
378378
if sess.opts.unstable_opts.packed_bundled_libs && flavor == RlibFlavor::Normal {
379379
let filename = lib.filename.unwrap();
380-
let lib_path = find_native_static_library(
381-
filename.as_str(),
382-
Some(true),
383-
&lib_search_paths,
384-
sess,
385-
);
380+
let lib_path =
381+
find_native_static_library(filename.as_str(), true, &lib_search_paths, sess);
386382
let src = read(lib_path)
387383
.map_err(|e| sess.emit_fatal(errors::ReadFileError { message: e }))?;
388384
let (data, _) = create_wrapper_file(sess, b".bundled_lib".to_vec(), &src);
@@ -465,7 +461,7 @@ fn collate_raw_dylibs<'a, 'b>(
465461

466462
for lib in used_libraries {
467463
if lib.kind == NativeLibKind::RawDylib {
468-
let ext = if matches!(lib.verbatim, Some(true)) { "" } else { ".dll" };
464+
let ext = if lib.verbatim { "" } else { ".dll" };
469465
let name = format!("{}{}", lib.name.expect("unnamed raw-dylib library"), ext);
470466
let imports = dylib_table.entry(name.clone()).or_default();
471467
for import in &lib.dll_imports {
@@ -1335,7 +1331,7 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
13351331
NativeLibKind::Static { bundle: Some(false), .. }
13361332
| NativeLibKind::Dylib { .. }
13371333
| NativeLibKind::Unspecified => {
1338-
let verbatim = lib.verbatim.unwrap_or(false);
1334+
let verbatim = lib.verbatim;
13391335
if sess.target.is_like_msvc {
13401336
Some(format!("{}{}", name, if verbatim { "" } else { ".lib" }))
13411337
} else if sess.target.linker_flavor.is_gnu() {
@@ -2306,7 +2302,7 @@ fn add_native_libs_from_crate(
23062302
_ => &codegen_results.crate_info.native_libraries[&cnum],
23072303
};
23082304

2309-
let mut last = (None, NativeLibKind::Unspecified, None);
2305+
let mut last = (None, NativeLibKind::Unspecified, false);
23102306
for lib in native_libs {
23112307
let Some(name) = lib.name else {
23122308
continue;
@@ -2323,7 +2319,7 @@ fn add_native_libs_from_crate(
23232319
};
23242320

23252321
let name = name.as_str();
2326-
let verbatim = lib.verbatim.unwrap_or(false);
2322+
let verbatim = lib.verbatim;
23272323
match lib.kind {
23282324
NativeLibKind::Static { bundle, whole_archive } => {
23292325
if link_static {

compiler/rustc_codegen_ssa/src/back/linker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ impl<'a> Linker for GccLinker<'a> {
515515
// -force_load is the macOS equivalent of --whole-archive, but it
516516
// involves passing the full path to the library to link.
517517
self.linker_arg("-force_load");
518-
let lib = find_native_static_library(lib, Some(verbatim), search_path, &self.sess);
518+
let lib = find_native_static_library(lib, verbatim, search_path, &self.sess);
519519
self.linker_arg(&lib);
520520
}
521521
}

compiler/rustc_codegen_ssa/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub struct NativeLib {
116116
pub name: Option<Symbol>,
117117
pub filename: Option<Symbol>,
118118
pub cfg: Option<ast::MetaItem>,
119-
pub verbatim: Option<bool>,
119+
pub verbatim: bool,
120120
pub dll_imports: Vec<cstore::DllImport>,
121121
}
122122

@@ -127,7 +127,7 @@ impl From<&cstore::NativeLib> for NativeLib {
127127
filename: lib.filename,
128128
name: lib.name,
129129
cfg: lib.cfg.clone(),
130-
verbatim: lib.verbatim,
130+
verbatim: lib.verbatim.unwrap_or(false),
131131
dll_imports: lib.dll_imports.clone(),
132132
}
133133
}

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ declare_features! (
237237
(accepted, native_link_modifiers, "1.61.0", Some(81490), None),
238238
/// Allows specifying the bundle link modifier
239239
(accepted, native_link_modifiers_bundle, "1.63.0", Some(81490), None),
240+
/// Allows specifying the verbatim link modifier
241+
(accepted, native_link_modifiers_verbatim, "CURRENT_RUSTC_VERSION", Some(81490), None),
240242
/// Allows specifying the whole-archive link modifier
241243
(accepted, native_link_modifiers_whole_archive, "1.61.0", Some(81490), None),
242244
/// Allows using non lexical lifetimes (RFC 2094).

compiler/rustc_feature/src/active.rs

-2
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,6 @@ declare_features! (
455455
(active, naked_functions, "1.9.0", Some(32408), None),
456456
/// Allows specifying the as-needed link modifier
457457
(active, native_link_modifiers_as_needed, "1.53.0", Some(81490), None),
458-
/// Allows specifying the verbatim link modifier
459-
(active, native_link_modifiers_verbatim, "1.53.0", Some(81490), None),
460458
/// Allow negative trait implementations.
461459
(active, negative_impls, "1.44.0", Some(68318), None),
462460
/// Allows the `!` type. Does not imply 'exhaustive_patterns' (below) any more.

compiler/rustc_metadata/src/native_libs.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ use std::path::PathBuf;
2929

3030
pub fn find_native_static_library(
3131
name: &str,
32-
verbatim: Option<bool>,
32+
verbatim: bool,
3333
search_paths: &[PathBuf],
3434
sess: &Session,
3535
) -> PathBuf {
36-
let formats = if verbatim.unwrap_or(false) {
36+
let formats = if verbatim {
3737
vec![("".into(), "".into())]
3838
} else {
3939
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
@@ -52,7 +52,7 @@ pub fn find_native_static_library(
5252
}
5353
}
5454

55-
sess.emit_fatal(MissingNativeLibrary::new(name, verbatim.unwrap_or(false)));
55+
sess.emit_fatal(MissingNativeLibrary::new(name, verbatim));
5656
}
5757

5858
fn find_bundled_library(
@@ -66,7 +66,7 @@ fn find_bundled_library(
6666
let NativeLibKind::Static { bundle: Some(true) | None, .. } = kind {
6767
find_native_static_library(
6868
name.unwrap().as_str(),
69-
verbatim,
69+
verbatim.unwrap_or(false),
7070
&sess.target_filesearch(PathKind::Native).search_path_dirs(),
7171
sess,
7272
).file_name().and_then(|s| s.to_str()).map(Symbol::intern)
@@ -311,10 +311,7 @@ impl<'tcx> Collector<'tcx> {
311311
sess.emit_err(BundleNeedsStatic { span });
312312
}
313313

314-
("verbatim", _) => {
315-
report_unstable_modifier!(native_link_modifiers_verbatim);
316-
assign_modifier(&mut verbatim)
317-
}
314+
("verbatim", _) => assign_modifier(&mut verbatim),
318315

319316
("whole-archive", Some(NativeLibKind::Static { whole_archive, .. })) => {
320317
assign_modifier(whole_archive)

compiler/rustc_session/src/config.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2029,10 +2029,7 @@ fn parse_native_lib_modifiers(
20292029
"linking modifier `bundle` is only compatible with `static` linking kind",
20302030
),
20312031

2032-
("verbatim", _) => {
2033-
report_unstable_modifier();
2034-
assign_modifier(&mut verbatim)
2035-
}
2032+
("verbatim", _) => assign_modifier(&mut verbatim),
20362033

20372034
("whole-archive", NativeLibKind::Static { whole_archive, .. }) => {
20382035
assign_modifier(whole_archive)

src/doc/rustc/src/command-line-arguments.md

+27
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,33 @@ This modifier has no effect when building other targets like executables or dyna
104104

105105
The default for this modifier is `+bundle`.
106106

107+
### Linking modifiers: `verbatim`
108+
109+
This modifier is compatible with all linking kinds.
110+
111+
`+verbatim` means that rustc itself won't add any target-specified library prefixes or suffixes
112+
(like `lib` or `.a`) to the library name, and will try its best to ask for the same thing from the
113+
linker.
114+
115+
For `ld`-like linkers supporting GNU extensions rustc will use the `-l:filename` syntax (note the
116+
colon) when passing the library, so the linker won't add any prefixes or suffixes to it.
117+
See [`-l namespec`](https://sourceware.org/binutils/docs/ld/Options.html) in ld documentation for
118+
more details. \
119+
For linkers not supporting any verbatim modifiers (e.g. `link.exe` or `ld64`) the library name will
120+
be passed as is. So the most reliable cross-platform use scenarios for this option are when no
121+
linker is involved, for example bundling native libraries into rlibs.
122+
123+
`-verbatim` means that rustc will either add a target-specific prefix and suffix to the library
124+
name before passing it to linker, or won't prevent linker from implicitly adding it. \
125+
In case of `raw-dylib` kind in particular `.dll` will be added to the library name on Windows.
126+
127+
The default for this modifier is `-verbatim`.
128+
129+
NOTE: Even with `+verbatim` and `-l:filename` syntax `ld`-like linkers do not typically support
130+
passing absolute paths to libraries. Usually such paths need to be passed as input files without
131+
using any options like `-l`, e.g. `ld /my/absolute/path`. \
132+
`-Clink-arg=/my/absolute/path` can be used for doing this from stable `rustc`.
133+
107134
<a id="option-crate-type"></a>
108135
## `--crate-type`: a list of types of crates for the compiler to emit
109136

src/doc/unstable-book/src/language-features/native-link-modifiers-verbatim.md

-20
This file was deleted.

src/test/run-make/native-link-modifier-verbatim-linker/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ include ../../run-make-fulldeps/tools.mk
66
all:
77
# Verbatim allows specify precise name.
88
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_some_strange_name.ext
9-
$(RUSTC) main.rs -Zunstable-options -l static:+verbatim=local_some_strange_name.ext
9+
$(RUSTC) main.rs -l static:+verbatim=local_some_strange_name.ext
1010

1111
# With verbatim any other name cannot be used (local).
1212
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/liblocal_native_dep.a
1313
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep.a
1414
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep.lib
15-
$(RUSTC) main.rs -Zunstable-options -l static:+verbatim=local_native_dep 2>&1 | $(CGREP) "local_native_dep"
15+
$(RUSTC) main.rs -l static:+verbatim=local_native_dep 2>&1 | $(CGREP) "local_native_dep"

src/test/run-make/native-link-modifier-verbatim-rustc/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ include ../../run-make-fulldeps/tools.mk
33
all:
44
# Verbatim allows specify precise name.
55
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_some_strange_name.ext
6-
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_some_strange_name.ext --crate-type rlib
6+
$(RUSTC) rust_dep.rs -l static:+verbatim=upstream_some_strange_name.ext --crate-type rlib
77

88
# With verbatim any other name cannot be used (upstream).
99
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/libupstream_native_dep.a
1010
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep.a
1111
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep.lib
12-
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_native_dep --crate-type rlib 2>&1 | $(CGREP) "upstream_native_dep"
12+
$(RUSTC) rust_dep.rs -l static:+verbatim=upstream_native_dep --crate-type rlib 2>&1 | $(CGREP) "upstream_native_dep"

src/test/run-make/raw-dylib-c/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(raw_dylib, native_link_modifiers_verbatim)]
1+
#![feature(raw_dylib)]
22

33
#[link(name = "extern_1.dll", kind = "raw-dylib", modifiers = "+verbatim")]
44
extern {

src/test/run-make/rlib-format-packed-bundled-libs-2/rust_dep.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(native_link_modifiers_verbatim)]
21
#[link(name = "native_dep.ext", kind = "static", modifiers = "+verbatim")]
32
extern "C" {
43
fn native_f1() -> i32;

src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.rs

-5
This file was deleted.

src/test/ui/feature-gates/feature-gate-native_link_modifiers_verbatim.stderr

-12
This file was deleted.

src/test/ui/linkage-attr/link-attr-validation-late.rs

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

43
// Top-level ill-formed

0 commit comments

Comments
 (0)