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.
This commit is contained in:
Jakub Beránek 2025-08-01 17:39:49 +02:00
parent b6fe04defc
commit 929b3bb4c3
No known key found for this signature in database
GPG key ID: 909CD0D26483516B
5 changed files with 85 additions and 52 deletions

View file

@ -1624,7 +1624,7 @@ impl Step for GccCodegenBackend {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CraneliftCodegenBackend {
compilers: RustcPrivateCompilers,
pub compilers: RustcPrivateCompilers,
}
impl Step for CraneliftCodegenBackend {

View file

@ -1389,38 +1389,39 @@ impl Step for Miri {
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct CodegenBackend {
pub compiler: Compiler,
pub backend: CodegenBackendKind,
pub struct CraneliftCodegenBackend {
pub build_compiler: Compiler,
}
impl Step for CodegenBackend {
impl Step for CraneliftCodegenBackend {
type Output = Option<GeneratedTarball>;
const DEFAULT: bool = true;
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.path("compiler/rustc_codegen_cranelift")
// We only want to build the cranelift backend in `x dist` if the backend was enabled
// in rust.codegen-backends.
// Sadly, we don't have access to the actual for which we're disting clif here..
// So we just use the host target.
let clif_enabled_by_default = run
.builder
.config
.codegen_backends(run.builder.host_target)
.contains(&CodegenBackendKind::Cranelift);
run.alias("rustc_codegen_cranelift").default_condition(clif_enabled_by_default)
}
fn make_run(run: RunConfig<'_>) {
for backend in run.builder.config.codegen_backends(run.target) {
if backend.is_llvm() {
continue; // Already built as part of rustc
}
run.builder.ensure(CodegenBackend {
compiler: run.builder.compiler(run.builder.top_stage, run.target),
backend: backend.clone(),
});
}
run.builder.ensure(CraneliftCodegenBackend {
build_compiler: run.builder.compiler_for(
run.builder.top_stage,
run.builder.config.host_target,
run.target,
),
});
}
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
if builder.config.dry_run() {
return None;
}
// This prevents rustc_codegen_cranelift from being built for "dist"
// or "install" on the stable/beta channels. It is not yet stable and
// should not be included.
@ -1428,46 +1429,39 @@ impl Step for CodegenBackend {
return None;
}
if !builder.config.codegen_backends(self.compiler.host).contains(&self.backend) {
return None;
}
if self.backend.is_cranelift() && !target_supports_cranelift_backend(self.compiler.host) {
let target = self.build_compiler.host;
let compilers =
RustcPrivateCompilers::from_build_compiler(builder, self.build_compiler, target);
if !target_supports_cranelift_backend(target) {
builder.info("target not supported by rustc_codegen_cranelift. skipping");
return None;
}
let compiler = self.compiler;
let backend = self.backend;
let mut tarball = Tarball::new(
builder,
&format!("rustc-codegen-{}", backend.name()),
&compiler.host.triple,
);
if backend.is_cranelift() {
tarball.set_overlay(OverlayKind::RustcCodegenCranelift);
} else {
panic!("Unknown codegen backend {}", backend.name());
}
let mut tarball = Tarball::new(builder, &"rustc-codegen-cranelift", &target.triple);
tarball.set_overlay(OverlayKind::RustcCodegenCranelift);
tarball.is_preview(true);
tarball.add_legal_and_readme_to(format!("share/doc/{}", backend.crate_name()));
tarball.add_legal_and_readme_to("share/doc/rustc_codegen_cranelift");
let src = builder.sysroot(compiler);
let backends_src = builder.sysroot_codegen_backends(compiler);
builder.ensure(compile::CraneliftCodegenBackend { compilers });
if builder.config.dry_run() {
return None;
}
let src = builder.sysroot(self.build_compiler);
let backends_src = builder.sysroot_codegen_backends(self.build_compiler);
let backends_rel = backends_src
.strip_prefix(src)
.unwrap()
.strip_prefix(builder.sysroot_libdir_relative(compiler))
.strip_prefix(builder.sysroot_libdir_relative(self.build_compiler))
.unwrap();
// Don't use custom libdir here because ^lib/ will be resolved again with installer
let backends_dst = PathBuf::from("lib").join(backends_rel);
let backend_name = backend.crate_name();
let mut found_backend = false;
for backend in fs::read_dir(&backends_src).unwrap() {
let file_name = backend.unwrap().file_name();
if file_name.to_str().unwrap().contains(&backend_name) {
if file_name.to_str().unwrap().contains("rustc_codegen_cranelift") {
tarball.add_file(
backends_src.join(file_name),
&backends_dst,
@ -1480,6 +1474,13 @@ impl Step for CodegenBackend {
Some(tarball.generate())
}
fn metadata(&self) -> Option<StepMetadata> {
Some(
StepMetadata::dist("rustc_codegen_cranelift", self.build_compiler.host)
.built_by(self.build_compiler),
)
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
@ -1590,9 +1591,8 @@ impl Step for Extended {
add_component!("clippy" => Clippy { build_compiler: compiler, target });
add_component!("miri" => Miri { build_compiler: compiler, target });
add_component!("analysis" => Analysis { compiler, target });
add_component!("rustc-codegen-cranelift" => CodegenBackend {
compiler: builder.compiler(stage, target),
backend: CodegenBackendKind::Cranelift,
add_component!("rustc-codegen-cranelift" => CraneliftCodegenBackend {
build_compiler: compiler,
});
add_component!("llvm-bitcode-linker" => LlvmBitcodeLinker {
build_compiler: compiler,

View file

@ -12,7 +12,7 @@ use crate::core::config::{Config, TargetSelection};
use crate::utils::exec::command;
use crate::utils::helpers::t;
use crate::utils::tarball::GeneratedTarball;
use crate::{CodegenBackendKind, Compiler, Kind};
use crate::{Compiler, Kind};
#[cfg(target_os = "illumos")]
const SHELL: &str = "bash";
@ -274,9 +274,8 @@ install!((self, builder, _config),
install_sh(builder, "rustc", self.compiler.stage, Some(self.target), &tarball);
};
RustcCodegenCranelift, alias = "rustc-codegen-cranelift", Self::should_build(_config), only_hosts: true, {
if let Some(tarball) = builder.ensure(dist::CodegenBackend {
compiler: self.compiler,
backend: CodegenBackendKind::Cranelift,
if let Some(tarball) = builder.ensure(dist::CraneliftCodegenBackend {
build_compiler: self.compiler,
}) {
install_sh(builder, "rustc-codegen-cranelift", self.compiler.stage, Some(self.target), &tarball);
} else {

View file

@ -1151,7 +1151,7 @@ impl<'a> Builder<'a> {
dist::JsonDocs,
dist::Mingw,
dist::Rustc,
dist::CodegenBackend,
dist::CraneliftCodegenBackend,
dist::Std,
dist::RustcDev,
dist::Analysis,

View file

@ -1272,6 +1272,40 @@ mod snapshot {
");
}
// Enable dist cranelift tarball by default with `x dist` if cranelift is enabled in
// `rust.codegen-backends`.
#[test]
fn dist_cranelift_by_default() {
let ctx = TestCtx::new();
insta::assert_snapshot!(
ctx
.config("dist")
.args(&["--set", "rust.codegen-backends=['llvm', 'cranelift']"])
.render_steps(), @r"
[build] rustc 0 <host> -> UnstableBookGen 1 <host>
[build] rustc 0 <host> -> Rustbook 1 <host>
[build] llvm <host>
[build] rustc 0 <host> -> rustc 1 <host>
[build] rustc 0 <host> -> rustc_codegen_cranelift 1 <host>
[build] rustc 1 <host> -> std 1 <host>
[build] rustc 1 <host> -> rustc 2 <host>
[build] rustc 1 <host> -> rustc_codegen_cranelift 2 <host>
[build] rustdoc 2 <host>
[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]
[build] rustc 2 <host> -> std 2 <host>
[build] rustc 0 <host> -> LintDocs 1 <host>
[build] rustc 0 <host> -> RustInstaller 1 <host>
[dist] docs <host>
[doc] std 2 <host> crates=[]
[dist] mingw <host>
[build] rustc 0 <host> -> GenerateCopyright 1 <host>
[dist] rustc <host>
[dist] rustc 1 <host> -> rustc_codegen_cranelift 2 <host>
[dist] rustc 1 <host> -> std 1 <host>
[dist] src <>
");
}
#[test]
fn check_compiler_no_explicit_stage() {
let ctx = TestCtx::new();