Skip to content

Commit 13b3998

Browse files
committed
Convert runner args to config keys
Variable length args are not possible since the executable name is appended at the end
1 parent 3739e4e commit 13b3998

File tree

5 files changed

+37
-41
lines changed

5 files changed

+37
-41
lines changed

example-kernels/runner/.cargo/config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
target = "../x86_64-bootimage-example-kernels.json"
33

44
[target.'cfg(target_os = "none")']
5-
runner = "bootimage runner --args -device isa-debug-exit,iobase=0xf4,iosize=0x04 -display none"
5+
runner = "bootimage runner"

example-kernels/runner/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ edition = "2018"
77
[dependencies]
88
bootloader = "0.5.0"
99
x86_64 = "0.5.3"
10+
11+
[package.metadata.bootimage]
12+
run-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-display", "none"]

src/args.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -207,56 +207,41 @@ fn parse_runner_args<A>(args: A) -> Result<Command, ErrorString>
207207
where
208208
A: Iterator<Item = String>,
209209
{
210+
let mut executable = None;
210211
let mut arg_iter = args.into_iter().fuse();
211-
let executable = PathBuf::from(
212-
arg_iter
213-
.next()
214-
.ok_or("excepted path to kernel executable as first argument")?,
215-
)
216-
.canonicalize()
217-
.map_err(|err| format!("Failed to canonicalize executable path: {}", err))?;
218-
let mut run_command = None;
219-
let mut run_args = None;
220212

221213
loop {
222214
match arg_iter.next().as_ref().map(|s| s.as_str()) {
223-
Some("--command") => {
224-
let old = mem::replace(&mut run_command, Some(arg_iter.collect()));
225-
if !old.is_none() {
226-
Err("multiple `--command` arguments")?;
227-
}
228-
break;
229-
}
230-
Some("--args") => {
231-
let old = mem::replace(&mut run_args, Some(arg_iter.collect()));
232-
if !old.is_none() {
233-
Err("multiple `--args` arguments")?;
234-
}
235-
break;
236-
}
237215
Some("--help") | Some("-h") => {
238216
return Ok(Command::RunnerHelp);
239217
}
240218
Some("--version") => {
241219
return Ok(Command::Version);
242220
}
243-
None => break,
221+
Some(exe) if executable.is_none() => {
222+
let path = Path::new(exe);
223+
let path_canonicalized = path.canonicalize().map_err(|err| {
224+
format!(
225+
"Failed to canonicalize executable path `{}`: {}",
226+
path.display(),
227+
err
228+
)
229+
})?;
230+
executable = Some(path_canonicalized);
231+
}
244232
Some(arg) => Err(format!("unexpected argument `{}`", arg))?,
233+
None => break,
245234
}
246235
}
247236

248237
Ok(Command::Runner(RunnerArgs {
249-
executable,
250-
run_command,
251-
run_args,
238+
executable: executable.ok_or("excepted path to kernel executable as first argument")?,
252239
}))
253240
}
254241

255242
#[derive(Debug, Clone)]
256243
pub struct RunnerArgs {
257244
pub executable: PathBuf,
258-
pub run_command: Option<Vec<String>>,
259-
pub run_args: Option<Vec<String>>,
260245
}
261246

262247
fn parse_tester_args<A>(args: A) -> Result<Command, ErrorString>

src/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub struct Config {
77
pub manifest_path: PathBuf,
88
pub default_target: Option<String>,
99
pub run_command: Vec<String>,
10+
pub run_args: Option<Vec<String>>,
1011
pub test_timeout: u32,
1112
}
1213

@@ -70,6 +71,16 @@ pub(crate) fn read_config_inner(manifest_path: PathBuf) -> Result<Config, ErrorS
7071
}
7172
config.run_command = Some(command);
7273
}
74+
("run-args", Value::Array(array)) => {
75+
let mut args = Vec::new();
76+
for value in array {
77+
match value {
78+
Value::String(s) => args.push(s),
79+
_ => Err(format!("run-args must be a list of strings"))?,
80+
}
81+
}
82+
config.run_args = Some(args);
83+
}
7384
(key, value) => Err(format!(
7485
"unexpected `package.metadata.bootimage` \
7586
key `{}` with value `{}`",
@@ -85,6 +96,7 @@ struct ConfigBuilder {
8596
manifest_path: Option<PathBuf>,
8697
default_target: Option<String>,
8798
run_command: Option<Vec<String>>,
99+
run_args: Option<Vec<String>>,
88100
test_timeout: Option<u32>,
89101
}
90102

@@ -98,6 +110,7 @@ impl Into<Config> for ConfigBuilder {
98110
"-drive".into(),
99111
"format=raw,file={}".into(),
100112
]),
113+
run_args: self.run_args,
101114
test_timeout: self.test_timeout.unwrap_or(60 * 5),
102115
}
103116
}

src/subcommand/runner.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use crate::{args::RunnerArgs, builder::Builder, ErrorString};
1+
use crate::{args::RunnerArgs, builder::Builder, config, ErrorString};
22
use std::{fs, process};
33

44
pub(crate) fn runner(args: RunnerArgs) -> Result<i32, ErrorString> {
55
let builder = Builder::new(None)?;
6+
let config = config::read_config(builder.kernel_manifest_path().to_owned())?;
67

78
let bootimage_bin = {
89
let kernel_target_dir = &builder.kernel_metadata().target_directory;
@@ -38,17 +39,11 @@ pub(crate) fn runner(args: RunnerArgs) -> Result<i32, ErrorString> {
3839

3940
builder.create_bootimage(&args.executable, &bootimage_bin, false)?;
4041

41-
let run_cmd = args.run_command.unwrap_or(vec![
42-
"qemu-system-x86_64".into(),
43-
"-drive".into(),
44-
"format=raw,file={bootimage}".into(),
45-
]);
46-
47-
let mut command = process::Command::new(&run_cmd[0]);
48-
for arg in &run_cmd[1..] {
49-
command.arg(arg.replace("{bootimage}", &format!("{}", bootimage_bin.display())));
42+
let mut command = process::Command::new(&config.run_command[0]);
43+
for arg in &config.run_command[1..] {
44+
command.arg(arg.replace("{}", &format!("{}", bootimage_bin.display())));
5045
}
51-
if let Some(run_args) = args.run_args {
46+
if let Some(run_args) = config.run_args {
5247
command.args(run_args);
5348
}
5449

0 commit comments

Comments
 (0)