Skip to content

Commit 43929a8

Browse files
committed
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.
1 parent 70e084a commit 43929a8

File tree

5 files changed

+15
-74
lines changed

5 files changed

+15
-74
lines changed

compiler/rustc_codegen_cranelift/src/archive.rs

-13
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,6 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
6161
}
6262
}
6363

64-
fn src_files(&mut self) -> Vec<String> {
65-
self.entries.iter().map(|(name, _)| String::from_utf8(name.clone()).unwrap()).collect()
66-
}
67-
68-
fn remove_file(&mut self, name: &str) {
69-
let index = self
70-
.entries
71-
.iter()
72-
.position(|(entry_name, _)| entry_name == name.as_bytes())
73-
.expect("Tried to remove file not existing in src archive");
74-
self.entries.remove(index);
75-
}
76-
7764
fn add_file(&mut self, file: &Path) {
7865
self.entries.push((
7966
file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(),

compiler/rustc_codegen_gcc/src/archive.rs

-13
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,6 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
7070
}
7171
}
7272

73-
fn src_files(&mut self) -> Vec<String> {
74-
self.entries.iter().map(|(name, _)| name.clone()).collect()
75-
}
76-
77-
fn remove_file(&mut self, name: &str) {
78-
let index = self
79-
.entries
80-
.iter()
81-
.position(|(entry_name, _)| entry_name == name)
82-
.expect("Tried to remove file not existing in src archive");
83-
self.entries.remove(index);
84-
}
85-
8673
fn add_file(&mut self, file: &Path) {
8774
self.entries.push((
8875
file.file_name().unwrap().to_str().unwrap().to_string(),

compiler/rustc_codegen_llvm/src/back/archive.rs

-29
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub struct LlvmArchiveBuilder<'a> {
2121
sess: &'a Session,
2222
dst: PathBuf,
2323
src: Option<PathBuf>,
24-
removals: Vec<String>,
2524
additions: Vec<Addition>,
2625
src_archive: Option<Option<ArchiveRO>>,
2726
}
@@ -65,35 +64,11 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
6564
sess,
6665
dst: output.to_path_buf(),
6766
src: input.map(|p| p.to_path_buf()),
68-
removals: Vec::new(),
6967
additions: Vec::new(),
7068
src_archive: None,
7169
}
7270
}
7371

74-
/// Removes a file from this archive
75-
fn remove_file(&mut self, file: &str) {
76-
self.removals.push(file.to_string());
77-
}
78-
79-
/// Lists all files in an archive
80-
fn src_files(&mut self) -> Vec<String> {
81-
if self.src_archive().is_none() {
82-
return Vec::new();
83-
}
84-
85-
let archive = self.src_archive.as_ref().unwrap().as_ref().unwrap();
86-
87-
archive
88-
.iter()
89-
.filter_map(|child| child.ok())
90-
.filter(is_relevant_child)
91-
.filter_map(|child| child.name())
92-
.filter(|name| !self.removals.iter().any(|x| x == name))
93-
.map(|name| name.to_owned())
94-
.collect()
95-
}
96-
9772
fn add_archive<F>(&mut self, archive: &Path, skip: F) -> io::Result<()>
9873
where
9974
F: FnMut(&str) -> bool + 'static,
@@ -296,7 +271,6 @@ impl<'a> LlvmArchiveBuilder<'a> {
296271
}
297272

298273
fn build_with_llvm(&mut self, kind: ArchiveKind) -> io::Result<()> {
299-
let removals = mem::take(&mut self.removals);
300274
let mut additions = mem::take(&mut self.additions);
301275
let mut strings = Vec::new();
302276
let mut members = Vec::new();
@@ -308,9 +282,6 @@ impl<'a> LlvmArchiveBuilder<'a> {
308282
for child in archive.iter() {
309283
let child = child.map_err(string_to_io_error)?;
310284
let Some(child_name) = child.name() else { continue };
311-
if removals.iter().any(|r| r == child_name) {
312-
continue;
313-
}
314285

315286
let name = CString::new(child_name)?;
316287
members.push(llvm::LLVMRustArchiveMemberNew(

compiler/rustc_codegen_ssa/src/back/archive.rs

-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ pub trait ArchiveBuilder<'a> {
4545
fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self;
4646

4747
fn add_file(&mut self, path: &Path);
48-
fn remove_file(&mut self, name: &str);
49-
fn src_files(&mut self) -> Vec<String>;
5048

5149
fn add_archive<F>(&mut self, archive: &Path, skip: F) -> io::Result<()>
5250
where

compiler/rustc_codegen_ssa/src/back/link.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -2466,17 +2466,19 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
24662466
let name = &name[3..name.len() - 5]; // chop off lib/.rlib
24672467

24682468
sess.prof.generic_activity_with_arg("link_altering_rlib", name).run(|| {
2469-
let mut archive = <B as ArchiveBuilder>::new(sess, &dst, Some(cratepath));
2470-
2471-
let mut any_objects = false;
2472-
for f in archive.src_files() {
2469+
let canonical_name = name.replace('-', "_");
2470+
let upstream_rust_objects_already_included =
2471+
are_upstream_rust_objects_already_included(sess);
2472+
let is_builtins = sess.target.no_builtins
2473+
|| !codegen_results.crate_info.is_no_builtins.contains(&cnum);
2474+
2475+
let mut archive = <B as ArchiveBuilder>::new(sess, &dst, None);
2476+
if let Err(e) = archive.add_archive(cratepath, move |f| {
24732477
if f == METADATA_FILENAME {
2474-
archive.remove_file(&f);
2475-
continue;
2478+
return true;
24762479
}
24772480

24782481
let canonical = f.replace('-', "_");
2479-
let canonical_name = name.replace('-', "_");
24802482

24812483
let is_rust_object =
24822484
canonical.starts_with(&canonical_name) && looks_like_rust_object_file(&f);
@@ -2490,20 +2492,16 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
24902492
// file, then we don't need the object file as it's part of the
24912493
// LTO module. Note that `#![no_builtins]` is excluded from LTO,
24922494
// though, so we let that object file slide.
2493-
let skip_because_lto = are_upstream_rust_objects_already_included(sess)
2494-
&& is_rust_object
2495-
&& (sess.target.no_builtins
2496-
|| !codegen_results.crate_info.is_no_builtins.contains(&cnum));
2495+
let skip_because_lto =
2496+
upstream_rust_objects_already_included && is_rust_object && is_builtins;
24972497

24982498
if skip_because_cfg_say_so || skip_because_lto {
2499-
archive.remove_file(&f);
2500-
} else {
2501-
any_objects = true;
2499+
return true;
25022500
}
2503-
}
25042501

2505-
if !any_objects {
2506-
return;
2502+
false
2503+
}) {
2504+
sess.fatal(&format!("failed to build archive from rlib: {}", e));
25072505
}
25082506
archive.build();
25092507
link_upstream(&dst);

0 commit comments

Comments
 (0)