@@ -685,8 +685,9 @@ fn link_natively(
685685 codegen_backend : & ' static str ,
686686) {
687687 info ! ( "preparing {:?} to {:?}" , crate_type, out_filename) ;
688- let ( linker_path, flavor) = linker_and_flavor ( sess) ;
689- let self_contained_components = self_contained_components ( sess, crate_type, & linker_path) ;
688+ let self_contained_components = self_contained_components ( sess, crate_type) ;
689+ let ( linker_path, flavor) =
690+ linker_and_flavor ( sess, self_contained_components. is_linker_enabled ( ) ) ;
690691
691692 // On AIX, we ship all libraries as .a big_af archive
692693 // the expected format is lib<name>.a(libname.so) for the actual
@@ -1314,7 +1315,7 @@ pub fn ignored_for_lto(sess: &Session, info: &CrateInfo, cnum: CrateNum) -> bool
13141315}
13151316
13161317/// This functions tries to determine the appropriate linker (and corresponding LinkerFlavor) to use
1317- pub fn linker_and_flavor ( sess : & Session ) -> ( PathBuf , LinkerFlavor ) {
1318+ pub fn linker_and_flavor ( sess : & Session , self_contained : bool ) -> ( PathBuf , LinkerFlavor ) {
13181319 fn infer_from (
13191320 sess : & Session ,
13201321 linker : Option < PathBuf > ,
@@ -1413,6 +1414,15 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
14131414 return ret;
14141415 }
14151416
1417+ // FIXME: do it better
1418+ if sess. target . os == "windows"
1419+ && sess. target . env == "gnu"
1420+ && sess. target . abi == "llvm"
1421+ && self_contained
1422+ {
1423+ return ( PathBuf :: from ( "rust-lld.exe" ) , LinkerFlavor :: Gnu ( Cc :: No , Lld :: Yes ) ) ;
1424+ }
1425+
14161426 if let Some ( ret) = infer_from (
14171427 sess,
14181428 sess. target . linker . as_deref ( ) . map ( PathBuf :: from) ,
@@ -1760,7 +1770,21 @@ fn link_output_kind(sess: &Session, crate_type: CrateType) -> LinkOutputKind {
17601770}
17611771
17621772// Returns true if linker is located within sysroot
1763- fn detect_self_contained_mingw ( sess : & Session , linker : & Path ) -> bool {
1773+ fn detect_self_contained_mingw ( sess : & Session ) -> bool {
1774+ // FIXME: this sort of duplicates `infer_from()` inside `linker_and_flavor()`
1775+ let path_buf = sess
1776+ . opts
1777+ . cg
1778+ . linker
1779+ . as_ref ( )
1780+ . map ( |l| l. as_path ( ) )
1781+ . or_else ( || sess. target . linker . as_ref ( ) . map ( |linker| Path :: new ( linker. as_ref ( ) ) ) ) ;
1782+ let linker = if let Some ( linker) = path_buf {
1783+ linker
1784+ } else {
1785+ return false ;
1786+ } ;
1787+
17641788 // Assume `-C linker=rust-lld` as self-contained mode
17651789 if linker == Path :: new ( "rust-lld" ) {
17661790 return true ;
@@ -1772,7 +1796,7 @@ fn detect_self_contained_mingw(sess: &Session, linker: &Path) -> bool {
17721796 } ;
17731797 for dir in env:: split_paths ( & env:: var_os ( "PATH" ) . unwrap_or_default ( ) ) {
17741798 let full_path = dir. join ( & linker_with_extension) ;
1775- // If linker comes from sysroot assume self-contained mode
1799+ // If linker doesn't come from sysroot assume non- self-contained mode
17761800 if full_path. is_file ( ) && !full_path. starts_with ( sess. opts . sysroot . path ( ) ) {
17771801 return false ;
17781802 }
@@ -1783,11 +1807,7 @@ fn detect_self_contained_mingw(sess: &Session, linker: &Path) -> bool {
17831807/// Various toolchain components used during linking are used from rustc distribution
17841808/// instead of being found somewhere on the host system.
17851809/// We only provide such support for a very limited number of targets.
1786- fn self_contained_components (
1787- sess : & Session ,
1788- crate_type : CrateType ,
1789- linker : & Path ,
1790- ) -> LinkSelfContainedComponents {
1810+ fn self_contained_components ( sess : & Session , crate_type : CrateType ) -> LinkSelfContainedComponents {
17911811 // Turn the backwards compatible bool values for `self_contained` into fully inferred
17921812 // `LinkSelfContainedComponents`.
17931813 let self_contained =
@@ -1816,7 +1836,7 @@ fn self_contained_components(
18161836 LinkSelfContainedDefault :: InferredForMingw => {
18171837 sess. host == sess. target
18181838 && sess. target . vendor != "uwp"
1819- && detect_self_contained_mingw ( sess, linker )
1839+ && detect_self_contained_mingw ( sess)
18201840 }
18211841 }
18221842 } ;
0 commit comments