2 use std::path::{Path, PathBuf};
4 use rustc_codegen_ssa::back::archive::{ArchiveBuilder, ArchiveBuilderBuilder};
5 use rustc_session::Session;
7 use rustc_session::cstore::DllImport;
9 struct ArchiveConfig<'a> {
12 use_gnu_style_archive: bool,
24 pub struct ArArchiveBuilderBuilder;
26 impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
27 fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a> {
28 let config = ArchiveConfig {
31 // FIXME test for linux and System V derivatives instead
32 use_gnu_style_archive: sess.target.options.archive_format == "gnu",
35 Box::new(ArArchiveBuilder {
42 fn create_dll_import_lib(
46 _dll_imports: &[DllImport],
53 pub struct ArArchiveBuilder<'a> {
54 config: ArchiveConfig<'a>,
55 src_archives: Vec<(PathBuf, ar::Archive<File>)>,
56 // Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
57 // the end of an archive for linkers to not get confused.
58 entries: Vec<(String, ArchiveEntry)>,
61 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
62 fn add_file(&mut self, file: &Path) {
64 file.file_name().unwrap().to_str().unwrap().to_string(),
65 ArchiveEntry::File(file.to_owned()),
72 mut skip: Box<dyn FnMut(&str) -> bool + 'static>,
73 ) -> std::io::Result<()> {
74 let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
75 let archive_index = self.src_archives.len();
78 while let Some(entry) = archive.next_entry() {
80 let file_name = String::from_utf8(entry.header().identifier().to_vec())
81 .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
82 if !skip(&file_name) {
84 .push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
89 self.src_archives.push((archive_path.to_owned(), archive));
93 fn build(mut self: Box<Self>, output: &Path) -> bool {
94 use std::process::Command;
96 fn add_file_using_ar(archive: &Path, file: &Path) {
98 .arg("r") // add or replace file
99 .arg("-c") // silence created file message
106 enum BuilderKind<'a> {
107 Bsd(ar::Builder<File>),
108 Gnu(ar::GnuBuilder<File>),
112 let mut builder = if self.config.use_native_ar {
113 BuilderKind::NativeAr(output)
114 } else if self.config.use_gnu_style_archive {
115 BuilderKind::Gnu(ar::GnuBuilder::new(
116 File::create(output).unwrap(),
119 .map(|(name, _)| name.as_bytes().to_vec())
123 BuilderKind::Bsd(ar::Builder::new(File::create(output).unwrap()))
126 let any_members = !self.entries.is_empty();
129 for (entry_name, entry) in self.entries.into_iter() {
131 ArchiveEntry::FromArchive {
135 let (ref src_archive_path, ref mut src_archive) =
136 self.src_archives[archive_index];
137 let entry = src_archive.jump_to_entry(entry_index).unwrap();
138 let header = entry.header().clone();
141 BuilderKind::Bsd(ref mut builder) => {
142 builder.append(&header, entry).unwrap()
144 BuilderKind::Gnu(ref mut builder) => {
145 builder.append(&header, entry).unwrap()
147 BuilderKind::NativeAr(archive_file) => {
150 .arg(src_archive_path)
154 add_file_using_ar(archive_file, Path::new(&entry_name));
155 std::fs::remove_file(entry_name).unwrap();
159 ArchiveEntry::File(file) =>
161 BuilderKind::Bsd(ref mut builder) => {
163 .append_file(entry_name.as_bytes(), &mut File::open(file).expect("file for bsd builder"))
166 BuilderKind::Gnu(ref mut builder) => {
168 .append_file(entry_name.as_bytes(), &mut File::open(&file).expect(&format!("file {:?} for gnu builder", file)))
171 BuilderKind::NativeAr(archive_file) => add_file_using_ar(archive_file, &file),
177 std::mem::drop(builder);
179 // Run ranlib to be able to link the archive
181 std::process::Command::new("ranlib").arg(output).status().expect("Couldn't run ranlib");
183 if !status.success() {
184 self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));