]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/archive.rs
Rollup merge of #99741 - compiler-errors:copy-impl-impl-generics, r=fee1-dead
[rust.git] / compiler / rustc_codegen_gcc / src / archive.rs
1 use std::fs::File;
2 use std::path::{Path, PathBuf};
3
4 use rustc_codegen_ssa::back::archive::ArchiveBuilder;
5 use rustc_session::Session;
6
7 use rustc_session::cstore::DllImport;
8
9 struct ArchiveConfig<'a> {
10     sess: &'a Session,
11     dst: PathBuf,
12     use_native_ar: bool,
13     use_gnu_style_archive: bool,
14 }
15
16 #[derive(Debug)]
17 enum ArchiveEntry {
18     FromArchive {
19         archive_index: usize,
20         entry_index: usize,
21     },
22     File(PathBuf),
23 }
24
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)>,
31 }
32
33 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
34     fn new(sess: &'a Session, output: &Path) -> Self {
35         let config = ArchiveConfig {
36             sess,
37             dst: output.to_path_buf(),
38             use_native_ar: false,
39             // FIXME test for linux and System V derivatives instead
40             use_gnu_style_archive: sess.target.options.archive_format == "gnu",
41         };
42
43         ArArchiveBuilder {
44             config,
45             src_archives: vec![],
46             entries: vec![],
47         }
48     }
49
50     fn add_file(&mut self, file: &Path) {
51         self.entries.push((
52             file.file_name().unwrap().to_str().unwrap().to_string(),
53             ArchiveEntry::File(file.to_owned()),
54         ));
55     }
56
57     fn add_archive<F>(&mut self, archive_path: &Path, mut skip: F) -> std::io::Result<()>
58     where
59         F: FnMut(&str) -> bool + 'static,
60     {
61         let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
62         let archive_index = self.src_archives.len();
63
64         let mut i = 0;
65         while let Some(entry) = archive.next_entry() {
66             let entry = 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) {
70                 self.entries
71                     .push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
72             }
73             i += 1;
74         }
75
76         self.src_archives.push((archive_path.to_owned(), archive));
77         Ok(())
78     }
79
80     fn build(mut self) -> bool {
81         use std::process::Command;
82
83         fn add_file_using_ar(archive: &Path, file: &Path) {
84             Command::new("ar")
85                 .arg("r") // add or replace file
86                 .arg("-c") // silence created file message
87                 .arg(archive)
88                 .arg(&file)
89                 .status()
90                 .unwrap();
91         }
92
93         enum BuilderKind<'a> {
94             Bsd(ar::Builder<File>),
95             Gnu(ar::GnuBuilder<File>),
96             NativeAr(&'a Path),
97         }
98
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(),
104                 self.entries
105                     .iter()
106                     .map(|(name, _)| name.as_bytes().to_vec())
107                     .collect(),
108             ))
109         } else {
110             BuilderKind::Bsd(ar::Builder::new(File::create(&self.config.dst).unwrap()))
111         };
112
113         let any_members = !self.entries.is_empty();
114
115         // Add all files
116         for (entry_name, entry) in self.entries.into_iter() {
117             match entry {
118                 ArchiveEntry::FromArchive {
119                     archive_index,
120                     entry_index,
121                 } => {
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();
126
127                     match builder {
128                         BuilderKind::Bsd(ref mut builder) => {
129                             builder.append(&header, entry).unwrap()
130                         }
131                         BuilderKind::Gnu(ref mut builder) => {
132                             builder.append(&header, entry).unwrap()
133                         }
134                         BuilderKind::NativeAr(archive_file) => {
135                             Command::new("ar")
136                                 .arg("x")
137                                 .arg(src_archive_path)
138                                 .arg(&entry_name)
139                                 .status()
140                                 .unwrap();
141                             add_file_using_ar(archive_file, Path::new(&entry_name));
142                             std::fs::remove_file(entry_name).unwrap();
143                         }
144                     }
145                 }
146                 ArchiveEntry::File(file) =>
147                     match builder {
148                         BuilderKind::Bsd(ref mut builder) => {
149                             builder
150                                 .append_file(entry_name.as_bytes(), &mut File::open(file).expect("file for bsd builder"))
151                                 .unwrap()
152                         },
153                         BuilderKind::Gnu(ref mut builder) => {
154                             builder
155                                 .append_file(entry_name.as_bytes(), &mut File::open(&file).expect(&format!("file {:?} for gnu builder", file)))
156                                 .unwrap()
157                         },
158                         BuilderKind::NativeAr(archive_file) => add_file_using_ar(archive_file, &file),
159                     },
160             }
161         }
162
163         // Finalize archive
164         std::mem::drop(builder);
165
166         // Run ranlib to be able to link the archive
167         let status = std::process::Command::new("ranlib")
168             .arg(self.config.dst)
169             .status()
170             .expect("Couldn't run ranlib");
171
172         if !status.success() {
173             self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
174         }
175
176         any_members
177     }
178
179     fn sess(&self) -> &Session {
180         self.config.sess
181     }
182
183     fn create_dll_import_lib(
184         _sess: &Session,
185         _lib_name: &str,
186         _dll_imports: &[DllImport],
187         _tmpdir: &Path,
188     ) -> PathBuf {
189         unimplemented!();
190     }
191 }