Skip to content

Commit 3a8ff61

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 56f3486 commit 3a8ff61

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

src/archive.rs

+32-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fs::File;
22
use std::path::{Path, PathBuf};
33

4-
use rustc_codegen_ssa::back::archive::ArchiveBuilder;
4+
use rustc_codegen_ssa::back::archive::{ArchiveBuilder, ArchiveBuilderBuilder};
55
use rustc_session::Session;
66

77
use rustc_session::cstore::DllImport;
@@ -21,41 +21,56 @@ enum ArchiveEntry {
2121
File(PathBuf),
2222
}
2323

24-
pub struct ArArchiveBuilder<'a> {
25-
config: ArchiveConfig<'a>,
26-
src_archives: Vec<(PathBuf, ar::Archive<File>)>,
27-
// Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
28-
// the end of an archive for linkers to not get confused.
29-
entries: Vec<(String, ArchiveEntry)>,
30-
}
24+
pub struct ArArchiveBuilderBuilder;
3125

32-
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
33-
fn new(sess: &'a Session) -> Self {
26+
impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
27+
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a> {
3428
let config = ArchiveConfig {
3529
sess,
3630
use_native_ar: false,
3731
// FIXME test for linux and System V derivatives instead
3832
use_gnu_style_archive: sess.target.options.archive_format == "gnu",
3933
};
4034

41-
ArArchiveBuilder {
35+
Box::new(ArArchiveBuilder {
4236
config,
4337
src_archives: vec![],
4438
entries: vec![],
45-
}
39+
})
4640
}
4741

42+
fn create_dll_import_lib(
43+
&self,
44+
_sess: &Session,
45+
_lib_name: &str,
46+
_dll_imports: &[DllImport],
47+
_tmpdir: &Path,
48+
) -> PathBuf {
49+
unimplemented!();
50+
}
51+
}
52+
53+
pub struct ArArchiveBuilder<'a> {
54+
config: ArchiveConfig<'a>,
55+
src_archives: Vec<(PathBuf, ar::Archive<File>)>,
56+
// Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
57+
// the end of an archive for linkers to not get confused.
58+
entries: Vec<(String, ArchiveEntry)>,
59+
}
60+
61+
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
4862
fn add_file(&mut self, file: &Path) {
4963
self.entries.push((
5064
file.file_name().unwrap().to_str().unwrap().to_string(),
5165
ArchiveEntry::File(file.to_owned()),
5266
));
5367
}
5468

55-
fn add_archive<F>(&mut self, archive_path: &Path, mut skip: F) -> std::io::Result<()>
56-
where
57-
F: FnMut(&str) -> bool + 'static,
58-
{
69+
fn add_archive(
70+
&mut self,
71+
archive_path: &Path,
72+
mut skip: Box<dyn FnMut(&str) -> bool + 'static>,
73+
) -> std::io::Result<()> {
5974
let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
6075
let archive_index = self.src_archives.len();
6176

@@ -75,7 +90,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
7590
Ok(())
7691
}
7792

78-
fn build(mut self, output: &Path) -> bool {
93+
fn build(mut self: Box<Self>, output: &Path) -> bool {
7994
use std::process::Command;
8095

8196
fn add_file_using_ar(archive: &Path, file: &Path) {
@@ -171,13 +186,4 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
171186

172187
any_members
173188
}
174-
175-
fn create_dll_import_lib(
176-
_sess: &Session,
177-
_lib_name: &str,
178-
_dll_imports: &[DllImport],
179-
_tmpdir: &Path,
180-
) -> PathBuf {
181-
unimplemented!();
182-
}
183189
}

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ impl CodegenBackend for GccCodegenBackend {
133133
fn link(&self, sess: &Session, codegen_results: CodegenResults, outputs: &OutputFilenames) -> Result<(), ErrorGuaranteed> {
134134
use rustc_codegen_ssa::back::link::link_binary;
135135

136-
link_binary::<crate::archive::ArArchiveBuilder<'_>>(
136+
link_binary(
137137
sess,
138+
&crate::archive::ArArchiveBuilderBuilder,
138139
&codegen_results,
139140
outputs,
140141
)

0 commit comments

Comments
 (0)