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
#[macro_use]
extern crate failure;
extern crate regex;
extern crate rustc_demangle;
extern crate rustc_version;
#[macro_use]
extern crate serde_derive;
extern crate toml;
extern crate walkdir;
use std::borrow::Cow;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, str};
pub use failure::Error;
use regex::{Captures, Regex};
use walkdir::WalkDir;
use config::Config;
mod config;
pub type Result<T> = std::result::Result<T, failure::Error>;
pub struct Context {
bindir: PathBuf,
host: String,
re: Regex,
target: Option<String>,
tool_args: Vec<String>,
verbose: bool,
}
impl Context {
fn new() -> Result<Self> {
let cwd = env::current_dir()?;
let config = Config::get(&cwd)?;
let meta = rustc_version::version_meta()?;
let host = meta.host;
let mut args = env::args().skip(2);
let mut target = None;
let mut error = false;
let mut verbose = false;
while let Some(arg) = args.next() {
if arg == "--target" {
if target.is_some() {
error = true;
break;
}
target = args.next();
if target.is_none() {
error = true;
break;
}
} else if arg.starts_with("--target=") {
if target.is_some() {
error = true;
break;
}
target = arg.split('=').nth(1).map(|s| s.to_owned());
if target.is_none() {
error = true;
break;
}
} else if arg == "--" {
break;
} else if arg == "--verbose" || arg == "-v" {
verbose = true;
} else {
error = true;
break;
}
}
let tool_args = args.collect();
if error {
bail!("malformed Cargo arguments");
}
target = target.or_else(|| config.and_then(|c| c.build.and_then(|b| b.target)));
if target.as_ref() == Some(&host) {
target = None;
}
let sysroot = String::from_utf8(
Command::new("rustc")
.arg("--print")
.arg("sysroot")
.output()?
.stdout,
)?;
for entry in WalkDir::new(sysroot.trim()).into_iter() {
let entry = entry?;
if entry.file_name() == "llvm-size" {
let bindir = entry.path().parent().unwrap().to_owned();
return Ok(Context {
bindir,
host,
re: Regex::new(r#"_Z.+?E\b"#).expect("BUG: Malformed Regex"),
target,
tool_args,
verbose,
});
}
}
bail!(
"`llvm-tools` component is missing or empty. Install it with `rustup component add \
llvm-tools`"
);
}
pub fn nm(&self) -> Command {
self.tool("llvm-nm")
}
pub fn objcopy(&self) -> Command {
self.tool("llvm-objcopy")
}
pub fn objdump(&self) -> Command {
let mut objdump = self.tool("llvm-objdump");
objdump.arg("-triple");
objdump.arg(self.target());
objdump
}
pub fn profdata(&self) -> Command {
self.tool("llvm-profdata")
}
pub fn size(&self) -> Command {
self.tool("llvm-size")
}
pub fn tool_args(&self) -> &[String] {
&self.tool_args
}
fn bindir(&self) -> &Path {
&self.bindir
}
fn demangle<'i>(&self, input: &'i str) -> Cow<'i, str> {
self.re.replace_all(input, |cs: &Captures| {
format!("{}", rustc_demangle::demangle(cs.get(0).unwrap().as_str()))
})
}
#[cfg(unused)]
fn host(&self) -> &str {
&self.host
}
fn target(&self) -> &str {
self.target.as_ref().unwrap_or(&self.host)
}
fn tool(&self, name: &str) -> Command {
Command::new(self.bindir().join(name))
}
}
pub fn run<F>(tool: F, demangle: bool) -> Result<i32>
where
F: FnOnce(&Context) -> Command,
{
let ctxt = Context::new()?;
let mut tool = tool(&ctxt);
tool.args(ctxt.tool_args());
let stderr = io::stderr();
let mut stderr = stderr.lock();
if ctxt.verbose {
writeln!(stderr, "{:?}", tool).ok();
}
let output = tool.output()?;
let stdout = io::stdout();
let mut stdout = stdout.lock();
let tool_stdout = if demangle {
match ctxt.demangle(str::from_utf8(&output.stdout)?) {
Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()),
Cow::Owned(s) => Cow::Owned(s.into_bytes()),
}
} else {
Cow::from(output.stdout)
};
Ok(if output.status.success() {
stdout.write_all(&*tool_stdout)?;
stderr.write_all(&output.stderr)?;
0
} else {
stdout.write_all(&*tool_stdout)?;
stderr.write_all(&output.stderr)?;
output.status.code().unwrap_or(1)
})
}