From 43929a8a75d85ae3939a80703bdd827e81c51ba5 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Tue, 14 Jun 2022 15:11:14 +0000 Subject: 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. --- compiler/rustc_codegen_cranelift/src/archive.rs | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'compiler/rustc_codegen_cranelift') diff --git a/compiler/rustc_codegen_cranelift/src/archive.rs b/compiler/rustc_codegen_cranelift/src/archive.rs index a099e8b3a6a..4822c7e03a9 100644 --- a/compiler/rustc_codegen_cranelift/src/archive.rs +++ b/compiler/rustc_codegen_cranelift/src/archive.rs @@ -61,19 +61,6 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { } } - fn src_files(&mut self) -> Vec { - 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) { self.entries.push(( file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(), -- cgit 1.4.1-3-g733a5 From 7ff0df51024a96e91f41c3760b5676ebbdc7a2c0 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 18 Jun 2022 17:55:24 +0000 Subject: Fix "Remove src_files and remove_file" --- compiler/rustc_codegen_cranelift/src/archive.rs | 6 +++++- compiler/rustc_codegen_gcc/src/archive.rs | 6 +++++- compiler/rustc_codegen_llvm/src/back/archive.rs | 11 ++++++----- compiler/rustc_codegen_ssa/src/back/archive.rs | 2 +- compiler/rustc_codegen_ssa/src/back/link.rs | 5 +++-- 5 files changed, 20 insertions(+), 10 deletions(-) (limited to 'compiler/rustc_codegen_cranelift') diff --git a/compiler/rustc_codegen_cranelift/src/archive.rs b/compiler/rustc_codegen_cranelift/src/archive.rs index 4822c7e03a9..e9b074e1837 100644 --- a/compiler/rustc_codegen_cranelift/src/archive.rs +++ b/compiler/rustc_codegen_cranelift/src/archive.rs @@ -92,7 +92,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { Ok(()) } - fn build(mut self) { + fn build(mut self) -> bool { enum BuilderKind { Bsd(ar::Builder), Gnu(ar::GnuBuilder), @@ -191,6 +191,8 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { ) }; + let any_members = !entries.is_empty(); + // Add all files for (entry_name, data) in entries.into_iter() { let header = ar::Header::new(entry_name, data.len() as u64); @@ -216,6 +218,8 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { self.sess.fatal(&format!("Ranlib exited with code {:?}", status.code())); } } + + any_members } fn inject_dll_import_lib( diff --git a/compiler/rustc_codegen_gcc/src/archive.rs b/compiler/rustc_codegen_gcc/src/archive.rs index 1975035ae20..999a54495ee 100644 --- a/compiler/rustc_codegen_gcc/src/archive.rs +++ b/compiler/rustc_codegen_gcc/src/archive.rs @@ -100,7 +100,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) { @@ -133,6 +133,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 { @@ -193,6 +195,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) { diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index 455c38ffb32..1f4bfffaafe 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -97,13 +97,14 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> { /// Combine the provided files, rlibs, and native libraries into a single /// `Archive`. - fn build(mut self) { + fn build(mut self) -> bool { let kind = self.llvm_archive_kind().unwrap_or_else(|kind| { self.sess.fatal(&format!("Don't know how to build archive of type: {}", kind)) }); - if let Err(e) = self.build_with_llvm(kind) { - self.sess.fatal(&format!("failed to build archive: {}", e)); + match self.build_with_llvm(kind) { + Ok(any_members) => any_members, + Err(e) => self.sess.fatal(&format!("failed to build archive: {}", e)), } } @@ -270,7 +271,7 @@ impl<'a> LlvmArchiveBuilder<'a> { kind.parse().map_err(|_| kind) } - fn build_with_llvm(&mut self, kind: ArchiveKind) -> io::Result<()> { + fn build_with_llvm(&mut self, kind: ArchiveKind) -> io::Result { let mut additions = mem::take(&mut self.additions); let mut strings = Vec::new(); let mut members = Vec::new(); @@ -353,7 +354,7 @@ impl<'a> LlvmArchiveBuilder<'a> { }; Err(io::Error::new(io::ErrorKind::Other, msg)) } else { - Ok(()) + Ok(!members.is_empty()) }; for member in members { llvm::LLVMRustArchiveMemberFree(member); diff --git a/compiler/rustc_codegen_ssa/src/back/archive.rs b/compiler/rustc_codegen_ssa/src/back/archive.rs index 1c9fc217ea4..d2dd3fbcc99 100644 --- a/compiler/rustc_codegen_ssa/src/back/archive.rs +++ b/compiler/rustc_codegen_ssa/src/back/archive.rs @@ -50,7 +50,7 @@ pub trait ArchiveBuilder<'a> { where F: FnMut(&str) -> bool + 'static; - fn build(self); + fn build(self) -> bool; fn inject_dll_import_lib( &mut self, diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 0f528c119f3..b39a9c988c6 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -2503,8 +2503,9 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>( }) { sess.fatal(&format!("failed to build archive from rlib: {}", e)); } - archive.build(); - link_upstream(&dst); + if archive.build() { + link_upstream(&dst); + } }); } -- cgit 1.4.1-3-g733a5 From 18c6fe5798c70d532742cfda6c21d61daee257a4 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Tue, 14 Jun 2022 15:16:51 +0000 Subject: 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. --- compiler/rustc_codegen_cranelift/src/archive.rs | 24 +++-------------- compiler/rustc_codegen_gcc/src/archive.rs | 28 +++----------------- compiler/rustc_codegen_llvm/src/back/archive.rs | 35 ++----------------------- compiler/rustc_codegen_ssa/src/back/archive.rs | 2 +- compiler/rustc_codegen_ssa/src/back/link.rs | 4 +-- 5 files changed, 11 insertions(+), 82 deletions(-) (limited to 'compiler/rustc_codegen_cranelift') diff --git a/compiler/rustc_codegen_cranelift/src/archive.rs b/compiler/rustc_codegen_cranelift/src/archive.rs index e9b074e1837..0812f930b5d 100644 --- a/compiler/rustc_codegen_cranelift/src/archive.rs +++ b/compiler/rustc_codegen_cranelift/src/archive.rs @@ -30,25 +30,7 @@ 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(), @@ -56,8 +38,8 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { // FIXME fix builtin ranlib on macOS no_builtin_ranlib: sess.target.is_like_osx, - src_archives, - entries, + src_archives: vec![], + entries: vec![], } } diff --git a/compiler/rustc_codegen_gcc/src/archive.rs b/compiler/rustc_codegen_gcc/src/archive.rs index 999a54495ee..411ec27139e 100644 --- a/compiler/rustc_codegen_gcc/src/archive.rs +++ b/compiler/rustc_codegen_gcc/src/archive.rs @@ -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(), @@ -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![], } } diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index 1f4bfffaafe..58ad8af7472 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -20,9 +20,7 @@ use rustc_session::Session; pub struct LlvmArchiveBuilder<'a> { sess: &'a Session, dst: PathBuf, - src: Option, additions: Vec, - src_archive: Option>, } enum Addition { @@ -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(&mut self, archive: &Path, skip: F) -> io::Result<()> @@ -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 { let kind = &*self.sess.target.archive_format; kind.parse().map_err(|_| kind) @@ -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 } => { diff --git a/compiler/rustc_codegen_ssa/src/back/archive.rs b/compiler/rustc_codegen_ssa/src/back/archive.rs index d2dd3fbcc99..553486ae8ec 100644 --- a/compiler/rustc_codegen_ssa/src/back/archive.rs +++ b/compiler/rustc_codegen_ssa/src/back/archive.rs @@ -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); diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index b39a9c988c6..9233982bfc0 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -270,7 +270,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>( let lib_search_paths = archive_search_paths(sess); - let mut ab = ::new(sess, out_filename, None); + let mut ab = ::new(sess, out_filename); let trailing_metadata = match flavor { RlibFlavor::Normal => { @@ -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 = ::new(sess, &dst, None); + let mut archive = ::new(sess, &dst); if let Err(e) = archive.add_archive(cratepath, move |f| { if f == METADATA_FILENAME { return true; -- cgit 1.4.1-3-g733a5