linker: Better support alternative static library naming on MSVC
Previously `libname.a` naming was supported as a fallback when producing rlibs, but not when producing executables or dynamic libraries
This commit is contained in:
parent
a1c36c6ae9
commit
05bd36de50
@ -7,7 +7,7 @@
|
||||
|
||||
use cc::windows_registry;
|
||||
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
|
||||
use rustc_metadata::find_native_static_library;
|
||||
use rustc_metadata::{find_native_static_library, try_find_native_static_library};
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::middle::dependency_format::Linkage;
|
||||
use rustc_middle::middle::exported_symbols;
|
||||
@ -891,10 +891,16 @@ fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
|
||||
}
|
||||
|
||||
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
|
||||
// On MSVC-like targets rustc supports static libraries using alternative naming
|
||||
// scheme (`libfoo.a`) unsupported by linker, search for such libraries manually.
|
||||
if let Some(path) = try_find_native_static_library(self.sess, name, verbatim) {
|
||||
self.link_staticlib_by_path(&path, whole_archive);
|
||||
} else {
|
||||
let prefix = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
|
||||
let suffix = if verbatim { "" } else { ".lib" };
|
||||
self.link_arg(format!("{prefix}{name}{suffix}"));
|
||||
}
|
||||
}
|
||||
|
||||
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
|
||||
if !whole_archive {
|
||||
|
2
tests/run-make/native-lib-alt-naming/native.rs
Normal file
2
tests/run-make/native-lib-alt-naming/native.rs
Normal file
@ -0,0 +1,2 @@
|
||||
#[no_mangle]
|
||||
pub extern "C" fn native_lib_alt_naming() {}
|
15
tests/run-make/native-lib-alt-naming/rmake.rs
Normal file
15
tests/run-make/native-lib-alt-naming/rmake.rs
Normal file
@ -0,0 +1,15 @@
|
||||
// On MSVC the alternative naming format for static libraries (`libfoo.a`) is accepted in addition
|
||||
// to the default format (`foo.lib`).
|
||||
|
||||
//REMOVE@ only-msvc
|
||||
|
||||
use run_make_support::rustc;
|
||||
|
||||
fn main() {
|
||||
// Prepare the native library.
|
||||
rustc().input("native.rs").crate_type("staticlib").output("libnative.a").run();
|
||||
|
||||
// Try to link to it from both a rlib and a bin.
|
||||
rustc().input("rust.rs").crate_type("rlib").arg("-lstatic=native").run();
|
||||
rustc().input("rust.rs").crate_type("bin").arg("-lstatic=native").run();
|
||||
}
|
1
tests/run-make/native-lib-alt-naming/rust.rs
Normal file
1
tests/run-make/native-lib-alt-naming/rust.rs
Normal file
@ -0,0 +1 @@
|
||||
pub fn main() {}
|
Loading…
Reference in New Issue
Block a user