Skip to content

Commit 771c2c6

Browse files
committed
Auto merge of rust-lang#88559 - bjorn3:archive_logic_dedup, r=cjgillot
Move add_rlib and add_native_library to cg_ssa This deduplicates logic between codegen backends. cc `@antoyo` and `@khyperia` for cg_gcc and rust-gpu.
2 parents e7e9303 + 677c786 commit 771c2c6

File tree

1 file changed

+19
-64
lines changed

1 file changed

+19
-64
lines changed

src/archive.rs

+19-64
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use std::collections::BTreeMap;
44
use std::fs::File;
55
use std::path::{Path, PathBuf};
66

7-
use rustc_codegen_ssa::back::archive::{find_library, ArchiveBuilder};
8-
use rustc_codegen_ssa::METADATA_FILENAME;
7+
use rustc_codegen_ssa::back::archive::ArchiveBuilder;
98
use rustc_session::Session;
109

1110
use object::{Object, ObjectSymbol, SymbolKind};
@@ -19,7 +18,6 @@ enum ArchiveEntry {
1918
pub(crate) struct ArArchiveBuilder<'a> {
2019
sess: &'a Session,
2120
dst: PathBuf,
22-
lib_search_paths: Vec<PathBuf>,
2321
use_gnu_style_archive: bool,
2422
no_builtin_ranlib: bool,
2523

@@ -31,8 +29,6 @@ pub(crate) struct ArArchiveBuilder<'a> {
3129

3230
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
3331
fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self {
34-
use rustc_codegen_ssa::back::link::archive_search_paths;
35-
3632
let (src_archives, entries) = if let Some(input) = input {
3733
let mut archive = ar::Archive::new(File::open(input).unwrap());
3834
let mut entries = Vec::new();
@@ -55,7 +51,6 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
5551
ArArchiveBuilder {
5652
sess,
5753
dst: output.to_path_buf(),
58-
lib_search_paths: archive_search_paths(sess),
5954
use_gnu_style_archive: sess.target.archive_format == "gnu",
6055
// FIXME fix builtin ranlib on macOS
6156
no_builtin_ranlib: sess.target.is_like_osx,
@@ -85,42 +80,27 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
8580
));
8681
}
8782

88-
fn add_native_library(&mut self, name: rustc_span::symbol::Symbol, verbatim: bool) {
89-
let location = find_library(name, verbatim, &self.lib_search_paths, self.sess);
90-
self.add_archive(location.clone(), |_| false).unwrap_or_else(|e| {
91-
panic!("failed to add native library {}: {}", location.to_string_lossy(), e);
92-
});
93-
}
94-
95-
fn add_rlib(
96-
&mut self,
97-
rlib: &Path,
98-
name: &str,
99-
lto: bool,
100-
skip_objects: bool,
101-
) -> std::io::Result<()> {
102-
let obj_start = name.to_owned();
103-
104-
self.add_archive(rlib.to_owned(), move |fname: &str| {
105-
// Ignore metadata files, no matter the name.
106-
if fname == METADATA_FILENAME {
107-
return true;
108-
}
109-
110-
// Don't include Rust objects if LTO is enabled
111-
if lto && fname.starts_with(&obj_start) && fname.ends_with(".o") {
112-
return true;
113-
}
83+
fn add_archive<F>(&mut self, archive_path: &Path, mut skip: F) -> std::io::Result<()>
84+
where
85+
F: FnMut(&str) -> bool + 'static,
86+
{
87+
let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
88+
let archive_index = self.src_archives.len();
11489

115-
// Otherwise if this is *not* a rust object and we're skipping
116-
// objects then skip this file
117-
if skip_objects && (!fname.starts_with(&obj_start) || !fname.ends_with(".o")) {
118-
return true;
90+
let mut i = 0;
91+
while let Some(entry) = archive.next_entry() {
92+
let entry = entry?;
93+
let file_name = String::from_utf8(entry.header().identifier().to_vec())
94+
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
95+
if !skip(&file_name) {
96+
self.entries
97+
.push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
11998
}
99+
i += 1;
100+
}
120101

121-
// ok, don't skip this
122-
false
123-
})
102+
self.src_archives.push((archive_path.to_owned(), archive));
103+
Ok(())
124104
}
125105

126106
fn update_symbols(&mut self) {}
@@ -264,28 +244,3 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
264244
bug!("injecting dll imports is not supported");
265245
}
266246
}
267-
268-
impl<'a> ArArchiveBuilder<'a> {
269-
fn add_archive<F>(&mut self, archive_path: PathBuf, mut skip: F) -> std::io::Result<()>
270-
where
271-
F: FnMut(&str) -> bool + 'static,
272-
{
273-
let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
274-
let archive_index = self.src_archives.len();
275-
276-
let mut i = 0;
277-
while let Some(entry) = archive.next_entry() {
278-
let entry = entry?;
279-
let file_name = String::from_utf8(entry.header().identifier().to_vec())
280-
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
281-
if !skip(&file_name) {
282-
self.entries
283-
.push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
284-
}
285-
i += 1;
286-
}
287-
288-
self.src_archives.push((archive_path, archive));
289-
Ok(())
290-
}
291-
}

0 commit comments

Comments
 (0)