1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use std::old_io::fs::PathExtensions;
use std::old_io::process::{Command, ProcessOutput};
use std::old_io::{fs, TempDir};
use std::old_io;
use std::env;
use std::str;
use syntax::diagnostic::Handler as ErrorHandler;
pub static METADATA_FILENAME: &'static str = "rust.metadata.bin";
pub struct ArchiveConfig<'a> {
pub handler: &'a ErrorHandler,
pub dst: Path,
pub lib_search_paths: Vec<Path>,
pub slib_prefix: String,
pub slib_suffix: String,
pub maybe_ar_prog: Option<String>
}
pub struct Archive<'a> {
handler: &'a ErrorHandler,
dst: Path,
lib_search_paths: Vec<Path>,
slib_prefix: String,
slib_suffix: String,
maybe_ar_prog: Option<String>
}
#[must_use = "must call build() to finish building the archive"]
pub struct ArchiveBuilder<'a> {
archive: Archive<'a>,
work_dir: TempDir,
members: Vec<Path>,
should_update_symbols: bool,
}
fn run_ar(handler: &ErrorHandler, maybe_ar_prog: &Option<String>,
args: &str, cwd: Option<&Path>,
paths: &[&Path]) -> ProcessOutput {
let ar = match *maybe_ar_prog {
Some(ref ar) => &ar[..],
None => "ar"
};
let mut cmd = Command::new(ar);
cmd.arg(args).args(paths);
debug!("{:?}", cmd);
match cwd {
Some(p) => {
cmd.cwd(p);
debug!("inside {:?}", p.display());
}
None => {}
}
match cmd.spawn() {
Ok(prog) => {
let o = prog.wait_with_output().unwrap();
if !o.status.success() {
handler.err(&format!("{:?} failed with: {}", cmd, o.status)[]);
handler.note(&format!("stdout ---\n{}",
str::from_utf8(&o.output[]).unwrap())[]);
handler.note(&format!("stderr ---\n{}",
str::from_utf8(&o.error[]).unwrap())
[]);
handler.abort_if_errors();
}
o
},
Err(e) => {
handler.err(&format!("could not exec `{}`: {}", &ar[..],
e)[]);
handler.abort_if_errors();
panic!("rustc::back::archive::run_ar() should not reach this point");
}
}
}
pub fn find_library(name: &str, osprefix: &str, ossuffix: &str,
search_paths: &[Path], handler: &ErrorHandler) -> Path {
let oslibname = format!("{}{}{}", osprefix, name, ossuffix);
let unixlibname = format!("lib{}.a", name);
for path in search_paths {
debug!("looking for {} inside {:?}", name, path.display());
let test = path.join(&oslibname[..]);
if test.exists() { return test }
if oslibname != unixlibname {
let test = path.join(&unixlibname[..]);
if test.exists() { return test }
}
}
handler.fatal(&format!("could not find native static library `{}`, \
perhaps an -L flag is missing?",
name)[]);
}
impl<'a> Archive<'a> {
fn new(config: ArchiveConfig<'a>) -> Archive<'a> {
let ArchiveConfig { handler, dst, lib_search_paths, slib_prefix, slib_suffix,
maybe_ar_prog } = config;
Archive {
handler: handler,
dst: dst,
lib_search_paths: lib_search_paths,
slib_prefix: slib_prefix,
slib_suffix: slib_suffix,
maybe_ar_prog: maybe_ar_prog
}
}
pub fn open(config: ArchiveConfig<'a>) -> Archive<'a> {
let archive = Archive::new(config);
assert!(archive.dst.exists());
archive
}
pub fn remove_file(&mut self, file: &str) {
run_ar(self.handler, &self.maybe_ar_prog, "d", None, &[&self.dst, &Path::new(file)]);
}
pub fn files(&self) -> Vec<String> {
let output = run_ar(self.handler, &self.maybe_ar_prog, "t", None, &[&self.dst]);
let output = str::from_utf8(&output.output[]).unwrap();
output.lines_any().map(|s| s.to_string()).collect()
}
pub fn extend(self) -> ArchiveBuilder<'a> {
ArchiveBuilder::new(self)
}
}
impl<'a> ArchiveBuilder<'a> {
fn new(archive: Archive<'a>) -> ArchiveBuilder<'a> {
ArchiveBuilder {
archive: archive,
work_dir: TempDir::new("rsar").unwrap(),
members: vec![],
should_update_symbols: false,
}
}
pub fn create(config: ArchiveConfig<'a>) -> ArchiveBuilder<'a> {
let archive = Archive::new(config);
ArchiveBuilder::new(archive)
}
pub fn add_native_library(&mut self, name: &str) -> old_io::IoResult<()> {
let location = find_library(name,
&self.archive.slib_prefix[],
&self.archive.slib_suffix[],
&self.archive.lib_search_paths[],
self.archive.handler);
self.add_archive(&location, name, |_| false)
}
pub fn add_rlib(&mut self, rlib: &Path, name: &str,
lto: bool) -> old_io::IoResult<()> {
let obj_start = format!("{}", name);
let obj_start = &obj_start[..];
let bc_ext = ".bytecode.deflate";
self.add_archive(rlib, &name[..], |fname: &str| {
let skip_obj = lto && fname.starts_with(obj_start)
&& fname.ends_with(".o");
skip_obj || fname.ends_with(bc_ext) || fname == METADATA_FILENAME
})
}
pub fn add_file(&mut self, file: &Path) -> old_io::IoResult<()> {
let filename = Path::new(file.filename().unwrap());
let new_file = self.work_dir.path().join(&filename);
try!(fs::copy(file, &new_file));
self.members.push(filename);
Ok(())
}
pub fn update_symbols(&mut self) {
self.should_update_symbols = true;
}
pub fn build(self) -> Archive<'a> {
let abs_dst = env::current_dir().unwrap().join(&self.archive.dst);
assert!(!abs_dst.is_relative());
let mut args = vec![&abs_dst];
let mut total_len = abs_dst.as_vec().len();
if self.members.is_empty() {
if self.should_update_symbols {
run_ar(self.archive.handler, &self.archive.maybe_ar_prog,
"s", Some(self.work_dir.path()), &args[..]);
}
return self.archive;
}
static ARG_LENGTH_LIMIT: uint = 32000;
for member_name in &self.members {
let len = member_name.as_vec().len();
if total_len + len + 1 > ARG_LENGTH_LIMIT {
run_ar(self.archive.handler, &self.archive.maybe_ar_prog,
"cruS", Some(self.work_dir.path()), &args[..]);
args.clear();
args.push(&abs_dst);
total_len = abs_dst.as_vec().len();
}
args.push(member_name);
total_len += len + 1;
}
let flags = if self.should_update_symbols { "crus" } else { "cruS" };
run_ar(self.archive.handler, &self.archive.maybe_ar_prog,
flags, Some(self.work_dir.path()), &args[..]);
self.archive
}
fn add_archive<F>(&mut self, archive: &Path, name: &str,
mut skip: F) -> old_io::IoResult<()>
where F: FnMut(&str) -> bool,
{
let loc = TempDir::new("rsar").unwrap();
let archive = env::current_dir().unwrap().join(archive);
run_ar(self.archive.handler, &self.archive.maybe_ar_prog,
"x", Some(loc.path()), &[&archive]);
let files = try!(fs::readdir(loc.path()));
for file in &files {
let filename = file.filename_str().unwrap();
if skip(filename) { continue }
if filename.contains(".SYMDEF") { continue }
let filename = format!("r-{}-{}", name, filename);
let filename = if filename.len() == 16 {
format!("lldb-fix-{}", filename)
} else {
filename
};
let new_filename = self.work_dir.path().join(&filename[..]);
try!(fs::rename(file, &new_filename));
self.members.push(Path::new(filename));
}
Ok(())
}
}