Skip to content

Commit

Permalink
Stabilize native library modifier verbatim
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Nov 27, 2022
1 parent 454784a commit 5b0e80e
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 95 deletions.
16 changes: 6 additions & 10 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,8 @@ fn link_rlib<'a>(
find_native_static_library(name.as_str(), lib.verbatim, &lib_search_paths, sess);
if sess.opts.unstable_opts.packed_bundled_libs && flavor == RlibFlavor::Normal {
let filename = lib.filename.unwrap();
let lib_path = find_native_static_library(
filename.as_str(),
Some(true),
&lib_search_paths,
sess,
);
let lib_path =
find_native_static_library(filename.as_str(), true, &lib_search_paths, sess);
let src = read(lib_path)
.map_err(|e| sess.emit_fatal(errors::ReadFileError { message: e }))?;
let (data, _) = create_wrapper_file(sess, b".bundled_lib".to_vec(), &src);
Expand Down Expand Up @@ -465,7 +461,7 @@ fn collate_raw_dylibs<'a, 'b>(

for lib in used_libraries {
if lib.kind == NativeLibKind::RawDylib {
let ext = if matches!(lib.verbatim, Some(true)) { "" } else { ".dll" };
let ext = if lib.verbatim { "" } else { ".dll" };
let name = format!("{}{}", lib.name.expect("unnamed raw-dylib library"), ext);
let imports = dylib_table.entry(name.clone()).or_default();
for import in &lib.dll_imports {
Expand Down Expand Up @@ -1335,7 +1331,7 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
NativeLibKind::Static { bundle: Some(false), .. }
| NativeLibKind::Dylib { .. }
| NativeLibKind::Unspecified => {
let verbatim = lib.verbatim.unwrap_or(false);
let verbatim = lib.verbatim;
if sess.target.is_like_msvc {
Some(format!("{}{}", name, if verbatim { "" } else { ".lib" }))
} else if sess.target.linker_flavor.is_gnu() {
Expand Down Expand Up @@ -2306,7 +2302,7 @@ fn add_native_libs_from_crate(
_ => &codegen_results.crate_info.native_libraries[&cnum],
};

let mut last = (None, NativeLibKind::Unspecified, None);
let mut last = (None, NativeLibKind::Unspecified, false);
for lib in native_libs {
let Some(name) = lib.name else {
continue;
Expand All @@ -2323,7 +2319,7 @@ fn add_native_libs_from_crate(
};

let name = name.as_str();
let verbatim = lib.verbatim.unwrap_or(false);
let verbatim = lib.verbatim;
match lib.kind {
NativeLibKind::Static { bundle, whole_archive } => {
if link_static {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ impl<'a> Linker for GccLinker<'a> {
// -force_load is the macOS equivalent of --whole-archive, but it
// involves passing the full path to the library to link.
self.linker_arg("-force_load");
let lib = find_native_static_library(lib, Some(verbatim), search_path, &self.sess);
let lib = find_native_static_library(lib, verbatim, search_path, &self.sess);
self.linker_arg(&lib);
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub struct NativeLib {
pub name: Option<Symbol>,
pub filename: Option<Symbol>,
pub cfg: Option<ast::MetaItem>,
pub verbatim: Option<bool>,
pub verbatim: bool,
pub dll_imports: Vec<cstore::DllImport>,
}

Expand All @@ -127,7 +127,7 @@ impl From<&cstore::NativeLib> for NativeLib {
filename: lib.filename,
name: lib.name,
cfg: lib.cfg.clone(),
verbatim: lib.verbatim,
verbatim: lib.verbatim.unwrap_or(false),
dll_imports: lib.dll_imports.clone(),
}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ declare_features! (
(accepted, native_link_modifiers, "1.61.0", Some(81490), None),
/// Allows specifying the bundle link modifier
(accepted, native_link_modifiers_bundle, "1.63.0", Some(81490), None),
/// Allows specifying the verbatim link modifier
(accepted, native_link_modifiers_verbatim, "CURRENT_RUSTC_VERSION", Some(81490), None),
/// Allows specifying the whole-archive link modifier
(accepted, native_link_modifiers_whole_archive, "1.61.0", Some(81490), None),
/// Allows using non lexical lifetimes (RFC 2094).
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,6 @@ declare_features! (
(active, naked_functions, "1.9.0", Some(32408), None),
/// Allows specifying the as-needed link modifier
(active, native_link_modifiers_as_needed, "1.53.0", Some(81490), None),
/// Allows specifying the verbatim link modifier
(active, native_link_modifiers_verbatim, "1.53.0", Some(81490), None),
/// Allow negative trait implementations.
(active, negative_impls, "1.44.0", Some(68318), None),
/// Allows the `!` type. Does not imply 'exhaustive_patterns' (below) any more.
Expand Down
13 changes: 5 additions & 8 deletions compiler/rustc_metadata/src/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ use std::path::PathBuf;

pub fn find_native_static_library(
name: &str,
verbatim: Option<bool>,
verbatim: bool,
search_paths: &[PathBuf],
sess: &Session,
) -> PathBuf {
let formats = if verbatim.unwrap_or(false) {
let formats = if verbatim {
vec![("".into(), "".into())]
} else {
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
Expand All @@ -52,7 +52,7 @@ pub fn find_native_static_library(
}
}

sess.emit_fatal(MissingNativeLibrary::new(name, verbatim.unwrap_or(false)));
sess.emit_fatal(MissingNativeLibrary::new(name, verbatim));
}

fn find_bundled_library(
Expand All @@ -66,7 +66,7 @@ fn find_bundled_library(
let NativeLibKind::Static { bundle: Some(true) | None, .. } = kind {
find_native_static_library(
name.unwrap().as_str(),
verbatim,
verbatim.unwrap_or(false),
&sess.target_filesearch(PathKind::Native).search_path_dirs(),
sess,
).file_name().and_then(|s| s.to_str()).map(Symbol::intern)
Expand Down Expand Up @@ -311,10 +311,7 @@ impl<'tcx> Collector<'tcx> {
sess.emit_err(BundleNeedsStatic { span });
}

("verbatim", _) => {
report_unstable_modifier!(native_link_modifiers_verbatim);
assign_modifier(&mut verbatim)
}
("verbatim", _) => assign_modifier(&mut verbatim),

("whole-archive", Some(NativeLibKind::Static { whole_archive, .. })) => {
assign_modifier(whole_archive)
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2029,10 +2029,7 @@ fn parse_native_lib_modifiers(
"linking modifier `bundle` is only compatible with `static` linking kind",
),

("verbatim", _) => {
report_unstable_modifier();
assign_modifier(&mut verbatim)
}
("verbatim", _) => assign_modifier(&mut verbatim),

("whole-archive", NativeLibKind::Static { whole_archive, .. }) => {
assign_modifier(whole_archive)
Expand Down
27 changes: 27 additions & 0 deletions src/doc/rustc/src/command-line-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ This modifier has no effect when building other targets like executables or dyna

The default for this modifier is `+bundle`.

### Linking modifiers: `verbatim`

This modifier is compatible with all linking kinds.

`+verbatim` means that rustc itself won't add any target-specified library prefixes or suffixes
(like `lib` or `.a`) to the library name, and will try its best to ask for the same thing from the
linker.

For `ld`-like linkers supporting GNU extensions rustc will use the `-l:filename` syntax (note the
colon) when passing the library, so the linker won't add any prefixes or suffixes to it.
See [`-l namespec`](https://sourceware.org/binutils/docs/ld/Options.html) in ld documentation for
more details. \
For linkers not supporting any verbatim modifiers (e.g. `link.exe` or `ld64`) the library name will
be passed as is. So the most reliable cross-platform use scenarios for this option are when no
linker is involved, for example bundling native libraries into rlibs.

`-verbatim` means that rustc will either add a target-specific prefix and suffix to the library
name before passing it to linker, or won't prevent linker from implicitly adding it. \
In case of `raw-dylib` kind in particular `.dll` will be added to the library name on Windows.

The default for this modifier is `-verbatim`.

NOTE: Even with `+verbatim` and `-l:filename` syntax `ld`-like linkers do not typically support
passing absolute paths to libraries. Usually such paths need to be passed as input files without
using any options like `-l`, e.g. `ld /my/absolute/path`. \
`-Clink-arg=/my/absolute/path` can be used for doing this from stable `rustc`.

<a id="option-crate-type"></a>
## `--crate-type`: a list of types of crates for the compiler to emit

Expand Down

This file was deleted.

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

# With verbatim any other name cannot be used (local).
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/liblocal_native_dep.a
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep.a
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep.lib
$(RUSTC) main.rs -Zunstable-options -l static:+verbatim=local_native_dep 2>&1 | $(CGREP) "local_native_dep"
$(RUSTC) main.rs -l static:+verbatim=local_native_dep 2>&1 | $(CGREP) "local_native_dep"
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ include ../../run-make-fulldeps/tools.mk
all:
# Verbatim allows specify precise name.
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_some_strange_name.ext
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_some_strange_name.ext --crate-type rlib
$(RUSTC) rust_dep.rs -l static:+verbatim=upstream_some_strange_name.ext --crate-type rlib

# With verbatim any other name cannot be used (upstream).
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/libupstream_native_dep.a
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep.a
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep.lib
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_native_dep --crate-type rlib 2>&1 | $(CGREP) "upstream_native_dep"
$(RUSTC) rust_dep.rs -l static:+verbatim=upstream_native_dep --crate-type rlib 2>&1 | $(CGREP) "upstream_native_dep"
2 changes: 1 addition & 1 deletion src/test/run-make/raw-dylib-c/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(raw_dylib, native_link_modifiers_verbatim)]
#![feature(raw_dylib)]

#[link(name = "extern_1.dll", kind = "raw-dylib", modifiers = "+verbatim")]
extern {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(native_link_modifiers_verbatim)]
#[link(name = "native_dep.ext", kind = "static", modifiers = "+verbatim")]
extern "C" {
fn native_f1() -> i32;
Expand Down

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion src/test/ui/linkage-attr/link-attr-validation-late.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(native_link_modifiers_verbatim)]
#![feature(link_cfg)]

// Top-level ill-formed
Expand Down
Loading

0 comments on commit 5b0e80e

Please sign in to comment.