Skip to content

Commit 430b82e

Browse files
Auto merge of #149178 - mati865:refactor-mingw-sc-detect, r=<try>
refactor mingw-w64 self-contained detection try-job: dist-x86_64-windows-gnu
2 parents e22dab3 + dd72465 commit 430b82e

File tree

1 file changed

+16
-15
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+16
-15
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ fn link_natively(
686686
) {
687687
info!("preparing {:?} to {:?}", crate_type, out_filename);
688688
let (linker_path, flavor) = linker_and_flavor(sess);
689-
let self_contained_components = self_contained_components(sess, crate_type, &linker_path);
689+
let self_contained_components = self_contained_components(sess, crate_type);
690690

691691
// On AIX, we ship all libraries as .a big_af archive
692692
// the expected format is lib<name>.a(libname.so) for the actual
@@ -1761,20 +1761,25 @@ fn link_output_kind(sess: &Session, crate_type: CrateType) -> LinkOutputKind {
17611761
}
17621762
}
17631763

1764-
// Returns true if linker is located within sysroot
1765-
fn detect_self_contained_mingw(sess: &Session, linker: &Path) -> bool {
1766-
// Assume `-C linker=rust-lld` as self-contained mode
1767-
if linker == Path::new("rust-lld") {
1768-
return true;
1764+
/// Returns true if linker is shipped by Rust.
1765+
// There are currently two different solutions for self-contained mode with mingw-w64:
1766+
// using rust-lld directly (`-gnullvm` and `-gnu`) or using shipped cc to call the linker (`-gnu`).
1767+
// Eventually, `-gnu` toolchains might be moved to calling the linker directly.
1768+
fn detect_self_contained_mingw(sess: &Session) -> bool {
1769+
// Passing explicit linker other than `rust-lld` means non-self-contained mode.
1770+
if let Some(linker) = &sess.opts.cg.linker {
1771+
return linker == Path::new("rust-lld");
17691772
}
1770-
let linker_with_extension = if cfg!(windows) && linker.extension().is_none() {
1773+
1774+
// If no explicit linker was given, proceed with implicit one (if present).
1775+
let linker_with_extension = if let Some(linker) = sess.target.linker.as_deref().map(Path::new) {
17711776
linker.with_extension("exe")
17721777
} else {
1773-
linker.to_path_buf()
1778+
return false;
17741779
};
17751780
for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) {
17761781
let full_path = dir.join(&linker_with_extension);
1777-
// If linker comes from sysroot assume self-contained mode
1782+
// If found linker doesn't come from sysroot assume non-self-contained mode.
17781783
if full_path.is_file() && !full_path.starts_with(sess.opts.sysroot.path()) {
17791784
return false;
17801785
}
@@ -1785,11 +1790,7 @@ fn detect_self_contained_mingw(sess: &Session, linker: &Path) -> bool {
17851790
/// Various toolchain components used during linking are used from rustc distribution
17861791
/// instead of being found somewhere on the host system.
17871792
/// We only provide such support for a very limited number of targets.
1788-
fn self_contained_components(
1789-
sess: &Session,
1790-
crate_type: CrateType,
1791-
linker: &Path,
1792-
) -> LinkSelfContainedComponents {
1793+
fn self_contained_components(sess: &Session, crate_type: CrateType) -> LinkSelfContainedComponents {
17931794
// Turn the backwards compatible bool values for `self_contained` into fully inferred
17941795
// `LinkSelfContainedComponents`.
17951796
let self_contained =
@@ -1818,7 +1819,7 @@ fn self_contained_components(
18181819
LinkSelfContainedDefault::InferredForMingw => {
18191820
sess.host == sess.target
18201821
&& sess.target.abi != Abi::Uwp
1821-
&& detect_self_contained_mingw(sess, linker)
1822+
&& detect_self_contained_mingw(sess)
18221823
}
18231824
}
18241825
};

0 commit comments

Comments
 (0)