Skip to content

Commit

Permalink
Auto merge of rust-lang#98098 - bjorn3:archive_refactor, r=michaelwoe…
Browse files Browse the repository at this point in the history
…rister

Remove the source archive functionality of ArchiveWriter

We now build archives through strictly additive means rather than taking an existing archive and potentially substracting parts. This is simpler and makes it easier to swap out the archive writer in rust-lang#97485.
  • Loading branch information
bors committed Jun 21, 2022
2 parents e4b0d3a + 0d28d64 commit 6bad0d5
Showing 1 changed file with 8 additions and 39 deletions.
47 changes: 8 additions & 39 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct 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) -> Self {
let config = ArchiveConfig {
sess,
dst: output.to_path_buf(),
Expand All @@ -41,48 +41,13 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
use_gnu_style_archive: sess.target.options.archive_format == "gnu",
};

let (src_archives, entries) = if let Some(input) = input {
let mut archive = ar::Archive::new(File::open(input).unwrap());
let mut entries = Vec::new();

let mut i = 0;
while let Some(entry) = archive.next_entry() {
let entry = entry.unwrap();
entries.push((
String::from_utf8(entry.header().identifier().to_vec()).unwrap(),
ArchiveEntry::FromArchive {
archive_index: 0,
entry_index: i,
},
));
i += 1;
}

(vec![(input.to_owned(), archive)], entries)
} else {
(vec![], Vec::new())
};

ArArchiveBuilder {
config,
src_archives,
entries,
src_archives: vec![],
entries: vec![],
}
}

fn src_files(&mut self) -> Vec<String> {
self.entries.iter().map(|(name, _)| name.clone()).collect()
}

fn remove_file(&mut self, name: &str) {
let index = self
.entries
.iter()
.position(|(entry_name, _)| entry_name == name)
.expect("Tried to remove file not existing in src archive");
self.entries.remove(index);
}

fn add_file(&mut self, file: &Path) {
self.entries.push((
file.file_name().unwrap().to_str().unwrap().to_string(),
Expand Down Expand Up @@ -113,7 +78,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
Ok(())
}

fn build(mut self) {
fn build(mut self) -> bool {
use std::process::Command;

fn add_file_using_ar(archive: &Path, file: &Path) {
Expand Down Expand Up @@ -146,6 +111,8 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
BuilderKind::Bsd(ar::Builder::new(File::create(&self.config.dst).unwrap()))
};

let any_members = !self.entries.is_empty();

// Add all files
for (entry_name, entry) in self.entries.into_iter() {
match entry {
Expand Down Expand Up @@ -206,6 +173,8 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
if !status.success() {
self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
}

any_members
}

fn inject_dll_import_lib(&mut self, _lib_name: &str, _dll_imports: &[DllImport], _tmpdir: &MaybeTempDir) {
Expand Down

0 comments on commit 6bad0d5

Please sign in to comment.