ADD - ExtractBundledLibsError. Migrated extract_bundled_libs to translatable diagnostics

This commit is contained in:
Jhonny Bill Mena 2022-10-31 01:36:32 -04:00
parent 4c80f50fc6
commit 1f4c5a624f
4 changed files with 98 additions and 19 deletions

View File

@ -6,11 +6,12 @@
use object::read::archive::ArchiveFile;
use std::fmt::Display;
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};
use crate::errors::{ExtractBundledLibsError, ExtractBundledLibsErrorKind::*};
pub trait ArchiveBuilderBuilder {
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a>;
@ -28,32 +29,54 @@ fn create_dll_import_lib(
is_direct_dependency: bool,
) -> PathBuf;
fn extract_bundled_libs(
&self,
rlib: &Path,
fn extract_bundled_libs<'a>(
&'a self,
rlib: &'a Path,
outdir: &Path,
bundled_lib_file_names: &FxHashSet<Symbol>,
) -> Result<(), String> {
let message = |msg: &str, e: &dyn Display| format!("{} '{}': {}", msg, &rlib.display(), e);
) -> Result<(), ExtractBundledLibsError<'_>> {
let archive_map = unsafe {
Mmap::map(File::open(rlib).map_err(|e| message("failed to open file", &e))?)
.map_err(|e| message("failed to mmap file", &e))?
Mmap::map(File::open(rlib).map_err(|e| ExtractBundledLibsError {
kind: OpenFile,
rlib,
error: e.to_string(),
})?)
.map_err(|e| ExtractBundledLibsError {
kind: MmapFile,
rlib,
error: e.to_string(),
})?
};
let archive = ArchiveFile::parse(&*archive_map)
.map_err(|e| message("failed to parse archive", &e))?;
let archive = ArchiveFile::parse(&*archive_map).map_err(|e| ExtractBundledLibsError {
kind: ParseArchive,
rlib,
error: e.to_string(),
})?;
for entry in archive.members() {
let entry = entry.map_err(|e| message("failed to read entry", &e))?;
let data = entry
.data(&*archive_map)
.map_err(|e| message("failed to get data from archive member", &e))?;
let name = std::str::from_utf8(entry.name())
.map_err(|e| message("failed to convert name", &e))?;
let entry = entry.map_err(|e| ExtractBundledLibsError {
kind: ReadEntry,
rlib,
error: e.to_string(),
})?;
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)) {
continue; // We need to extract only native libraries.
}
std::fs::write(&outdir.join(&name), data)
.map_err(|e| message("failed to write file", &e))?;
std::fs::write(&outdir.join(&name), data).map_err(|e| ExtractBundledLibsError {
kind: WriteFile,
rlib,
error: e.to_string(),
})?;
}
Ok(())
}

View File

@ -2447,7 +2447,7 @@ fn add_upstream_rust_crates<'a>(
let rlib = &src.rlib.as_ref().unwrap().0;
archive_builder_builder
.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);

View File

@ -484,3 +484,51 @@ pub struct RlibArchiveBuildFailure {
#[derive(Diagnostic)]
#[diag(codegen_ssa_option_gcc_only)]
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
}
}

View File

@ -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_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}