1
Fork 0

rustc_session: Add a helper function for obtaining staticlib prefix and suffix

This commit is contained in:
Vadim Petrochenkov 2025-03-25 00:00:10 +03:00
parent 27e95f95e6
commit 7c55782e0c
5 changed files with 24 additions and 24 deletions

View file

@ -1494,11 +1494,7 @@ fn print_native_static_libs(
| NativeLibKind::Unspecified => { | NativeLibKind::Unspecified => {
let verbatim = lib.verbatim; let verbatim = lib.verbatim;
if sess.target.is_like_msvc { if sess.target.is_like_msvc {
let (prefix, suffix) = if verbatim { let (prefix, suffix) = sess.staticlib_components(verbatim);
("", "")
} else {
(&*sess.target.staticlib_prefix, &*sess.target.staticlib_suffix)
};
Some(format!("{prefix}{name}{suffix}")) Some(format!("{prefix}{name}{suffix}"))
} else if sess.target.linker_flavor.is_gnu() { } else if sess.target.linker_flavor.is_gnu() {
Some(format!("-l{}{}", if verbatim { ":" } else { "" }, name)) Some(format!("-l{}{}", if verbatim { ":" } else { "" }, name))

View file

@ -450,9 +450,10 @@ impl<'a> GccLinker<'a> {
// The output filename already contains `dll_suffix` so // The output filename already contains `dll_suffix` so
// the resulting import library will have a name in the // the resulting import library will have a name in the
// form of libfoo.dll.a // form of libfoo.dll.a
let mut implib_name = OsString::from(&*self.sess.target.staticlib_prefix); let (prefix, suffix) = self.sess.staticlib_components(false);
let mut implib_name = OsString::from(prefix);
implib_name.push(name); implib_name.push(name);
implib_name.push(&*self.sess.target.staticlib_suffix); implib_name.push(suffix);
let mut out_implib = OsString::from("--out-implib="); let mut out_implib = OsString::from("--out-implib=");
out_implib.push(out_filename.with_file_name(implib_name)); out_implib.push(out_filename.with_file_name(implib_name));
self.link_arg(out_implib); self.link_arg(out_implib);
@ -959,11 +960,7 @@ impl<'a> Linker for MsvcLinker<'a> {
self.link_staticlib_by_path(&path, whole_archive); self.link_staticlib_by_path(&path, whole_archive);
} else { } else {
let opts = if whole_archive { "/WHOLEARCHIVE:" } else { "" }; let opts = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
let (prefix, suffix) = if verbatim { let (prefix, suffix) = self.sess.staticlib_components(verbatim);
("", "")
} else {
(&*self.sess.target.staticlib_prefix, &*self.sess.target.staticlib_suffix)
};
self.link_arg(format!("{opts}{prefix}{name}{suffix}")); self.link_arg(format!("{opts}{prefix}{name}{suffix}"));
} }
} }

View file

@ -95,14 +95,14 @@ pub fn try_find_native_static_library(
name: &str, name: &str,
verbatim: bool, verbatim: bool,
) -> Option<PathBuf> { ) -> Option<PathBuf> {
let default = sess.staticlib_components(verbatim);
let formats = if verbatim { let formats = if verbatim {
vec![("".into(), "".into())] vec![default]
} else { } else {
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
// On Windows, static libraries sometimes show up as libfoo.a and other // On Windows, static libraries sometimes show up as libfoo.a and other
// times show up as foo.lib // times show up as foo.lib
let unix = ("lib".into(), ".a".into()); let unix = ("lib", ".a");
if os == unix { vec![os] } else { vec![os, unix] } if default == unix { vec![default] } else { vec![default, unix] }
}; };
walk_native_lib_search_dirs(sess, None, |dir, is_framework| { walk_native_lib_search_dirs(sess, None, |dir, is_framework| {
@ -124,18 +124,17 @@ pub fn try_find_native_dynamic_library(
name: &str, name: &str,
verbatim: bool, verbatim: bool,
) -> Option<PathBuf> { ) -> Option<PathBuf> {
let default = sess.staticlib_components(verbatim);
let formats = if verbatim { let formats = if verbatim {
vec![("".into(), "".into())] vec![default]
} else { } else {
// While the official naming convention for MSVC import libraries // While the official naming convention for MSVC import libraries
// is foo.lib... // is foo.lib, Meson follows the libfoo.dll.a convention to
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
// ... Meson follows the libfoo.dll.a convention to
// disambiguate .a for static libraries // disambiguate .a for static libraries
let meson = ("lib".into(), ".dll.a".into()); let meson = ("lib", ".dll.a");
// and MinGW uses .a altogether // and MinGW uses .a altogether
let mingw = ("lib".into(), ".a".into()); let mingw = ("lib", ".a");
vec![os, meson, mingw] vec![default, meson, mingw]
}; };
walk_native_lib_search_dirs(sess, None, |dir, is_framework| { walk_native_lib_search_dirs(sess, None, |dir, is_framework| {

View file

@ -103,7 +103,7 @@ pub fn filename_for_input(
OutFileName::Real(outputs.out_directory.join(&format!("{prefix}{libname}{suffix}"))) OutFileName::Real(outputs.out_directory.join(&format!("{prefix}{libname}{suffix}")))
} }
CrateType::Staticlib => { CrateType::Staticlib => {
let (prefix, suffix) = (&sess.target.staticlib_prefix, &sess.target.staticlib_suffix); let (prefix, suffix) = sess.staticlib_components(false);
OutFileName::Real(outputs.out_directory.join(&format!("{prefix}{libname}{suffix}"))) OutFileName::Real(outputs.out_directory.join(&format!("{prefix}{libname}{suffix}")))
} }
CrateType::Executable => { CrateType::Executable => {

View file

@ -586,6 +586,14 @@ impl Session {
.or(self.target.options.default_visibility) .or(self.target.options.default_visibility)
.unwrap_or(SymbolVisibility::Interposable) .unwrap_or(SymbolVisibility::Interposable)
} }
pub fn staticlib_components(&self, verbatim: bool) -> (&str, &str) {
if verbatim {
("", "")
} else {
(&*self.target.staticlib_prefix, &*self.target.staticlib_suffix)
}
}
} }
// JUSTIFICATION: defn of the suggested wrapper fns // JUSTIFICATION: defn of the suggested wrapper fns