1 //! Creation of ar archives like for the lib and staticlib crate type
3 use std::collections::BTreeMap;
5 use std::io::{self, Read, Seek};
6 use std::path::{Path, PathBuf};
8 use rustc_codegen_ssa::back::archive::{ArchiveBuilder, ArchiveBuilderBuilder};
9 use rustc_session::Session;
11 use object::read::archive::ArchiveFile;
12 use object::{Object, ObjectSymbol, ReadCache};
16 FromArchive { archive_index: usize, file_range: (u64, u64) },
20 pub(crate) struct ArArchiveBuilderBuilder;
22 impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
23 fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a> {
24 Box::new(ArArchiveBuilder {
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,
35 fn create_dll_import_lib(
39 _dll_imports: &[rustc_session::cstore::DllImport],
42 bug!("creating dll imports is not supported");
46 pub(crate) struct ArArchiveBuilder<'a> {
48 use_gnu_style_archive: bool,
49 no_builtin_ranlib: bool,
51 src_archives: Vec<File>,
52 // Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
53 // the end of an archive for linkers to not get confused.
54 entries: Vec<(Vec<u8>, ArchiveEntry)>,
57 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
58 fn add_file(&mut self, file: &Path) {
60 file.file_name().unwrap().to_str().unwrap().to_string().into_bytes(),
61 ArchiveEntry::File(file.to_owned()),
68 mut skip: Box<dyn FnMut(&str) -> bool + 'static>,
69 ) -> std::io::Result<()> {
70 let read_cache = ReadCache::new(std::fs::File::open(&archive_path)?);
71 let archive = ArchiveFile::parse(&read_cache).unwrap();
72 let archive_index = self.src_archives.len();
74 for entry in archive.members() {
75 let entry = entry.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
76 let file_name = String::from_utf8(entry.name().to_vec())
77 .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
78 if !skip(&file_name) {
80 file_name.into_bytes(),
81 ArchiveEntry::FromArchive { archive_index, file_range: entry.file_range() },
86 self.src_archives.push(read_cache.into_inner());
90 fn build(mut self: Box<Self>, output: &Path) -> bool {
92 Bsd(ar::Builder<File>),
93 Gnu(ar::GnuBuilder<File>),
98 let mut symbol_table = BTreeMap::new();
100 let mut entries = Vec::new();
102 for (mut entry_name, entry) in self.entries {
103 // FIXME only read the symbol table of the object files to avoid having to keep all
104 // object files in memory at once, or read them twice.
105 let data = match entry {
106 ArchiveEntry::FromArchive { archive_index, file_range } => {
107 // FIXME read symbols from symtab
108 let src_read_cache = &mut self.src_archives[archive_index];
110 src_read_cache.seek(io::SeekFrom::Start(file_range.0)).unwrap();
111 let mut data = std::vec::from_elem(0, usize::try_from(file_range.1).unwrap());
112 src_read_cache.read_exact(&mut data).unwrap();
116 ArchiveEntry::File(file) => std::fs::read(file).unwrap_or_else(|err| {
118 "error while reading object file during archive building: {}",
124 if !self.no_builtin_ranlib {
125 if symbol_table.contains_key(&entry_name) {
126 // The ar crate can't handle creating a symbol table in case of multiple archive
127 // members with the same name. Work around this by prepending a number until we
128 // get a unique name.
130 let new_name = format!("{}_", i)
133 .chain(entry_name.iter().copied())
134 .collect::<Vec<_>>();
135 if !symbol_table.contains_key(&new_name) {
136 entry_name = new_name;
142 match object::File::parse(&*data) {
148 .filter_map(|symbol| {
149 if symbol.is_undefined() || symbol.is_local() {
152 symbol.name().map(|name| name.as_bytes().to_vec()).ok()
155 .collect::<Vec<_>>(),
159 let err = err.to_string();
160 if err == "Unknown file magic" {
161 // Not an object file; skip it.
164 "error parsing `{}` during archive creation: {}",
165 String::from_utf8_lossy(&entry_name),
173 entries.push((entry_name, data));
176 let mut builder = if self.use_gnu_style_archive {
179 File::create(output).unwrap_or_else(|err| {
181 "error opening destination during archive building: {}",
185 entries.iter().map(|(name, _)| name.clone()).collect(),
186 ar::GnuSymbolTableFormat::Size32,
194 File::create(output).unwrap_or_else(|err| {
196 "error opening destination during archive building: {}",
206 let any_members = !entries.is_empty();
209 for (entry_name, data) in entries.into_iter() {
210 let header = ar::Header::new(entry_name, data.len() as u64);
212 BuilderKind::Bsd(ref mut builder) => builder.append(&header, &mut &*data).unwrap(),
213 BuilderKind::Gnu(ref mut builder) => builder.append(&header, &mut &*data).unwrap(),
218 std::mem::drop(builder);
220 if self.no_builtin_ranlib {
221 let ranlib = crate::toolchain::get_toolchain_binary(self.sess, "ranlib");
223 // Run ranlib to be able to link the archive
224 let status = std::process::Command::new(ranlib)
227 .expect("Couldn't run ranlib");
229 if !status.success() {
230 self.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));