2 use std::path::{Path, PathBuf};
4 use rustc_session::Session;
5 use rustc_codegen_ssa::back::archive::ArchiveBuilder;
7 use rustc_data_structures::temp_dir::MaybeTempDir;
8 use rustc_middle::middle::cstore::DllImport;
11 struct ArchiveConfig<'a> {
15 use_gnu_style_archive: bool,
27 pub struct ArArchiveBuilder<'a> {
28 config: ArchiveConfig<'a>,
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)>,
35 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
36 fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self {
37 let config = ArchiveConfig {
39 dst: output.to_path_buf(),
41 // FIXME test for linux and System V derivatives instead
42 use_gnu_style_archive: sess.target.options.archive_format == "gnu",
45 let (src_archives, entries) = if let Some(input) = input {
46 let mut archive = ar::Archive::new(File::open(input).unwrap());
47 let mut entries = Vec::new();
50 while let Some(entry) = archive.next_entry() {
51 let entry = entry.unwrap();
53 String::from_utf8(entry.header().identifier().to_vec()).unwrap(),
54 ArchiveEntry::FromArchive {
62 (vec![(input.to_owned(), archive)], entries)
74 fn src_files(&mut self) -> Vec<String> {
75 self.entries.iter().map(|(name, _)| name.clone()).collect()
78 fn remove_file(&mut self, name: &str) {
82 .position(|(entry_name, _)| entry_name == name)
83 .expect("Tried to remove file not existing in src archive");
84 self.entries.remove(index);
87 fn add_file(&mut self, file: &Path) {
89 file.file_name().unwrap().to_str().unwrap().to_string(),
90 ArchiveEntry::File(file.to_owned()),
94 fn add_archive<F>(&mut self, archive_path: &Path, mut skip: F) -> std::io::Result<()>
96 F: FnMut(&str) -> bool + 'static,
98 let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
99 let archive_index = self.src_archives.len();
102 while let Some(entry) = archive.next_entry() {
104 let file_name = String::from_utf8(entry.header().identifier().to_vec())
105 .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
106 if !skip(&file_name) {
108 .push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
113 self.src_archives.push((archive_path.to_owned(), archive));
117 fn update_symbols(&mut self) {
121 use std::process::Command;
123 fn add_file_using_ar(archive: &Path, file: &Path) {
125 .arg("r") // add or replace file
126 .arg("-c") // silence created file message
133 enum BuilderKind<'a> {
134 Bsd(ar::Builder<File>),
135 Gnu(ar::GnuBuilder<File>),
139 let mut builder = if self.config.use_native_ar {
140 BuilderKind::NativeAr(&self.config.dst)
141 } else if self.config.use_gnu_style_archive {
142 BuilderKind::Gnu(ar::GnuBuilder::new(
143 File::create(&self.config.dst).unwrap(),
146 .map(|(name, _)| name.as_bytes().to_vec())
150 BuilderKind::Bsd(ar::Builder::new(File::create(&self.config.dst).unwrap()))
154 for (entry_name, entry) in self.entries.into_iter() {
156 ArchiveEntry::FromArchive {
160 let (ref src_archive_path, ref mut src_archive) =
161 self.src_archives[archive_index];
162 let entry = src_archive.jump_to_entry(entry_index).unwrap();
163 let header = entry.header().clone();
166 BuilderKind::Bsd(ref mut builder) => {
167 builder.append(&header, entry).unwrap()
169 BuilderKind::Gnu(ref mut builder) => {
170 builder.append(&header, entry).unwrap()
172 BuilderKind::NativeAr(archive_file) => {
175 .arg(src_archive_path)
179 add_file_using_ar(archive_file, Path::new(&entry_name));
180 std::fs::remove_file(entry_name).unwrap();
184 ArchiveEntry::File(file) =>
186 BuilderKind::Bsd(ref mut builder) => {
188 .append_file(entry_name.as_bytes(), &mut File::open(file).expect("file for bsd builder"))
191 BuilderKind::Gnu(ref mut builder) => {
193 .append_file(entry_name.as_bytes(), &mut File::open(&file).expect(&format!("file {:?} for gnu builder", file)))
196 BuilderKind::NativeAr(archive_file) => add_file_using_ar(archive_file, &file),
202 std::mem::drop(builder);
204 // Run ranlib to be able to link the archive
205 let status = std::process::Command::new("ranlib")
206 .arg(self.config.dst)
208 .expect("Couldn't run ranlib");
210 if !status.success() {
211 self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
215 fn inject_dll_import_lib(&mut self, _lib_name: &str, _dll_imports: &[DllImport], _tmpdir: &MaybeTempDir) {