Skip to content

Commit b85b030

Browse files
Auto merge of #145207 - Kobzol:codegen-backend-clif-dist, r=<try>
Ship correct Cranelift library in its dist component try-job: dist-x86_64-linux
2 parents 7f7b8ef + 1329f91 commit b85b030

File tree

4 files changed

+55
-30
lines changed

4 files changed

+55
-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
}

src/tools/opt-dist/src/tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ pub fn run_tests(env: &Environment) -> anyhow::Result<()> {
3636
let cargo_dir = extract_dist_dir(&format!("cargo-{version}-{host_triple}"))?.join("cargo");
3737
let extracted_src_dir = extract_dist_dir(&format!("rust-src-{version}"))?.join("rust-src");
3838

39+
// If we have a Cranelift archive, copy it to the rustc sysroot
40+
if let Ok(_) = find_file_in_dir(&dist_dir, "rustc-codegen-cranelift-", ".tar.xz") {
41+
let extracted_codegen_dir =
42+
extract_dist_dir(&format!("rustc-codegen-cranelift-{version}-{host_triple}"))?
43+
.join("rustc-codegen-cranelift-preview");
44+
let rel_path =
45+
Utf8Path::new("lib").join("rustlib").join(host_triple).join("codegen-backends");
46+
copy_directory(&extracted_codegen_dir.join(&rel_path), &rustc_dir.join(&rel_path))?;
47+
}
48+
3949
// We need to manually copy libstd to the extracted rustc sysroot
4050
copy_directory(
4151
&libstd_dir.join("lib").join("rustlib").join(host_triple).join("lib"),
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Ensure that Cranelift can be used to compile a simple program with `x86_64-unknown-linux-gnu`
2+
// dist artifacts.
3+
4+
//@ only-dist
5+
//@ only-x86_64-unknown-linux-gnu
6+
//@ compile-flags: -Z codegen-backend=cranelift
7+
8+
fn main() {
9+
println!("Hello world!");
10+
}

0 commit comments

Comments
 (0)