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