@@ -27,7 +27,7 @@ use crate::core::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
2727use crate :: core:: config:: TargetSelection ;
2828use crate :: utils:: cache:: { Interned , INTERNER } ;
2929use crate :: utils:: channel;
30- use crate :: utils:: helpers:: { exe, is_dylib, output, t, timeit} ;
30+ use crate :: utils:: helpers:: { exe, is_dylib, output, t, target_supports_cranelift_backend , timeit} ;
3131use crate :: utils:: tarball:: { GeneratedTarball , OverlayKind , Tarball } ;
3232use crate :: { Compiler , DependencyType , Mode , LLVM_TOOLS } ;
3333
@@ -443,19 +443,6 @@ impl Step for Rustc {
443443 }
444444 }
445445
446- // Copy over the codegen backends
447- let backends_src = builder. sysroot_codegen_backends ( compiler) ;
448- let backends_rel = backends_src
449- . strip_prefix ( & src)
450- . unwrap ( )
451- . strip_prefix ( builder. sysroot_libdir_relative ( compiler) )
452- . unwrap ( ) ;
453- // Don't use custom libdir here because ^lib/ will be resolved again with installer
454- let backends_dst = image. join ( "lib" ) . join ( & backends_rel) ;
455-
456- t ! ( fs:: create_dir_all( & backends_dst) ) ;
457- builder. cp_r ( & backends_src, & backends_dst) ;
458-
459446 // Copy libLLVM.so to the lib dir as well, if needed. While not
460447 // technically needed by rustc itself it's needed by lots of other
461448 // components like the llvm tools and LLD. LLD is included below and
@@ -1282,6 +1269,91 @@ impl Step for Miri {
12821269 }
12831270}
12841271
1272+ #[ derive( Debug , PartialOrd , Ord , Copy , Clone , Hash , PartialEq , Eq ) ]
1273+ pub struct CodegenBackend {
1274+ pub compiler : Compiler ,
1275+ pub backend : Interned < String > ,
1276+ }
1277+
1278+ impl Step for CodegenBackend {
1279+ type Output = Option < GeneratedTarball > ;
1280+ const DEFAULT : bool = true ;
1281+ const ONLY_HOSTS : bool = true ;
1282+
1283+ fn should_run ( run : ShouldRun < ' _ > ) -> ShouldRun < ' _ > {
1284+ run. path ( "compiler/rustc_codegen_cranelift" )
1285+ }
1286+
1287+ fn make_run ( run : RunConfig < ' _ > ) {
1288+ for & backend in & run. builder . config . rust_codegen_backends {
1289+ if backend == "llvm" {
1290+ continue ; // Already built as part of rustc
1291+ }
1292+
1293+ run. builder . ensure ( CodegenBackend {
1294+ compiler : run. builder . compiler ( run. builder . top_stage , run. target ) ,
1295+ backend,
1296+ } ) ;
1297+ }
1298+ }
1299+
1300+ fn run ( self , builder : & Builder < ' _ > ) -> Option < GeneratedTarball > {
1301+ // This prevents rustc_codegen_cranelift from being built for "dist"
1302+ // or "install" on the stable/beta channels. It is not yet stable and
1303+ // should not be included.
1304+ if !builder. build . unstable_features ( ) {
1305+ return None ;
1306+ }
1307+
1308+ if self . backend == "cranelift" {
1309+ if !target_supports_cranelift_backend ( self . compiler . host ) {
1310+ builder. info ( "target not supported by rustc_codegen_cranelift. skipping" ) ;
1311+ return None ;
1312+ }
1313+
1314+ if self . compiler . host . contains ( "windows" ) {
1315+ builder. info (
1316+ "dist currently disabled for windows by rustc_codegen_cranelift. skipping" ,
1317+ ) ;
1318+ return None ;
1319+ }
1320+ }
1321+
1322+ let compiler = self . compiler ;
1323+ let backend = self . backend ;
1324+
1325+ let mut tarball =
1326+ Tarball :: new ( builder, & format ! ( "rustc-codegen-{}" , backend) , & compiler. host . triple ) ;
1327+ if backend == "cranelift" {
1328+ tarball. set_overlay ( OverlayKind :: RustcCodegenCranelift ) ;
1329+ } else {
1330+ panic ! ( "Unknown backend rustc_codegen_{}" , backend) ;
1331+ }
1332+ tarball. is_preview ( true ) ;
1333+ tarball. add_legal_and_readme_to ( format ! ( "share/doc/rustc_codegen_{}" , backend) ) ;
1334+
1335+ let src = builder. sysroot ( compiler) ;
1336+ let backends_src = builder. sysroot_codegen_backends ( compiler) ;
1337+ let backends_rel = backends_src
1338+ . strip_prefix ( & src)
1339+ . unwrap ( )
1340+ . strip_prefix ( builder. sysroot_libdir_relative ( compiler) )
1341+ . unwrap ( ) ;
1342+ // Don't use custom libdir here because ^lib/ will be resolved again with installer
1343+ let backends_dst = PathBuf :: from ( "lib" ) . join ( & backends_rel) ;
1344+
1345+ let backend_name = format ! ( "rustc_codegen_{}" , backend) ;
1346+ for backend in fs:: read_dir ( & backends_src) . unwrap ( ) {
1347+ let file_name = backend. unwrap ( ) . file_name ( ) ;
1348+ if file_name. to_str ( ) . unwrap ( ) . contains ( & backend_name) {
1349+ tarball. add_file ( backends_src. join ( file_name) , & backends_dst, 0o644 ) ;
1350+ }
1351+ }
1352+
1353+ Some ( tarball. generate ( ) )
1354+ }
1355+ }
1356+
12851357#[ derive( Debug , PartialOrd , Ord , Copy , Clone , Hash , PartialEq , Eq ) ]
12861358pub struct Rustfmt {
12871359 pub compiler : Compiler ,
@@ -1452,6 +1524,10 @@ impl Step for Extended {
14521524 add_component ! ( "clippy" => Clippy { compiler, target } ) ;
14531525 add_component ! ( "miri" => Miri { compiler, target } ) ;
14541526 add_component ! ( "analysis" => Analysis { compiler, target } ) ;
1527+ add_component ! ( "rustc-codegen-cranelift" => CodegenBackend {
1528+ compiler: builder. compiler( stage, target) ,
1529+ backend: INTERNER . intern_str( "cranelift" ) ,
1530+ } ) ;
14551531
14561532 let etc = builder. src . join ( "src/etc/installer" ) ;
14571533
@@ -1548,6 +1624,7 @@ impl Step for Extended {
15481624 prepare ( tool) ;
15491625 }
15501626 }
1627+ prepare ( "rustc-codegen-cranelift" ) ;
15511628 // create an 'uninstall' package
15521629 builder. install ( & etc. join ( "pkg/postinstall" ) , & pkg. join ( "uninstall" ) , 0o755 ) ;
15531630 pkgbuild ( "uninstall" ) ;
@@ -1587,6 +1664,10 @@ impl Step for Extended {
15871664 "rust-demangler-preview" . to_string ( )
15881665 } else if name == "miri" {
15891666 "miri-preview" . to_string ( )
1667+ } else if name == "rustc-codegen-cranelift" {
1668+ // FIXME add installer support for cg_clif once it is ready to be distributed on
1669+ // windows.
1670+ unreachable ! ( "cg_clif shouldn't be built for windows" ) ;
15901671 } else {
15911672 name. to_string ( )
15921673 } ;
0 commit comments