Skip to content

Commit 882f7ec

Browse files
committed
Add FileCheck mode
1 parent 9197df0 commit 882f7ec

File tree

3 files changed

+100
-7
lines changed

3 files changed

+100
-7
lines changed

src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ a date (YYYY-MM-DD), git tag name (e.g. 1.58.0) or git commit SHA."
188188
#[arg(long, help = "Script replacement for `cargo build` command")]
189189
script: Option<PathBuf>,
190190

191+
#[arg(
192+
long,
193+
help = "Run in LLVM FileCheck, using the given path for annotations"
194+
)]
195+
filecheck: Option<PathBuf>,
196+
197+
#[arg(long, help = "Path to LLVM FileCheck")]
198+
filecheck_path: Option<PathBuf>,
199+
191200
#[arg(long, help = "Do not install cargo [default: install cargo]")]
192201
without_cargo: bool,
193202

src/toolchains.rs

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,24 @@ impl Toolchain {
248248
}
249249
});
250250

251-
let mut cmd = match (script, cfg.args.timeout) {
252-
(Some(script), None) => {
251+
let target_dir = format!("target-{}", self.rustup_name());
252+
// Used in filecheck mode
253+
let llir_path = PathBuf::from(&target_dir)
254+
.join("debug")
255+
.join("deps")
256+
.join("output.ll");
257+
258+
let mut cmd = match (script, cfg.args.timeout, &cfg.args.filecheck) {
259+
(Some(_), _, Some(_)) => {
260+
panic!("incompatbile options `--script` and `--filecheck` used");
261+
}
262+
(Some(script), None, None) => {
253263
let mut cmd = Command::new(script);
254264
cmd.env("RUSTUP_TOOLCHAIN", self.rustup_name());
255265
cmd.args(&cfg.args.command_args);
256266
cmd
257267
}
258-
(None, None) => {
268+
(None, None, None) => {
259269
let mut cmd = Command::new("cargo");
260270
cmd.arg(&format!("+{}", self.rustup_name()));
261271
if cfg.args.command_args.is_empty() {
@@ -265,15 +275,15 @@ impl Toolchain {
265275
}
266276
cmd
267277
}
268-
(Some(script), Some(timeout)) => {
278+
(Some(script), Some(timeout), None) => {
269279
let mut cmd = Command::new("timeout");
270280
cmd.arg(timeout.to_string());
271281
cmd.arg(script);
272282
cmd.args(&cfg.args.command_args);
273283
cmd.env("RUSTUP_TOOLCHAIN", self.rustup_name());
274284
cmd
275285
}
276-
(None, Some(timeout)) => {
286+
(None, Some(timeout), None) => {
277287
let mut cmd = Command::new("timeout");
278288
cmd.arg(timeout.to_string());
279289
cmd.arg("cargo");
@@ -285,9 +295,39 @@ impl Toolchain {
285295
}
286296
cmd
287297
}
298+
(None, None, Some(_)) => {
299+
let mut cmd = Command::new("cargo");
300+
cmd.arg(&format!("+{}", self.rustup_name()));
301+
if cfg.args.command_args.is_empty() {
302+
cmd.arg("rustc")
303+
.arg("--")
304+
.arg("-Copt-level=3")
305+
.arg("-Cdebuginfo=0")
306+
.arg(format!("--emit=llvm-ir={}", llir_path.display()));
307+
} else {
308+
cmd.args(&cfg.args.command_args);
309+
}
310+
cmd
311+
}
312+
(None, Some(timeout), Some(_)) => {
313+
let mut cmd = Command::new("timeout");
314+
cmd.arg(timeout.to_string());
315+
cmd.arg("cargo");
316+
cmd.arg(&format!("+{}", self.rustup_name()));
317+
if cfg.args.command_args.is_empty() {
318+
cmd.arg("rustc")
319+
.arg("--")
320+
.arg("-Copt-level=3")
321+
.arg("-Cdebuginfo=0")
322+
.arg(format!("--emit=llvm-ir={}", llir_path.display()));
323+
} else {
324+
cmd.args(&cfg.args.command_args);
325+
}
326+
cmd
327+
}
288328
};
289329
cmd.current_dir(&cfg.args.test_dir);
290-
cmd.env("CARGO_TARGET_DIR", format!("target-{}", self.rustup_name()));
330+
cmd.env("CARGO_TARGET_DIR", &target_dir);
291331
if let Some(target) = &cfg.args.target {
292332
cmd.env("CARGO_BUILD_TARGET", target);
293333
}
@@ -319,6 +359,44 @@ impl Toolchain {
319359
io::stdout().write_all(&output.stdout).unwrap();
320360
io::stderr().write_all(&output.stderr).unwrap();
321361
}
362+
363+
let Some(check_file) = &cfg.args.filecheck else {
364+
return output;
365+
};
366+
367+
if !output.status.success() {
368+
return output;
369+
}
370+
371+
let filecheck_path = cfg
372+
.args
373+
.filecheck_path
374+
.clone()
375+
.unwrap_or_else(|| PathBuf::from("FileCheck"));
376+
377+
let mut cmd = Command::new(filecheck_path);
378+
cmd.arg("--input-file")
379+
.arg(
380+
PathBuf::from(target_dir)
381+
.join("debug")
382+
.join("deps")
383+
.join("output.ll"),
384+
)
385+
.arg(check_file);
386+
387+
cmd.stdout(default_stdio());
388+
cmd.stderr(default_stdio());
389+
let output = match cmd.output() {
390+
Ok(output) => output,
391+
Err(err) => {
392+
panic!("thiserror::Errored to run {:?}: {:?}", cmd, err);
393+
}
394+
};
395+
if must_capture_output && emit_output {
396+
io::stdout().write_all(&output.stdout).unwrap();
397+
io::stderr().write_all(&output.stderr).unwrap();
398+
}
399+
322400
output
323401
}
324402

tests/cmd/help.stdout

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ Options:
2626
Right bound for search (*with* regression). You can use a date (YYYY-MM-DD), git tag name
2727
(e.g. 1.58.0) or git commit SHA.
2828

29+
--filecheck <FILECHECK>
30+
Run in LLVM FileCheck, using the given path for annotations
31+
32+
--filecheck-path <FILECHECK_PATH>
33+
Path to LLVM FileCheck
34+
2935
--force-install
3036
Force installation over existing artifacts
3137

@@ -35,7 +41,7 @@ Options:
3541
--host <HOST>
3642
Host triple for the compiler
3743

38-
[default: [..]]
44+
[default: x86_64-unknown-linux-gnu]
3945

4046
--install <INSTALL>
4147
Install the given artifact

0 commit comments

Comments
 (0)