Refactor archive.rs for future usage of ar::GnuBuilder
This commit is contained in:
parent
3fedac85e7
commit
004dc33031
1 changed files with 54 additions and 43 deletions
|
@ -14,25 +14,29 @@ struct ArchiveConfig<'a> {
|
||||||
pub lib_search_paths: Vec<PathBuf>,
|
pub lib_search_paths: Vec<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum ArchiveEntry {
|
||||||
|
FromArchive { archive_index: usize, entry_index: usize },
|
||||||
|
File(File),
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ArArchiveBuilder<'a> {
|
pub struct ArArchiveBuilder<'a> {
|
||||||
config: ArchiveConfig<'a>,
|
config: ArchiveConfig<'a>,
|
||||||
src_archive: Option<ar::Archive<File>>,
|
src_archives: Vec<ar::Archive<File>>,
|
||||||
src_entries: HashMap<String, usize>,
|
entries: HashMap<String, ArchiveEntry>,
|
||||||
builder: ar::Builder<File>,
|
|
||||||
update_symbols: bool,
|
update_symbols: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
use rustc_codegen_ssa::back::link::archive_search_paths;
|
||||||
let cfg = ArchiveConfig {
|
let config = ArchiveConfig {
|
||||||
sess,
|
sess,
|
||||||
dst: output.to_path_buf(),
|
dst: output.to_path_buf(),
|
||||||
src: input.map(|p| p.to_path_buf()),
|
src: input.map(|p| p.to_path_buf()),
|
||||||
lib_search_paths: archive_search_paths(sess),
|
lib_search_paths: archive_search_paths(sess),
|
||||||
};
|
};
|
||||||
|
|
||||||
let (src_archive, src_entries) = if let Some(src) = &cfg.src {
|
let (src_archives, entries) = if let Some(src) = &config.src {
|
||||||
let mut archive = ar::Archive::new(File::open(src).unwrap());
|
let mut archive = ar::Archive::new(File::open(src).unwrap());
|
||||||
let mut entries = HashMap::new();
|
let mut entries = HashMap::new();
|
||||||
|
|
||||||
|
@ -41,33 +45,30 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
let entry = entry.unwrap();
|
let entry = entry.unwrap();
|
||||||
entries.insert(
|
entries.insert(
|
||||||
String::from_utf8(entry.header().identifier().to_vec()).unwrap(),
|
String::from_utf8(entry.header().identifier().to_vec()).unwrap(),
|
||||||
i,
|
ArchiveEntry::FromArchive { archive_index: 0, entry_index: i },
|
||||||
);
|
);
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
(Some(archive), entries)
|
(vec![archive], entries)
|
||||||
} else {
|
} else {
|
||||||
(None, HashMap::new())
|
(vec![], HashMap::new())
|
||||||
};
|
};
|
||||||
|
|
||||||
let builder = ar::Builder::new(File::create(&cfg.dst).unwrap());
|
|
||||||
|
|
||||||
ArArchiveBuilder {
|
ArArchiveBuilder {
|
||||||
config: cfg,
|
config,
|
||||||
src_archive,
|
src_archives,
|
||||||
src_entries,
|
entries,
|
||||||
builder,
|
|
||||||
update_symbols: false,
|
update_symbols: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn src_files(&mut self) -> Vec<String> {
|
fn src_files(&mut self) -> Vec<String> {
|
||||||
self.src_entries.keys().cloned().collect()
|
self.entries.keys().cloned().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_file(&mut self, name: &str) {
|
fn remove_file(&mut self, name: &str) {
|
||||||
let file = self.src_entries.remove(name);
|
let file = self.entries.remove(name);
|
||||||
assert!(
|
assert!(
|
||||||
file.is_some(),
|
file.is_some(),
|
||||||
"Tried to remove file not existing in src archive",
|
"Tried to remove file not existing in src archive",
|
||||||
|
@ -75,7 +76,10 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_file(&mut self, file: &Path) {
|
fn add_file(&mut self, file: &Path) {
|
||||||
self.builder.append_path(file).unwrap();
|
self.entries.insert(
|
||||||
|
file.file_name().unwrap().to_str().unwrap().to_string(),
|
||||||
|
ArchiveEntry::File(File::open(file).unwrap()),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_native_library(&mut self, name: &str) {
|
fn add_native_library(&mut self, name: &str) {
|
||||||
|
@ -115,23 +119,30 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build(mut self) {
|
fn build(mut self) {
|
||||||
// Add files from original archive
|
let mut builder = ar::Builder::new(File::create(&self.config.dst).unwrap());
|
||||||
if let Some(mut src_archive) = self.src_archive {
|
|
||||||
for (_entry_name, entry_idx) in self.src_entries.into_iter() {
|
// Add all files
|
||||||
let entry = src_archive.jump_to_entry(entry_idx).unwrap();
|
for (entry_name, entry) in self.entries.into_iter() {
|
||||||
let orig_header = entry.header();
|
match entry {
|
||||||
let mut header =
|
ArchiveEntry::FromArchive { archive_index, entry_index } => {
|
||||||
ar::Header::new(orig_header.identifier().to_vec(), orig_header.size());
|
let entry = self.src_archives[archive_index].jump_to_entry(entry_index).unwrap();
|
||||||
header.set_mtime(orig_header.mtime());
|
let orig_header = entry.header();
|
||||||
header.set_uid(orig_header.uid());
|
let mut header =
|
||||||
header.set_gid(orig_header.gid());
|
ar::Header::new(orig_header.identifier().to_vec(), orig_header.size());
|
||||||
header.set_mode(orig_header.mode());
|
header.set_mtime(orig_header.mtime());
|
||||||
self.builder.append(&header, entry).unwrap();
|
header.set_uid(orig_header.uid());
|
||||||
|
header.set_gid(orig_header.gid());
|
||||||
|
header.set_mode(orig_header.mode());
|
||||||
|
builder.append(&header, entry).unwrap();
|
||||||
|
}
|
||||||
|
ArchiveEntry::File(mut file) => {
|
||||||
|
builder.append_file(entry_name.as_bytes(), &mut file).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finalize archive
|
// Finalize archive
|
||||||
std::mem::drop(self.builder);
|
std::mem::drop(builder);
|
||||||
|
|
||||||
// Run ranlib to be able to link the archive
|
// Run ranlib to be able to link the archive
|
||||||
let status = std::process::Command::new("ranlib")
|
let status = std::process::Command::new("ranlib")
|
||||||
|
@ -151,22 +162,22 @@ impl<'a> ArArchiveBuilder<'a> {
|
||||||
where F: FnMut(&str) -> bool + 'static
|
where F: FnMut(&str) -> bool + 'static
|
||||||
{
|
{
|
||||||
let mut archive = ar::Archive::new(std::fs::File::open(archive)?);
|
let mut archive = ar::Archive::new(std::fs::File::open(archive)?);
|
||||||
|
let archive_index = self.src_archives.len();
|
||||||
|
|
||||||
|
let mut i = 0;
|
||||||
while let Some(entry) = archive.next_entry() {
|
while let Some(entry) = archive.next_entry() {
|
||||||
let entry = entry?;
|
let entry = entry.unwrap();
|
||||||
let orig_header = entry.header();
|
let file_name = String::from_utf8(entry.header().identifier().to_vec()).unwrap();
|
||||||
|
if !skip(&file_name) {
|
||||||
if skip(std::str::from_utf8(orig_header.identifier()).unwrap()) {
|
self.entries.insert(
|
||||||
continue;
|
file_name,
|
||||||
|
ArchiveEntry::FromArchive { archive_index, entry_index: i },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
i += 1;
|
||||||
let mut header =
|
|
||||||
ar::Header::new(orig_header.identifier().to_vec(), orig_header.size());
|
|
||||||
header.set_mtime(orig_header.mtime());
|
|
||||||
header.set_uid(orig_header.uid());
|
|
||||||
header.set_gid(orig_header.gid());
|
|
||||||
header.set_mode(orig_header.mode());
|
|
||||||
self.builder.append(&header, entry).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.src_archives.push(archive);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue