Skip to content

Commit d082400

Browse files
committed
Make link functions generic over archive builder
1 parent 38524a9 commit d082400

File tree

3 files changed

+101
-84
lines changed

3 files changed

+101
-84
lines changed

src/librustc_codegen_llvm/back/archive.rs

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use std::str;
1010
use crate::llvm::archive_ro::{ArchiveRO, Child};
1111
use crate::llvm::{self, ArchiveKind};
1212
use rustc_codegen_ssa::{METADATA_FILENAME, RLIB_BYTECODE_EXTENSION};
13-
use rustc_codegen_ssa::back::archive::find_library;
13+
use rustc_codegen_ssa::back::archive::{ArchiveBuilder, find_library};
1414
use rustc::session::Session;
1515

16-
pub struct ArchiveConfig<'a> {
16+
struct ArchiveConfig<'a> {
1717
pub sess: &'a Session,
1818
pub dst: PathBuf,
1919
pub src: Option<PathBuf>,
@@ -22,7 +22,7 @@ pub struct ArchiveConfig<'a> {
2222

2323
/// Helper for adding many files to an archive.
2424
#[must_use = "must call build() to finish building the archive"]
25-
pub struct ArchiveBuilder<'a> {
25+
pub struct LlvmArchiveBuilder<'a> {
2626
config: ArchiveConfig<'a>,
2727
removals: Vec<String>,
2828
additions: Vec<Addition>,
@@ -48,11 +48,26 @@ fn is_relevant_child(c: &Child<'_>) -> bool {
4848
}
4949
}
5050

51-
impl<'a> ArchiveBuilder<'a> {
51+
fn archive_config<'a>(sess: &'a Session,
52+
output: &Path,
53+
input: Option<&Path>) -> ArchiveConfig<'a> {
54+
use rustc_codegen_ssa::back::link::archive_search_paths;
55+
ArchiveConfig {
56+
sess,
57+
dst: output.to_path_buf(),
58+
src: input.map(|p| p.to_path_buf()),
59+
lib_search_paths: archive_search_paths(sess),
60+
}
61+
}
62+
63+
impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
5264
/// Creates a new static archive, ready for modifying the archive specified
5365
/// by `config`.
54-
pub fn new(config: ArchiveConfig<'a>) -> ArchiveBuilder<'a> {
55-
ArchiveBuilder {
66+
fn new(sess: &'a Session,
67+
output: &Path,
68+
input: Option<&Path>) -> LlvmArchiveBuilder<'a> {
69+
let config = archive_config(sess, output, input);
70+
LlvmArchiveBuilder {
5671
config,
5772
removals: Vec::new(),
5873
additions: Vec::new(),
@@ -62,12 +77,12 @@ impl<'a> ArchiveBuilder<'a> {
6277
}
6378

6479
/// Removes a file from this archive
65-
pub fn remove_file(&mut self, file: &str) {
80+
fn remove_file(&mut self, file: &str) {
6681
self.removals.push(file.to_string());
6782
}
6883

6984
/// Lists all files in an archive
70-
pub fn src_files(&mut self) -> Vec<String> {
85+
fn src_files(&mut self) -> Vec<String> {
7186
if self.src_archive().is_none() {
7287
return Vec::new()
7388
}
@@ -83,18 +98,9 @@ impl<'a> ArchiveBuilder<'a> {
8398
.collect()
8499
}
85100

86-
fn src_archive(&mut self) -> Option<&ArchiveRO> {
87-
if let Some(ref a) = self.src_archive {
88-
return a.as_ref()
89-
}
90-
let src = self.config.src.as_ref()?;
91-
self.src_archive = Some(ArchiveRO::open(src).ok());
92-
self.src_archive.as_ref().unwrap().as_ref()
93-
}
94-
95101
/// Adds all of the contents of a native library to this archive. This will
96102
/// search in the relevant locations for a library named `name`.
97-
pub fn add_native_library(&mut self, name: &str) {
103+
fn add_native_library(&mut self, name: &str) {
98104
let location = find_library(name, &self.config.lib_search_paths,
99105
self.config.sess);
100106
self.add_archive(&location, |_| false).unwrap_or_else(|e| {
@@ -108,7 +114,7 @@ impl<'a> ArchiveBuilder<'a> {
108114
///
109115
/// This ignores adding the bytecode from the rlib, and if LTO is enabled
110116
/// then the object file also isn't added.
111-
pub fn add_rlib(&mut self,
117+
fn add_rlib(&mut self,
112118
rlib: &Path,
113119
name: &str,
114120
lto: bool,
@@ -140,23 +146,8 @@ impl<'a> ArchiveBuilder<'a> {
140146
})
141147
}
142148

143-
fn add_archive<F>(&mut self, archive: &Path, skip: F)
144-
-> io::Result<()>
145-
where F: FnMut(&str) -> bool + 'static
146-
{
147-
let archive = match ArchiveRO::open(archive) {
148-
Ok(ar) => ar,
149-
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
150-
};
151-
self.additions.push(Addition::Archive {
152-
archive,
153-
skip: Box::new(skip),
154-
});
155-
Ok(())
156-
}
157-
158149
/// Adds an arbitrary file to this archive
159-
pub fn add_file(&mut self, file: &Path) {
150+
fn add_file(&mut self, file: &Path) {
160151
let name = file.file_name().unwrap().to_str().unwrap();
161152
self.additions.push(Addition::File {
162153
path: file.to_path_buf(),
@@ -166,13 +157,13 @@ impl<'a> ArchiveBuilder<'a> {
166157

167158
/// Indicate that the next call to `build` should update all symbols in
168159
/// the archive (equivalent to running 'ar s' over it).
169-
pub fn update_symbols(&mut self) {
160+
fn update_symbols(&mut self) {
170161
self.should_update_symbols = true;
171162
}
172163

173164
/// Combine the provided files, rlibs, and native libraries into a single
174165
/// `Archive`.
175-
pub fn build(&mut self) {
166+
fn build(mut self) {
176167
let kind = self.llvm_archive_kind().unwrap_or_else(|kind|
177168
self.config.sess.fatal(&format!("Don't know how to build archive of type: {}", kind)));
178169

@@ -181,6 +172,32 @@ impl<'a> ArchiveBuilder<'a> {
181172
}
182173

183174
}
175+
}
176+
177+
impl<'a> LlvmArchiveBuilder<'a> {
178+
fn src_archive(&mut self) -> Option<&ArchiveRO> {
179+
if let Some(ref a) = self.src_archive {
180+
return a.as_ref()
181+
}
182+
let src = self.config.src.as_ref()?;
183+
self.src_archive = Some(ArchiveRO::open(src).ok());
184+
self.src_archive.as_ref().unwrap().as_ref()
185+
}
186+
187+
fn add_archive<F>(&mut self, archive: &Path, skip: F)
188+
-> io::Result<()>
189+
where F: FnMut(&str) -> bool + 'static
190+
{
191+
let archive = match ArchiveRO::open(archive) {
192+
Ok(ar) => ar,
193+
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
194+
};
195+
self.additions.push(Addition::Archive {
196+
archive,
197+
skip: Box::new(skip),
198+
});
199+
Ok(())
200+
}
184201

185202
fn llvm_archive_kind(&self) -> Result<ArchiveKind, &str> {
186203
let kind = &*self.config.sess.target.target.options.archive_format;

0 commit comments

Comments
 (0)