Skip to content

Commit d8062fb

Browse files
Fix formatting
1 parent 5aa3c00 commit d8062fb

File tree

2 files changed

+126
-29
lines changed

2 files changed

+126
-29
lines changed

riscv-rt/examples/hello.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ const UART_LSR: usize = UART_BASE + 5;
1111
const LCR_DLAB: u8 = 1 << 7;
1212
const LCR_8N1: u8 = 0x03;
1313
const LSR_THRE: u8 = 1 << 5;
14-
unsafe fn uart_write_reg(off: usize, v: u8) { (off as *mut u8).write_volatile(v); }
15-
unsafe fn uart_read_reg(off: usize) -> u8 { (off as *const u8).read_volatile() }
14+
unsafe fn uart_write_reg(off: usize, v: u8) {
15+
(off as *mut u8).write_volatile(v);
16+
}
17+
unsafe fn uart_read_reg(off: usize) -> u8 {
18+
(off as *const u8).read_volatile()
19+
}
1620
fn uart_init() {
1721
unsafe {
1822
uart_write_reg(UART_LCR, LCR_DLAB);
@@ -23,9 +27,16 @@ fn uart_init() {
2327
}
2428
}
2529
fn uart_write_byte(b: u8) {
26-
unsafe { while (uart_read_reg(UART_LSR) & LSR_THRE) == 0 {} uart_write_reg(UART_THR, b); }
30+
unsafe {
31+
while (uart_read_reg(UART_LSR) & LSR_THRE) == 0 {}
32+
uart_write_reg(UART_THR, b);
33+
}
34+
}
35+
fn uart_write_str(s: &str) {
36+
for &b in s.as_bytes() {
37+
uart_write_byte(b);
38+
}
2739
}
28-
fn uart_write_str(s: &str) { for &b in s.as_bytes() { uart_write_byte(b); } }
2940
#[entry]
3041
fn main() -> ! {
3142
uart_init();

xtask/src/main.rs

Lines changed: 111 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,139 @@
1-
use std::{process::{Command, Stdio}, fs, path::PathBuf, thread, time::Duration};
21
use anyhow::{bail, Context};
2+
use std::{
3+
fs,
4+
path::PathBuf,
5+
process::{Command, Stdio},
6+
thread,
7+
time::Duration,
8+
};
39

410
fn main() -> anyhow::Result<()> {
511
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+
}
715
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+
}
1539
let target = target.context("--target required")?;
1640
let example = example.context("--example required")?;
1741
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+
}
1947

2048
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+
]);
2359
cmd.apply_features(features.as_deref());
2460
let status = cmd.status()?;
25-
if !status.success() { bail!("build failed"); }
61+
if !status.success() {
62+
bail!("build failed");
63+
}
2664

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+
}
3083
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")?;
3292
thread::sleep(Duration::from_secs(2));
3393
let _ = child.kill();
3494
let output = child.wait_with_output()?;
3595
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+
};
38106

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+
}
41115
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+
}
44126
Ok(())
45127
}
46128

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+
}
48132
impl CmdExt for std::process::Command {
49133
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+
}
51137
self
52138
}
53139
}

0 commit comments

Comments
 (0)