2 use std::path::{Path, PathBuf};
4 use rustc_codegen_ssa::back::archive::ArchiveBuilder;
5 use rustc_session::Session;
7 use rustc_data_structures::temp_dir::MaybeTempDir;
8 use rustc_session::cstore::DllImport;
10 struct ArchiveConfig<'a> {
14 use_gnu_style_archive: bool,
26 pub struct ArArchiveBuilder<'a> {
27 config: ArchiveConfig<'a>,
28 src_archives: Vec<(PathBuf, ar::Archive<File>)>,
29 // Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
30 // the end of an archive for linkers to not get confused.
31 entries: Vec<(String, ArchiveEntry)>,
34 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
35 fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self {
36 let config = ArchiveConfig {
38 dst: output.to_path_buf(),
40 // FIXME test for linux and System V derivatives instead
41 use_gnu_style_archive: sess.target.options.archive_format == "gnu",
44 let (src_archives, entries) = if let Some(input) = input {
45 let mut archive = ar::Archive::new(File::open(input).unwrap());
46 let mut entries = Vec::new();
49 while let Some(entry) = archive.next_entry() {
50 let entry = entry.unwrap();
52 String::from_utf8(entry.header().identifier().to_vec()).unwrap(),
53 ArchiveEntry::FromArchive {
61 (vec![(input.to_owned(), archive)], entries)
73 fn src_files(&mut self) -> Vec<String> {
74 self.entries.iter().map(|(name, _)| name.clone()).collect()
77 fn remove_file(&mut self, name: &str) {
81 .position(|(entry_name, _)| entry_name == name)
82 .expect("Tried to remove file not existing in src archive");
83 self.entries.remove(index);
86 fn add_file(&mut self, file: &Path) {
88 file.file_name().unwrap().to_str().unwrap().to_string(),
89 ArchiveEntry::File(file.to_owned()),
93 fn add_archive<F>(&mut self, archive_path: &Path, mut skip: F) -> std::io::Result<()>
95 F: FnMut(&str) -> bool + 'static,
97 let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
98 let archive_index = self.src_archives.len();
101 while let Some(entry) = archive.next_entry() {
103 let file_name = String::from_utf8(entry.header().identifier().to_vec())
104 .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
105 if !skip(&file_name) {
107 .push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
112 self.src_archives.push((archive_path.to_owned(), archive));
116 fn update_symbols(&mut self) {
120 use std::process::Command;
122 fn add_file_using_ar(archive: &Path, file: &Path) {
124 .arg("r") // add or replace file
125 .arg("-c") // silence created file message
132 enum BuilderKind<'a> {
133 Bsd(ar::Builder<File>),
134 Gnu(ar::GnuBuilder<File>),
138 let mut builder = if self.config.use_native_ar {
139 BuilderKind::NativeAr(&self.config.dst)
140 } else if self.config.use_gnu_style_archive {
141 BuilderKind::Gnu(ar::GnuBuilder::new(
142 File::create(&self.config.dst).unwrap(),
145 .map(|(name, _)| name.as_bytes().to_vec())
149 BuilderKind::Bsd(ar::Builder::new(File::create(&self.config.dst).unwrap()))
153 for (entry_name, entry) in self.entries.into_iter() {
155 ArchiveEntry::FromArchive {
159 let (ref src_archive_path, ref mut src_archive) =
160 self.src_archives[archive_index];
161 let entry = src_archive.jump_to_entry(entry_index).unwrap();
162 let header = entry.header().clone();
165 BuilderKind::Bsd(ref mut builder) => {
166 builder.append(&header, entry).unwrap()
168 BuilderKind::Gnu(ref mut builder) => {
169 builder.append(&header, entry).unwrap()
171 BuilderKind::NativeAr(archive_file) => {
174 .arg(src_archive_path)
178 add_file_using_ar(archive_file, Path::new(&entry_name));
179 std::fs::remove_file(entry_name).unwrap();
183 ArchiveEntry::File(file) =>
185 BuilderKind::Bsd(ref mut builder) => {
187 .append_file(entry_name.as_bytes(), &mut File::open(file).expect("file for bsd builder"))
190 BuilderKind::Gnu(ref mut builder) => {
192 .append_file(entry_name.as_bytes(), &mut File::open(&file).expect(&format!("file {:?} for gnu builder", file)))
195 BuilderKind::NativeAr(archive_file) => add_file_using_ar(archive_file, &file),
201 std::mem::drop(builder);
203 // Run ranlib to be able to link the archive
204 let status = std::process::Command::new("ranlib")
205 .arg(self.config.dst)
207 .expect("Couldn't run ranlib");
209 if !status.success() {
210 self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
214 fn inject_dll_import_lib(&mut self, _lib_name: &str, _dll_imports: &[DllImport], _tmpdir: &MaybeTempDir) {