From cef5ebc5ffdbb65b85c4312335cb4fa80b9ac0c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Sun, 10 Aug 2025 11:10:31 +0200 Subject: [PATCH 1/3] Ship the correct Cranelift backend in its dist step --- src/bootstrap/src/core/build_steps/compile.rs | 32 +++++++++++------- src/bootstrap/src/core/build_steps/dist.rs | 33 ++++++++----------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs index 79174eb281f65..4519731ada7f3 100644 --- a/src/bootstrap/src/core/build_steps/compile.rs +++ b/src/bootstrap/src/core/build_steps/compile.rs @@ -1761,20 +1761,30 @@ fn copy_codegen_backends_to_sysroot( } if stamp.path().exists() { - let dylib = t!(fs::read_to_string(stamp.path())); - let file = Path::new(&dylib); - let filename = file.file_name().unwrap().to_str().unwrap(); - // change `librustc_codegen_cranelift-xxxxxx.so` to - // `librustc_codegen_cranelift-release.so` - let target_filename = { - let dash = filename.find('-').unwrap(); - let dot = filename.find('.').unwrap(); - format!("{}-{}{}", &filename[..dash], builder.rust_release(), &filename[dot..]) - }; - builder.copy_link(file, &dst.join(target_filename), FileType::NativeLibrary); + let file = get_codegen_backend_file(&stamp); + builder.copy_link( + &file, + &dst.join(normalize_codegen_backend_name(builder, &file)), + FileType::NativeLibrary, + ); } } +/// Gets the path to a dynamic codegen backend library from its build stamp. +pub fn get_codegen_backend_file(stamp: &BuildStamp) -> PathBuf { + PathBuf::from(t!(fs::read_to_string(stamp.path()))) +} + +/// Normalize the name of a dynamic codegen backend library. +pub fn normalize_codegen_backend_name(builder: &Builder<'_>, path: &Path) -> String { + let filename = path.file_name().unwrap().to_str().unwrap(); + // change e.g. `librustc_codegen_cranelift-xxxxxx.so` to + // `librustc_codegen_cranelift-release.so` + let dash = filename.find('-').unwrap(); + let dot = filename.find('.').unwrap(); + format!("{}-{}{}", &filename[..dash], builder.rust_release(), &filename[dot..]) +} + pub fn compiler_file( builder: &Builder<'_>, compiler: &Path, diff --git a/src/bootstrap/src/core/build_steps/dist.rs b/src/bootstrap/src/core/build_steps/dist.rs index 3fbc8cdbcd5ad..8f0de4c87d815 100644 --- a/src/bootstrap/src/core/build_steps/dist.rs +++ b/src/bootstrap/src/core/build_steps/dist.rs @@ -19,6 +19,7 @@ use object::read::archive::ArchiveFile; #[cfg(feature = "tracing")] use tracing::instrument; +use crate::core::build_steps::compile::{get_codegen_backend_file, normalize_codegen_backend_name}; use crate::core::build_steps::doc::DocumentationFormat; use crate::core::build_steps::tool::{self, RustcPrivateCompilers, Tool}; use crate::core::build_steps::vendor::{VENDOR_DIR, Vendor}; @@ -1442,35 +1443,29 @@ impl Step for CraneliftCodegenBackend { tarball.is_preview(true); tarball.add_legal_and_readme_to("share/doc/rustc_codegen_cranelift"); - builder.ensure(compile::CraneliftCodegenBackend { compilers }); + let stamp = 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) + // Get the relative path of where the codegen backend should be stored. + let backends_dst = builder.sysroot_codegen_backends(compilers.target_compiler()); + let backends_rel = backends_dst + .strip_prefix(builder.sysroot(compilers.target_compiler())) .unwrap() - .strip_prefix(builder.sysroot_libdir_relative(self.build_compiler)) + .strip_prefix(builder.sysroot_libdir_relative(compilers.target_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 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("rustc_codegen_cranelift") { - tarball.add_file( - backends_src.join(file_name), - &backends_dst, - FileType::NativeLibrary, - ); - found_backend = true; - } - } - assert!(found_backend); + let codegen_backend_dylib = get_codegen_backend_file(&stamp); + tarball.add_renamed_file( + &codegen_backend_dylib, + &backends_dst, + &normalize_codegen_backend_name(builder, &codegen_backend_dylib), + FileType::NativeLibrary, + ); Some(tarball.generate()) } From 01652d37ae86bae3d773ccc2de01993a45ef15be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Sun, 10 Aug 2025 13:55:55 +0200 Subject: [PATCH 2/3] Extract Cranelift component --- src/tools/opt-dist/src/tests.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/tools/opt-dist/src/tests.rs b/src/tools/opt-dist/src/tests.rs index d5121b8c7869a..fa55805bbf212 100644 --- a/src/tools/opt-dist/src/tests.rs +++ b/src/tools/opt-dist/src/tests.rs @@ -36,6 +36,16 @@ pub fn run_tests(env: &Environment) -> anyhow::Result<()> { let cargo_dir = extract_dist_dir(&format!("cargo-{version}-{host_triple}"))?.join("cargo"); let extracted_src_dir = extract_dist_dir(&format!("rust-src-{version}"))?.join("rust-src"); + // If we have a Cranelift archive, copy it to the rustc sysroot + if let Ok(_) = find_file_in_dir(&dist_dir, "rustc-codegen-cranelift-", ".tar.xz") { + let extracted_codegen_dir = + extract_dist_dir(&format!("rustc-codegen-cranelift-{version}-{host_triple}"))? + .join("rustc-codegen-cranelift-preview"); + let rel_path = + Utf8Path::new("lib").join("rustlib").join(host_triple).join("codegen-backends"); + copy_directory(&extracted_codegen_dir.join(&rel_path), &rustc_dir.join(&rel_path))?; + } + // We need to manually copy libstd to the extracted rustc sysroot copy_directory( &libstd_dir.join("lib").join("rustlib").join(host_triple).join("lib"), From 6893e691ea71b294f87a2c4a5c8f0a1f8168751d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Sun, 10 Aug 2025 20:46:30 +0200 Subject: [PATCH 3/3] Add a post-dist test for compiling a basic program with Cranelift --- .../dist/cranelift-x86_64-unknown-linux-gnu-dist.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/ui/dist/cranelift-x86_64-unknown-linux-gnu-dist.rs diff --git a/tests/ui/dist/cranelift-x86_64-unknown-linux-gnu-dist.rs b/tests/ui/dist/cranelift-x86_64-unknown-linux-gnu-dist.rs new file mode 100644 index 0000000000000..198f8d1bc10c1 --- /dev/null +++ b/tests/ui/dist/cranelift-x86_64-unknown-linux-gnu-dist.rs @@ -0,0 +1,11 @@ +// Ensure that Cranelift can be used to compile a simple program with `x86_64-unknown-linux-gnu` +// dist artifacts. + +//@ only-dist +//@ only-x86_64-unknown-linux-gnu +//@ compile-flags: -Z codegen-backend=cranelift +//@ run-pass + +fn main() { + println!("Hello world!"); +}