@@ -787,6 +787,33 @@ impl<'a> Builder<'a> {
787787 }
788788 }
789789
790+ // If we're statically linking in an LLVM compiled for ThinLTO,
791+ // configure the linker accordingly.
792+ if Mode :: Rustc == mode && self . config . llvm_thin_lto && !self . config . llvm_link_shared {
793+ if target. contains ( "msvc" ) {
794+ // Here we assume the linker is lld-link.exe.
795+ rustflags. arg ( & format ! ( "-Clink-arg=/opt:lldtojobs={}" , self . jobs( ) ) ) ;
796+ } else {
797+ // Here we assume the linker is clang and that lld is available.
798+ // If not, there'll be linker errors.
799+ rustflags. arg ( "-Clink-arg=-fuse-ld=lld" ) ;
800+ rustflags. arg ( "-Clink-arg=-flto=thin" ) ;
801+
802+ // Copy the optimization flags LLVM uses for its Release and
803+ // RelWithDebugInfo builds.
804+ if self . config . llvm_optimize {
805+ if self . config . llvm_release_debuginfo {
806+ rustflags. arg ( "-Clink-arg=-O2" ) ;
807+ } else {
808+ rustflags. arg ( "-Clink-arg=-O3" ) ;
809+ }
810+ }
811+
812+ // Make LLD respect the `-j` option.
813+ rustflags. arg ( & format ! ( "-Clink-arg=-Wl,--thinlto-jobs={}" , self . jobs( ) ) ) ;
814+ }
815+ }
816+
790817 // This tells Cargo (and in turn, rustc) to output more complete
791818 // dependency information. Most importantly for rustbuild, this
792819 // includes sysroot artifacts, like libstd, which means that we don't
0 commit comments