Skip to content

Commit

Permalink
Remove the source archive functionality of ArchiveWriter
Browse files Browse the repository at this point in the history
We now build archives through strictly additive means rather than taking
an existing archive and potentially substracting parts.
  • Loading branch information
bjorn3 committed Jun 19, 2022
1 parent 7ff0df5 commit 18c6fe5
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 82 deletions.
24 changes: 3 additions & 21 deletions compiler/rustc_codegen_cranelift/src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,16 @@ pub(crate) struct ArArchiveBuilder<'a> {
}

impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self {
let (src_archives, entries) = if let Some(input) = input {
let read_cache = ReadCache::new(File::open(input).unwrap());
let archive = ArchiveFile::parse(&read_cache).unwrap();
let mut entries = Vec::new();

for entry in archive.members() {
let entry = entry.unwrap();
entries.push((
entry.name().to_vec(),
ArchiveEntry::FromArchive { archive_index: 0, file_range: entry.file_range() },
));
}

(vec![read_cache.into_inner()], entries)
} else {
(vec![], Vec::new())
};

fn new(sess: &'a Session, output: &Path) -> Self {
ArArchiveBuilder {
sess,
dst: output.to_path_buf(),
use_gnu_style_archive: sess.target.archive_format == "gnu",
// FIXME fix builtin ranlib on macOS
no_builtin_ranlib: sess.target.is_like_osx,

src_archives,
entries,
src_archives: vec![],
entries: vec![],
}
}

Expand Down
28 changes: 3 additions & 25 deletions compiler/rustc_codegen_gcc/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,32 +41,10 @@ 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![],
}
}

Expand Down
35 changes: 2 additions & 33 deletions compiler/rustc_codegen_llvm/src/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ use rustc_session::Session;
pub struct LlvmArchiveBuilder<'a> {
sess: &'a Session,
dst: PathBuf,
src: Option<PathBuf>,
additions: Vec<Addition>,
src_archive: Option<Option<ArchiveRO>>,
}

enum Addition {
Expand Down Expand Up @@ -59,14 +57,8 @@ fn llvm_machine_type(cpu: &str) -> LLVMMachineType {
impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
/// Creates a new static archive, ready for modifying the archive specified
/// by `config`.
fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> LlvmArchiveBuilder<'a> {
LlvmArchiveBuilder {
sess,
dst: output.to_path_buf(),
src: input.map(|p| p.to_path_buf()),
additions: Vec::new(),
src_archive: None,
}
fn new(sess: &'a Session, output: &Path) -> LlvmArchiveBuilder<'a> {
LlvmArchiveBuilder { sess, dst: output.to_path_buf(), additions: Vec::new() }
}

fn add_archive<F>(&mut self, archive: &Path, skip: F) -> io::Result<()>
Expand Down Expand Up @@ -257,15 +249,6 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
}

impl<'a> LlvmArchiveBuilder<'a> {
fn src_archive(&mut self) -> Option<&ArchiveRO> {
if let Some(ref a) = self.src_archive {
return a.as_ref();
}
let src = self.src.as_ref()?;
self.src_archive = Some(ArchiveRO::open(src).ok());
self.src_archive.as_ref().unwrap().as_ref()
}

fn llvm_archive_kind(&self) -> Result<ArchiveKind, &str> {
let kind = &*self.sess.target.archive_format;
kind.parse().map_err(|_| kind)
Expand All @@ -279,20 +262,6 @@ impl<'a> LlvmArchiveBuilder<'a> {
let dst = CString::new(self.dst.to_str().unwrap())?;

unsafe {
if let Some(archive) = self.src_archive() {
for child in archive.iter() {
let child = child.map_err(string_to_io_error)?;
let Some(child_name) = child.name() else { continue };

let name = CString::new(child_name)?;
members.push(llvm::LLVMRustArchiveMemberNew(
ptr::null(),
name.as_ptr(),
Some(child.raw),
));
strings.push(name);
}
}
for addition in &mut additions {
match addition {
Addition::File { path, name_in_archive } => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(super) fn find_library(
}

pub trait ArchiveBuilder<'a> {
fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self;
fn new(sess: &'a Session, output: &Path) -> Self;

fn add_file(&mut self, path: &Path);

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(

let lib_search_paths = archive_search_paths(sess);

let mut ab = <B as ArchiveBuilder>::new(sess, out_filename, None);
let mut ab = <B as ArchiveBuilder>::new(sess, out_filename);

let trailing_metadata = match flavor {
RlibFlavor::Normal => {
Expand Down Expand Up @@ -2472,7 +2472,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
let is_builtins = sess.target.no_builtins
|| !codegen_results.crate_info.is_no_builtins.contains(&cnum);

let mut archive = <B as ArchiveBuilder>::new(sess, &dst, None);
let mut archive = <B as ArchiveBuilder>::new(sess, &dst);
if let Err(e) = archive.add_archive(cratepath, move |f| {
if f == METADATA_FILENAME {
return true;
Expand Down

0 comments on commit 18c6fe5

Please sign in to comment.