ADD - ExtractBundledLibsError. Migrated extract_bundled_libs to translatable diagnostics
This commit is contained in:
parent
4c80f50fc6
commit
1f4c5a624f
4 changed files with 98 additions and 19 deletions
|
@ -6,11 +6,12 @@ use rustc_span::symbol::Symbol;
|
||||||
|
|
||||||
use object::read::archive::ArchiveFile;
|
use object::read::archive::ArchiveFile;
|
||||||
|
|
||||||
use std::fmt::Display;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use crate::errors::{ExtractBundledLibsError, ExtractBundledLibsErrorKind::*};
|
||||||
|
|
||||||
pub trait ArchiveBuilderBuilder {
|
pub trait ArchiveBuilderBuilder {
|
||||||
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a>;
|
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a>;
|
||||||
|
|
||||||
|
@ -28,32 +29,54 @@ pub trait ArchiveBuilderBuilder {
|
||||||
is_direct_dependency: bool,
|
is_direct_dependency: bool,
|
||||||
) -> PathBuf;
|
) -> PathBuf;
|
||||||
|
|
||||||
fn extract_bundled_libs(
|
fn extract_bundled_libs<'a>(
|
||||||
&self,
|
&'a self,
|
||||||
rlib: &Path,
|
rlib: &'a Path,
|
||||||
outdir: &Path,
|
outdir: &Path,
|
||||||
bundled_lib_file_names: &FxHashSet<Symbol>,
|
bundled_lib_file_names: &FxHashSet<Symbol>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), ExtractBundledLibsError<'_>> {
|
||||||
let message = |msg: &str, e: &dyn Display| format!("{} '{}': {}", msg, &rlib.display(), e);
|
|
||||||
let archive_map = unsafe {
|
let archive_map = unsafe {
|
||||||
Mmap::map(File::open(rlib).map_err(|e| message("failed to open file", &e))?)
|
Mmap::map(File::open(rlib).map_err(|e| ExtractBundledLibsError {
|
||||||
.map_err(|e| message("failed to mmap file", &e))?
|
kind: OpenFile,
|
||||||
|
rlib,
|
||||||
|
error: e.to_string(),
|
||||||
|
})?)
|
||||||
|
.map_err(|e| ExtractBundledLibsError {
|
||||||
|
kind: MmapFile,
|
||||||
|
rlib,
|
||||||
|
error: e.to_string(),
|
||||||
|
})?
|
||||||
};
|
};
|
||||||
let archive = ArchiveFile::parse(&*archive_map)
|
let archive = ArchiveFile::parse(&*archive_map).map_err(|e| ExtractBundledLibsError {
|
||||||
.map_err(|e| message("failed to parse archive", &e))?;
|
kind: ParseArchive,
|
||||||
|
rlib,
|
||||||
|
error: e.to_string(),
|
||||||
|
})?;
|
||||||
|
|
||||||
for entry in archive.members() {
|
for entry in archive.members() {
|
||||||
let entry = entry.map_err(|e| message("failed to read entry", &e))?;
|
let entry = entry.map_err(|e| ExtractBundledLibsError {
|
||||||
let data = entry
|
kind: ReadEntry,
|
||||||
.data(&*archive_map)
|
rlib,
|
||||||
.map_err(|e| message("failed to get data from archive member", &e))?;
|
error: e.to_string(),
|
||||||
let name = std::str::from_utf8(entry.name())
|
})?;
|
||||||
.map_err(|e| message("failed to convert name", &e))?;
|
let data = entry.data(&*archive_map).map_err(|e| ExtractBundledLibsError {
|
||||||
|
kind: ArchiveMember,
|
||||||
|
rlib,
|
||||||
|
error: e.to_string(),
|
||||||
|
})?;
|
||||||
|
let name = std::str::from_utf8(entry.name()).map_err(|e| ExtractBundledLibsError {
|
||||||
|
kind: ConvertName,
|
||||||
|
rlib,
|
||||||
|
error: e.to_string(),
|
||||||
|
})?;
|
||||||
if !bundled_lib_file_names.contains(&Symbol::intern(name)) {
|
if !bundled_lib_file_names.contains(&Symbol::intern(name)) {
|
||||||
continue; // We need to extract only native libraries.
|
continue; // We need to extract only native libraries.
|
||||||
}
|
}
|
||||||
std::fs::write(&outdir.join(&name), data)
|
std::fs::write(&outdir.join(&name), data).map_err(|e| ExtractBundledLibsError {
|
||||||
.map_err(|e| message("failed to write file", &e))?;
|
kind: WriteFile,
|
||||||
|
rlib,
|
||||||
|
error: e.to_string(),
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -2447,7 +2447,7 @@ fn add_upstream_rust_crates<'a>(
|
||||||
let rlib = &src.rlib.as_ref().unwrap().0;
|
let rlib = &src.rlib.as_ref().unwrap().0;
|
||||||
archive_builder_builder
|
archive_builder_builder
|
||||||
.extract_bundled_libs(rlib, tmpdir, &bundled_libs)
|
.extract_bundled_libs(rlib, tmpdir, &bundled_libs)
|
||||||
.unwrap_or_else(|e| sess.fatal(e));
|
.unwrap_or_else(|e| sess.emit_fatal(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut last = (None, NativeLibKind::Unspecified, None);
|
let mut last = (None, NativeLibKind::Unspecified, None);
|
||||||
|
|
|
@ -484,3 +484,51 @@ pub struct RlibArchiveBuildFailure {
|
||||||
#[derive(Diagnostic)]
|
#[derive(Diagnostic)]
|
||||||
#[diag(codegen_ssa_option_gcc_only)]
|
#[diag(codegen_ssa_option_gcc_only)]
|
||||||
pub struct OptionGccOnly;
|
pub struct OptionGccOnly;
|
||||||
|
|
||||||
|
pub struct ExtractBundledLibsError<'a> {
|
||||||
|
pub kind: ExtractBundledLibsErrorKind,
|
||||||
|
pub rlib: &'a Path,
|
||||||
|
pub error: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum ExtractBundledLibsErrorKind {
|
||||||
|
OpenFile,
|
||||||
|
MmapFile,
|
||||||
|
ParseArchive,
|
||||||
|
ReadEntry,
|
||||||
|
ArchiveMember,
|
||||||
|
ConvertName,
|
||||||
|
WriteFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoDiagnostic<'_, !> for ExtractBundledLibsError<'_> {
|
||||||
|
fn into_diagnostic(self, handler: &'_ Handler) -> DiagnosticBuilder<'_, !> {
|
||||||
|
let mut diag = match self.kind {
|
||||||
|
ExtractBundledLibsErrorKind::OpenFile => {
|
||||||
|
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_open_file)
|
||||||
|
}
|
||||||
|
ExtractBundledLibsErrorKind::MmapFile => {
|
||||||
|
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_mmap_file)
|
||||||
|
}
|
||||||
|
ExtractBundledLibsErrorKind::ParseArchive => {
|
||||||
|
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_parse_archive)
|
||||||
|
}
|
||||||
|
ExtractBundledLibsErrorKind::ReadEntry => {
|
||||||
|
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_read_entry)
|
||||||
|
}
|
||||||
|
ExtractBundledLibsErrorKind::ArchiveMember => {
|
||||||
|
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_archive_member)
|
||||||
|
}
|
||||||
|
ExtractBundledLibsErrorKind::ConvertName => {
|
||||||
|
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_convert_name)
|
||||||
|
}
|
||||||
|
ExtractBundledLibsErrorKind::WriteFile => {
|
||||||
|
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_write_file)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
diag.set_arg("rlib", self.rlib);
|
||||||
|
diag.set_arg("error", self.error);
|
||||||
|
diag
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -170,3 +170,11 @@ codegen_ssa_unable_to_write_debugger_visualizer = Unable to write debugger visua
|
||||||
codegen_ssa_rlib_archive_build_failure = failed to build archive from rlib: {$error}
|
codegen_ssa_rlib_archive_build_failure = failed to build archive from rlib: {$error}
|
||||||
|
|
||||||
codegen_ssa_option_gcc_only = option `-Z gcc-ld` is used even though linker flavor is not gcc
|
codegen_ssa_option_gcc_only = option `-Z gcc-ld` is used even though linker flavor is not gcc
|
||||||
|
|
||||||
|
codegen_ssa_extract_bundled_libs_open_file = failed to open file '{$rlib}': {$error}
|
||||||
|
codegen_ssa_extract_bundled_libs_mmap_file = failed to mmap file '{$rlib}': {$error}
|
||||||
|
codegen_ssa_extract_bundled_libs_parse_archive = failed to parse archive '{$rlib}': {$error}
|
||||||
|
codegen_ssa_extract_bundled_libs_read_entry = failed to read entry '{$rlib}': {$error}
|
||||||
|
codegen_ssa_extract_bundled_libs_archive_member = failed to get data from archive member '{$rlib}': {$error}
|
||||||
|
codegen_ssa_extract_bundled_libs_convert_name = failed to convert name '{$rlib}': {$error}
|
||||||
|
codegen_ssa_extract_bundled_libs_write_file = failed to write file '{$rlib}': {$error}
|
||||||
|
|
Loading…
Add table
Reference in a new issue