Skip to content

Commit 6355cd3

Browse files
committed
Auto merge of #145207 - Kobzol:codegen-backend-clif-dist, r=jieyouxu
Ship correct Cranelift library in its dist component The first commit adds a post-dist UI test to check that Cranelift can be used with the extracted dist x64 Linux archive. The original codegen copy logic in the Cranelift dist step was a bit redundant, and I didn't notice in #144787 that it's copying the codegen backend from the build compiler's sysroot, rather than the target compiler's sysroot. The second commit modifies the logic to directly access the built codegen file (there is no need to search for it in the compiler's sysroot, in fact when you run just `x dist rustc_codegen_cranelift`, it shouldn't "taint" the sysroot with the codegen backend! Which it did before #144787) and copy it to the tarball under a normalized name. Thus we get around any similar potential issues in the future, and make previously implicit logic more explicit. This also fixes running just `x dist rustc_codegen_cranelift` without enabling `cranelift` in `rust.codegen-backends`, which should have been enabled by #144787, but it didn't work fully, because the dist step tried to copy the codegen backend from the compiler's sysroot, but it didn't contain the codegen backend if it was not enabled by `rust.codegen-backends`. Fixes: #145201 try-job: dist-x86_64-linux
2 parents fce0e74 + 6893e69 commit 6355cd3

File tree

4 files changed

+56
-30
lines changed

4 files changed

+56
-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};
@@ -1460,35 +1461,29 @@ impl Step for CraneliftCodegenBackend {
14601461
tarball.is_preview(true);
14611462
tarball.add_legal_and_readme_to("share/doc/rustc_codegen_cranelift");
14621463

1463-
builder.ensure(compile::CraneliftCodegenBackend { compilers });
1464+
let stamp = builder.ensure(compile::CraneliftCodegenBackend { compilers });
14641465

14651466
if builder.config.dry_run() {
14661467
return None;
14671468
}
14681469

1469-
let src = builder.sysroot(self.build_compiler);
1470-
let backends_src = builder.sysroot_codegen_backends(self.build_compiler);
1471-
let backends_rel = backends_src
1472-
.strip_prefix(src)
1470+
// Get the relative path of where the codegen backend should be stored.
1471+
let backends_dst = builder.sysroot_codegen_backends(compilers.target_compiler());
1472+
let backends_rel = backends_dst
1473+
.strip_prefix(builder.sysroot(compilers.target_compiler()))
14731474
.unwrap()
1474-
.strip_prefix(builder.sysroot_libdir_relative(self.build_compiler))
1475+
.strip_prefix(builder.sysroot_libdir_relative(compilers.target_compiler()))
14751476
.unwrap();
14761477
// Don't use custom libdir here because ^lib/ will be resolved again with installer
14771478
let backends_dst = PathBuf::from("lib").join(backends_rel);
14781479

1479-
let mut found_backend = false;
1480-
for backend in fs::read_dir(&backends_src).unwrap() {
1481-
let file_name = backend.unwrap().file_name();
1482-
if file_name.to_str().unwrap().contains("rustc_codegen_cranelift") {
1483-
tarball.add_file(
1484-
backends_src.join(file_name),
1485-
&backends_dst,
1486-
FileType::NativeLibrary,
1487-
);
1488-
found_backend = true;
1489-
}
1490-
}
1491-
assert!(found_backend);
1480+
let codegen_backend_dylib = get_codegen_backend_file(&stamp);
1481+
tarball.add_renamed_file(
1482+
&codegen_backend_dylib,
1483+
&backends_dst,
1484+
&normalize_codegen_backend_name(builder, &codegen_backend_dylib),
1485+
FileType::NativeLibrary,
1486+
);
14921487

14931488
Some(tarball.generate())
14941489
}

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
//@ run-pass
8+
9+
fn main() {
10+
println!("Hello world!");
11+
}

0 commit comments

Comments
 (0)