Skip to content

Commit 77a7a32

Browse files
authored
Merge pull request #700 from nicholasbishop/bishop-check-env
xtask: Add OVMF_CODE/OVMF_VARS env vars
2 parents bd0b183 + 531f6f6 commit 77a7a32

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

xtask/src/qemu.rs

+29-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ impl OvmfFileType {
2828
Self::Vars => "vars",
2929
}
3030
}
31+
32+
/// Get a user-provided path for the given OVMF file type.
33+
///
34+
/// This uses the command-line arg if present, otherwise it falls back to an
35+
/// environment variable. If neither is present, returns `None`.
36+
fn get_user_provided_path(self, opt: &QemuOpt) -> Option<PathBuf> {
37+
let opt_path;
38+
let var_name;
39+
match self {
40+
Self::Code => {
41+
opt_path = &opt.ovmf_code;
42+
var_name = "OVMF_CODE";
43+
}
44+
Self::Vars => {
45+
opt_path = &opt.ovmf_vars;
46+
var_name = "OVMF_VARS";
47+
}
48+
}
49+
if let Some(path) = opt_path {
50+
Some(path.clone())
51+
} else {
52+
env::var_os(var_name).map(PathBuf::from)
53+
}
54+
}
3155
}
3256

3357
struct OvmfPaths {
@@ -173,14 +197,14 @@ impl OvmfPaths {
173197
/// that exists. If none of them exist, an error is returned.
174198
fn find_ovmf_file(
175199
file_type: OvmfFileType,
176-
user_provided_path: &Option<PathBuf>,
200+
opt: &QemuOpt,
177201
candidates: &[Self],
178202
) -> Result<PathBuf> {
179-
if let Some(path) = user_provided_path {
203+
if let Some(path) = file_type.get_user_provided_path(opt) {
180204
// The user provided an exact path to use; verify that it
181205
// exists.
182206
if path.exists() {
183-
Ok(path.to_owned())
207+
Ok(path)
184208
} else {
185209
bail!(
186210
"ovmf {} file does not exist: {}",
@@ -211,8 +235,8 @@ impl OvmfPaths {
211235
fn find(opt: &QemuOpt, arch: UefiArch) -> Result<Self> {
212236
let candidates = Self::get_candidate_paths(arch);
213237

214-
let code = Self::find_ovmf_file(OvmfFileType::Code, &opt.ovmf_code, &candidates)?;
215-
let vars = Self::find_ovmf_file(OvmfFileType::Vars, &opt.ovmf_vars, &candidates)?;
238+
let code = Self::find_ovmf_file(OvmfFileType::Code, opt, &candidates)?;
239+
let vars = Self::find_ovmf_file(OvmfFileType::Vars, opt, &candidates)?;
216240

217241
Ok(Self { code, vars })
218242
}

0 commit comments

Comments
 (0)