Skip to content

Commit c680928

Browse files
upsuperGabrielMajeri
authored andcommitted
Try to find installed OVMF files automatically
1 parent 99a276f commit c680928

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

uefi-test-runner/build.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
# QEMU executable to use
3030
'qemu_binary': 'qemu-system-x86_64',
3131
# Path to directory containing `OVMF_{CODE/VARS}.fd`.
32-
# TODO: use installed OVMF, if available.
33-
'ovmf_dir': WORKSPACE_DIR / 'uefi-test-runner',
32+
# `find_ovmf` function will try to find one if this isn't specified.
33+
'ovmf_dir': None,
3434
}
3535

3636
# Path to target directory. If None, it will be initialized with information
@@ -112,17 +112,50 @@ def doc():
112112
'--package', 'uefi-services',
113113
])
114114

115+
def ovmf_files(ovmf_dir):
116+
'Returns the tuple of paths to OVMF_CODE.fd and OVMF_VARS.fd given the directory'
117+
return ovmf_dir / 'OVMF_CODE.fd', ovmf_dir / 'OVMF_VARS.fd'
118+
119+
def check_ovmf_dir(ovmf_dir):
120+
'Check whether the given directory contains necessary OVMF files'
121+
ovmf_code, ovmf_vars = ovmf_files(ovmf_dir)
122+
return ovmf_code.is_file() and ovmf_vars.is_file()
123+
124+
def find_ovmf():
125+
'Find path to OVMF files'
126+
127+
# If the path is specified in the settings, use it.
128+
if SETTINGS['ovmf_dir'] is not None:
129+
ovmf_dir = SETTINGS['ovmf_dir']
130+
if check_ovmf_dir(ovmf_dir):
131+
return ovmf_dir
132+
raise FileNotFoundError(f'OVMF files not found in `{ovmf_dir}`')
133+
134+
# Check whether the test runner directory contains the files.
135+
ovmf_dir = WORKSPACE_DIR / 'uefi-test-runner'
136+
if check_ovmf_dir(ovmf_dir):
137+
return ovmf_dir
138+
139+
if sys.platform.startswith('linux'):
140+
possible_paths = [
141+
# Most distros, including CentOS, Fedora, Debian, and Ubuntu.
142+
Path('/usr/share/OVMF'),
143+
# Arch Linux
144+
Path('/usr/share/ovmf/x64'),
145+
]
146+
for path in possible_paths:
147+
if check_ovmf_dir(path):
148+
return path
149+
150+
raise FileNotFoundError(f'OVMF files not found anywhere')
151+
115152
def run_qemu():
116153
'Runs the code in QEMU.'
117154

118155
# Rebuild all the changes.
119156
build('--features', 'qemu')
120157

121-
ovmf_dir = SETTINGS['ovmf_dir']
122-
ovmf_code, ovmf_vars = ovmf_dir / 'OVMF_CODE.fd', ovmf_dir / 'OVMF_VARS.fd'
123-
124-
if not ovmf_code.is_file():
125-
raise FileNotFoundError(f'OVMF_CODE.fd not found in the `{ovmf_dir}` directory')
158+
ovmf_code, ovmf_vars = ovmf_files(find_ovmf())
126159

127160
examples_dir = build_dir() / 'examples'
128161

0 commit comments

Comments
 (0)