|
1 | | -use std::{process::{Command, Stdio}, fs, path::PathBuf, thread, time::Duration}; |
2 | 1 | use anyhow::{bail, Context}; |
| 2 | +use std::{ |
| 3 | + fs, |
| 4 | + path::PathBuf, |
| 5 | + process::{Command, Stdio}, |
| 6 | + thread, |
| 7 | + time::Duration, |
| 8 | +}; |
3 | 9 |
|
4 | 10 | fn main() -> anyhow::Result<()> { |
5 | 11 | let mut args = std::env::args().skip(1).collect::<Vec<_>>(); |
6 | | - if args.is_empty() || args[0] != "qemu" { bail!("usage: cargo run -p xtask -- qemu --target <triple> --example <name>"); } |
| 12 | + if args.is_empty() || args[0] != "qemu" { |
| 13 | + bail!("usage: cargo run -p xtask -- qemu --target <triple> --example <name>"); |
| 14 | + } |
7 | 15 | args.remove(0); |
8 | | - let mut target = None; let mut example = None; let mut features: Option<String> = None; |
9 | | - let mut i=0; while i < args.len() { match args[i].as_str() { |
10 | | - "--target" => { target = Some(args.get(i+1).context("missing target")?.clone()); i+=2; }, |
11 | | - "--example" => { example = Some(args.get(i+1).context("missing example")?.clone()); i+=2; }, |
12 | | - "--features" => { features = Some(args.get(i+1).context("missing features")?.clone()); i+=2; }, |
13 | | - _ => { bail!("unknown arg {}", args[i]); } |
14 | | - }} |
| 16 | + let mut target = None; |
| 17 | + let mut example = None; |
| 18 | + let mut features: Option<String> = None; |
| 19 | + let mut i = 0; |
| 20 | + while i < args.len() { |
| 21 | + match args[i].as_str() { |
| 22 | + "--target" => { |
| 23 | + target = Some(args.get(i + 1).context("missing target")?.clone()); |
| 24 | + i += 2; |
| 25 | + } |
| 26 | + "--example" => { |
| 27 | + example = Some(args.get(i + 1).context("missing example")?.clone()); |
| 28 | + i += 2; |
| 29 | + } |
| 30 | + "--features" => { |
| 31 | + features = Some(args.get(i + 1).context("missing features")?.clone()); |
| 32 | + i += 2; |
| 33 | + } |
| 34 | + _ => { |
| 35 | + bail!("unknown arg {}", args[i]); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
15 | 39 | let target = target.context("--target required")?; |
16 | 40 | let example = example.context("--example required")?; |
17 | 41 | let mut rustflags = "-C link-arg=-Triscv-rt/examples/device_virt_m.x".to_string(); |
18 | | - if let Some(f) = &features { if f.contains("s-mode") { rustflags = "-C link-arg=-Triscv-rt/examples/device_virt_s.x".into(); } } |
| 42 | + if let Some(f) = &features { |
| 43 | + if f.contains("s-mode") { |
| 44 | + rustflags = "-C link-arg=-Triscv-rt/examples/device_virt_s.x".into(); |
| 45 | + } |
| 46 | + } |
19 | 47 |
|
20 | 48 | let mut cmd = Command::new("cargo"); |
21 | | - cmd.env("RUSTFLAGS", rustflags) |
22 | | - .args(["build","--package","riscv-rt","--release","--target", &target, "--example", &example]); |
| 49 | + cmd.env("RUSTFLAGS", rustflags).args([ |
| 50 | + "build", |
| 51 | + "--package", |
| 52 | + "riscv-rt", |
| 53 | + "--release", |
| 54 | + "--target", |
| 55 | + &target, |
| 56 | + "--example", |
| 57 | + &example, |
| 58 | + ]); |
23 | 59 | cmd.apply_features(features.as_deref()); |
24 | 60 | let status = cmd.status()?; |
25 | | - if !status.success() { bail!("build failed"); } |
| 61 | + if !status.success() { |
| 62 | + bail!("build failed"); |
| 63 | + } |
26 | 64 |
|
27 | | - let qemu = if target.starts_with("riscv32") { "qemu-system-riscv32" } else { "qemu-system-riscv64" }; |
28 | | - let mut qemu_args = vec!["-machine","virt","-nographic","-serial","stdio","-monitor","none"]; |
29 | | - if !features.as_deref().unwrap_or("").contains("s-mode") { qemu_args.push("-bios"); qemu_args.push("none"); } |
| 65 | + let qemu = if target.starts_with("riscv32") { |
| 66 | + "qemu-system-riscv32" |
| 67 | + } else { |
| 68 | + "qemu-system-riscv64" |
| 69 | + }; |
| 70 | + let mut qemu_args = vec![ |
| 71 | + "-machine", |
| 72 | + "virt", |
| 73 | + "-nographic", |
| 74 | + "-serial", |
| 75 | + "stdio", |
| 76 | + "-monitor", |
| 77 | + "none", |
| 78 | + ]; |
| 79 | + if !features.as_deref().unwrap_or("").contains("s-mode") { |
| 80 | + qemu_args.push("-bios"); |
| 81 | + qemu_args.push("none"); |
| 82 | + } |
30 | 83 | let kernel_path = format!("target/{}/release/examples/{}", target, example); |
31 | | - let mut child = Command::new(qemu).args(&qemu_args).arg("-kernel").arg(&kernel_path).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().context("running qemu")?; |
| 84 | + let mut child = Command::new(qemu) |
| 85 | + .args(&qemu_args) |
| 86 | + .arg("-kernel") |
| 87 | + .arg(&kernel_path) |
| 88 | + .stdout(Stdio::piped()) |
| 89 | + .stderr(Stdio::piped()) |
| 90 | + .spawn() |
| 91 | + .context("running qemu")?; |
32 | 92 | thread::sleep(Duration::from_secs(2)); |
33 | 93 | let _ = child.kill(); |
34 | 94 | let output = child.wait_with_output()?; |
35 | 95 | let raw_stdout = String::from_utf8_lossy(&output.stdout).into_owned(); |
36 | | - let stdout = raw_stdout.lines().filter(|l| !l.contains("QEMU") && !l.contains("monitor")).collect::<Vec<_>>().join("\n"); |
37 | | - let stdout = if stdout.is_empty() { String::new() } else { format!("{}\n", stdout.trim()) }; |
| 96 | + let stdout = raw_stdout |
| 97 | + .lines() |
| 98 | + .filter(|l| !l.contains("QEMU") && !l.contains("monitor")) |
| 99 | + .collect::<Vec<_>>() |
| 100 | + .join("\n"); |
| 101 | + let stdout = if stdout.is_empty() { |
| 102 | + String::new() |
| 103 | + } else { |
| 104 | + format!("{}\n", stdout.trim()) |
| 105 | + }; |
38 | 106 |
|
39 | | - let expected_path: PathBuf = ["ci","expected", &target, &format!("{}.run", example)].iter().collect(); |
40 | | - if !expected_path.exists() { fs::create_dir_all(expected_path.parent().unwrap())?; fs::write(&expected_path, stdout.as_bytes())?; bail!("expected output created; re-run CI"); } |
| 107 | + let expected_path: PathBuf = ["ci", "expected", &target, &format!("{}.run", example)] |
| 108 | + .iter() |
| 109 | + .collect(); |
| 110 | + if !expected_path.exists() { |
| 111 | + fs::create_dir_all(expected_path.parent().unwrap())?; |
| 112 | + fs::write(&expected_path, stdout.as_bytes())?; |
| 113 | + bail!("expected output created; re-run CI"); |
| 114 | + } |
41 | 115 | let expected = fs::read_to_string(&expected_path)?; |
42 | | - if expected != stdout { bail!("output mismatch\nexpected: {}\nactual: {}", expected, stdout); } |
43 | | - if !stdout.is_empty() { println!("{}", stdout.trim_end()); } |
| 116 | + if expected != stdout { |
| 117 | + bail!( |
| 118 | + "output mismatch\nexpected: {}\nactual: {}", |
| 119 | + expected, |
| 120 | + stdout |
| 121 | + ); |
| 122 | + } |
| 123 | + if !stdout.is_empty() { |
| 124 | + println!("{}", stdout.trim_end()); |
| 125 | + } |
44 | 126 | Ok(()) |
45 | 127 | } |
46 | 128 |
|
47 | | -trait CmdExt { fn apply_features(&mut self, f: Option<&str>) -> &mut Self; } |
| 129 | +trait CmdExt { |
| 130 | + fn apply_features(&mut self, f: Option<&str>) -> &mut Self; |
| 131 | +} |
48 | 132 | impl CmdExt for std::process::Command { |
49 | 133 | fn apply_features(&mut self, f: Option<&str>) -> &mut Self { |
50 | | - if let Some(feat)=f { self.arg("--features").arg(feat); } |
| 134 | + if let Some(feat) = f { |
| 135 | + self.arg("--features").arg(feat); |
| 136 | + } |
51 | 137 | self |
52 | 138 | } |
53 | 139 | } |
0 commit comments