Skip to content

Commit 451817e

Browse files
committed
Introduce an ArchiveBuilderBuilder
This avoids monomorphizing all linker code for each codegen backend and will allow passing in extra information to the archive builder from the codegen backend.
1 parent 34b37e7 commit 451817e

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

Diff for: src/archive.rs

+33-27
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fs::File;
55
use std::io::{self, Read, Seek};
66
use std::path::{Path, PathBuf};
77

8-
use rustc_codegen_ssa::back::archive::ArchiveBuilder;
8+
use rustc_codegen_ssa::back::archive::{ArchiveBuilder, ArchiveBuilderBuilder};
99
use rustc_session::Session;
1010

1111
use object::read::archive::ArchiveFile;
@@ -17,6 +17,32 @@ enum ArchiveEntry {
1717
File(PathBuf),
1818
}
1919

20+
pub(crate) struct ArArchiveBuilderBuilder;
21+
22+
impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
23+
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a> {
24+
Box::new(ArArchiveBuilder {
25+
sess,
26+
use_gnu_style_archive: sess.target.archive_format == "gnu",
27+
// FIXME fix builtin ranlib on macOS
28+
no_builtin_ranlib: sess.target.is_like_osx,
29+
30+
src_archives: vec![],
31+
entries: vec![],
32+
})
33+
}
34+
35+
fn create_dll_import_lib(
36+
&self,
37+
_sess: &Session,
38+
_lib_name: &str,
39+
_dll_imports: &[rustc_session::cstore::DllImport],
40+
_tmpdir: &Path,
41+
) -> PathBuf {
42+
bug!("creating dll imports is not supported");
43+
}
44+
}
45+
2046
pub(crate) struct ArArchiveBuilder<'a> {
2147
sess: &'a Session,
2248
use_gnu_style_archive: bool,
@@ -29,29 +55,18 @@ pub(crate) struct ArArchiveBuilder<'a> {
2955
}
3056

3157
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
32-
fn new(sess: &'a Session) -> Self {
33-
ArArchiveBuilder {
34-
sess,
35-
use_gnu_style_archive: sess.target.archive_format == "gnu",
36-
// FIXME fix builtin ranlib on macOS
37-
no_builtin_ranlib: sess.target.is_like_osx,
38-
39-
src_archives: vec![],
40-
entries: vec![],
41-
}
42-
}
43-
4458
fn add_file(&mut self, file: &Path) {
4559
self.entries.push((
4660
file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(),
4761
ArchiveEntry::File(file.to_owned()),
4862
));
4963
}
5064

51-
fn add_archive<F>(&mut self, archive_path: &Path, mut skip: F) -> std::io::Result<()>
52-
where
53-
F: FnMut(&str) -> bool + 'static,
54-
{
65+
fn add_archive(
66+
&mut self,
67+
archive_path: &Path,
68+
mut skip: Box<dyn FnMut(&str) -> bool + 'static>,
69+
) -> std::io::Result<()> {
5570
let read_cache = ReadCache::new(std::fs::File::open(&archive_path)?);
5671
let archive = ArchiveFile::parse(&read_cache).unwrap();
5772
let archive_index = self.src_archives.len();
@@ -72,7 +87,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
7287
Ok(())
7388
}
7489

75-
fn build(mut self, output: &Path) -> bool {
90+
fn build(mut self: Box<Self>, output: &Path) -> bool {
7691
enum BuilderKind {
7792
Bsd(ar::Builder<File>),
7893
Gnu(ar::GnuBuilder<File>),
@@ -218,13 +233,4 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
218233

219234
any_members
220235
}
221-
222-
fn create_dll_import_lib(
223-
_sess: &Session,
224-
_lib_name: &str,
225-
_dll_imports: &[rustc_session::cstore::DllImport],
226-
_tmpdir: &Path,
227-
) -> PathBuf {
228-
bug!("creating dll imports is not supported");
229-
}
230236
}

Diff for: src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
226226
) -> Result<(), ErrorGuaranteed> {
227227
use rustc_codegen_ssa::back::link::link_binary;
228228

229-
link_binary::<crate::archive::ArArchiveBuilder<'_>>(sess, &codegen_results, outputs)
229+
link_binary(sess, &crate::archive::ArArchiveBuilderBuilder, &codegen_results, outputs)
230230
}
231231
}
232232

0 commit comments

Comments
 (0)