Skip to content

Commit 7c55782

Browse files
committed
rustc_session: Add a helper function for obtaining staticlib prefix and suffix
1 parent 27e95f9 commit 7c55782

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1494,11 +1494,7 @@ fn print_native_static_libs(
14941494
| NativeLibKind::Unspecified => {
14951495
let verbatim = lib.verbatim;
14961496
if sess.target.is_like_msvc {
1497-
let (prefix, suffix) = if verbatim {
1498-
("", "")
1499-
} else {
1500-
(&*sess.target.staticlib_prefix, &*sess.target.staticlib_suffix)
1501-
};
1497+
let (prefix, suffix) = sess.staticlib_components(verbatim);
15021498
Some(format!("{prefix}{name}{suffix}"))
15031499
} else if sess.target.linker_flavor.is_gnu() {
15041500
Some(format!("-l{}{}", if verbatim { ":" } else { "" }, name))

compiler/rustc_codegen_ssa/src/back/linker.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,10 @@ impl<'a> GccLinker<'a> {
450450
// The output filename already contains `dll_suffix` so
451451
// the resulting import library will have a name in the
452452
// form of libfoo.dll.a
453-
let mut implib_name = OsString::from(&*self.sess.target.staticlib_prefix);
453+
let (prefix, suffix) = self.sess.staticlib_components(false);
454+
let mut implib_name = OsString::from(prefix);
454455
implib_name.push(name);
455-
implib_name.push(&*self.sess.target.staticlib_suffix);
456+
implib_name.push(suffix);
456457
let mut out_implib = OsString::from("--out-implib=");
457458
out_implib.push(out_filename.with_file_name(implib_name));
458459
self.link_arg(out_implib);
@@ -959,11 +960,7 @@ impl<'a> Linker for MsvcLinker<'a> {
959960
self.link_staticlib_by_path(&path, whole_archive);
960961
} else {
961962
let opts = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
962-
let (prefix, suffix) = if verbatim {
963-
("", "")
964-
} else {
965-
(&*self.sess.target.staticlib_prefix, &*self.sess.target.staticlib_suffix)
966-
};
963+
let (prefix, suffix) = self.sess.staticlib_components(verbatim);
967964
self.link_arg(format!("{opts}{prefix}{name}{suffix}"));
968965
}
969966
}

compiler/rustc_metadata/src/native_libs.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ pub fn try_find_native_static_library(
9595
name: &str,
9696
verbatim: bool,
9797
) -> Option<PathBuf> {
98+
let default = sess.staticlib_components(verbatim);
9899
let formats = if verbatim {
99-
vec![("".into(), "".into())]
100+
vec![default]
100101
} else {
101-
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
102102
// On Windows, static libraries sometimes show up as libfoo.a and other
103103
// times show up as foo.lib
104-
let unix = ("lib".into(), ".a".into());
105-
if os == unix { vec![os] } else { vec![os, unix] }
104+
let unix = ("lib", ".a");
105+
if default == unix { vec![default] } else { vec![default, unix] }
106106
};
107107

108108
walk_native_lib_search_dirs(sess, None, |dir, is_framework| {
@@ -124,18 +124,17 @@ pub fn try_find_native_dynamic_library(
124124
name: &str,
125125
verbatim: bool,
126126
) -> Option<PathBuf> {
127+
let default = sess.staticlib_components(verbatim);
127128
let formats = if verbatim {
128-
vec![("".into(), "".into())]
129+
vec![default]
129130
} else {
130131
// While the official naming convention for MSVC import libraries
131-
// is foo.lib...
132-
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
133-
// ... Meson follows the libfoo.dll.a convention to
132+
// is foo.lib, Meson follows the libfoo.dll.a convention to
134133
// disambiguate .a for static libraries
135-
let meson = ("lib".into(), ".dll.a".into());
134+
let meson = ("lib", ".dll.a");
136135
// and MinGW uses .a altogether
137-
let mingw = ("lib".into(), ".a".into());
138-
vec![os, meson, mingw]
136+
let mingw = ("lib", ".a");
137+
vec![default, meson, mingw]
139138
};
140139

141140
walk_native_lib_search_dirs(sess, None, |dir, is_framework| {

compiler/rustc_session/src/output.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn filename_for_input(
103103
OutFileName::Real(outputs.out_directory.join(&format!("{prefix}{libname}{suffix}")))
104104
}
105105
CrateType::Staticlib => {
106-
let (prefix, suffix) = (&sess.target.staticlib_prefix, &sess.target.staticlib_suffix);
106+
let (prefix, suffix) = sess.staticlib_components(false);
107107
OutFileName::Real(outputs.out_directory.join(&format!("{prefix}{libname}{suffix}")))
108108
}
109109
CrateType::Executable => {

compiler/rustc_session/src/session.rs

+8
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,14 @@ impl Session {
586586
.or(self.target.options.default_visibility)
587587
.unwrap_or(SymbolVisibility::Interposable)
588588
}
589+
590+
pub fn staticlib_components(&self, verbatim: bool) -> (&str, &str) {
591+
if verbatim {
592+
("", "")
593+
} else {
594+
(&*self.target.staticlib_prefix, &*self.target.staticlib_suffix)
595+
}
596+
}
589597
}
590598

591599
// JUSTIFICATION: defn of the suggested wrapper fns

0 commit comments

Comments
 (0)