diff options
| author | bjorn3 <bjorn3@users.noreply.github.com> | 2019-03-30 12:59:40 +0100 |
|---|---|---|
| committer | bjorn3 <bjorn3@users.noreply.github.com> | 2019-04-20 13:21:40 +0200 |
| commit | d8bce01a04958337dc39b895655f8aa2e577b6d2 (patch) | |
| tree | 9e65e7563effc025a3e91e28618e791541d91c90 /src/librustc_codegen_llvm/back/archive.rs | |
| parent | 892c88b32f88777e9afcd3cdad6a0b2fa7ec526e (diff) | |
| download | rust-d8bce01a04958337dc39b895655f8aa2e577b6d2.tar.gz rust-d8bce01a04958337dc39b895655f8aa2e577b6d2.zip | |
Make link functions generic over archive builder
Diffstat (limited to 'src/librustc_codegen_llvm/back/archive.rs')
| -rw-r--r-- | src/librustc_codegen_llvm/back/archive.rs | 91 |
1 files changed, 54 insertions, 37 deletions
diff --git a/src/librustc_codegen_llvm/back/archive.rs b/src/librustc_codegen_llvm/back/archive.rs index 8de0f9ac980..e0e26e9af25 100644 --- a/src/librustc_codegen_llvm/back/archive.rs +++ b/src/librustc_codegen_llvm/back/archive.rs @@ -10,10 +10,10 @@ use std::str; use crate::llvm::archive_ro::{ArchiveRO, Child}; use crate::llvm::{self, ArchiveKind}; use rustc_codegen_ssa::{METADATA_FILENAME, RLIB_BYTECODE_EXTENSION}; -use rustc_codegen_ssa::back::archive::find_library; +use rustc_codegen_ssa::back::archive::{ArchiveBuilder, find_library}; use rustc::session::Session; -pub struct ArchiveConfig<'a> { +struct ArchiveConfig<'a> { pub sess: &'a Session, pub dst: PathBuf, pub src: Option<PathBuf>, @@ -22,7 +22,7 @@ pub struct ArchiveConfig<'a> { /// Helper for adding many files to an archive. #[must_use = "must call build() to finish building the archive"] -pub struct ArchiveBuilder<'a> { +pub struct LlvmArchiveBuilder<'a> { config: ArchiveConfig<'a>, removals: Vec<String>, additions: Vec<Addition>, @@ -48,11 +48,26 @@ fn is_relevant_child(c: &Child<'_>) -> bool { } } -impl<'a> ArchiveBuilder<'a> { +fn archive_config<'a>(sess: &'a Session, + output: &Path, + input: Option<&Path>) -> ArchiveConfig<'a> { + use rustc_codegen_ssa::back::link::archive_search_paths; + ArchiveConfig { + sess, + dst: output.to_path_buf(), + src: input.map(|p| p.to_path_buf()), + lib_search_paths: archive_search_paths(sess), + } +} + +impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> { /// Creates a new static archive, ready for modifying the archive specified /// by `config`. - pub fn new(config: ArchiveConfig<'a>) -> ArchiveBuilder<'a> { - ArchiveBuilder { + fn new(sess: &'a Session, + output: &Path, + input: Option<&Path>) -> LlvmArchiveBuilder<'a> { + let config = archive_config(sess, output, input); + LlvmArchiveBuilder { config, removals: Vec::new(), additions: Vec::new(), @@ -62,12 +77,12 @@ impl<'a> ArchiveBuilder<'a> { } /// Removes a file from this archive - pub fn remove_file(&mut self, file: &str) { + fn remove_file(&mut self, file: &str) { self.removals.push(file.to_string()); } /// Lists all files in an archive - pub fn src_files(&mut self) -> Vec<String> { + fn src_files(&mut self) -> Vec<String> { if self.src_archive().is_none() { return Vec::new() } @@ -83,18 +98,9 @@ impl<'a> ArchiveBuilder<'a> { .collect() } - fn src_archive(&mut self) -> Option<&ArchiveRO> { - if let Some(ref a) = self.src_archive { - return a.as_ref() - } - let src = self.config.src.as_ref()?; - self.src_archive = Some(ArchiveRO::open(src).ok()); - self.src_archive.as_ref().unwrap().as_ref() - } - /// Adds all of the contents of a native library to this archive. This will /// search in the relevant locations for a library named `name`. - pub fn add_native_library(&mut self, name: &str) { + fn add_native_library(&mut self, name: &str) { let location = find_library(name, &self.config.lib_search_paths, self.config.sess); self.add_archive(&location, |_| false).unwrap_or_else(|e| { @@ -108,7 +114,7 @@ impl<'a> ArchiveBuilder<'a> { /// /// This ignores adding the bytecode from the rlib, and if LTO is enabled /// then the object file also isn't added. - pub fn add_rlib(&mut self, + fn add_rlib(&mut self, rlib: &Path, name: &str, lto: bool, @@ -140,23 +146,8 @@ impl<'a> ArchiveBuilder<'a> { }) } - fn add_archive<F>(&mut self, archive: &Path, skip: F) - -> io::Result<()> - where F: FnMut(&str) -> bool + 'static - { - let archive = match ArchiveRO::open(archive) { - Ok(ar) => ar, - Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)), - }; - self.additions.push(Addition::Archive { - archive, - skip: Box::new(skip), - }); - Ok(()) - } - /// Adds an arbitrary file to this archive - pub fn add_file(&mut self, file: &Path) { + fn add_file(&mut self, file: &Path) { let name = file.file_name().unwrap().to_str().unwrap(); self.additions.push(Addition::File { path: file.to_path_buf(), @@ -166,13 +157,13 @@ impl<'a> ArchiveBuilder<'a> { /// Indicate that the next call to `build` should update all symbols in /// the archive (equivalent to running 'ar s' over it). - pub fn update_symbols(&mut self) { + fn update_symbols(&mut self) { self.should_update_symbols = true; } /// Combine the provided files, rlibs, and native libraries into a single /// `Archive`. - pub fn build(&mut self) { + fn build(mut self) { let kind = self.llvm_archive_kind().unwrap_or_else(|kind| self.config.sess.fatal(&format!("Don't know how to build archive of type: {}", kind))); @@ -181,6 +172,32 @@ impl<'a> ArchiveBuilder<'a> { } } +} + +impl<'a> LlvmArchiveBuilder<'a> { + fn src_archive(&mut self) -> Option<&ArchiveRO> { + if let Some(ref a) = self.src_archive { + return a.as_ref() + } + let src = self.config.src.as_ref()?; + self.src_archive = Some(ArchiveRO::open(src).ok()); + self.src_archive.as_ref().unwrap().as_ref() + } + + fn add_archive<F>(&mut self, archive: &Path, skip: F) + -> io::Result<()> + where F: FnMut(&str) -> bool + 'static + { + let archive = match ArchiveRO::open(archive) { + Ok(ar) => ar, + Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)), + }; + self.additions.push(Addition::Archive { + archive, + skip: Box::new(skip), + }); + Ok(()) + } fn llvm_archive_kind(&self) -> Result<ArchiveKind, &str> { let kind = &*self.config.sess.target.target.options.archive_format; |
