Skip to content

Commit cef5ebc

Browse files
committed
Ship the correct Cranelift backend in its dist step
1 parent 18eeac0 commit cef5ebc

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,20 +1761,30 @@ fn copy_codegen_backends_to_sysroot(
17611761
}
17621762

17631763
if stamp.path().exists() {
1764-
let dylib = t!(fs::read_to_string(stamp.path()));
1765-
let file = Path::new(&dylib);
1766-
let filename = file.file_name().unwrap().to_str().unwrap();
1767-
// change `librustc_codegen_cranelift-xxxxxx.so` to
1768-
// `librustc_codegen_cranelift-release.so`
1769-
let target_filename = {
1770-
let dash = filename.find('-').unwrap();
1771-
let dot = filename.find('.').unwrap();
1772-
format!("{}-{}{}", &filename[..dash], builder.rust_release(), &filename[dot..])
1773-
};
1774-
builder.copy_link(file, &dst.join(target_filename), FileType::NativeLibrary);
1764+
let file = get_codegen_backend_file(&stamp);
1765+
builder.copy_link(
1766+
&file,
1767+
&dst.join(normalize_codegen_backend_name(builder, &file)),
1768+
FileType::NativeLibrary,
1769+
);
17751770
}
17761771
}
17771772

1773+
/// Gets the path to a dynamic codegen backend library from its build stamp.
1774+
pub fn get_codegen_backend_file(stamp: &BuildStamp) -> PathBuf {
1775+
PathBuf::from(t!(fs::read_to_string(stamp.path())))
1776+
}
1777+
1778+
/// Normalize the name of a dynamic codegen backend library.
1779+
pub fn normalize_codegen_backend_name(builder: &Builder<'_>, path: &Path) -> String {
1780+
let filename = path.file_name().unwrap().to_str().unwrap();
1781+
// change e.g. `librustc_codegen_cranelift-xxxxxx.so` to
1782+
// `librustc_codegen_cranelift-release.so`
1783+
let dash = filename.find('-').unwrap();
1784+
let dot = filename.find('.').unwrap();
1785+
format!("{}-{}{}", &filename[..dash], builder.rust_release(), &filename[dot..])
1786+
}
1787+
17781788
pub fn compiler_file(
17791789
builder: &Builder<'_>,
17801790
compiler: &Path,

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use object::read::archive::ArchiveFile;
1919
#[cfg(feature = "tracing")]
2020
use tracing::instrument;
2121

22+
use crate::core::build_steps::compile::{get_codegen_backend_file, normalize_codegen_backend_name};
2223
use crate::core::build_steps::doc::DocumentationFormat;
2324
use crate::core::build_steps::tool::{self, RustcPrivateCompilers, Tool};
2425
use crate::core::build_steps::vendor::{VENDOR_DIR, Vendor};
@@ -1442,35 +1443,29 @@ impl Step for CraneliftCodegenBackend {
14421443
tarball.is_preview(true);
14431444
tarball.add_legal_and_readme_to("share/doc/rustc_codegen_cranelift");
14441445

1445-
builder.ensure(compile::CraneliftCodegenBackend { compilers });
1446+
let stamp = builder.ensure(compile::CraneliftCodegenBackend { compilers });
14461447

14471448
if builder.config.dry_run() {
14481449
return None;
14491450
}
14501451

1451-
let src = builder.sysroot(self.build_compiler);
1452-
let backends_src = builder.sysroot_codegen_backends(self.build_compiler);
1453-
let backends_rel = backends_src
1454-
.strip_prefix(src)
1452+
// Get the relative path of where the codegen backend should be stored.
1453+
let backends_dst = builder.sysroot_codegen_backends(compilers.target_compiler());
1454+
let backends_rel = backends_dst
1455+
.strip_prefix(builder.sysroot(compilers.target_compiler()))
14551456
.unwrap()
1456-
.strip_prefix(builder.sysroot_libdir_relative(self.build_compiler))
1457+
.strip_prefix(builder.sysroot_libdir_relative(compilers.target_compiler()))
14571458
.unwrap();
14581459
// Don't use custom libdir here because ^lib/ will be resolved again with installer
14591460
let backends_dst = PathBuf::from("lib").join(backends_rel);
14601461

1461-
let mut found_backend = false;
1462-
for backend in fs::read_dir(&backends_src).unwrap() {
1463-
let file_name = backend.unwrap().file_name();
1464-
if file_name.to_str().unwrap().contains("rustc_codegen_cranelift") {
1465-
tarball.add_file(
1466-
backends_src.join(file_name),
1467-
&backends_dst,
1468-
FileType::NativeLibrary,
1469-
);
1470-
found_backend = true;
1471-
}
1472-
}
1473-
assert!(found_backend);
1462+
let codegen_backend_dylib = get_codegen_backend_file(&stamp);
1463+
tarball.add_renamed_file(
1464+
&codegen_backend_dylib,
1465+
&backends_dst,
1466+
&normalize_codegen_backend_name(builder, &codegen_backend_dylib),
1467+
FileType::NativeLibrary,
1468+
);
14741469

14751470
Some(tarball.generate())
14761471
}

0 commit comments

Comments
 (0)