2 use std::path::{Path, PathBuf};
4 use rustc_codegen_ssa::back::archive::ArchiveBuilder;
5 use rustc_session::Session;
7 use rustc_session::cstore::DllImport;
9 struct ArchiveConfig<'a> {
13 use_gnu_style_archive: bool,
25 pub struct ArArchiveBuilder<'a> {
26 config: ArchiveConfig<'a>,
27 src_archives: Vec<(PathBuf, ar::Archive<File>)>,
28 // Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
29 // the end of an archive for linkers to not get confused.
30 entries: Vec<(String, ArchiveEntry)>,
33 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
34 fn new(sess: &'a Session, output: &Path) -> Self {
35 let config = ArchiveConfig {
37 dst: output.to_path_buf(),
39 // FIXME test for linux and System V derivatives instead
40 use_gnu_style_archive: sess.target.options.archive_format == "gnu",
50 fn add_file(&mut self, file: &Path) {
52 file.file_name().unwrap().to_str().unwrap().to_string(),
53 ArchiveEntry::File(file.to_owned()),
57 fn add_archive<F>(&mut self, archive_path: &Path, mut skip: F) -> std::io::Result<()>
59 F: FnMut(&str) -> bool + 'static,
61 let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
62 let archive_index = self.src_archives.len();
65 while let Some(entry) = archive.next_entry() {
67 let file_name = String::from_utf8(entry.header().identifier().to_vec())
68 .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
69 if !skip(&file_name) {
71 .push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
76 self.src_archives.push((archive_path.to_owned(), archive));
80 fn build(mut self) -> bool {
81 use std::process::Command;
83 fn add_file_using_ar(archive: &Path, file: &Path) {
85 .arg("r") // add or replace file
86 .arg("-c") // silence created file message
93 enum BuilderKind<'a> {
94 Bsd(ar::Builder<File>),
95 Gnu(ar::GnuBuilder<File>),
99 let mut builder = if self.config.use_native_ar {
100 BuilderKind::NativeAr(&self.config.dst)
101 } else if self.config.use_gnu_style_archive {
102 BuilderKind::Gnu(ar::GnuBuilder::new(
103 File::create(&self.config.dst).unwrap(),
106 .map(|(name, _)| name.as_bytes().to_vec())
110 BuilderKind::Bsd(ar::Builder::new(File::create(&self.config.dst).unwrap()))
113 let any_members = !self.entries.is_empty();
116 for (entry_name, entry) in self.entries.into_iter() {
118 ArchiveEntry::FromArchive {
122 let (ref src_archive_path, ref mut src_archive) =
123 self.src_archives[archive_index];
124 let entry = src_archive.jump_to_entry(entry_index).unwrap();
125 let header = entry.header().clone();
128 BuilderKind::Bsd(ref mut builder) => {
129 builder.append(&header, entry).unwrap()
131 BuilderKind::Gnu(ref mut builder) => {
132 builder.append(&header, entry).unwrap()
134 BuilderKind::NativeAr(archive_file) => {
137 .arg(src_archive_path)
141 add_file_using_ar(archive_file, Path::new(&entry_name));
142 std::fs::remove_file(entry_name).unwrap();
146 ArchiveEntry::File(file) =>
148 BuilderKind::Bsd(ref mut builder) => {
150 .append_file(entry_name.as_bytes(), &mut File::open(file).expect("file for bsd builder"))
153 BuilderKind::Gnu(ref mut builder) => {
155 .append_file(entry_name.as_bytes(), &mut File::open(&file).expect(&format!("file {:?} for gnu builder", file)))
158 BuilderKind::NativeAr(archive_file) => add_file_using_ar(archive_file, &file),
164 std::mem::drop(builder);
166 // Run ranlib to be able to link the archive
167 let status = std::process::Command::new("ranlib")
168 .arg(self.config.dst)
170 .expect("Couldn't run ranlib");
172 if !status.success() {
173 self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
179 fn sess(&self) -> &Session {
183 fn create_dll_import_lib(
186 _dll_imports: &[DllImport],