]> git.lizzy.rs Git - rust.git/blob - src/archive.rs
Implement more simd_reduce_* intrinsics
[rust.git] / src / archive.rs
1 //! Creation of ar archives like for the lib and staticlib crate type
2
3 use std::collections::BTreeMap;
4 use std::fs::File;
5 use std::path::{Path, PathBuf};
6
7 use rustc_codegen_ssa::back::archive::{find_library, ArchiveBuilder};
8 use rustc_codegen_ssa::METADATA_FILENAME;
9 use rustc_session::Session;
10
11 use object::{Object, ObjectSymbol, SymbolKind};
12
13 #[derive(Debug)]
14 enum ArchiveEntry {
15     FromArchive {
16         archive_index: usize,
17         entry_index: usize,
18     },
19     File(PathBuf),
20 }
21
22 pub(crate) struct ArArchiveBuilder<'a> {
23     sess: &'a Session,
24     dst: PathBuf,
25     lib_search_paths: Vec<PathBuf>,
26     use_gnu_style_archive: bool,
27     no_builtin_ranlib: bool,
28
29     src_archives: Vec<(PathBuf, ar::Archive<File>)>,
30     // Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
31     // the end of an archive for linkers to not get confused.
32     entries: Vec<(String, ArchiveEntry)>,
33     update_symbols: bool,
34 }
35
36 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
37     fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self {
38         use rustc_codegen_ssa::back::link::archive_search_paths;
39
40         let (src_archives, entries) = if let Some(input) = input {
41             let mut archive = ar::Archive::new(File::open(input).unwrap());
42             let mut entries = Vec::new();
43
44             let mut i = 0;
45             while let Some(entry) = archive.next_entry() {
46                 let entry = entry.unwrap();
47                 entries.push((
48                     String::from_utf8(entry.header().identifier().to_vec()).unwrap(),
49                     ArchiveEntry::FromArchive {
50                         archive_index: 0,
51                         entry_index: i,
52                     },
53                 ));
54                 i += 1;
55             }
56
57             (vec![(input.to_owned(), archive)], entries)
58         } else {
59             (vec![], Vec::new())
60         };
61
62         ArArchiveBuilder {
63             sess,
64             dst: output.to_path_buf(),
65             lib_search_paths: archive_search_paths(sess),
66             use_gnu_style_archive: sess.target.archive_format == "gnu",
67             // FIXME fix builtin ranlib on macOS
68             no_builtin_ranlib: sess.target.is_like_osx,
69
70             src_archives,
71             entries,
72             update_symbols: false,
73         }
74     }
75
76     fn src_files(&mut self) -> Vec<String> {
77         self.entries.iter().map(|(name, _)| name.clone()).collect()
78     }
79
80     fn remove_file(&mut self, name: &str) {
81         let index = self
82             .entries
83             .iter()
84             .position(|(entry_name, _)| entry_name == name)
85             .expect("Tried to remove file not existing in src archive");
86         self.entries.remove(index);
87     }
88
89     fn add_file(&mut self, file: &Path) {
90         self.entries.push((
91             file.file_name().unwrap().to_str().unwrap().to_string(),
92             ArchiveEntry::File(file.to_owned()),
93         ));
94     }
95
96     fn add_native_library(&mut self, name: rustc_span::symbol::Symbol) {
97         let location = find_library(name, &self.lib_search_paths, self.sess);
98         self.add_archive(location.clone(), |_| false)
99             .unwrap_or_else(|e| {
100                 panic!(
101                     "failed to add native library {}: {}",
102                     location.to_string_lossy(),
103                     e
104                 );
105             });
106     }
107
108     fn add_rlib(
109         &mut self,
110         rlib: &Path,
111         name: &str,
112         lto: bool,
113         skip_objects: bool,
114     ) -> std::io::Result<()> {
115         let obj_start = name.to_owned();
116
117         self.add_archive(rlib.to_owned(), move |fname: &str| {
118             // Ignore metadata files, no matter the name.
119             if fname == METADATA_FILENAME {
120                 return true;
121             }
122
123             // Don't include Rust objects if LTO is enabled
124             if lto && fname.starts_with(&obj_start) && fname.ends_with(".o") {
125                 return true;
126             }
127
128             // Otherwise if this is *not* a rust object and we're skipping
129             // objects then skip this file
130             if skip_objects && (!fname.starts_with(&obj_start) || !fname.ends_with(".o")) {
131                 return true;
132             }
133
134             // ok, don't skip this
135             false
136         })
137     }
138
139     fn update_symbols(&mut self) {
140         self.update_symbols = true;
141     }
142
143     fn build(mut self) {
144         enum BuilderKind {
145             Bsd(ar::Builder<File>),
146             Gnu(ar::GnuBuilder<File>),
147         }
148
149         let sess = self.sess;
150
151         let mut symbol_table = BTreeMap::new();
152
153         let mut entries = Vec::new();
154
155         for (entry_name, entry) in self.entries {
156             // FIXME only read the symbol table of the object files to avoid having to keep all
157             // object files in memory at once, or read them twice.
158             let data = match entry {
159                 ArchiveEntry::FromArchive {
160                     archive_index,
161                     entry_index,
162                 } => {
163                     // FIXME read symbols from symtab
164                     use std::io::Read;
165                     let (ref _src_archive_path, ref mut src_archive) =
166                         self.src_archives[archive_index];
167                     let mut entry = src_archive.jump_to_entry(entry_index).unwrap();
168                     let mut data = Vec::new();
169                     entry.read_to_end(&mut data).unwrap();
170                     data
171                 }
172                 ArchiveEntry::File(file) => std::fs::read(file).unwrap_or_else(|err| {
173                     sess.fatal(&format!(
174                         "error while reading object file during archive building: {}",
175                         err
176                     ));
177                 }),
178             };
179
180             if !self.no_builtin_ranlib {
181                 match object::File::parse(&data) {
182                     Ok(object) => {
183                         symbol_table.insert(
184                             entry_name.as_bytes().to_vec(),
185                             object
186                                 .symbols()
187                                 .filter_map(|symbol| {
188                                     if symbol.is_undefined()
189                                         || symbol.is_local()
190                                         || symbol.kind() != SymbolKind::Data
191                                             && symbol.kind() != SymbolKind::Text
192                                             && symbol.kind() != SymbolKind::Tls
193                                     {
194                                         None
195                                     } else {
196                                         symbol.name().map(|name| name.as_bytes().to_vec()).ok()
197                                     }
198                                 })
199                                 .collect::<Vec<_>>(),
200                         );
201                     }
202                     Err(err) => {
203                         let err = err.to_string();
204                         if err == "Unknown file magic" {
205                             // Not an object file; skip it.
206                         } else {
207                             sess.fatal(&format!(
208                                 "error parsing `{}` during archive creation: {}",
209                                 entry_name, err
210                             ));
211                         }
212                     }
213                 }
214             }
215
216             entries.push((entry_name, data));
217         }
218
219         let mut builder = if self.use_gnu_style_archive {
220             BuilderKind::Gnu(
221                 ar::GnuBuilder::new(
222                     File::create(&self.dst).unwrap_or_else(|err| {
223                         sess.fatal(&format!(
224                             "error opening destination during archive building: {}",
225                             err
226                         ));
227                     }),
228                     entries
229                         .iter()
230                         .map(|(name, _)| name.as_bytes().to_vec())
231                         .collect(),
232                     ar::GnuSymbolTableFormat::Size32,
233                     symbol_table,
234                 )
235                 .unwrap(),
236             )
237         } else {
238             BuilderKind::Bsd(
239                 ar::Builder::new(
240                     File::create(&self.dst).unwrap_or_else(|err| {
241                         sess.fatal(&format!(
242                             "error opening destination during archive building: {}",
243                             err
244                         ));
245                     }),
246                     symbol_table,
247                 )
248                 .unwrap(),
249             )
250         };
251
252         // Add all files
253         for (entry_name, data) in entries.into_iter() {
254             let header = ar::Header::new(entry_name.into_bytes(), data.len() as u64);
255             match builder {
256                 BuilderKind::Bsd(ref mut builder) => builder.append(&header, &mut &*data).unwrap(),
257                 BuilderKind::Gnu(ref mut builder) => builder.append(&header, &mut &*data).unwrap(),
258             }
259         }
260
261         // Finalize archive
262         std::mem::drop(builder);
263
264         if self.no_builtin_ranlib {
265             let ranlib = crate::toolchain::get_toolchain_binary(self.sess, "ranlib");
266
267             // Run ranlib to be able to link the archive
268             let status = std::process::Command::new(ranlib)
269                 .arg(self.dst)
270                 .status()
271                 .expect("Couldn't run ranlib");
272
273             if !status.success() {
274                 self.sess
275                     .fatal(&format!("Ranlib exited with code {:?}", status.code()));
276             }
277         }
278     }
279 }
280
281 impl<'a> ArArchiveBuilder<'a> {
282     fn add_archive<F>(&mut self, archive_path: PathBuf, mut skip: F) -> std::io::Result<()>
283     where
284         F: FnMut(&str) -> bool + 'static,
285     {
286         let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
287         let archive_index = self.src_archives.len();
288
289         let mut i = 0;
290         while let Some(entry) = archive.next_entry() {
291             let entry = entry?;
292             let file_name = String::from_utf8(entry.header().identifier().to_vec())
293                 .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
294             if !skip(&file_name) {
295                 self.entries.push((
296                     file_name,
297                     ArchiveEntry::FromArchive {
298                         archive_index,
299                         entry_index: i,
300                     },
301                 ));
302             }
303             i += 1;
304         }
305
306         self.src_archives.push((archive_path, archive));
307         Ok(())
308     }
309 }