Ship the correct Cranelift backend in its dist step

This commit is contained in:
Jakub Beránek 2025-08-10 11:10:31 +02:00
parent 18eeac04fc
commit cef5ebc5ff
No known key found for this signature in database
GPG key ID: 909CD0D26483516B
2 changed files with 35 additions and 30 deletions

View file

@ -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,

View file

@ -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())
}