Skip to content

Commit 6540798

Browse files
committed
Refactor dist::CraneliftCodegenBackend
Make it clear that it only works for the Cranelift backend, add step metadata, add a test and change the default enablement logic for this step.
1 parent 4ce56f0 commit 6540798

File tree

5 files changed

+84
-51
lines changed

5 files changed

+84
-51
lines changed

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ impl Step for GccCodegenBackend {
16281628

16291629
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
16301630
pub struct CraneliftCodegenBackend {
1631-
compilers: RustcPrivateCompilers,
1631+
pub compilers: RustcPrivateCompilers,
16321632
}
16331633

16341634
impl Step for CraneliftCodegenBackend {

src/bootstrap/src/core/build_steps/dist.rs

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,85 +1367,79 @@ impl Step for Miri {
13671367
}
13681368

13691369
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1370-
pub struct CodegenBackend {
1371-
pub compiler: Compiler,
1372-
pub backend: CodegenBackendKind,
1370+
pub struct CraneliftCodegenBackend {
1371+
pub build_compiler: Compiler,
13731372
}
13741373

1375-
impl Step for CodegenBackend {
1374+
impl Step for CraneliftCodegenBackend {
13761375
type Output = Option<GeneratedTarball>;
13771376
const DEFAULT: bool = true;
13781377
const ONLY_HOSTS: bool = true;
13791378

13801379
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1381-
run.path("compiler/rustc_codegen_cranelift")
1380+
// We only want to build the cranelift backend in `x dist` if the backend was enabled
1381+
// in rust.codegen-backends.
1382+
// Sadly, we don't have access to the actual for which we're disting clif here..
1383+
// So we just use the host target.
1384+
let clif_enabled_by_default = run
1385+
.builder
1386+
.config
1387+
.codegen_backends(run.builder.host_target)
1388+
.contains(&CodegenBackendKind::Cranelift);
1389+
run.alias("rustc_codegen_cranelift").default_condition(clif_enabled_by_default)
13821390
}
13831391

13841392
fn make_run(run: RunConfig<'_>) {
1385-
for backend in run.builder.config.codegen_backends(run.target) {
1386-
if backend.is_llvm() {
1387-
continue; // Already built as part of rustc
1388-
}
1389-
1390-
run.builder.ensure(CodegenBackend {
1391-
compiler: run.builder.compiler(run.builder.top_stage, run.target),
1392-
backend: backend.clone(),
1393-
});
1394-
}
1393+
run.builder.ensure(CraneliftCodegenBackend {
1394+
build_compiler: run.builder.compiler_for(
1395+
run.builder.top_stage,
1396+
run.builder.config.host_target,
1397+
run.target,
1398+
),
1399+
});
13951400
}
13961401

13971402
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
1398-
if builder.config.dry_run() {
1399-
return None;
1400-
}
1401-
14021403
// This prevents rustc_codegen_cranelift from being built for "dist"
14031404
// or "install" on the stable/beta channels. It is not yet stable and
14041405
// should not be included.
14051406
if !builder.build.unstable_features() {
14061407
return None;
14071408
}
14081409

1409-
if !builder.config.codegen_backends(self.compiler.host).contains(&self.backend) {
1410-
return None;
1411-
}
1412-
1413-
if self.backend.is_cranelift() && !target_supports_cranelift_backend(self.compiler.host) {
1410+
let target = self.build_compiler.host;
1411+
let compilers =
1412+
RustcPrivateCompilers::from_build_compiler(builder, self.build_compiler, target);
1413+
if !target_supports_cranelift_backend(target) {
14141414
builder.info("target not supported by rustc_codegen_cranelift. skipping");
14151415
return None;
14161416
}
14171417

1418-
let compiler = self.compiler;
1419-
let backend = self.backend;
1418+
let mut tarball = Tarball::new(builder, &"rustc-codegen-cranelift", &target.triple);
1419+
tarball.set_overlay(OverlayKind::RustcCodegenCranelift);
1420+
tarball.is_preview(true);
1421+
tarball.add_legal_and_readme_to("share/doc/rustc_codegen_cranelift");
14201422

1421-
let mut tarball = Tarball::new(
1422-
builder,
1423-
&format!("rustc-codegen-{}", backend.name()),
1424-
&compiler.host.triple,
1425-
);
1426-
if backend.is_cranelift() {
1427-
tarball.set_overlay(OverlayKind::RustcCodegenCranelift);
1428-
} else {
1429-
panic!("Unknown codegen backend {}", backend.name());
1423+
builder.ensure(compile::CraneliftCodegenBackend { compilers });
1424+
1425+
if builder.config.dry_run() {
1426+
return None;
14301427
}
1431-
tarball.is_preview(true);
1432-
tarball.add_legal_and_readme_to(format!("share/doc/{}", backend.crate_name()));
14331428

1434-
let src = builder.sysroot(compiler);
1435-
let backends_src = builder.sysroot_codegen_backends(compiler);
1429+
let src = builder.sysroot(self.build_compiler);
1430+
let backends_src = builder.sysroot_codegen_backends(self.build_compiler);
14361431
let backends_rel = backends_src
14371432
.strip_prefix(src)
14381433
.unwrap()
1439-
.strip_prefix(builder.sysroot_libdir_relative(compiler))
1434+
.strip_prefix(builder.sysroot_libdir_relative(self.build_compiler))
14401435
.unwrap();
14411436
// Don't use custom libdir here because ^lib/ will be resolved again with installer
14421437
let backends_dst = PathBuf::from("lib").join(backends_rel);
14431438

1444-
let backend_name = backend.crate_name();
14451439
let mut found_backend = false;
14461440
for backend in fs::read_dir(&backends_src).unwrap() {
14471441
let file_name = backend.unwrap().file_name();
1448-
if file_name.to_str().unwrap().contains(&backend_name) {
1442+
if file_name.to_str().unwrap().contains("rustc_codegen_cranelift") {
14491443
tarball.add_file(
14501444
backends_src.join(file_name),
14511445
&backends_dst,
@@ -1458,6 +1452,13 @@ impl Step for CodegenBackend {
14581452

14591453
Some(tarball.generate())
14601454
}
1455+
1456+
fn metadata(&self) -> Option<StepMetadata> {
1457+
Some(
1458+
StepMetadata::dist("rustc_codegen_cranelift", self.build_compiler.host)
1459+
.built_by(self.build_compiler),
1460+
)
1461+
}
14611462
}
14621463

14631464
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
@@ -1568,9 +1569,8 @@ impl Step for Extended {
15681569
add_component!("clippy" => Clippy { build_compiler: compiler, target });
15691570
add_component!("miri" => Miri { build_compiler: compiler, target });
15701571
add_component!("analysis" => Analysis { compiler, target });
1571-
add_component!("rustc-codegen-cranelift" => CodegenBackend {
1572-
compiler: builder.compiler(stage, target),
1573-
backend: CodegenBackendKind::Cranelift,
1572+
add_component!("rustc-codegen-cranelift" => CraneliftCodegenBackend {
1573+
build_compiler: compiler,
15741574
});
15751575
add_component!("llvm-bitcode-linker" => LlvmBitcodeLinker {
15761576
build_compiler: compiler,

src/bootstrap/src/core/build_steps/install.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::core::config::{Config, TargetSelection};
1212
use crate::utils::exec::command;
1313
use crate::utils::helpers::t;
1414
use crate::utils::tarball::GeneratedTarball;
15-
use crate::{CodegenBackendKind, Compiler, Kind};
15+
use crate::{Compiler, Kind};
1616

1717
#[cfg(target_os = "illumos")]
1818
const SHELL: &str = "bash";
@@ -274,9 +274,8 @@ install!((self, builder, _config),
274274
install_sh(builder, "rustc", self.compiler.stage, Some(self.target), &tarball);
275275
};
276276
RustcCodegenCranelift, alias = "rustc-codegen-cranelift", Self::should_build(_config), only_hosts: true, {
277-
if let Some(tarball) = builder.ensure(dist::CodegenBackend {
278-
compiler: self.compiler,
279-
backend: CodegenBackendKind::Cranelift,
277+
if let Some(tarball) = builder.ensure(dist::CraneliftCodegenBackend {
278+
build_compiler: self.compiler,
280279
}) {
281280
install_sh(builder, "rustc-codegen-cranelift", self.compiler.stage, Some(self.target), &tarball);
282281
} else {

src/bootstrap/src/core/builder/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ impl<'a> Builder<'a> {
11511151
dist::JsonDocs,
11521152
dist::Mingw,
11531153
dist::Rustc,
1154-
dist::CodegenBackend,
1154+
dist::CraneliftCodegenBackend,
11551155
dist::Std,
11561156
dist::RustcDev,
11571157
dist::Analysis,

src/bootstrap/src/core/builder/tests.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,40 @@ mod snapshot {
12901290
");
12911291
}
12921292

1293+
// Enable dist cranelift tarball by default with `x dist` if cranelift is enabled in
1294+
// `rust.codegen-backends`.
1295+
#[test]
1296+
fn dist_cranelift_by_default() {
1297+
let ctx = TestCtx::new();
1298+
insta::assert_snapshot!(
1299+
ctx
1300+
.config("dist")
1301+
.args(&["--set", "rust.codegen-backends=['llvm', 'cranelift']"])
1302+
.render_steps(), @r"
1303+
[build] rustc 0 <host> -> UnstableBookGen 1 <host>
1304+
[build] rustc 0 <host> -> Rustbook 1 <host>
1305+
[build] llvm <host>
1306+
[build] rustc 0 <host> -> rustc 1 <host>
1307+
[build] rustc 0 <host> -> rustc_codegen_cranelift 1 <host>
1308+
[build] rustc 1 <host> -> std 1 <host>
1309+
[build] rustc 1 <host> -> rustc 2 <host>
1310+
[build] rustc 1 <host> -> rustc_codegen_cranelift 2 <host>
1311+
[build] rustdoc 2 <host>
1312+
[doc] std 2 <host> crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind]
1313+
[build] rustc 2 <host> -> std 2 <host>
1314+
[build] rustc 0 <host> -> LintDocs 1 <host>
1315+
[build] rustc 0 <host> -> RustInstaller 1 <host>
1316+
[dist] docs <host>
1317+
[doc] std 2 <host> crates=[]
1318+
[dist] mingw <host>
1319+
[build] rustc 0 <host> -> GenerateCopyright 1 <host>
1320+
[dist] rustc <host>
1321+
[dist] rustc 1 <host> -> rustc_codegen_cranelift 2 <host>
1322+
[dist] rustc 1 <host> -> std 1 <host>
1323+
[dist] src <>
1324+
");
1325+
}
1326+
12931327
#[test]
12941328
fn check_compiler_no_explicit_stage() {
12951329
let ctx = TestCtx::new();

0 commit comments

Comments
 (0)