2 use std::path::{Path, PathBuf};
4 use crate::errors::RanlibFailure;
6 use rustc_codegen_ssa::back::archive::{ArchiveBuilder, ArchiveBuilderBuilder};
7 use rustc_session::Session;
9 use rustc_session::cstore::DllImport;
11 struct ArchiveConfig<'a> {
14 use_gnu_style_archive: bool,
26 pub struct ArArchiveBuilderBuilder;
28 impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
29 fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a> {
30 let config = ArchiveConfig {
33 // FIXME test for linux and System V derivatives instead
34 use_gnu_style_archive: sess.target.options.archive_format == "gnu",
37 Box::new(ArArchiveBuilder {
44 fn create_dll_import_lib(
48 _dll_imports: &[DllImport],
50 _is_direct_dependency: bool,
56 pub struct ArArchiveBuilder<'a> {
57 config: ArchiveConfig<'a>,
58 src_archives: Vec<(PathBuf, ar::Archive<File>)>,
59 // Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
60 // the end of an archive for linkers to not get confused.
61 entries: Vec<(String, ArchiveEntry)>,
64 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
65 fn add_file(&mut self, file: &Path) {
67 file.file_name().unwrap().to_str().unwrap().to_string(),
68 ArchiveEntry::File(file.to_owned()),
75 mut skip: Box<dyn FnMut(&str) -> bool + 'static>,
76 ) -> std::io::Result<()> {
77 let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
78 let archive_index = self.src_archives.len();
81 while let Some(entry) = archive.next_entry() {
83 let file_name = String::from_utf8(entry.header().identifier().to_vec())
84 .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
85 if !skip(&file_name) {
87 .push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
92 self.src_archives.push((archive_path.to_owned(), archive));
96 fn build(mut self: Box<Self>, output: &Path) -> bool {
97 use std::process::Command;
99 fn add_file_using_ar(archive: &Path, file: &Path) {
101 .arg("r") // add or replace file
102 .arg("-c") // silence created file message
109 enum BuilderKind<'a> {
110 Bsd(ar::Builder<File>),
111 Gnu(ar::GnuBuilder<File>),
115 let mut builder = if self.config.use_native_ar {
116 BuilderKind::NativeAr(output)
117 } else if self.config.use_gnu_style_archive {
118 BuilderKind::Gnu(ar::GnuBuilder::new(
119 File::create(output).unwrap(),
122 .map(|(name, _)| name.as_bytes().to_vec())
126 BuilderKind::Bsd(ar::Builder::new(File::create(output).unwrap()))
129 let any_members = !self.entries.is_empty();
132 for (entry_name, entry) in self.entries.into_iter() {
134 ArchiveEntry::FromArchive {
138 let (ref src_archive_path, ref mut src_archive) =
139 self.src_archives[archive_index];
140 let entry = src_archive.jump_to_entry(entry_index).unwrap();
141 let header = entry.header().clone();
144 BuilderKind::Bsd(ref mut builder) => {
145 builder.append(&header, entry).unwrap()
147 BuilderKind::Gnu(ref mut builder) => {
148 builder.append(&header, entry).unwrap()
150 BuilderKind::NativeAr(archive_file) => {
153 .arg(src_archive_path)
157 add_file_using_ar(archive_file, Path::new(&entry_name));
158 std::fs::remove_file(entry_name).unwrap();
162 ArchiveEntry::File(file) =>
164 BuilderKind::Bsd(ref mut builder) => {
166 .append_file(entry_name.as_bytes(), &mut File::open(file).expect("file for bsd builder"))
169 BuilderKind::Gnu(ref mut builder) => {
171 .append_file(entry_name.as_bytes(), &mut File::open(&file).expect(&format!("file {:?} for gnu builder", file)))
174 BuilderKind::NativeAr(archive_file) => add_file_using_ar(archive_file, &file),
180 std::mem::drop(builder);
182 // Run ranlib to be able to link the archive
184 std::process::Command::new("ranlib").arg(output).status().expect("Couldn't run ranlib");
186 if !status.success() {
187 self.config.sess.emit_fatal(RanlibFailure::new(status.code()));