]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/archive.rs
Rollup merge of #97805 - coolreader18:trace-suggestions, r=oli-obk
[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_data_structures::temp_dir::MaybeTempDir;
8 use rustc_session::cstore::DllImport;
9
10 struct ArchiveConfig<'a> {
11     sess: &'a Session,
12     dst: PathBuf,
13     use_native_ar: bool,
14     use_gnu_style_archive: bool,
15 }
16
17 #[derive(Debug)]
18 enum ArchiveEntry {
19     FromArchive {
20         archive_index: usize,
21         entry_index: usize,
22     },
23     File(PathBuf),
24 }
25
26 pub struct ArArchiveBuilder<'a> {
27     config: ArchiveConfig<'a>,
28     src_archives: Vec<(PathBuf, ar::Archive<File>)>,
29     // Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
30     // the end of an archive for linkers to not get confused.
31     entries: Vec<(String, ArchiveEntry)>,
32 }
33
34 impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
35     fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self {
36         let config = ArchiveConfig {
37             sess,
38             dst: output.to_path_buf(),
39             use_native_ar: false,
40             // FIXME test for linux and System V derivatives instead
41             use_gnu_style_archive: sess.target.options.archive_format == "gnu",
42         };
43
44         let (src_archives, entries) = if let Some(input) = input {
45             let mut archive = ar::Archive::new(File::open(input).unwrap());
46             let mut entries = Vec::new();
47
48             let mut i = 0;
49             while let Some(entry) = archive.next_entry() {
50                 let entry = entry.unwrap();
51                 entries.push((
52                     String::from_utf8(entry.header().identifier().to_vec()).unwrap(),
53                     ArchiveEntry::FromArchive {
54                         archive_index: 0,
55                         entry_index: i,
56                     },
57                 ));
58                 i += 1;
59             }
60
61             (vec![(input.to_owned(), archive)], entries)
62         } else {
63             (vec![], Vec::new())
64         };
65
66         ArArchiveBuilder {
67             config,
68             src_archives,
69             entries,
70         }
71     }
72
73     fn src_files(&mut self) -> Vec<String> {
74         self.entries.iter().map(|(name, _)| name.clone()).collect()
75     }
76
77     fn remove_file(&mut self, name: &str) {
78         let index = self
79             .entries
80             .iter()
81             .position(|(entry_name, _)| entry_name == name)
82             .expect("Tried to remove file not existing in src archive");
83         self.entries.remove(index);
84     }
85
86     fn add_file(&mut self, file: &Path) {
87         self.entries.push((
88             file.file_name().unwrap().to_str().unwrap().to_string(),
89             ArchiveEntry::File(file.to_owned()),
90         ));
91     }
92
93     fn add_archive<F>(&mut self, archive_path: &Path, mut skip: F) -> std::io::Result<()>
94     where
95         F: FnMut(&str) -> bool + 'static,
96     {
97         let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
98         let archive_index = self.src_archives.len();
99
100         let mut i = 0;
101         while let Some(entry) = archive.next_entry() {
102             let entry = entry?;
103             let file_name = String::from_utf8(entry.header().identifier().to_vec())
104                 .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
105             if !skip(&file_name) {
106                 self.entries
107                     .push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
108             }
109             i += 1;
110         }
111
112         self.src_archives.push((archive_path.to_owned(), archive));
113         Ok(())
114     }
115
116     fn build(mut self) {
117         use std::process::Command;
118
119         fn add_file_using_ar(archive: &Path, file: &Path) {
120             Command::new("ar")
121                 .arg("r") // add or replace file
122                 .arg("-c") // silence created file message
123                 .arg(archive)
124                 .arg(&file)
125                 .status()
126                 .unwrap();
127         }
128
129         enum BuilderKind<'a> {
130             Bsd(ar::Builder<File>),
131             Gnu(ar::GnuBuilder<File>),
132             NativeAr(&'a Path),
133         }
134
135         let mut builder = if self.config.use_native_ar {
136             BuilderKind::NativeAr(&self.config.dst)
137         } else if self.config.use_gnu_style_archive {
138             BuilderKind::Gnu(ar::GnuBuilder::new(
139                 File::create(&self.config.dst).unwrap(),
140                 self.entries
141                     .iter()
142                     .map(|(name, _)| name.as_bytes().to_vec())
143                     .collect(),
144             ))
145         } else {
146             BuilderKind::Bsd(ar::Builder::new(File::create(&self.config.dst).unwrap()))
147         };
148
149         // Add all files
150         for (entry_name, entry) in self.entries.into_iter() {
151             match entry {
152                 ArchiveEntry::FromArchive {
153                     archive_index,
154                     entry_index,
155                 } => {
156                     let (ref src_archive_path, ref mut src_archive) =
157                         self.src_archives[archive_index];
158                     let entry = src_archive.jump_to_entry(entry_index).unwrap();
159                     let header = entry.header().clone();
160
161                     match builder {
162                         BuilderKind::Bsd(ref mut builder) => {
163                             builder.append(&header, entry).unwrap()
164                         }
165                         BuilderKind::Gnu(ref mut builder) => {
166                             builder.append(&header, entry).unwrap()
167                         }
168                         BuilderKind::NativeAr(archive_file) => {
169                             Command::new("ar")
170                                 .arg("x")
171                                 .arg(src_archive_path)
172                                 .arg(&entry_name)
173                                 .status()
174                                 .unwrap();
175                             add_file_using_ar(archive_file, Path::new(&entry_name));
176                             std::fs::remove_file(entry_name).unwrap();
177                         }
178                     }
179                 }
180                 ArchiveEntry::File(file) =>
181                     match builder {
182                         BuilderKind::Bsd(ref mut builder) => {
183                             builder
184                                 .append_file(entry_name.as_bytes(), &mut File::open(file).expect("file for bsd builder"))
185                                 .unwrap()
186                         },
187                         BuilderKind::Gnu(ref mut builder) => {
188                             builder
189                                 .append_file(entry_name.as_bytes(), &mut File::open(&file).expect(&format!("file {:?} for gnu builder", file)))
190                                 .unwrap()
191                         },
192                         BuilderKind::NativeAr(archive_file) => add_file_using_ar(archive_file, &file),
193                     },
194             }
195         }
196
197         // Finalize archive
198         std::mem::drop(builder);
199
200         // Run ranlib to be able to link the archive
201         let status = std::process::Command::new("ranlib")
202             .arg(self.config.dst)
203             .status()
204             .expect("Couldn't run ranlib");
205
206         if !status.success() {
207             self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
208         }
209     }
210
211     fn inject_dll_import_lib(&mut self, _lib_name: &str, _dll_imports: &[DllImport], _tmpdir: &MaybeTempDir) {
212         unimplemented!();
213     }
214 }