Remove src_files and remove_file

They only apply to the main source archive and their role can be
fulfilled through the skip argument of add_archive too.
This commit is contained in:
bjorn3 2022-06-14 15:11:14 +00:00
parent 70e084aa21
commit 43929a8a75
5 changed files with 14 additions and 73 deletions

View File

@ -61,19 +61,6 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
} }
} }
fn src_files(&mut self) -> Vec<String> {
self.entries.iter().map(|(name, _)| String::from_utf8(name.clone()).unwrap()).collect()
}
fn remove_file(&mut self, name: &str) {
let index = self
.entries
.iter()
.position(|(entry_name, _)| entry_name == name.as_bytes())
.expect("Tried to remove file not existing in src archive");
self.entries.remove(index);
}
fn add_file(&mut self, file: &Path) { fn add_file(&mut self, file: &Path) {
self.entries.push(( self.entries.push((
file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(), file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(),

View File

@ -70,19 +70,6 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
} }
} }
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) { fn add_file(&mut self, file: &Path) {
self.entries.push(( self.entries.push((
file.file_name().unwrap().to_str().unwrap().to_string(), file.file_name().unwrap().to_str().unwrap().to_string(),

View File

@ -21,7 +21,6 @@ pub struct LlvmArchiveBuilder<'a> {
sess: &'a Session, sess: &'a Session,
dst: PathBuf, dst: PathBuf,
src: Option<PathBuf>, src: Option<PathBuf>,
removals: Vec<String>,
additions: Vec<Addition>, additions: Vec<Addition>,
src_archive: Option<Option<ArchiveRO>>, src_archive: Option<Option<ArchiveRO>>,
} }
@ -65,35 +64,11 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
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()),
removals: Vec::new(),
additions: Vec::new(), additions: Vec::new(),
src_archive: None, src_archive: None,
} }
} }
/// Removes a file from this archive
fn remove_file(&mut self, file: &str) {
self.removals.push(file.to_string());
}
/// Lists all files in an archive
fn src_files(&mut self) -> Vec<String> {
if self.src_archive().is_none() {
return Vec::new();
}
let archive = self.src_archive.as_ref().unwrap().as_ref().unwrap();
archive
.iter()
.filter_map(|child| child.ok())
.filter(is_relevant_child)
.filter_map(|child| child.name())
.filter(|name| !self.removals.iter().any(|x| x == name))
.map(|name| name.to_owned())
.collect()
}
fn add_archive<F>(&mut self, archive: &Path, skip: F) -> io::Result<()> fn add_archive<F>(&mut self, archive: &Path, skip: F) -> io::Result<()>
where where
F: FnMut(&str) -> bool + 'static, F: FnMut(&str) -> bool + 'static,
@ -296,7 +271,6 @@ impl<'a> LlvmArchiveBuilder<'a> {
} }
fn build_with_llvm(&mut self, kind: ArchiveKind) -> io::Result<()> { fn build_with_llvm(&mut self, kind: ArchiveKind) -> io::Result<()> {
let removals = mem::take(&mut self.removals);
let mut additions = mem::take(&mut self.additions); let mut additions = mem::take(&mut self.additions);
let mut strings = Vec::new(); let mut strings = Vec::new();
let mut members = Vec::new(); let mut members = Vec::new();
@ -308,9 +282,6 @@ impl<'a> LlvmArchiveBuilder<'a> {
for child in archive.iter() { for child in archive.iter() {
let child = child.map_err(string_to_io_error)?; let child = child.map_err(string_to_io_error)?;
let Some(child_name) = child.name() else { continue }; let Some(child_name) = child.name() else { continue };
if removals.iter().any(|r| r == child_name) {
continue;
}
let name = CString::new(child_name)?; let name = CString::new(child_name)?;
members.push(llvm::LLVMRustArchiveMemberNew( members.push(llvm::LLVMRustArchiveMemberNew(

View File

@ -45,8 +45,6 @@ pub trait ArchiveBuilder<'a> {
fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self; fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self;
fn add_file(&mut self, path: &Path); fn add_file(&mut self, path: &Path);
fn remove_file(&mut self, name: &str);
fn src_files(&mut self) -> Vec<String>;
fn add_archive<F>(&mut self, archive: &Path, skip: F) -> io::Result<()> fn add_archive<F>(&mut self, archive: &Path, skip: F) -> io::Result<()>
where where

View File

@ -2466,17 +2466,19 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
let name = &name[3..name.len() - 5]; // chop off lib/.rlib let name = &name[3..name.len() - 5]; // chop off lib/.rlib
sess.prof.generic_activity_with_arg("link_altering_rlib", name).run(|| { sess.prof.generic_activity_with_arg("link_altering_rlib", name).run(|| {
let mut archive = <B as ArchiveBuilder>::new(sess, &dst, Some(cratepath)); let canonical_name = name.replace('-', "_");
let upstream_rust_objects_already_included =
are_upstream_rust_objects_already_included(sess);
let is_builtins = sess.target.no_builtins
|| !codegen_results.crate_info.is_no_builtins.contains(&cnum);
let mut any_objects = false; let mut archive = <B as ArchiveBuilder>::new(sess, &dst, None);
for f in archive.src_files() { if let Err(e) = archive.add_archive(cratepath, move |f| {
if f == METADATA_FILENAME { if f == METADATA_FILENAME {
archive.remove_file(&f); return true;
continue;
} }
let canonical = f.replace('-', "_"); let canonical = f.replace('-', "_");
let canonical_name = name.replace('-', "_");
let is_rust_object = let is_rust_object =
canonical.starts_with(&canonical_name) && looks_like_rust_object_file(&f); canonical.starts_with(&canonical_name) && looks_like_rust_object_file(&f);
@ -2490,20 +2492,16 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
// file, then we don't need the object file as it's part of the // file, then we don't need the object file as it's part of the
// LTO module. Note that `#![no_builtins]` is excluded from LTO, // LTO module. Note that `#![no_builtins]` is excluded from LTO,
// though, so we let that object file slide. // though, so we let that object file slide.
let skip_because_lto = are_upstream_rust_objects_already_included(sess) let skip_because_lto =
&& is_rust_object upstream_rust_objects_already_included && is_rust_object && is_builtins;
&& (sess.target.no_builtins
|| !codegen_results.crate_info.is_no_builtins.contains(&cnum));
if skip_because_cfg_say_so || skip_because_lto { if skip_because_cfg_say_so || skip_because_lto {
archive.remove_file(&f); return true;
} else {
any_objects = true;
}
} }
if !any_objects { false
return; }) {
sess.fatal(&format!("failed to build archive from rlib: {}", e));
} }
archive.build(); archive.build();
link_upstream(&dst); link_upstream(&dst);