Move add_rlib and add_native_library to cg_ssa

This deduplicates logic between codegen backends
This commit is contained in:
bjorn3 2021-09-01 14:38:37 +02:00
parent 39e16da529
commit 677c786109
1 changed files with 19 additions and 64 deletions

View File

@ -4,8 +4,7 @@ use std::collections::BTreeMap;
use std::fs::File; use std::fs::File;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use rustc_codegen_ssa::back::archive::{find_library, ArchiveBuilder}; use rustc_codegen_ssa::back::archive::ArchiveBuilder;
use rustc_codegen_ssa::METADATA_FILENAME;
use rustc_session::Session; use rustc_session::Session;
use object::{Object, ObjectSymbol, SymbolKind}; use object::{Object, ObjectSymbol, SymbolKind};
@ -19,7 +18,6 @@ enum ArchiveEntry {
pub(crate) struct ArArchiveBuilder<'a> { pub(crate) struct ArArchiveBuilder<'a> {
sess: &'a Session, sess: &'a Session,
dst: PathBuf, dst: PathBuf,
lib_search_paths: Vec<PathBuf>,
use_gnu_style_archive: bool, use_gnu_style_archive: bool,
no_builtin_ranlib: bool, no_builtin_ranlib: bool,
@ -31,8 +29,6 @@ pub(crate) struct ArArchiveBuilder<'a> {
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self { fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self {
use rustc_codegen_ssa::back::link::archive_search_paths;
let (src_archives, entries) = if let Some(input) = input { let (src_archives, entries) = if let Some(input) = input {
let mut archive = ar::Archive::new(File::open(input).unwrap()); let mut archive = ar::Archive::new(File::open(input).unwrap());
let mut entries = Vec::new(); let mut entries = Vec::new();
@ -55,7 +51,6 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
ArArchiveBuilder { ArArchiveBuilder {
sess, sess,
dst: output.to_path_buf(), dst: output.to_path_buf(),
lib_search_paths: archive_search_paths(sess),
use_gnu_style_archive: sess.target.archive_format == "gnu", use_gnu_style_archive: sess.target.archive_format == "gnu",
// FIXME fix builtin ranlib on macOS // FIXME fix builtin ranlib on macOS
no_builtin_ranlib: sess.target.is_like_osx, no_builtin_ranlib: sess.target.is_like_osx,
@ -85,42 +80,27 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
)); ));
} }
fn add_native_library(&mut self, name: rustc_span::symbol::Symbol, verbatim: bool) { fn add_archive<F>(&mut self, archive_path: &Path, mut skip: F) -> std::io::Result<()>
let location = find_library(name, verbatim, &self.lib_search_paths, self.sess); where
self.add_archive(location.clone(), |_| false).unwrap_or_else(|e| { F: FnMut(&str) -> bool + 'static,
panic!("failed to add native library {}: {}", location.to_string_lossy(), e); {
}); let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
} let archive_index = self.src_archives.len();
fn add_rlib( let mut i = 0;
&mut self, while let Some(entry) = archive.next_entry() {
rlib: &Path, let entry = entry?;
name: &str, let file_name = String::from_utf8(entry.header().identifier().to_vec())
lto: bool, .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
skip_objects: bool, if !skip(&file_name) {
) -> std::io::Result<()> { self.entries
let obj_start = name.to_owned(); .push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
self.add_archive(rlib.to_owned(), move |fname: &str| {
// Ignore metadata files, no matter the name.
if fname == METADATA_FILENAME {
return true;
} }
i += 1;
}
// Don't include Rust objects if LTO is enabled self.src_archives.push((archive_path.to_owned(), archive));
if lto && fname.starts_with(&obj_start) && fname.ends_with(".o") { Ok(())
return true;
}
// Otherwise if this is *not* a rust object and we're skipping
// objects then skip this file
if skip_objects && (!fname.starts_with(&obj_start) || !fname.ends_with(".o")) {
return true;
}
// ok, don't skip this
false
})
} }
fn update_symbols(&mut self) {} fn update_symbols(&mut self) {}
@ -264,28 +244,3 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
bug!("injecting dll imports is not supported"); bug!("injecting dll imports is not supported");
} }
} }
impl<'a> ArArchiveBuilder<'a> {
fn add_archive<F>(&mut self, archive_path: PathBuf, mut skip: F) -> std::io::Result<()>
where
F: FnMut(&str) -> bool + 'static,
{
let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
let archive_index = self.src_archives.len();
let mut i = 0;
while let Some(entry) = archive.next_entry() {
let entry = entry?;
let file_name = String::from_utf8(entry.header().identifier().to_vec())
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
if !skip(&file_name) {
self.entries
.push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
}
i += 1;
}
self.src_archives.push((archive_path, archive));
Ok(())
}
}