Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
| 2 | # |
| 3 | # Runs UML kernel, collects output, and handles errors. |
| 4 | # |
| 5 | # Copyright (C) 2019, Google LLC. |
| 6 | # Author: Felix Guo <felixguoxiuping@gmail.com> |
| 7 | # Author: Brendan Higgins <brendanhiggins@google.com> |
| 8 | |
MichaĆ Winiarski | 2355280 | 2022-01-13 00:36:57 +0100 | [diff] [blame] | 9 | import importlib.abc |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 10 | import importlib.util |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 11 | import logging |
| 12 | import subprocess |
| 13 | import os |
Andy Shevchenko | fcdb0bc | 2020-10-26 18:59:25 +0200 | [diff] [blame] | 14 | import shutil |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 15 | import signal |
Daniel Latypov | 7d7c48d | 2021-10-04 18:13:40 -0700 | [diff] [blame] | 16 | import threading |
| 17 | from typing import Iterator, List, Optional, Tuple |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 18 | |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 19 | import kunit_config |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 20 | import kunit_parser |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 21 | import qemu_config |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 22 | |
| 23 | KCONFIG_PATH = '.config' |
Andy Shevchenko | fcdb0bc | 2020-10-26 18:59:25 +0200 | [diff] [blame] | 24 | KUNITCONFIG_PATH = '.kunitconfig' |
Daniel Latypov | 4c2911f | 2021-11-19 15:23:16 -0800 | [diff] [blame] | 25 | OLD_KUNITCONFIG_PATH = 'last_used_kunitconfig' |
David Gow | d9d6b82 | 2021-05-21 21:42:40 -0700 | [diff] [blame] | 26 | DEFAULT_KUNITCONFIG_PATH = 'tools/testing/kunit/configs/default.config' |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 27 | BROKEN_ALLCONFIG_PATH = 'tools/testing/kunit/configs/broken_on_uml.config' |
Andy Shevchenko | 128dc4b | 2020-10-26 18:59:26 +0200 | [diff] [blame] | 28 | OUTFILE_PATH = 'test.log' |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 29 | ABS_TOOL_PATH = os.path.abspath(os.path.dirname(__file__)) |
| 30 | QEMU_CONFIGS_DIR = os.path.join(ABS_TOOL_PATH, 'qemu_configs') |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 31 | |
Andy Shevchenko | f3ed003 | 2020-10-26 18:59:27 +0200 | [diff] [blame] | 32 | def get_file_path(build_dir, default): |
| 33 | if build_dir: |
| 34 | default = os.path.join(build_dir, default) |
| 35 | return default |
| 36 | |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 37 | class ConfigError(Exception): |
| 38 | """Represents an error trying to configure the Linux kernel.""" |
| 39 | |
| 40 | |
| 41 | class BuildError(Exception): |
| 42 | """Represents an error trying to build the Linux kernel.""" |
| 43 | |
| 44 | |
| 45 | class LinuxSourceTreeOperations(object): |
| 46 | """An abstraction over command line operations performed on a source tree.""" |
| 47 | |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 48 | def __init__(self, linux_arch: str, cross_compile: Optional[str]): |
| 49 | self._linux_arch = linux_arch |
| 50 | self._cross_compile = cross_compile |
| 51 | |
Daniel Latypov | 09641f7 | 2021-01-14 16:39:11 -0800 | [diff] [blame] | 52 | def make_mrproper(self) -> None: |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 53 | try: |
Will Chen | 5a9fcad | 2020-07-08 14:35:43 -0700 | [diff] [blame] | 54 | subprocess.check_output(['make', 'mrproper'], stderr=subprocess.STDOUT) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 55 | except OSError as e: |
Daniel Latypov | 1abdd39 | 2020-09-30 11:31:51 -0700 | [diff] [blame] | 56 | raise ConfigError('Could not call make command: ' + str(e)) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 57 | except subprocess.CalledProcessError as e: |
Daniel Latypov | 1abdd39 | 2020-09-30 11:31:51 -0700 | [diff] [blame] | 58 | raise ConfigError(e.output.decode()) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 59 | |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 60 | def make_arch_qemuconfig(self, kconfig: kunit_config.Kconfig) -> None: |
| 61 | pass |
| 62 | |
| 63 | def make_allyesconfig(self, build_dir, make_options) -> None: |
| 64 | raise ConfigError('Only the "um" arch is supported for alltests') |
| 65 | |
Daniel Latypov | 09641f7 | 2021-01-14 16:39:11 -0800 | [diff] [blame] | 66 | def make_olddefconfig(self, build_dir, make_options) -> None: |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 67 | command = ['make', 'ARCH=' + self._linux_arch, 'olddefconfig'] |
| 68 | if self._cross_compile: |
| 69 | command += ['CROSS_COMPILE=' + self._cross_compile] |
Greg Thelen | 0476e69 | 2020-03-23 12:04:59 -0700 | [diff] [blame] | 70 | if make_options: |
| 71 | command.extend(make_options) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 72 | if build_dir: |
| 73 | command += ['O=' + build_dir] |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 74 | print('Populating config with:\n$', ' '.join(command)) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 75 | try: |
Will Chen | 5a9fcad | 2020-07-08 14:35:43 -0700 | [diff] [blame] | 76 | subprocess.check_output(command, stderr=subprocess.STDOUT) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 77 | except OSError as e: |
Daniel Latypov | 1abdd39 | 2020-09-30 11:31:51 -0700 | [diff] [blame] | 78 | raise ConfigError('Could not call make command: ' + str(e)) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 79 | except subprocess.CalledProcessError as e: |
Daniel Latypov | 1abdd39 | 2020-09-30 11:31:51 -0700 | [diff] [blame] | 80 | raise ConfigError(e.output.decode()) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 81 | |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 82 | def make(self, jobs, build_dir, make_options) -> None: |
| 83 | command = ['make', 'ARCH=' + self._linux_arch, '--jobs=' + str(jobs)] |
| 84 | if make_options: |
| 85 | command.extend(make_options) |
| 86 | if self._cross_compile: |
| 87 | command += ['CROSS_COMPILE=' + self._cross_compile] |
| 88 | if build_dir: |
| 89 | command += ['O=' + build_dir] |
| 90 | print('Building with:\n$', ' '.join(command)) |
| 91 | try: |
| 92 | proc = subprocess.Popen(command, |
| 93 | stderr=subprocess.PIPE, |
| 94 | stdout=subprocess.DEVNULL) |
| 95 | except OSError as e: |
| 96 | raise BuildError('Could not call execute make: ' + str(e)) |
| 97 | except subprocess.CalledProcessError as e: |
| 98 | raise BuildError(e.output) |
| 99 | _, stderr = proc.communicate() |
| 100 | if proc.returncode != 0: |
| 101 | raise BuildError(stderr.decode()) |
| 102 | if stderr: # likely only due to build warnings |
| 103 | print(stderr.decode()) |
| 104 | |
Daniel Latypov | 7d7c48d | 2021-10-04 18:13:40 -0700 | [diff] [blame] | 105 | def start(self, params: List[str], build_dir: str) -> subprocess.Popen: |
| 106 | raise RuntimeError('not implemented!') |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 107 | |
| 108 | |
| 109 | class LinuxSourceTreeOperationsQemu(LinuxSourceTreeOperations): |
| 110 | |
| 111 | def __init__(self, qemu_arch_params: qemu_config.QemuArchParams, cross_compile: Optional[str]): |
| 112 | super().__init__(linux_arch=qemu_arch_params.linux_arch, |
| 113 | cross_compile=cross_compile) |
| 114 | self._kconfig = qemu_arch_params.kconfig |
| 115 | self._qemu_arch = qemu_arch_params.qemu_arch |
| 116 | self._kernel_path = qemu_arch_params.kernel_path |
| 117 | self._kernel_command_line = qemu_arch_params.kernel_command_line + ' kunit_shutdown=reboot' |
| 118 | self._extra_qemu_params = qemu_arch_params.extra_qemu_params |
| 119 | |
| 120 | def make_arch_qemuconfig(self, base_kunitconfig: kunit_config.Kconfig) -> None: |
Daniel Latypov | 9897849 | 2021-11-05 18:30:57 -0700 | [diff] [blame] | 121 | kconfig = kunit_config.parse_from_string(self._kconfig) |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 122 | base_kunitconfig.merge_in_entries(kconfig) |
| 123 | |
Daniel Latypov | 7d7c48d | 2021-10-04 18:13:40 -0700 | [diff] [blame] | 124 | def start(self, params: List[str], build_dir: str) -> subprocess.Popen: |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 125 | kernel_path = os.path.join(build_dir, self._kernel_path) |
| 126 | qemu_command = ['qemu-system-' + self._qemu_arch, |
| 127 | '-nodefaults', |
| 128 | '-m', '1024', |
| 129 | '-kernel', kernel_path, |
| 130 | '-append', '\'' + ' '.join(params + [self._kernel_command_line]) + '\'', |
| 131 | '-no-reboot', |
| 132 | '-nographic', |
| 133 | '-serial stdio'] + self._extra_qemu_params |
| 134 | print('Running tests with:\n$', ' '.join(qemu_command)) |
Daniel Latypov | 7d7c48d | 2021-10-04 18:13:40 -0700 | [diff] [blame] | 135 | return subprocess.Popen(' '.join(qemu_command), |
| 136 | stdin=subprocess.PIPE, |
| 137 | stdout=subprocess.PIPE, |
| 138 | stderr=subprocess.STDOUT, |
Daniel Latypov | 2ab5d5e | 2021-10-20 16:21:21 -0700 | [diff] [blame] | 139 | text=True, shell=True, errors='backslashreplace') |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 140 | |
| 141 | class LinuxSourceTreeOperationsUml(LinuxSourceTreeOperations): |
| 142 | """An abstraction over command line operations performed on a source tree.""" |
| 143 | |
| 144 | def __init__(self, cross_compile=None): |
| 145 | super().__init__(linux_arch='um', cross_compile=cross_compile) |
| 146 | |
Daniel Latypov | 09641f7 | 2021-01-14 16:39:11 -0800 | [diff] [blame] | 147 | def make_allyesconfig(self, build_dir, make_options) -> None: |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 148 | kunit_parser.print_with_timestamp( |
| 149 | 'Enabling all CONFIGs for UML...') |
Brendan Higgins | 67e2fae | 2020-09-23 14:19:38 -0700 | [diff] [blame] | 150 | command = ['make', 'ARCH=um', 'allyesconfig'] |
| 151 | if make_options: |
| 152 | command.extend(make_options) |
| 153 | if build_dir: |
| 154 | command += ['O=' + build_dir] |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 155 | process = subprocess.Popen( |
Brendan Higgins | 67e2fae | 2020-09-23 14:19:38 -0700 | [diff] [blame] | 156 | command, |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 157 | stdout=subprocess.DEVNULL, |
| 158 | stderr=subprocess.STDOUT) |
| 159 | process.wait() |
| 160 | kunit_parser.print_with_timestamp( |
| 161 | 'Disabling broken configs to run KUnit tests...') |
Daniel Latypov | a54ea2e | 2021-09-28 15:11:11 -0700 | [diff] [blame] | 162 | |
| 163 | with open(get_kconfig_path(build_dir), 'a') as config: |
| 164 | with open(BROKEN_ALLCONFIG_PATH, 'r') as disable: |
| 165 | config.write(disable.read()) |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 166 | kunit_parser.print_with_timestamp( |
| 167 | 'Starting Kernel with all configs takes a few minutes...') |
| 168 | |
Daniel Latypov | 7d7c48d | 2021-10-04 18:13:40 -0700 | [diff] [blame] | 169 | def start(self, params: List[str], build_dir: str) -> subprocess.Popen: |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 170 | """Runs the Linux UML binary. Must be named 'linux'.""" |
Andy Shevchenko | f3ed003 | 2020-10-26 18:59:27 +0200 | [diff] [blame] | 171 | linux_bin = get_file_path(build_dir, 'linux') |
Daniel Latypov | 7d7c48d | 2021-10-04 18:13:40 -0700 | [diff] [blame] | 172 | return subprocess.Popen([linux_bin] + params, |
| 173 | stdin=subprocess.PIPE, |
| 174 | stdout=subprocess.PIPE, |
| 175 | stderr=subprocess.STDOUT, |
Daniel Latypov | 2ab5d5e | 2021-10-20 16:21:21 -0700 | [diff] [blame] | 176 | text=True, errors='backslashreplace') |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 177 | |
Daniel Latypov | 09641f7 | 2021-01-14 16:39:11 -0800 | [diff] [blame] | 178 | def get_kconfig_path(build_dir) -> str: |
Andy Shevchenko | f3ed003 | 2020-10-26 18:59:27 +0200 | [diff] [blame] | 179 | return get_file_path(build_dir, KCONFIG_PATH) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 180 | |
Daniel Latypov | 09641f7 | 2021-01-14 16:39:11 -0800 | [diff] [blame] | 181 | def get_kunitconfig_path(build_dir) -> str: |
Andy Shevchenko | f3ed003 | 2020-10-26 18:59:27 +0200 | [diff] [blame] | 182 | return get_file_path(build_dir, KUNITCONFIG_PATH) |
Andy Shevchenko | fcdb0bc | 2020-10-26 18:59:25 +0200 | [diff] [blame] | 183 | |
Daniel Latypov | 4c2911f | 2021-11-19 15:23:16 -0800 | [diff] [blame] | 184 | def get_old_kunitconfig_path(build_dir) -> str: |
| 185 | return get_file_path(build_dir, OLD_KUNITCONFIG_PATH) |
| 186 | |
Daniel Latypov | 09641f7 | 2021-01-14 16:39:11 -0800 | [diff] [blame] | 187 | def get_outfile_path(build_dir) -> str: |
Andy Shevchenko | f3ed003 | 2020-10-26 18:59:27 +0200 | [diff] [blame] | 188 | return get_file_path(build_dir, OUTFILE_PATH) |
Andy Shevchenko | 128dc4b | 2020-10-26 18:59:26 +0200 | [diff] [blame] | 189 | |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 190 | def get_source_tree_ops(arch: str, cross_compile: Optional[str]) -> LinuxSourceTreeOperations: |
| 191 | config_path = os.path.join(QEMU_CONFIGS_DIR, arch + '.py') |
| 192 | if arch == 'um': |
| 193 | return LinuxSourceTreeOperationsUml(cross_compile=cross_compile) |
| 194 | elif os.path.isfile(config_path): |
| 195 | return get_source_tree_ops_from_qemu_config(config_path, cross_compile)[1] |
Daniel Latypov | fe678fe | 2021-09-29 16:25:34 -0700 | [diff] [blame] | 196 | |
| 197 | options = [f[:-3] for f in os.listdir(QEMU_CONFIGS_DIR) if f.endswith('.py')] |
| 198 | raise ConfigError(arch + ' is not a valid arch, options are ' + str(sorted(options))) |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 199 | |
| 200 | def get_source_tree_ops_from_qemu_config(config_path: str, |
Daniel Latypov | 58c965d | 2021-06-23 12:09:19 -0700 | [diff] [blame] | 201 | cross_compile: Optional[str]) -> Tuple[ |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 202 | str, LinuxSourceTreeOperations]: |
| 203 | # The module name/path has very little to do with where the actual file |
| 204 | # exists (I learned this through experimentation and could not find it |
| 205 | # anywhere in the Python documentation). |
| 206 | # |
| 207 | # Bascially, we completely ignore the actual file location of the config |
| 208 | # we are loading and just tell Python that the module lives in the |
| 209 | # QEMU_CONFIGS_DIR for import purposes regardless of where it actually |
| 210 | # exists as a file. |
| 211 | module_path = '.' + os.path.join(os.path.basename(QEMU_CONFIGS_DIR), os.path.basename(config_path)) |
| 212 | spec = importlib.util.spec_from_file_location(module_path, config_path) |
Daniel Latypov | 85310a6 | 2021-12-15 12:39:23 -0800 | [diff] [blame] | 213 | assert spec is not None |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 214 | config = importlib.util.module_from_spec(spec) |
Daniel Latypov | 52a5d80 | 2021-10-21 17:49:36 -0700 | [diff] [blame] | 215 | # See https://github.com/python/typeshed/pull/2626 for context. |
| 216 | assert isinstance(spec.loader, importlib.abc.Loader) |
| 217 | spec.loader.exec_module(config) |
| 218 | |
| 219 | if not hasattr(config, 'QEMU_ARCH'): |
| 220 | raise ValueError('qemu_config module missing "QEMU_ARCH": ' + config_path) |
| 221 | params: qemu_config.QemuArchParams = config.QEMU_ARCH # type: ignore |
| 222 | return params.linux_arch, LinuxSourceTreeOperationsQemu( |
| 223 | params, cross_compile=cross_compile) |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 224 | |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 225 | class LinuxSourceTree(object): |
| 226 | """Represents a Linux kernel source tree with KUnit tests.""" |
| 227 | |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 228 | def __init__( |
| 229 | self, |
| 230 | build_dir: str, |
| 231 | load_config=True, |
| 232 | kunitconfig_path='', |
Daniel Latypov | 9f57cc7 | 2021-11-05 18:30:58 -0700 | [diff] [blame] | 233 | kconfig_add: Optional[List[str]]=None, |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 234 | arch=None, |
| 235 | cross_compile=None, |
| 236 | qemu_config_path=None) -> None: |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 237 | signal.signal(signal.SIGINT, self.signal_handler) |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 238 | if qemu_config_path: |
| 239 | self._arch, self._ops = get_source_tree_ops_from_qemu_config( |
| 240 | qemu_config_path, cross_compile) |
| 241 | else: |
| 242 | self._arch = 'um' if arch is None else arch |
| 243 | self._ops = get_source_tree_ops(self._arch, cross_compile) |
Daniel Latypov | 2b8fdbb | 2021-01-14 16:39:13 -0800 | [diff] [blame] | 244 | |
| 245 | if not load_config: |
| 246 | return |
| 247 | |
Daniel Latypov | 243180f | 2021-02-01 12:55:14 -0800 | [diff] [blame] | 248 | if kunitconfig_path: |
Daniel Latypov | 9854781 | 2021-02-22 14:52:41 -0800 | [diff] [blame] | 249 | if os.path.isdir(kunitconfig_path): |
| 250 | kunitconfig_path = os.path.join(kunitconfig_path, KUNITCONFIG_PATH) |
Daniel Latypov | 243180f | 2021-02-01 12:55:14 -0800 | [diff] [blame] | 251 | if not os.path.exists(kunitconfig_path): |
| 252 | raise ConfigError(f'Specified kunitconfig ({kunitconfig_path}) does not exist') |
| 253 | else: |
| 254 | kunitconfig_path = get_kunitconfig_path(build_dir) |
| 255 | if not os.path.exists(kunitconfig_path): |
| 256 | shutil.copyfile(DEFAULT_KUNITCONFIG_PATH, kunitconfig_path) |
Daniel Latypov | 2b8fdbb | 2021-01-14 16:39:13 -0800 | [diff] [blame] | 257 | |
Daniel Latypov | 9897849 | 2021-11-05 18:30:57 -0700 | [diff] [blame] | 258 | self._kconfig = kunit_config.parse_file(kunitconfig_path) |
Daniel Latypov | 9f57cc7 | 2021-11-05 18:30:58 -0700 | [diff] [blame] | 259 | if kconfig_add: |
| 260 | kconfig = kunit_config.parse_from_string('\n'.join(kconfig_add)) |
| 261 | self._kconfig.merge_in_entries(kconfig) |
| 262 | |
Daniel Latypov | 2b8fdbb | 2021-01-14 16:39:13 -0800 | [diff] [blame] | 263 | |
Daniel Latypov | 09641f7 | 2021-01-14 16:39:11 -0800 | [diff] [blame] | 264 | def clean(self) -> bool: |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 265 | try: |
| 266 | self._ops.make_mrproper() |
| 267 | except ConfigError as e: |
| 268 | logging.error(e) |
| 269 | return False |
| 270 | return True |
| 271 | |
Daniel Latypov | 09641f7 | 2021-01-14 16:39:11 -0800 | [diff] [blame] | 272 | def validate_config(self, build_dir) -> bool: |
Heidi Fahim | dde54b9 | 2019-11-26 14:36:16 -0800 | [diff] [blame] | 273 | kconfig_path = get_kconfig_path(build_dir) |
Daniel Latypov | 9897849 | 2021-11-05 18:30:57 -0700 | [diff] [blame] | 274 | validated_kconfig = kunit_config.parse_file(kconfig_path) |
Daniel Latypov | c44895b | 2021-11-11 10:33:56 -0800 | [diff] [blame] | 275 | if self._kconfig.is_subset_of(validated_kconfig): |
| 276 | return True |
| 277 | invalid = self._kconfig.entries() - validated_kconfig.entries() |
| 278 | message = 'Not all Kconfig options selected in kunitconfig were in the generated .config.\n' \ |
| 279 | 'This is probably due to unsatisfied dependencies.\n' \ |
| 280 | 'Missing: ' + ', '.join([str(e) for e in invalid]) |
| 281 | if self._arch == 'um': |
| 282 | message += '\nNote: many Kconfig options aren\'t available on UML. You can try running ' \ |
| 283 | 'on a different architecture with something like "--arch=x86_64".' |
| 284 | logging.error(message) |
| 285 | return False |
Heidi Fahim | dde54b9 | 2019-11-26 14:36:16 -0800 | [diff] [blame] | 286 | |
Daniel Latypov | 09641f7 | 2021-01-14 16:39:11 -0800 | [diff] [blame] | 287 | def build_config(self, build_dir, make_options) -> bool: |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 288 | kconfig_path = get_kconfig_path(build_dir) |
| 289 | if build_dir and not os.path.exists(build_dir): |
| 290 | os.mkdir(build_dir) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 291 | try: |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 292 | self._ops.make_arch_qemuconfig(self._kconfig) |
| 293 | self._kconfig.write_to_file(kconfig_path) |
Greg Thelen | 0476e69 | 2020-03-23 12:04:59 -0700 | [diff] [blame] | 294 | self._ops.make_olddefconfig(build_dir, make_options) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 295 | except ConfigError as e: |
| 296 | logging.error(e) |
| 297 | return False |
Daniel Latypov | 4c2911f | 2021-11-19 15:23:16 -0800 | [diff] [blame] | 298 | if not self.validate_config(build_dir): |
| 299 | return False |
| 300 | |
| 301 | old_path = get_old_kunitconfig_path(build_dir) |
| 302 | if os.path.exists(old_path): |
| 303 | os.remove(old_path) # write_to_file appends to the file |
| 304 | self._kconfig.write_to_file(old_path) |
| 305 | return True |
| 306 | |
| 307 | def _kunitconfig_changed(self, build_dir: str) -> bool: |
| 308 | old_path = get_old_kunitconfig_path(build_dir) |
| 309 | if not os.path.exists(old_path): |
| 310 | return True |
| 311 | |
| 312 | old_kconfig = kunit_config.parse_file(old_path) |
| 313 | return old_kconfig.entries() != self._kconfig.entries() |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 314 | |
Daniel Latypov | 09641f7 | 2021-01-14 16:39:11 -0800 | [diff] [blame] | 315 | def build_reconfig(self, build_dir, make_options) -> bool: |
SeongJae Park | 14ee5cfd | 2019-12-20 05:14:07 +0000 | [diff] [blame] | 316 | """Creates a new .config if it is not a subset of the .kunitconfig.""" |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 317 | kconfig_path = get_kconfig_path(build_dir) |
Daniel Latypov | 4c2911f | 2021-11-19 15:23:16 -0800 | [diff] [blame] | 318 | if not os.path.exists(kconfig_path): |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 319 | print('Generating .config ...') |
Greg Thelen | 0476e69 | 2020-03-23 12:04:59 -0700 | [diff] [blame] | 320 | return self.build_config(build_dir, make_options) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 321 | |
Daniel Latypov | 4c2911f | 2021-11-19 15:23:16 -0800 | [diff] [blame] | 322 | existing_kconfig = kunit_config.parse_file(kconfig_path) |
| 323 | self._ops.make_arch_qemuconfig(self._kconfig) |
| 324 | if self._kconfig.is_subset_of(existing_kconfig) and not self._kunitconfig_changed(build_dir): |
| 325 | return True |
| 326 | print('Regenerating .config ...') |
| 327 | os.remove(kconfig_path) |
| 328 | return self.build_config(build_dir, make_options) |
| 329 | |
Brendan Higgins | 87c9c16 | 2021-05-26 14:24:06 -0700 | [diff] [blame] | 330 | def build_kernel(self, alltests, jobs, build_dir, make_options) -> bool: |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 331 | try: |
Brendan Higgins | 67e2fae | 2020-09-23 14:19:38 -0700 | [diff] [blame] | 332 | if alltests: |
| 333 | self._ops.make_allyesconfig(build_dir, make_options) |
Greg Thelen | 0476e69 | 2020-03-23 12:04:59 -0700 | [diff] [blame] | 334 | self._ops.make_olddefconfig(build_dir, make_options) |
| 335 | self._ops.make(jobs, build_dir, make_options) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 336 | except (ConfigError, BuildError) as e: |
| 337 | logging.error(e) |
| 338 | return False |
Heidi Fahim | dde54b9 | 2019-11-26 14:36:16 -0800 | [diff] [blame] | 339 | return self.validate_config(build_dir) |
Felix Guo | 6ebf586 | 2019-09-23 02:02:43 -0700 | [diff] [blame] | 340 | |
Daniel Latypov | 7af2914 | 2021-02-05 16:08:54 -0800 | [diff] [blame] | 341 | def run_kernel(self, args=None, build_dir='', filter_glob='', timeout=None) -> Iterator[str]: |
| 342 | if not args: |
| 343 | args = [] |
David Gow | b6d5799 | 2021-05-26 14:24:04 -0700 | [diff] [blame] | 344 | args.extend(['mem=1G', 'console=tty', 'kunit_shutdown=halt']) |
Daniel Latypov | d992880b | 2021-02-05 16:08:53 -0800 | [diff] [blame] | 345 | if filter_glob: |
| 346 | args.append('kunit.filter_glob='+filter_glob) |
Daniel Latypov | 7d7c48d | 2021-10-04 18:13:40 -0700 | [diff] [blame] | 347 | |
| 348 | process = self._ops.start(args, build_dir) |
| 349 | assert process.stdout is not None # tell mypy it's set |
| 350 | |
| 351 | # Enforce the timeout in a background thread. |
| 352 | def _wait_proc(): |
| 353 | try: |
| 354 | process.wait(timeout=timeout) |
| 355 | except Exception as e: |
| 356 | print(e) |
| 357 | process.terminate() |
| 358 | process.wait() |
| 359 | waiter = threading.Thread(target=_wait_proc) |
| 360 | waiter.start() |
| 361 | |
| 362 | output = open(get_outfile_path(build_dir), 'w') |
| 363 | try: |
| 364 | # Tee the output to the file and to our caller in real time. |
| 365 | for line in process.stdout: |
| 366 | output.write(line) |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 367 | yield line |
Daniel Latypov | 7d7c48d | 2021-10-04 18:13:40 -0700 | [diff] [blame] | 368 | # This runs even if our caller doesn't consume every line. |
| 369 | finally: |
| 370 | # Flush any leftover output to the file |
| 371 | output.write(process.stdout.read()) |
| 372 | output.close() |
| 373 | process.stdout.close() |
| 374 | |
| 375 | waiter.join() |
| 376 | subprocess.call(['stty', 'sane']) |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 377 | |
Daniel Latypov | 09641f7 | 2021-01-14 16:39:11 -0800 | [diff] [blame] | 378 | def signal_handler(self, sig, frame) -> None: |
Heidi Fahim | 021ed9f | 2020-03-16 13:21:25 -0700 | [diff] [blame] | 379 | logging.error('Build interruption occurred. Cleaning console.') |
| 380 | subprocess.call(['stty', 'sane']) |