Skip to content

Commit a883b23

Browse files
authoredMar 26, 2025
Rollup merge of #138867 - petrochenkov:linkfix, r=nnethercote
linker: Fix staticlib naming for UEFI And one minor refactoring in the second commit.
2 parents 277902b + 7c55782 commit a883b23

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed
 

‎compiler/rustc_codegen_ssa/src/back/link.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ fn link_natively(
670670
) {
671671
info!("preparing {:?} to {:?}", crate_type, out_filename);
672672
let (linker_path, flavor) = linker_and_flavor(sess);
673-
let self_contained_components = self_contained_components(sess, crate_type);
673+
let self_contained_components = self_contained_components(sess, crate_type, &linker_path);
674674

675675
// On AIX, we ship all libraries as .a big_af archive
676676
// the expected format is lib<name>.a(libname.so) for the actual
@@ -1494,7 +1494,8 @@ fn print_native_static_libs(
14941494
| NativeLibKind::Unspecified => {
14951495
let verbatim = lib.verbatim;
14961496
if sess.target.is_like_msvc {
1497-
Some(format!("{}{}", name, if verbatim { "" } else { ".lib" }))
1497+
let (prefix, suffix) = sess.staticlib_components(verbatim);
1498+
Some(format!("{prefix}{name}{suffix}"))
14981499
} else if sess.target.linker_flavor.is_gnu() {
14991500
Some(format!("-l{}{}", if verbatim { ":" } else { "" }, name))
15001501
} else {
@@ -1783,16 +1784,15 @@ fn link_output_kind(sess: &Session, crate_type: CrateType) -> LinkOutputKind {
17831784
}
17841785

17851786
// Returns true if linker is located within sysroot
1786-
fn detect_self_contained_mingw(sess: &Session) -> bool {
1787-
let (linker, _) = linker_and_flavor(sess);
1787+
fn detect_self_contained_mingw(sess: &Session, linker: &Path) -> bool {
17881788
// Assume `-C linker=rust-lld` as self-contained mode
17891789
if linker == Path::new("rust-lld") {
17901790
return true;
17911791
}
17921792
let linker_with_extension = if cfg!(windows) && linker.extension().is_none() {
17931793
linker.with_extension("exe")
17941794
} else {
1795-
linker
1795+
linker.to_path_buf()
17961796
};
17971797
for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) {
17981798
let full_path = dir.join(&linker_with_extension);
@@ -1807,7 +1807,11 @@ fn detect_self_contained_mingw(sess: &Session) -> bool {
18071807
/// Various toolchain components used during linking are used from rustc distribution
18081808
/// instead of being found somewhere on the host system.
18091809
/// We only provide such support for a very limited number of targets.
1810-
fn self_contained_components(sess: &Session, crate_type: CrateType) -> LinkSelfContainedComponents {
1810+
fn self_contained_components(
1811+
sess: &Session,
1812+
crate_type: CrateType,
1813+
linker: &Path,
1814+
) -> LinkSelfContainedComponents {
18111815
// Turn the backwards compatible bool values for `self_contained` into fully inferred
18121816
// `LinkSelfContainedComponents`.
18131817
let self_contained =
@@ -1836,7 +1840,7 @@ fn self_contained_components(sess: &Session, crate_type: CrateType) -> LinkSelfC
18361840
LinkSelfContainedDefault::InferredForMingw => {
18371841
sess.host == sess.target
18381842
&& sess.target.vendor != "uwp"
1839-
&& detect_self_contained_mingw(sess)
1843+
&& detect_self_contained_mingw(sess, linker)
18401844
}
18411845
}
18421846
};

‎compiler/rustc_codegen_ssa/src/back/linker.rs

+6-5
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);
@@ -958,9 +959,9 @@ impl<'a> Linker for MsvcLinker<'a> {
958959
if let Some(path) = try_find_native_static_library(self.sess, name, verbatim) {
959960
self.link_staticlib_by_path(&path, whole_archive);
960961
} else {
961-
let prefix = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
962-
let suffix = if verbatim { "" } else { ".lib" };
963-
self.link_arg(format!("{prefix}{name}{suffix}"));
962+
let opts = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
963+
let (prefix, suffix) = self.sess.staticlib_components(verbatim);
964+
self.link_arg(format!("{opts}{prefix}{name}{suffix}"));
964965
}
965966
}
966967

‎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)
Failed to load comments.