Skip to content

Commit 929b3bb

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 b6fe04d commit 929b3bb

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
@@ -1624,7 +1624,7 @@ impl Step for GccCodegenBackend {
16241624

16251625
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
16261626
pub struct CraneliftCodegenBackend {
1627-
compilers: RustcPrivateCompilers,
1627+
pub compilers: RustcPrivateCompilers,
16281628
}
16291629

16301630
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
@@ -1389,85 +1389,79 @@ impl Step for Miri {
13891389
}
13901390

13911391
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1392-
pub struct CodegenBackend {
1393-
pub compiler: Compiler,
1394-
pub backend: CodegenBackendKind,
1392+
pub struct CraneliftCodegenBackend {
1393+
pub build_compiler: Compiler,
13951394
}
13961395

1397-
impl Step for CodegenBackend {
1396+
impl Step for CraneliftCodegenBackend {
13981397
type Output = Option<GeneratedTarball>;
13991398
const DEFAULT: bool = true;
14001399
const ONLY_HOSTS: bool = true;
14011400

14021401
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1403-
run.path("compiler/rustc_codegen_cranelift")
1402+
// We only want to build the cranelift backend in `x dist` if the backend was enabled
1403+
// in rust.codegen-backends.
1404+
// Sadly, we don't have access to the actual for which we're disting clif here..
1405+
// So we just use the host target.
1406+
let clif_enabled_by_default = run
1407+
.builder
1408+
.config
1409+
.codegen_backends(run.builder.host_target)
1410+
.contains(&CodegenBackendKind::Cranelift);
1411+
run.alias("rustc_codegen_cranelift").default_condition(clif_enabled_by_default)
14041412
}
14051413

14061414
fn make_run(run: RunConfig<'_>) {
1407-
for backend in run.builder.config.codegen_backends(run.target) {
1408-
if backend.is_llvm() {
1409-
continue; // Already built as part of rustc
1410-
}
1411-
1412-
run.builder.ensure(CodegenBackend {
1413-
compiler: run.builder.compiler(run.builder.top_stage, run.target),
1414-
backend: backend.clone(),
1415-
});
1416-
}
1415+
run.builder.ensure(CraneliftCodegenBackend {
1416+
build_compiler: run.builder.compiler_for(
1417+
run.builder.top_stage,
1418+
run.builder.config.host_target,
1419+
run.target,
1420+
),
1421+
});
14171422
}
14181423

14191424
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
1420-
if builder.config.dry_run() {
1421-
return None;
1422-
}
1423-
14241425
// This prevents rustc_codegen_cranelift from being built for "dist"
14251426
// or "install" on the stable/beta channels. It is not yet stable and
14261427
// should not be included.
14271428
if !builder.build.unstable_features() {
14281429
return None;
14291430
}
14301431

1431-
if !builder.config.codegen_backends(self.compiler.host).contains(&self.backend) {
1432-
return None;
1433-
}
1434-
1435-
if self.backend.is_cranelift() && !target_supports_cranelift_backend(self.compiler.host) {
1432+
let target = self.build_compiler.host;
1433+
let compilers =
1434+
RustcPrivateCompilers::from_build_compiler(builder, self.build_compiler, target);
1435+
if !target_supports_cranelift_backend(target) {
14361436
builder.info("target not supported by rustc_codegen_cranelift. skipping");
14371437
return None;
14381438
}
14391439

1440-
let compiler = self.compiler;
1441-
let backend = self.backend;
1440+
let mut tarball = Tarball::new(builder, &"rustc-codegen-cranelift", &target.triple);
1441+
tarball.set_overlay(OverlayKind::RustcCodegenCranelift);
1442+
tarball.is_preview(true);
1443+
tarball.add_legal_and_readme_to("share/doc/rustc_codegen_cranelift");
14421444

1443-
let mut tarball = Tarball::new(
1444-
builder,
1445-
&format!("rustc-codegen-{}", backend.name()),
1446-
&compiler.host.triple,
1447-
);
1448-
if backend.is_cranelift() {
1449-
tarball.set_overlay(OverlayKind::RustcCodegenCranelift);
1450-
} else {
1451-
panic!("Unknown codegen backend {}", backend.name());
1445+
builder.ensure(compile::CraneliftCodegenBackend { compilers });
1446+
1447+
if builder.config.dry_run() {
1448+
return None;
14521449
}
1453-
tarball.is_preview(true);
1454-
tarball.add_legal_and_readme_to(format!("share/doc/{}", backend.crate_name()));
14551450

1456-
let src = builder.sysroot(compiler);
1457-
let backends_src = builder.sysroot_codegen_backends(compiler);
1451+
let src = builder.sysroot(self.build_compiler);
1452+
let backends_src = builder.sysroot_codegen_backends(self.build_compiler);
14581453
let backends_rel = backends_src
14591454
.strip_prefix(src)
14601455
.unwrap()
1461-
.strip_prefix(builder.sysroot_libdir_relative(compiler))
1456+
.strip_prefix(builder.sysroot_libdir_relative(self.build_compiler))
14621457
.unwrap();
14631458
// Don't use custom libdir here because ^lib/ will be resolved again with installer
14641459
let backends_dst = PathBuf::from("lib").join(backends_rel);
14651460

1466-
let backend_name = backend.crate_name();
14671461
let mut found_backend = false;
14681462
for backend in fs::read_dir(&backends_src).unwrap() {
14691463
let file_name = backend.unwrap().file_name();
1470-
if file_name.to_str().unwrap().contains(&backend_name) {
1464+
if file_name.to_str().unwrap().contains("rustc_codegen_cranelift") {
14711465
tarball.add_file(
14721466
backends_src.join(file_name),
14731467
&backends_dst,
@@ -1480,6 +1474,13 @@ impl Step for CodegenBackend {
14801474

14811475
Some(tarball.generate())
14821476
}
1477+
1478+
fn metadata(&self) -> Option<StepMetadata> {
1479+
Some(
1480+
StepMetadata::dist("rustc_codegen_cranelift", self.build_compiler.host)
1481+
.built_by(self.build_compiler),
1482+
)
1483+
}
14831484
}
14841485

14851486
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
@@ -1590,9 +1591,8 @@ impl Step for Extended {
15901591
add_component!("clippy" => Clippy { build_compiler: compiler, target });
15911592
add_component!("miri" => Miri { build_compiler: compiler, target });
15921593
add_component!("analysis" => Analysis { compiler, target });
1593-
add_component!("rustc-codegen-cranelift" => CodegenBackend {
1594-
compiler: builder.compiler(stage, target),
1595-
backend: CodegenBackendKind::Cranelift,
1594+
add_component!("rustc-codegen-cranelift" => CraneliftCodegenBackend {
1595+
build_compiler: compiler,
15961596
});
15971597
add_component!("llvm-bitcode-linker" => LlvmBitcodeLinker {
15981598
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
@@ -1272,6 +1272,40 @@ mod snapshot {
12721272
");
12731273
}
12741274

1275+
// Enable dist cranelift tarball by default with `x dist` if cranelift is enabled in
1276+
// `rust.codegen-backends`.
1277+
#[test]
1278+
fn dist_cranelift_by_default() {
1279+
let ctx = TestCtx::new();
1280+
insta::assert_snapshot!(
1281+
ctx
1282+
.config("dist")
1283+
.args(&["--set", "rust.codegen-backends=['llvm', 'cranelift']"])
1284+
.render_steps(), @r"
1285+
[build] rustc 0 <host> -> UnstableBookGen 1 <host>
1286+
[build] rustc 0 <host> -> Rustbook 1 <host>
1287+
[build] llvm <host>
1288+
[build] rustc 0 <host> -> rustc 1 <host>
1289+
[build] rustc 0 <host> -> rustc_codegen_cranelift 1 <host>
1290+
[build] rustc 1 <host> -> std 1 <host>
1291+
[build] rustc 1 <host> -> rustc 2 <host>
1292+
[build] rustc 1 <host> -> rustc_codegen_cranelift 2 <host>
1293+
[build] rustdoc 2 <host>
1294+
[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]
1295+
[build] rustc 2 <host> -> std 2 <host>
1296+
[build] rustc 0 <host> -> LintDocs 1 <host>
1297+
[build] rustc 0 <host> -> RustInstaller 1 <host>
1298+
[dist] docs <host>
1299+
[doc] std 2 <host> crates=[]
1300+
[dist] mingw <host>
1301+
[build] rustc 0 <host> -> GenerateCopyright 1 <host>
1302+
[dist] rustc <host>
1303+
[dist] rustc 1 <host> -> rustc_codegen_cranelift 2 <host>
1304+
[dist] rustc 1 <host> -> std 1 <host>
1305+
[dist] src <>
1306+
");
1307+
}
1308+
12751309
#[test]
12761310
fn check_compiler_no_explicit_stage() {
12771311
let ctx = TestCtx::new();

0 commit comments

Comments
 (0)