Simon Glass | 793dca3 | 2019-10-31 07:42:57 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 3 | |
Simon Glass | f876e96 | 2024-07-17 16:56:52 +0100 | [diff] [blame] | 4 | """Build and query a Kconfig database for boards. |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 5 | |
Simon Glass | 495e58c | 2024-07-17 16:56:54 +0100 | [diff] [blame] | 6 | See doc/develop/qconfig.rst for documentation. |
Simon Glass | f876e96 | 2024-07-17 16:56:52 +0100 | [diff] [blame] | 7 | |
| 8 | Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
| 9 | Author: Simon Glass <sjg@chromium.org> |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 10 | """ |
| 11 | |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 12 | from argparse import ArgumentParser |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 13 | import collections |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 14 | from contextlib import ExitStack |
Simon Glass | 84067a5 | 2021-12-18 08:09:45 -0700 | [diff] [blame] | 15 | import doctest |
Masahiro Yamada | c8e1b10 | 2016-05-19 15:52:07 +0900 | [diff] [blame] | 16 | import filecmp |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 17 | import fnmatch |
Masahiro Yamada | 0dbc9b5 | 2016-10-19 14:39:54 +0900 | [diff] [blame] | 18 | import glob |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 19 | import multiprocessing |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 20 | import os |
Simon Glass | 793dca3 | 2019-10-31 07:42:57 -0600 | [diff] [blame] | 21 | import queue |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 22 | import re |
| 23 | import shutil |
| 24 | import subprocess |
| 25 | import sys |
| 26 | import tempfile |
Simon Glass | d73fcb1 | 2017-06-01 19:39:02 -0600 | [diff] [blame] | 27 | import threading |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 28 | import time |
Simon Glass | 84067a5 | 2021-12-18 08:09:45 -0700 | [diff] [blame] | 29 | import unittest |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 30 | |
Simon Glass | 0ede00f | 2020-04-17 18:09:02 -0600 | [diff] [blame] | 31 | from buildman import bsettings |
| 32 | from buildman import kconfiglib |
| 33 | from buildman import toolchain |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 34 | from u_boot_pylib import terminal |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 35 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 36 | SHOW_GNU_MAKE = 'scripts/show-gnu-make' |
| 37 | SLEEP_TIME=0.03 |
| 38 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 39 | STATE_IDLE = 0 |
| 40 | STATE_DEFCONFIG = 1 |
| 41 | STATE_AUTOCONF = 2 |
Joe Hershberger | 96464ba | 2015-05-19 13:21:17 -0500 | [diff] [blame] | 42 | STATE_SAVEDEFCONFIG = 3 |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 43 | |
Simon Glass | f3b8e64 | 2017-06-01 19:39:01 -0600 | [diff] [blame] | 44 | AUTO_CONF_PATH = 'include/config/auto.conf' |
Simon Glass | 5196379 | 2023-09-23 13:44:15 -0600 | [diff] [blame] | 45 | CONFIG_DATABASE = 'qconfig.db' |
| 46 | FAILED_LIST = 'qconfig.failed' |
Simon Glass | f3b8e64 | 2017-06-01 19:39:01 -0600 | [diff] [blame] | 47 | |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 48 | CONFIG_LEN = len('CONFIG_') |
Simon Glass | f3b8e64 | 2017-06-01 19:39:01 -0600 | [diff] [blame] | 49 | |
Markus Klotzbuecher | b237d35 | 2019-05-15 15:15:52 +0200 | [diff] [blame] | 50 | SIZES = { |
Simon Glass | daa694d | 2021-12-18 14:54:30 -0700 | [diff] [blame] | 51 | 'SZ_1': 0x00000001, 'SZ_2': 0x00000002, |
| 52 | 'SZ_4': 0x00000004, 'SZ_8': 0x00000008, |
| 53 | 'SZ_16': 0x00000010, 'SZ_32': 0x00000020, |
| 54 | 'SZ_64': 0x00000040, 'SZ_128': 0x00000080, |
| 55 | 'SZ_256': 0x00000100, 'SZ_512': 0x00000200, |
| 56 | 'SZ_1K': 0x00000400, 'SZ_2K': 0x00000800, |
| 57 | 'SZ_4K': 0x00001000, 'SZ_8K': 0x00002000, |
| 58 | 'SZ_16K': 0x00004000, 'SZ_32K': 0x00008000, |
| 59 | 'SZ_64K': 0x00010000, 'SZ_128K': 0x00020000, |
| 60 | 'SZ_256K': 0x00040000, 'SZ_512K': 0x00080000, |
| 61 | 'SZ_1M': 0x00100000, 'SZ_2M': 0x00200000, |
| 62 | 'SZ_4M': 0x00400000, 'SZ_8M': 0x00800000, |
| 63 | 'SZ_16M': 0x01000000, 'SZ_32M': 0x02000000, |
| 64 | 'SZ_64M': 0x04000000, 'SZ_128M': 0x08000000, |
| 65 | 'SZ_256M': 0x10000000, 'SZ_512M': 0x20000000, |
| 66 | 'SZ_1G': 0x40000000, 'SZ_2G': 0x80000000, |
| 67 | 'SZ_4G': 0x100000000 |
Markus Klotzbuecher | b237d35 | 2019-05-15 15:15:52 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Simon Glass | b8d11da | 2022-02-08 11:49:45 -0700 | [diff] [blame] | 70 | RE_REMOVE_DEFCONFIG = re.compile(r'(.*)_defconfig') |
| 71 | |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 72 | # CONFIG symbols present in the build system (from Linux) but not actually used |
| 73 | # in U-Boot; KCONFIG symbols |
| 74 | IGNORE_SYMS = ['DEBUG_SECTION_MISMATCH', 'FTRACE_MCOUNT_RECORD', 'GCOV_KERNEL', |
| 75 | 'GCOV_PROFILE_ALL', 'KALLSYMS', 'KASAN', 'MODVERSIONS', 'SHELL', |
| 76 | 'TPL_BUILD', 'VPL_BUILD', 'IS_ENABLED', 'FOO', 'IF_ENABLED_INT', |
| 77 | 'IS_ENABLED_', 'IS_ENABLED_1', 'IS_ENABLED_2', 'IS_ENABLED_3', |
| 78 | 'SPL_', 'TPL_', 'SPL_FOO', 'TPL_FOO', 'TOOLS_FOO', |
| 79 | 'ACME', 'SPL_ACME', 'TPL_ACME', 'TRACE_BRANCH_PROFILING', |
| 80 | 'VAL', '_UNDEFINED', 'SPL_BUILD', ] |
| 81 | |
| 82 | SPL_PREFIXES = ['SPL_', 'TPL_', 'VPL_', 'TOOLS_'] |
| 83 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 84 | ### helper functions ### |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 85 | def check_top_directory(): |
| 86 | """Exit if we are not at the top of source directory.""" |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 87 | for fname in 'README', 'Licenses': |
| 88 | if not os.path.exists(fname): |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 89 | sys.exit('Please run at the top of source directory.') |
| 90 | |
Masahiro Yamada | bd63e5b | 2016-05-19 15:51:54 +0900 | [diff] [blame] | 91 | def check_clean_directory(): |
| 92 | """Exit if the source tree is not clean.""" |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 93 | for fname in '.config', 'include/config': |
| 94 | if os.path.exists(fname): |
Masahiro Yamada | bd63e5b | 2016-05-19 15:51:54 +0900 | [diff] [blame] | 95 | sys.exit("source tree is not clean, please run 'make mrproper'") |
| 96 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 97 | def get_make_cmd(): |
| 98 | """Get the command name of GNU Make. |
| 99 | |
| 100 | U-Boot needs GNU Make for building, but the command name is not |
| 101 | necessarily "make". (for example, "gmake" on FreeBSD). |
| 102 | Returns the most appropriate command name on your system. |
| 103 | """ |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 104 | with subprocess.Popen([SHOW_GNU_MAKE], stdout=subprocess.PIPE) as proc: |
| 105 | ret = proc.communicate() |
| 106 | if proc.returncode: |
| 107 | sys.exit('GNU Make not found') |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 108 | return ret[0].rstrip() |
| 109 | |
Simon Glass | 25f978c | 2017-06-01 19:38:58 -0600 | [diff] [blame] | 110 | def get_matched_defconfig(line): |
| 111 | """Get the defconfig files that match a pattern |
| 112 | |
| 113 | Args: |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 114 | line (str): Path or filename to match, e.g. 'configs/snow_defconfig' or |
Simon Glass | 25f978c | 2017-06-01 19:38:58 -0600 | [diff] [blame] | 115 | 'k2*_defconfig'. If no directory is provided, 'configs/' is |
| 116 | prepended |
| 117 | |
| 118 | Returns: |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 119 | list of str: a list of matching defconfig files |
Simon Glass | 25f978c | 2017-06-01 19:38:58 -0600 | [diff] [blame] | 120 | """ |
| 121 | dirname = os.path.dirname(line) |
| 122 | if dirname: |
| 123 | pattern = line |
| 124 | else: |
| 125 | pattern = os.path.join('configs', line) |
| 126 | return glob.glob(pattern) + glob.glob(pattern + '_defconfig') |
| 127 | |
Masahiro Yamada | 0dbc9b5 | 2016-10-19 14:39:54 +0900 | [diff] [blame] | 128 | def get_matched_defconfigs(defconfigs_file): |
Simon Glass | ee4e61b | 2017-06-01 19:38:59 -0600 | [diff] [blame] | 129 | """Get all the defconfig files that match the patterns in a file. |
| 130 | |
| 131 | Args: |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 132 | defconfigs_file (str): File containing a list of defconfigs to process, |
| 133 | or '-' to read the list from stdin |
Simon Glass | ee4e61b | 2017-06-01 19:38:59 -0600 | [diff] [blame] | 134 | |
| 135 | Returns: |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 136 | list of str: A list of paths to defconfig files, with no duplicates |
Simon Glass | ee4e61b | 2017-06-01 19:38:59 -0600 | [diff] [blame] | 137 | """ |
Masahiro Yamada | 0dbc9b5 | 2016-10-19 14:39:54 +0900 | [diff] [blame] | 138 | defconfigs = [] |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 139 | with ExitStack() as stack: |
| 140 | if defconfigs_file == '-': |
| 141 | inf = sys.stdin |
| 142 | defconfigs_file = 'stdin' |
| 143 | else: |
| 144 | inf = stack.enter_context(open(defconfigs_file, encoding='utf-8')) |
| 145 | for i, line in enumerate(inf): |
| 146 | line = line.strip() |
| 147 | if not line: |
| 148 | continue # skip blank lines silently |
| 149 | if ' ' in line: |
| 150 | line = line.split(' ')[0] # handle 'git log' input |
| 151 | matched = get_matched_defconfig(line) |
| 152 | if not matched: |
| 153 | print(f"warning: {defconfigs_file}:{i + 1}: no defconfig matched '{line}'", |
| 154 | file=sys.stderr) |
Masahiro Yamada | 0dbc9b5 | 2016-10-19 14:39:54 +0900 | [diff] [blame] | 155 | |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 156 | defconfigs += matched |
Masahiro Yamada | 0dbc9b5 | 2016-10-19 14:39:54 +0900 | [diff] [blame] | 157 | |
| 158 | # use set() to drop multiple matching |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 159 | return [defconfig[len('configs') + 1:] for defconfig in set(defconfigs)] |
Masahiro Yamada | 0dbc9b5 | 2016-10-19 14:39:54 +0900 | [diff] [blame] | 160 | |
Masahiro Yamada | 684c306 | 2016-07-25 19:15:28 +0900 | [diff] [blame] | 161 | def get_all_defconfigs(): |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 162 | """Get all the defconfig files under the configs/ directory. |
| 163 | |
| 164 | Returns: |
| 165 | list of str: List of paths to defconfig files |
| 166 | """ |
Masahiro Yamada | 684c306 | 2016-07-25 19:15:28 +0900 | [diff] [blame] | 167 | defconfigs = [] |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 168 | for (dirpath, _, filenames) in os.walk('configs'): |
Masahiro Yamada | 684c306 | 2016-07-25 19:15:28 +0900 | [diff] [blame] | 169 | dirpath = dirpath[len('configs') + 1:] |
| 170 | for filename in fnmatch.filter(filenames, '*_defconfig'): |
| 171 | defconfigs.append(os.path.join(dirpath, filename)) |
| 172 | |
| 173 | return defconfigs |
| 174 | |
Simon Glass | 2fd85bd | 2021-12-18 14:54:33 -0700 | [diff] [blame] | 175 | def write_file(fname, data): |
| 176 | """Write data to a file |
| 177 | |
| 178 | Args: |
| 179 | fname (str): Filename to write to |
| 180 | data (list of str): Lines to write (with or without trailing newline); |
| 181 | or str to write |
| 182 | """ |
| 183 | with open(fname, 'w', encoding='utf-8') as out: |
| 184 | if isinstance(data, list): |
| 185 | for line in data: |
| 186 | print(line.rstrip('\n'), file=out) |
| 187 | else: |
| 188 | out.write(data) |
| 189 | |
Simon Glass | 37f815c | 2021-12-18 14:54:34 -0700 | [diff] [blame] | 190 | def read_file(fname, as_lines=True, skip_unicode=False): |
| 191 | """Read a file and return the contents |
| 192 | |
| 193 | Args: |
| 194 | fname (str): Filename to read from |
Simon Glass | 549d422 | 2023-09-23 13:43:58 -0600 | [diff] [blame] | 195 | as_lines (bool): Return file contents as a list of lines |
Simon Glass | 37f815c | 2021-12-18 14:54:34 -0700 | [diff] [blame] | 196 | skip_unicode (bool): True to report unicode errors and continue |
| 197 | |
| 198 | Returns: |
| 199 | iter of str: List of ;ines from the file with newline removed; str if |
| 200 | as_lines is False with newlines intact; or None if a unicode error |
| 201 | occurred |
| 202 | |
| 203 | Raises: |
| 204 | UnicodeDecodeError: Unicode error occurred when reading |
| 205 | """ |
| 206 | with open(fname, encoding='utf-8') as inf: |
| 207 | try: |
| 208 | if as_lines: |
| 209 | return [line.rstrip('\n') for line in inf.readlines()] |
Simon Glass | f297ba3 | 2023-09-23 13:44:05 -0600 | [diff] [blame] | 210 | return inf.read() |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 211 | except UnicodeDecodeError as exc: |
Simon Glass | 37f815c | 2021-12-18 14:54:34 -0700 | [diff] [blame] | 212 | if not skip_unicode: |
Simon Glass | 68a0b71 | 2022-02-11 13:23:22 -0700 | [diff] [blame] | 213 | raise |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 214 | print(f"Failed on file '{fname}: {exc}") |
Simon Glass | 37f815c | 2021-12-18 14:54:34 -0700 | [diff] [blame] | 215 | return None |
| 216 | |
Chris Packham | ca43834 | 2017-05-02 21:30:47 +1200 | [diff] [blame] | 217 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 218 | ### classes ### |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 219 | class Progress: |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 220 | """Progress Indicator""" |
| 221 | |
Simon Glass | 6b25d21 | 2023-09-23 13:44:10 -0600 | [diff] [blame] | 222 | def __init__(self, col, total): |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 223 | """Create a new progress indicator. |
| 224 | |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 225 | Args: |
Simon Glass | 0229eee | 2024-07-17 16:57:07 +0100 | [diff] [blame^] | 226 | col (terminal.Color): Colour-output class |
Simon Glass | 6b25d21 | 2023-09-23 13:44:10 -0600 | [diff] [blame] | 227 | total (int): A number of defconfig files to process. |
Simon Glass | 0229eee | 2024-07-17 16:57:07 +0100 | [diff] [blame^] | 228 | |
| 229 | current (int): Number of boards processed so far |
| 230 | failed (int): Number of failed boards |
| 231 | failure_msg (str): Message indicating number of failures, '' if none |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 232 | """ |
Simon Glass | 6b25d21 | 2023-09-23 13:44:10 -0600 | [diff] [blame] | 233 | self.col = col |
Simon Glass | 0229eee | 2024-07-17 16:57:07 +0100 | [diff] [blame^] | 234 | self.total = total |
| 235 | |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 236 | self.current = 0 |
Simon Glass | 6b25d21 | 2023-09-23 13:44:10 -0600 | [diff] [blame] | 237 | self.good = 0 |
Simon Glass | 0229eee | 2024-07-17 16:57:07 +0100 | [diff] [blame^] | 238 | self.failed = None |
| 239 | self.failure_msg = None |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 240 | |
Simon Glass | 6b25d21 | 2023-09-23 13:44:10 -0600 | [diff] [blame] | 241 | def inc(self, success): |
| 242 | """Increment the number of processed defconfig files. |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 243 | |
Simon Glass | 6b25d21 | 2023-09-23 13:44:10 -0600 | [diff] [blame] | 244 | Args: |
| 245 | success (bool): True if processing succeeded |
| 246 | """ |
| 247 | self.good += success |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 248 | self.current += 1 |
| 249 | |
| 250 | def show(self): |
| 251 | """Display the progress.""" |
Simon Glass | 95f0914 | 2023-09-23 13:44:08 -0600 | [diff] [blame] | 252 | if self.current != self.total: |
Simon Glass | 6b25d21 | 2023-09-23 13:44:10 -0600 | [diff] [blame] | 253 | line = self.col.build(self.col.GREEN, f'{self.good:5d}') |
| 254 | line += self.col.build(self.col.RED, |
| 255 | f'{self.current - self.good:5d}') |
| 256 | line += self.col.build(self.col.MAGENTA, |
| 257 | f'/{self.total - self.current}') |
| 258 | print(f'{line} \r', end='') |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 259 | sys.stdout.flush() |
| 260 | |
Simon Glass | 0229eee | 2024-07-17 16:57:07 +0100 | [diff] [blame^] | 261 | def completed(self): |
| 262 | """Set up extra properties when completed""" |
| 263 | self.failed = self.total - self.good |
| 264 | self.failure_msg = f'{self.failed} failed, ' if self.failed else '' |
| 265 | |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 266 | |
Simon Glass | 0a0c124 | 2024-07-17 16:56:51 +0100 | [diff] [blame] | 267 | def scan_kconfig(): |
| 268 | """Scan all the Kconfig files and create a Config object |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 269 | |
Simon Glass | 0a0c124 | 2024-07-17 16:56:51 +0100 | [diff] [blame] | 270 | Returns: |
| 271 | Kconfig object |
| 272 | """ |
| 273 | # Define environment variables referenced from Kconfig |
| 274 | os.environ['srctree'] = os.getcwd() |
| 275 | os.environ['UBOOTVERSION'] = 'dummy' |
| 276 | os.environ['KCONFIG_OBJDIR'] = '' |
| 277 | os.environ['CC'] = 'gcc' |
| 278 | return kconfiglib.Kconfig() |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 279 | |
| 280 | |
Simon Glass | f876e96 | 2024-07-17 16:56:52 +0100 | [diff] [blame] | 281 | # pylint: disable=R0903 |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 282 | class KconfigParser: |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 283 | """A parser of .config and include/autoconf.mk.""" |
| 284 | |
| 285 | re_arch = re.compile(r'CONFIG_SYS_ARCH="(.*)"') |
| 286 | re_cpu = re.compile(r'CONFIG_SYS_CPU="(.*)"') |
| 287 | |
Simon Glass | 882c8e4 | 2023-09-23 13:43:54 -0600 | [diff] [blame] | 288 | def __init__(self, args, build_dir): |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 289 | """Create a new parser. |
| 290 | |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 291 | Args: |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 292 | args (Namespace): program arguments |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 293 | build_dir: Build directory. |
| 294 | """ |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 295 | self.args = args |
Masahiro Yamada | 1f16992 | 2016-05-19 15:52:00 +0900 | [diff] [blame] | 296 | self.dotconfig = os.path.join(build_dir, '.config') |
| 297 | self.autoconf = os.path.join(build_dir, 'include', 'autoconf.mk') |
Masahiro Yamada | 07913d1 | 2016-08-22 22:18:22 +0900 | [diff] [blame] | 298 | self.spl_autoconf = os.path.join(build_dir, 'spl', 'include', |
| 299 | 'autoconf.mk') |
Simon Glass | f3b8e64 | 2017-06-01 19:39:01 -0600 | [diff] [blame] | 300 | self.config_autoconf = os.path.join(build_dir, AUTO_CONF_PATH) |
Masahiro Yamada | 5da4f85 | 2016-05-19 15:52:06 +0900 | [diff] [blame] | 301 | self.defconfig = os.path.join(build_dir, 'defconfig') |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 302 | |
Simon Glass | 6821a74 | 2017-07-10 14:47:47 -0600 | [diff] [blame] | 303 | def get_arch(self): |
| 304 | """Parse .config file and return the architecture. |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 305 | |
| 306 | Returns: |
Simon Glass | 6821a74 | 2017-07-10 14:47:47 -0600 | [diff] [blame] | 307 | Architecture name (e.g. 'arm'). |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 308 | """ |
| 309 | arch = '' |
| 310 | cpu = '' |
Simon Glass | 37f815c | 2021-12-18 14:54:34 -0700 | [diff] [blame] | 311 | for line in read_file(self.dotconfig): |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 312 | m_arch = self.re_arch.match(line) |
| 313 | if m_arch: |
| 314 | arch = m_arch.group(1) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 315 | continue |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 316 | m_cpu = self.re_cpu.match(line) |
| 317 | if m_cpu: |
| 318 | cpu = m_cpu.group(1) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 319 | |
Masahiro Yamada | 90ed6cb | 2016-05-19 15:51:53 +0900 | [diff] [blame] | 320 | if not arch: |
| 321 | return None |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 322 | |
| 323 | # fix-up for aarch64 |
| 324 | if arch == 'arm' and cpu == 'armv8': |
| 325 | arch = 'aarch64' |
| 326 | |
Simon Glass | 6821a74 | 2017-07-10 14:47:47 -0600 | [diff] [blame] | 327 | return arch |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 328 | |
Simon Glass | d73fcb1 | 2017-06-01 19:39:02 -0600 | [diff] [blame] | 329 | |
| 330 | class DatabaseThread(threading.Thread): |
| 331 | """This thread processes results from Slot threads. |
| 332 | |
| 333 | It collects the data in the master config directary. There is only one |
| 334 | result thread, and this helps to serialise the build output. |
| 335 | """ |
| 336 | def __init__(self, config_db, db_queue): |
| 337 | """Set up a new result thread |
| 338 | |
| 339 | Args: |
| 340 | builder: Builder which will be sent each result |
| 341 | """ |
| 342 | threading.Thread.__init__(self) |
| 343 | self.config_db = config_db |
| 344 | self.db_queue= db_queue |
| 345 | |
| 346 | def run(self): |
| 347 | """Called to start up the result thread. |
| 348 | |
| 349 | We collect the next result job and pass it on to the build. |
| 350 | """ |
| 351 | while True: |
| 352 | defconfig, configs = self.db_queue.get() |
| 353 | self.config_db[defconfig] = configs |
| 354 | self.db_queue.task_done() |
| 355 | |
| 356 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 357 | class Slot: |
| 358 | |
| 359 | """A slot to store a subprocess. |
| 360 | |
| 361 | Each instance of this class handles one subprocess. |
| 362 | This class is useful to control multiple threads |
| 363 | for faster processing. |
| 364 | """ |
| 365 | |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 366 | def __init__(self, toolchains, args, progress, devnull, make_cmd, |
| 367 | reference_src_dir, db_queue, col): |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 368 | """Create a new process slot. |
| 369 | |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 370 | Args: |
Simon Glass | 6821a74 | 2017-07-10 14:47:47 -0600 | [diff] [blame] | 371 | toolchains: Toolchains object containing toolchains. |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 372 | args: Program arguments |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 373 | progress: A progress indicator. |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 374 | devnull: A file object of '/dev/null'. |
| 375 | make_cmd: command name of GNU Make. |
Joe Hershberger | 6b96c1a | 2016-06-10 14:53:32 -0500 | [diff] [blame] | 376 | reference_src_dir: Determine the true starting config state from this |
| 377 | source tree. |
Simon Glass | d73fcb1 | 2017-06-01 19:39:02 -0600 | [diff] [blame] | 378 | db_queue: output queue to write config info for the database |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 379 | col (terminal.Color): Colour object |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 380 | """ |
Simon Glass | 6821a74 | 2017-07-10 14:47:47 -0600 | [diff] [blame] | 381 | self.toolchains = toolchains |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 382 | self.args = args |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 383 | self.progress = progress |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 384 | self.build_dir = tempfile.mkdtemp() |
| 385 | self.devnull = devnull |
| 386 | self.make_cmd = (make_cmd, 'O=' + self.build_dir) |
Joe Hershberger | 6b96c1a | 2016-06-10 14:53:32 -0500 | [diff] [blame] | 387 | self.reference_src_dir = reference_src_dir |
Simon Glass | d73fcb1 | 2017-06-01 19:39:02 -0600 | [diff] [blame] | 388 | self.db_queue = db_queue |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 389 | self.col = col |
Simon Glass | 882c8e4 | 2023-09-23 13:43:54 -0600 | [diff] [blame] | 390 | self.parser = KconfigParser(args, self.build_dir) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 391 | self.state = STATE_IDLE |
Masahiro Yamada | 09c6c06 | 2016-08-22 22:18:20 +0900 | [diff] [blame] | 392 | self.failed_boards = set() |
Simon Glass | a6ab4db | 2023-09-23 13:44:02 -0600 | [diff] [blame] | 393 | self.defconfig = None |
Simon Glass | 9461bf0 | 2023-09-23 13:44:07 -0600 | [diff] [blame] | 394 | self.log = [] |
Simon Glass | a6ab4db | 2023-09-23 13:44:02 -0600 | [diff] [blame] | 395 | self.current_src_dir = None |
| 396 | self.proc = None |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 397 | |
| 398 | def __del__(self): |
| 399 | """Delete the working directory |
| 400 | |
| 401 | This function makes sure the temporary directory is cleaned away |
| 402 | even if Python suddenly dies due to error. It should be done in here |
Joe Hershberger | f2dae75 | 2016-06-10 14:53:29 -0500 | [diff] [blame] | 403 | because it is guaranteed the destructor is always invoked when the |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 404 | instance of the class gets unreferenced. |
| 405 | |
| 406 | If the subprocess is still running, wait until it finishes. |
| 407 | """ |
| 408 | if self.state != STATE_IDLE: |
Simon Glass | f297ba3 | 2023-09-23 13:44:05 -0600 | [diff] [blame] | 409 | while self.proc.poll() is None: |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 410 | pass |
| 411 | shutil.rmtree(self.build_dir) |
| 412 | |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 413 | def add(self, defconfig): |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 414 | """Assign a new subprocess for defconfig and add it to the slot. |
| 415 | |
| 416 | If the slot is vacant, create a new subprocess for processing the |
| 417 | given defconfig and add it to the slot. Just returns False if |
| 418 | the slot is occupied (i.e. the current subprocess is still running). |
| 419 | |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 420 | Args: |
Simon Glass | 549d422 | 2023-09-23 13:43:58 -0600 | [diff] [blame] | 421 | defconfig (str): defconfig name. |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 422 | |
| 423 | Returns: |
| 424 | Return True on success or False on failure |
| 425 | """ |
| 426 | if self.state != STATE_IDLE: |
| 427 | return False |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 428 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 429 | self.defconfig = defconfig |
Simon Glass | 9461bf0 | 2023-09-23 13:44:07 -0600 | [diff] [blame] | 430 | self.log = [] |
Masahiro Yamada | f432c33 | 2016-06-15 14:33:52 +0900 | [diff] [blame] | 431 | self.current_src_dir = self.reference_src_dir |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 432 | self.do_defconfig() |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 433 | return True |
| 434 | |
| 435 | def poll(self): |
| 436 | """Check the status of the subprocess and handle it as needed. |
| 437 | |
| 438 | Returns True if the slot is vacant (i.e. in idle state). |
| 439 | If the configuration is successfully finished, assign a new |
| 440 | subprocess to build include/autoconf.mk. |
| 441 | If include/autoconf.mk is generated, invoke the parser to |
Masahiro Yamada | 7fb0bac | 2016-05-19 15:52:04 +0900 | [diff] [blame] | 442 | parse the .config and the include/autoconf.mk, moving |
| 443 | config options to the .config as needed. |
| 444 | If the .config was updated, run "make savedefconfig" to sync |
| 445 | it, update the original defconfig, and then set the slot back |
| 446 | to the idle state. |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 447 | |
| 448 | Returns: |
| 449 | Return True if the subprocess is terminated, False otherwise |
| 450 | """ |
| 451 | if self.state == STATE_IDLE: |
| 452 | return True |
| 453 | |
Simon Glass | f297ba3 | 2023-09-23 13:44:05 -0600 | [diff] [blame] | 454 | if self.proc.poll() is None: |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 455 | return False |
| 456 | |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 457 | if self.proc.poll() != 0: |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 458 | self.handle_error() |
| 459 | elif self.state == STATE_DEFCONFIG: |
Masahiro Yamada | f432c33 | 2016-06-15 14:33:52 +0900 | [diff] [blame] | 460 | if self.reference_src_dir and not self.current_src_dir: |
Joe Hershberger | 6b96c1a | 2016-06-10 14:53:32 -0500 | [diff] [blame] | 461 | self.do_savedefconfig() |
| 462 | else: |
| 463 | self.do_autoconf() |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 464 | elif self.state == STATE_AUTOCONF: |
Masahiro Yamada | f432c33 | 2016-06-15 14:33:52 +0900 | [diff] [blame] | 465 | if self.current_src_dir: |
| 466 | self.current_src_dir = None |
Joe Hershberger | 6b96c1a | 2016-06-10 14:53:32 -0500 | [diff] [blame] | 467 | self.do_defconfig() |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 468 | elif self.args.build_db: |
Simon Glass | d73fcb1 | 2017-06-01 19:39:02 -0600 | [diff] [blame] | 469 | self.do_build_db() |
Joe Hershberger | 6b96c1a | 2016-06-10 14:53:32 -0500 | [diff] [blame] | 470 | else: |
| 471 | self.do_savedefconfig() |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 472 | elif self.state == STATE_SAVEDEFCONFIG: |
| 473 | self.update_defconfig() |
| 474 | else: |
Simon Glass | daa694d | 2021-12-18 14:54:30 -0700 | [diff] [blame] | 475 | sys.exit('Internal Error. This should not happen.') |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 476 | |
Simon Glass | f297ba3 | 2023-09-23 13:44:05 -0600 | [diff] [blame] | 477 | return self.state == STATE_IDLE |
Joe Hershberger | 96464ba | 2015-05-19 13:21:17 -0500 | [diff] [blame] | 478 | |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 479 | def handle_error(self): |
| 480 | """Handle error cases.""" |
Masahiro Yamada | 8513dc0 | 2016-05-19 15:52:08 +0900 | [diff] [blame] | 481 | |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 482 | self.log.append(self.col.build(self.col.RED, 'Failed to process', |
| 483 | bright=True)) |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 484 | if self.args.verbose: |
Simon Glass | 9461bf0 | 2023-09-23 13:44:07 -0600 | [diff] [blame] | 485 | for line in self.proc.stderr.read().decode().splitlines(): |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 486 | self.log.append(self.col.build(self.col.CYAN, line, True)) |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 487 | self.finish(False) |
Joe Hershberger | 96464ba | 2015-05-19 13:21:17 -0500 | [diff] [blame] | 488 | |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 489 | def do_defconfig(self): |
| 490 | """Run 'make <board>_defconfig' to create the .config file.""" |
Masahiro Yamada | c8e1b10 | 2016-05-19 15:52:07 +0900 | [diff] [blame] | 491 | |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 492 | cmd = list(self.make_cmd) |
| 493 | cmd.append(self.defconfig) |
Simon Glass | f876e96 | 2024-07-17 16:56:52 +0100 | [diff] [blame] | 494 | # pylint: disable=R1732 |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 495 | self.proc = subprocess.Popen(cmd, stdout=self.devnull, |
| 496 | stderr=subprocess.PIPE, |
| 497 | cwd=self.current_src_dir) |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 498 | self.state = STATE_DEFCONFIG |
Masahiro Yamada | c8e1b10 | 2016-05-19 15:52:07 +0900 | [diff] [blame] | 499 | |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 500 | def do_autoconf(self): |
Simon Glass | f3b8e64 | 2017-06-01 19:39:01 -0600 | [diff] [blame] | 501 | """Run 'make AUTO_CONF_PATH'.""" |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 502 | |
Simon Glass | 6821a74 | 2017-07-10 14:47:47 -0600 | [diff] [blame] | 503 | arch = self.parser.get_arch() |
| 504 | try: |
Simon Glass | f297ba3 | 2023-09-23 13:44:05 -0600 | [diff] [blame] | 505 | tchain = self.toolchains.Select(arch) |
Simon Glass | 6821a74 | 2017-07-10 14:47:47 -0600 | [diff] [blame] | 506 | except ValueError: |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 507 | self.log.append(self.col.build( |
| 508 | self.col.YELLOW, |
| 509 | f"Tool chain for '{arch}' is missing: do nothing")) |
Masahiro Yamada | 4efef99 | 2016-05-19 15:52:03 +0900 | [diff] [blame] | 510 | self.finish(False) |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 511 | return |
Simon Glass | f297ba3 | 2023-09-23 13:44:05 -0600 | [diff] [blame] | 512 | env = tchain.MakeEnvironment(False) |
Masahiro Yamada | 90ed6cb | 2016-05-19 15:51:53 +0900 | [diff] [blame] | 513 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 514 | cmd = list(self.make_cmd) |
Joe Hershberger | 7740f65 | 2015-05-19 13:21:18 -0500 | [diff] [blame] | 515 | cmd.append('KCONFIG_IGNORE_DUPLICATES=1') |
Simon Glass | f3b8e64 | 2017-06-01 19:39:01 -0600 | [diff] [blame] | 516 | cmd.append(AUTO_CONF_PATH) |
Simon Glass | f876e96 | 2024-07-17 16:56:52 +0100 | [diff] [blame] | 517 | # pylint: disable=R1732 |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 518 | self.proc = subprocess.Popen(cmd, stdout=self.devnull, env=env, |
| 519 | stderr=subprocess.PIPE, |
| 520 | cwd=self.current_src_dir) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 521 | self.state = STATE_AUTOCONF |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 522 | |
Simon Glass | d73fcb1 | 2017-06-01 19:39:02 -0600 | [diff] [blame] | 523 | def do_build_db(self): |
| 524 | """Add the board to the database""" |
| 525 | configs = {} |
Simon Glass | 37f815c | 2021-12-18 14:54:34 -0700 | [diff] [blame] | 526 | for line in read_file(os.path.join(self.build_dir, AUTO_CONF_PATH)): |
| 527 | if line.startswith('CONFIG'): |
| 528 | config, value = line.split('=', 1) |
| 529 | configs[config] = value.rstrip() |
Simon Glass | d73fcb1 | 2017-06-01 19:39:02 -0600 | [diff] [blame] | 530 | self.db_queue.put([self.defconfig, configs]) |
| 531 | self.finish(True) |
| 532 | |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 533 | def do_savedefconfig(self): |
| 534 | """Update the .config and run 'make savedefconfig'.""" |
Simon Glass | c734561 | 2023-09-23 13:43:55 -0600 | [diff] [blame] | 535 | if not self.args.force_sync: |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 536 | self.finish(True) |
| 537 | return |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 538 | |
| 539 | cmd = list(self.make_cmd) |
| 540 | cmd.append('savedefconfig') |
Simon Glass | f876e96 | 2024-07-17 16:56:52 +0100 | [diff] [blame] | 541 | # pylint: disable=R1732 |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 542 | self.proc = subprocess.Popen(cmd, stdout=self.devnull, |
| 543 | stderr=subprocess.PIPE) |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 544 | self.state = STATE_SAVEDEFCONFIG |
| 545 | |
| 546 | def update_defconfig(self): |
| 547 | """Update the input defconfig and go back to the idle state.""" |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 548 | orig_defconfig = os.path.join('configs', self.defconfig) |
| 549 | new_defconfig = os.path.join(self.build_dir, 'defconfig') |
| 550 | updated = not filecmp.cmp(orig_defconfig, new_defconfig) |
| 551 | |
| 552 | if updated: |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 553 | self.log.append( |
| 554 | self.col.build(self.col.BLUE, 'defconfig updated', bright=True)) |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 555 | |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 556 | if not self.args.dry_run and updated: |
Masahiro Yamada | e307fa9 | 2016-06-08 11:47:37 +0900 | [diff] [blame] | 557 | shutil.move(new_defconfig, orig_defconfig) |
| 558 | self.finish(True) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 559 | |
Masahiro Yamada | 4efef99 | 2016-05-19 15:52:03 +0900 | [diff] [blame] | 560 | def finish(self, success): |
| 561 | """Display log along with progress and go to the idle state. |
Masahiro Yamada | 1d08556 | 2016-05-19 15:52:02 +0900 | [diff] [blame] | 562 | |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 563 | Args: |
Simon Glass | 549d422 | 2023-09-23 13:43:58 -0600 | [diff] [blame] | 564 | success (bool): Should be True when the defconfig was processed |
Masahiro Yamada | 4efef99 | 2016-05-19 15:52:03 +0900 | [diff] [blame] | 565 | successfully, or False when it fails. |
Masahiro Yamada | 1d08556 | 2016-05-19 15:52:02 +0900 | [diff] [blame] | 566 | """ |
| 567 | # output at least 30 characters to hide the "* defconfigs out of *". |
Simon Glass | dc1d2e6 | 2023-09-23 13:44:11 -0600 | [diff] [blame] | 568 | name = self.defconfig[:-len('_defconfig')] |
Simon Glass | 5aba58c | 2023-09-23 13:44:06 -0600 | [diff] [blame] | 569 | if self.log: |
Masahiro Yamada | 1d08556 | 2016-05-19 15:52:02 +0900 | [diff] [blame] | 570 | |
Simon Glass | 9461bf0 | 2023-09-23 13:44:07 -0600 | [diff] [blame] | 571 | # Put the first log line on the first line |
| 572 | log = name.ljust(20) + ' ' + self.log[0] |
| 573 | |
| 574 | if len(self.log) > 1: |
| 575 | log += '\n' + '\n'.join([' ' + s for s in self.log[1:]]) |
Simon Glass | 5aba58c | 2023-09-23 13:44:06 -0600 | [diff] [blame] | 576 | # Some threads are running in parallel. |
| 577 | # Print log atomically to not mix up logs from different threads. |
| 578 | print(log, file=(sys.stdout if success else sys.stderr)) |
Masahiro Yamada | 4efef99 | 2016-05-19 15:52:03 +0900 | [diff] [blame] | 579 | |
| 580 | if not success: |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 581 | if self.args.exit_on_error: |
Simon Glass | daa694d | 2021-12-18 14:54:30 -0700 | [diff] [blame] | 582 | sys.exit('Exit on error.') |
Masahiro Yamada | 4efef99 | 2016-05-19 15:52:03 +0900 | [diff] [blame] | 583 | # If --exit-on-error flag is not set, skip this board and continue. |
| 584 | # Record the failed board. |
Simon Glass | dc1d2e6 | 2023-09-23 13:44:11 -0600 | [diff] [blame] | 585 | self.failed_boards.add(name) |
Masahiro Yamada | 4efef99 | 2016-05-19 15:52:03 +0900 | [diff] [blame] | 586 | |
Simon Glass | 6b25d21 | 2023-09-23 13:44:10 -0600 | [diff] [blame] | 587 | self.progress.inc(success) |
Masahiro Yamada | 1d08556 | 2016-05-19 15:52:02 +0900 | [diff] [blame] | 588 | self.progress.show() |
Masahiro Yamada | 4efef99 | 2016-05-19 15:52:03 +0900 | [diff] [blame] | 589 | self.state = STATE_IDLE |
Masahiro Yamada | 1d08556 | 2016-05-19 15:52:02 +0900 | [diff] [blame] | 590 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 591 | def get_failed_boards(self): |
Masahiro Yamada | 09c6c06 | 2016-08-22 22:18:20 +0900 | [diff] [blame] | 592 | """Returns a set of failed boards (defconfigs) in this slot. |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 593 | """ |
| 594 | return self.failed_boards |
| 595 | |
| 596 | class Slots: |
| 597 | |
| 598 | """Controller of the array of subprocess slots.""" |
| 599 | |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 600 | def __init__(self, toolchains, args, progress, reference_src_dir, db_queue, |
| 601 | col): |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 602 | """Create a new slots controller. |
| 603 | |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 604 | Args: |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 605 | toolchains (Toolchains): Toolchains object containing toolchains |
| 606 | args (Namespace): Program arguments |
| 607 | progress (Progress): A progress indicator. |
| 608 | reference_src_dir (str): Determine the true starting config state |
| 609 | from this source tree (None for none) |
| 610 | db_queue (Queue): output queue to write config info for the database |
| 611 | col (terminal.Color): Colour object |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 612 | """ |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 613 | self.args = args |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 614 | self.slots = [] |
Simon Glass | dc1d2e6 | 2023-09-23 13:44:11 -0600 | [diff] [blame] | 615 | self.progress = progress |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 616 | self.col = col |
Simon Glass | 478920d | 2021-12-18 14:54:32 -0700 | [diff] [blame] | 617 | devnull = subprocess.DEVNULL |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 618 | make_cmd = get_make_cmd() |
Simon Glass | 62fae4b | 2023-09-23 13:44:00 -0600 | [diff] [blame] | 619 | for _ in range(args.jobs): |
Simon Glass | 882c8e4 | 2023-09-23 13:43:54 -0600 | [diff] [blame] | 620 | self.slots.append(Slot(toolchains, args, progress, devnull, |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 621 | make_cmd, reference_src_dir, db_queue, col)) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 622 | |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 623 | def add(self, defconfig): |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 624 | """Add a new subprocess if a vacant slot is found. |
| 625 | |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 626 | Args: |
Simon Glass | 549d422 | 2023-09-23 13:43:58 -0600 | [diff] [blame] | 627 | defconfig (str): defconfig name to be put into. |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 628 | |
| 629 | Returns: |
| 630 | Return True on success or False on failure |
| 631 | """ |
| 632 | for slot in self.slots: |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 633 | if slot.add(defconfig): |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 634 | return True |
| 635 | return False |
| 636 | |
| 637 | def available(self): |
| 638 | """Check if there is a vacant slot. |
| 639 | |
| 640 | Returns: |
| 641 | Return True if at lease one vacant slot is found, False otherwise. |
| 642 | """ |
| 643 | for slot in self.slots: |
| 644 | if slot.poll(): |
| 645 | return True |
| 646 | return False |
| 647 | |
| 648 | def empty(self): |
| 649 | """Check if all slots are vacant. |
| 650 | |
| 651 | Returns: |
| 652 | Return True if all the slots are vacant, False otherwise. |
| 653 | """ |
| 654 | ret = True |
| 655 | for slot in self.slots: |
| 656 | if not slot.poll(): |
| 657 | ret = False |
| 658 | return ret |
| 659 | |
Simon Glass | 94e2ed7 | 2023-09-23 13:44:13 -0600 | [diff] [blame] | 660 | def write_failed_boards(self): |
Simon Glass | dc1d2e6 | 2023-09-23 13:44:11 -0600 | [diff] [blame] | 661 | """Show the results of processing""" |
Masahiro Yamada | 09c6c06 | 2016-08-22 22:18:20 +0900 | [diff] [blame] | 662 | boards = set() |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 663 | |
| 664 | for slot in self.slots: |
Masahiro Yamada | 09c6c06 | 2016-08-22 22:18:20 +0900 | [diff] [blame] | 665 | boards |= slot.get_failed_boards() |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 666 | |
Masahiro Yamada | 96dccd9 | 2016-06-15 14:33:53 +0900 | [diff] [blame] | 667 | if boards: |
Simon Glass | dc1d2e6 | 2023-09-23 13:44:11 -0600 | [diff] [blame] | 668 | boards = '\n'.join(sorted(boards)) + '\n' |
Simon Glass | 94e2ed7 | 2023-09-23 13:44:13 -0600 | [diff] [blame] | 669 | write_file(FAILED_LIST, boards) |
| 670 | |
Joe Hershberger | 2559cd8 | 2015-05-19 13:21:22 -0500 | [diff] [blame] | 671 | |
Masahiro Yamada | 5cc42a5 | 2016-06-15 14:33:51 +0900 | [diff] [blame] | 672 | class ReferenceSource: |
| 673 | |
| 674 | """Reference source against which original configs should be parsed.""" |
| 675 | |
| 676 | def __init__(self, commit): |
| 677 | """Create a reference source directory based on a specified commit. |
| 678 | |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 679 | Args: |
Masahiro Yamada | 5cc42a5 | 2016-06-15 14:33:51 +0900 | [diff] [blame] | 680 | commit: commit to git-clone |
| 681 | """ |
| 682 | self.src_dir = tempfile.mkdtemp() |
Simon Glass | daa694d | 2021-12-18 14:54:30 -0700 | [diff] [blame] | 683 | print('Cloning git repo to a separate work directory...') |
Masahiro Yamada | 5cc42a5 | 2016-06-15 14:33:51 +0900 | [diff] [blame] | 684 | subprocess.check_output(['git', 'clone', os.getcwd(), '.'], |
| 685 | cwd=self.src_dir) |
Simon Glass | 1bd4306 | 2023-09-23 13:43:59 -0600 | [diff] [blame] | 686 | rev = subprocess.check_output(['git', 'rev-parse', '--short', |
| 687 | commit]).strip() |
| 688 | print(f"Checkout '{rev}' to build the original autoconf.mk.") |
Masahiro Yamada | 5cc42a5 | 2016-06-15 14:33:51 +0900 | [diff] [blame] | 689 | subprocess.check_output(['git', 'checkout', commit], |
| 690 | stderr=subprocess.STDOUT, cwd=self.src_dir) |
Joe Hershberger | 6b96c1a | 2016-06-10 14:53:32 -0500 | [diff] [blame] | 691 | |
| 692 | def __del__(self): |
Masahiro Yamada | 5cc42a5 | 2016-06-15 14:33:51 +0900 | [diff] [blame] | 693 | """Delete the reference source directory |
Joe Hershberger | 6b96c1a | 2016-06-10 14:53:32 -0500 | [diff] [blame] | 694 | |
| 695 | This function makes sure the temporary directory is cleaned away |
| 696 | even if Python suddenly dies due to error. It should be done in here |
| 697 | because it is guaranteed the destructor is always invoked when the |
| 698 | instance of the class gets unreferenced. |
| 699 | """ |
Masahiro Yamada | 5cc42a5 | 2016-06-15 14:33:51 +0900 | [diff] [blame] | 700 | shutil.rmtree(self.src_dir) |
Joe Hershberger | 6b96c1a | 2016-06-10 14:53:32 -0500 | [diff] [blame] | 701 | |
Masahiro Yamada | 5cc42a5 | 2016-06-15 14:33:51 +0900 | [diff] [blame] | 702 | def get_dir(self): |
| 703 | """Return the absolute path to the reference source directory.""" |
| 704 | |
| 705 | return self.src_dir |
Joe Hershberger | 6b96c1a | 2016-06-10 14:53:32 -0500 | [diff] [blame] | 706 | |
Simon Glass | eb1df3f | 2024-07-17 16:57:05 +0100 | [diff] [blame] | 707 | def move_config(args, col): |
Simon Glass | 882c8e4 | 2023-09-23 13:43:54 -0600 | [diff] [blame] | 708 | """Build database or sync config options to defconfig files. |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 709 | |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 710 | Args: |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 711 | args (Namespace): Program arguments |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 712 | col (terminal.Color): Colour object |
Simon Glass | 94e2ed7 | 2023-09-23 13:44:13 -0600 | [diff] [blame] | 713 | |
| 714 | Returns: |
Simon Glass | eb1df3f | 2024-07-17 16:57:05 +0100 | [diff] [blame] | 715 | tuple: |
| 716 | config_db (dict of configs for each defconfig): |
| 717 | key: defconfig name, e.g. "MPC8548CDS_legacy_defconfig" |
| 718 | value: dict: |
| 719 | key: CONFIG option |
| 720 | value: Value of option |
| 721 | Progress: Progress indicator |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 722 | """ |
Simon Glass | eb1df3f | 2024-07-17 16:57:05 +0100 | [diff] [blame] | 723 | config_db = {} |
| 724 | db_queue = queue.Queue() |
| 725 | dbt = DatabaseThread(config_db, db_queue) |
| 726 | dbt.daemon = True |
| 727 | dbt.start() |
| 728 | |
| 729 | check_clean_directory() |
| 730 | bsettings.setup('') |
| 731 | |
| 732 | # Get toolchains to use |
| 733 | toolchains = toolchain.Toolchains() |
| 734 | toolchains.GetSettings() |
| 735 | toolchains.Scan(verbose=False) |
| 736 | |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 737 | if args.git_ref: |
| 738 | reference_src = ReferenceSource(args.git_ref) |
Masahiro Yamada | 5cc42a5 | 2016-06-15 14:33:51 +0900 | [diff] [blame] | 739 | reference_src_dir = reference_src.get_dir() |
| 740 | else: |
Masahiro Yamada | f432c33 | 2016-06-15 14:33:52 +0900 | [diff] [blame] | 741 | reference_src_dir = None |
Joe Hershberger | 6b96c1a | 2016-06-10 14:53:32 -0500 | [diff] [blame] | 742 | |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 743 | if args.defconfigs: |
| 744 | defconfigs = get_matched_defconfigs(args.defconfigs) |
Joe Hershberger | 91040e8 | 2015-05-19 13:21:19 -0500 | [diff] [blame] | 745 | else: |
Masahiro Yamada | 684c306 | 2016-07-25 19:15:28 +0900 | [diff] [blame] | 746 | defconfigs = get_all_defconfigs() |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 747 | |
Simon Glass | 6b25d21 | 2023-09-23 13:44:10 -0600 | [diff] [blame] | 748 | progress = Progress(col, len(defconfigs)) |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 749 | slots = Slots(toolchains, args, progress, reference_src_dir, db_queue, col) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 750 | |
| 751 | # Main loop to process defconfig files: |
| 752 | # Add a new subprocess into a vacant slot. |
| 753 | # Sleep if there is no available slot. |
Masahiro Yamada | c5e60fd | 2016-05-19 15:51:55 +0900 | [diff] [blame] | 754 | for defconfig in defconfigs: |
| 755 | while not slots.add(defconfig): |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 756 | while not slots.available(): |
| 757 | # No available slot: sleep for a while |
| 758 | time.sleep(SLEEP_TIME) |
| 759 | |
| 760 | # wait until all the subprocesses finish |
| 761 | while not slots.empty(): |
| 762 | time.sleep(SLEEP_TIME) |
| 763 | |
Simon Glass | 94e2ed7 | 2023-09-23 13:44:13 -0600 | [diff] [blame] | 764 | slots.write_failed_boards() |
Simon Glass | eb1df3f | 2024-07-17 16:57:05 +0100 | [diff] [blame] | 765 | db_queue.join() |
Simon Glass | 0229eee | 2024-07-17 16:57:07 +0100 | [diff] [blame^] | 766 | progress.completed() |
Simon Glass | eb1df3f | 2024-07-17 16:57:05 +0100 | [diff] [blame] | 767 | return config_db, progress |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 768 | |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 769 | def find_kconfig_rules(kconf, config, imply_config): |
| 770 | """Check whether a config has a 'select' or 'imply' keyword |
| 771 | |
| 772 | Args: |
Simon Glass | 549d422 | 2023-09-23 13:43:58 -0600 | [diff] [blame] | 773 | kconf (Kconfiglib.Kconfig): Kconfig object |
| 774 | config (str): Name of config to check (without CONFIG_ prefix) |
| 775 | imply_config (str): Implying config (without CONFIG_ prefix) which may |
| 776 | or may not have an 'imply' for 'config') |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 777 | |
| 778 | Returns: |
| 779 | Symbol object for 'config' if found, else None |
| 780 | """ |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 781 | sym = kconf.syms.get(imply_config) |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 782 | if sym: |
Simon Glass | 62fae4b | 2023-09-23 13:44:00 -0600 | [diff] [blame] | 783 | for sel, _ in (sym.selects + sym.implies): |
Simon Glass | a362708 | 2021-12-18 08:09:42 -0700 | [diff] [blame] | 784 | if sel.name == config: |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 785 | return sym |
| 786 | return None |
| 787 | |
Simon Glass | f876e96 | 2024-07-17 16:56:52 +0100 | [diff] [blame] | 788 | def check_imply_rule(kconf, imply_config): |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 789 | """Check if we can add an 'imply' option |
| 790 | |
| 791 | This finds imply_config in the Kconfig and looks to see if it is possible |
| 792 | to add an 'imply' for 'config' to that part of the Kconfig. |
| 793 | |
| 794 | Args: |
Simon Glass | 549d422 | 2023-09-23 13:43:58 -0600 | [diff] [blame] | 795 | kconf (Kconfiglib.Kconfig): Kconfig object |
Simon Glass | 549d422 | 2023-09-23 13:43:58 -0600 | [diff] [blame] | 796 | imply_config (str): Implying config (without CONFIG_ prefix) which may |
| 797 | or may not have an 'imply' for 'config') |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 798 | |
| 799 | Returns: |
| 800 | tuple: |
Simon Glass | 549d422 | 2023-09-23 13:43:58 -0600 | [diff] [blame] | 801 | str: filename of Kconfig file containing imply_config, or None if |
| 802 | none |
| 803 | int: line number within the Kconfig file, or 0 if none |
| 804 | str: message indicating the result |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 805 | """ |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 806 | sym = kconf.syms.get(imply_config) |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 807 | if not sym: |
| 808 | return 'cannot find sym' |
Simon Glass | ea40b20 | 2021-07-21 21:35:53 -0600 | [diff] [blame] | 809 | nodes = sym.nodes |
| 810 | if len(nodes) != 1: |
Simon Glass | 1bd4306 | 2023-09-23 13:43:59 -0600 | [diff] [blame] | 811 | return f'{len(nodes)} locations' |
Simon Glass | a362708 | 2021-12-18 08:09:42 -0700 | [diff] [blame] | 812 | node = nodes[0] |
| 813 | fname, linenum = node.filename, node.linenr |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 814 | cwd = os.getcwd() |
| 815 | if cwd and fname.startswith(cwd): |
| 816 | fname = fname[len(cwd) + 1:] |
Simon Glass | 1bd4306 | 2023-09-23 13:43:59 -0600 | [diff] [blame] | 817 | file_line = f' at {fname}:{linenum}' |
Simon Glass | 37f815c | 2021-12-18 14:54:34 -0700 | [diff] [blame] | 818 | data = read_file(fname) |
Simon Glass | 1bd4306 | 2023-09-23 13:43:59 -0600 | [diff] [blame] | 819 | if data[linenum - 1] != f'config {imply_config}': |
| 820 | return None, 0, f'bad sym format {data[linenum]}{file_line})' |
| 821 | return fname, linenum, f'adding{file_line}' |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 822 | |
| 823 | def add_imply_rule(config, fname, linenum): |
| 824 | """Add a new 'imply' option to a Kconfig |
| 825 | |
| 826 | Args: |
Simon Glass | 549d422 | 2023-09-23 13:43:58 -0600 | [diff] [blame] | 827 | config (str): config option to add an imply for (without CONFIG_ prefix) |
| 828 | fname (str): Kconfig filename to update |
| 829 | linenum (int): Line number to place the 'imply' before |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 830 | |
| 831 | Returns: |
| 832 | Message indicating the result |
| 833 | """ |
Simon Glass | 1bd4306 | 2023-09-23 13:43:59 -0600 | [diff] [blame] | 834 | file_line = f' at {fname}:{linenum}' |
Simon Glass | 37f815c | 2021-12-18 14:54:34 -0700 | [diff] [blame] | 835 | data = read_file(fname) |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 836 | linenum -= 1 |
| 837 | |
| 838 | for offset, line in enumerate(data[linenum:]): |
| 839 | if line.strip().startswith('help') or not line: |
Simon Glass | 1bd4306 | 2023-09-23 13:43:59 -0600 | [diff] [blame] | 840 | data.insert(linenum + offset, f'\timply {config}') |
Simon Glass | 2fd85bd | 2021-12-18 14:54:33 -0700 | [diff] [blame] | 841 | write_file(fname, data) |
Simon Glass | 1bd4306 | 2023-09-23 13:43:59 -0600 | [diff] [blame] | 842 | return f'added{file_line}' |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 843 | |
| 844 | return 'could not insert%s' |
| 845 | |
| 846 | (IMPLY_MIN_2, IMPLY_TARGET, IMPLY_CMD, IMPLY_NON_ARCH_BOARD) = ( |
| 847 | 1, 2, 4, 8) |
Simon Glass | 9b2a2e8 | 2017-06-15 21:39:32 -0600 | [diff] [blame] | 848 | |
| 849 | IMPLY_FLAGS = { |
| 850 | 'min2': [IMPLY_MIN_2, 'Show options which imply >2 boards (normally >5)'], |
| 851 | 'target': [IMPLY_TARGET, 'Allow CONFIG_TARGET_... options to imply'], |
| 852 | 'cmd': [IMPLY_CMD, 'Allow CONFIG_CMD_... to imply'], |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 853 | 'non-arch-board': [ |
| 854 | IMPLY_NON_ARCH_BOARD, |
| 855 | 'Allow Kconfig options outside arch/ and /board/ to imply'], |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 856 | } |
Simon Glass | 9b2a2e8 | 2017-06-15 21:39:32 -0600 | [diff] [blame] | 857 | |
Simon Glass | 9d60339 | 2021-12-18 08:09:43 -0700 | [diff] [blame] | 858 | |
| 859 | def read_database(): |
| 860 | """Read in the config database |
| 861 | |
| 862 | Returns: |
| 863 | tuple: |
| 864 | set of all config options seen (each a str) |
| 865 | set of all defconfigs seen (each a str) |
| 866 | dict of configs for each defconfig: |
| 867 | key: defconfig name, e.g. "MPC8548CDS_legacy_defconfig" |
| 868 | value: dict: |
| 869 | key: CONFIG option |
| 870 | value: Value of option |
| 871 | dict of defconfigs for each config: |
| 872 | key: CONFIG option |
| 873 | value: set of boards using that option |
| 874 | |
| 875 | """ |
| 876 | configs = {} |
| 877 | |
| 878 | # key is defconfig name, value is dict of (CONFIG_xxx, value) |
| 879 | config_db = {} |
| 880 | |
| 881 | # Set of all config options we have seen |
| 882 | all_configs = set() |
| 883 | |
| 884 | # Set of all defconfigs we have seen |
| 885 | all_defconfigs = set() |
| 886 | |
| 887 | defconfig_db = collections.defaultdict(set) |
Simon Glass | 61d555d | 2024-07-17 16:56:49 +0100 | [diff] [blame] | 888 | defconfig = None |
Simon Glass | 37f815c | 2021-12-18 14:54:34 -0700 | [diff] [blame] | 889 | for line in read_file(CONFIG_DATABASE): |
| 890 | line = line.rstrip() |
| 891 | if not line: # Separator between defconfigs |
| 892 | config_db[defconfig] = configs |
| 893 | all_defconfigs.add(defconfig) |
| 894 | configs = {} |
| 895 | elif line[0] == ' ': # CONFIG line |
| 896 | config, value = line.strip().split('=', 1) |
| 897 | configs[config] = value |
| 898 | defconfig_db[config].add(defconfig) |
| 899 | all_configs.add(config) |
| 900 | else: # New defconfig |
| 901 | defconfig = line |
Simon Glass | 9d60339 | 2021-12-18 08:09:43 -0700 | [diff] [blame] | 902 | |
| 903 | return all_configs, all_defconfigs, config_db, defconfig_db |
| 904 | |
| 905 | |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 906 | def do_imply_config(config_list, add_imply, imply_flags, skip_added, |
| 907 | check_kconfig=True, find_superset=False): |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 908 | """Find CONFIG options which imply those in the list |
| 909 | |
| 910 | Some CONFIG options can be implied by others and this can help to reduce |
| 911 | the size of the defconfig files. For example, CONFIG_X86 implies |
| 912 | CONFIG_CMD_IRQ, so we can put 'imply CMD_IRQ' under 'config X86' and |
| 913 | all x86 boards will have that option, avoiding adding CONFIG_CMD_IRQ to |
| 914 | each of the x86 defconfig files. |
| 915 | |
Simon Glass | ea4d6de | 2023-09-23 13:44:14 -0600 | [diff] [blame] | 916 | This function uses the qconfig database to find such options. It |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 917 | displays a list of things that could possibly imply those in the list. |
| 918 | The algorithm ignores any that start with CONFIG_TARGET since these |
| 919 | typically refer to only a few defconfigs (often one). It also does not |
| 920 | display a config with less than 5 defconfigs. |
| 921 | |
| 922 | The algorithm works using sets. For each target config in config_list: |
| 923 | - Get the set 'defconfigs' which use that target config |
| 924 | - For each config (from a list of all configs): |
| 925 | - Get the set 'imply_defconfig' of defconfigs which use that config |
| 926 | - |
| 927 | - If imply_defconfigs contains anything not in defconfigs then |
| 928 | this config does not imply the target config |
| 929 | |
Simon Glass | cc628f5 | 2024-07-17 16:57:03 +0100 | [diff] [blame] | 930 | Args: |
| 931 | config_list (list of str): List of CONFIG options to check |
| 932 | add_imply (bool): Automatically add an 'imply' for each config. |
| 933 | imply_flags (int): Flags which control which implying configs are allowed |
Simon Glass | 9b2a2e8 | 2017-06-15 21:39:32 -0600 | [diff] [blame] | 934 | (IMPLY_...) |
Simon Glass | cc628f5 | 2024-07-17 16:57:03 +0100 | [diff] [blame] | 935 | skip_added (bool): Don't show options which already have an imply added. |
| 936 | check_kconfig (bool): Check if implied symbols already have an 'imply' or |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 937 | 'select' for the target config, and show this information if so. |
Simon Glass | cc628f5 | 2024-07-17 16:57:03 +0100 | [diff] [blame] | 938 | find_superset (bool): True to look for configs which are a superset of those |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 939 | already found. So for example if CONFIG_EXYNOS5 implies an option, |
| 940 | but CONFIG_EXYNOS covers a larger set of defconfigs and also |
| 941 | implies that option, this will drop the former in favour of the |
| 942 | latter. In practice this option has not proved very used. |
| 943 | |
| 944 | Note the terminoloy: |
| 945 | config - a CONFIG_XXX options (a string, e.g. 'CONFIG_CMD_EEPROM') |
| 946 | defconfig - a defconfig file (a string, e.g. 'configs/snow_defconfig') |
| 947 | """ |
Simon Glass | 0a0c124 | 2024-07-17 16:56:51 +0100 | [diff] [blame] | 948 | kconf = scan_kconfig() if check_kconfig else None |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 949 | if add_imply and add_imply != 'all': |
Simon Glass | a362708 | 2021-12-18 08:09:42 -0700 | [diff] [blame] | 950 | add_imply = add_imply.split(',') |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 951 | |
Simon Glass | 62fae4b | 2023-09-23 13:44:00 -0600 | [diff] [blame] | 952 | all_configs, all_defconfigs, _, defconfig_db = read_database() |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 953 | |
Simon Glass | a362708 | 2021-12-18 08:09:42 -0700 | [diff] [blame] | 954 | # Work through each target config option in turn, independently |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 955 | for config in config_list: |
| 956 | defconfigs = defconfig_db.get(config) |
| 957 | if not defconfigs: |
Simon Glass | 1bd4306 | 2023-09-23 13:43:59 -0600 | [diff] [blame] | 958 | print(f'{config} not found in any defconfig') |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 959 | continue |
| 960 | |
| 961 | # Get the set of defconfigs without this one (since a config cannot |
| 962 | # imply itself) |
| 963 | non_defconfigs = all_defconfigs - defconfigs |
| 964 | num_defconfigs = len(defconfigs) |
Simon Glass | 1bd4306 | 2023-09-23 13:43:59 -0600 | [diff] [blame] | 965 | print(f'{config} found in {num_defconfigs}/{len(all_configs)} defconfigs') |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 966 | |
| 967 | # This will hold the results: key=config, value=defconfigs containing it |
| 968 | imply_configs = {} |
| 969 | rest_configs = all_configs - set([config]) |
| 970 | |
| 971 | # Look at every possible config, except the target one |
| 972 | for imply_config in rest_configs: |
Simon Glass | 9b2a2e8 | 2017-06-15 21:39:32 -0600 | [diff] [blame] | 973 | if 'ERRATUM' in imply_config: |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 974 | continue |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 975 | if not imply_flags & IMPLY_CMD: |
Simon Glass | 9b2a2e8 | 2017-06-15 21:39:32 -0600 | [diff] [blame] | 976 | if 'CONFIG_CMD' in imply_config: |
| 977 | continue |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 978 | if not imply_flags & IMPLY_TARGET: |
Simon Glass | 9b2a2e8 | 2017-06-15 21:39:32 -0600 | [diff] [blame] | 979 | if 'CONFIG_TARGET' in imply_config: |
| 980 | continue |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 981 | |
| 982 | # Find set of defconfigs that have this config |
| 983 | imply_defconfig = defconfig_db[imply_config] |
| 984 | |
| 985 | # Get the intersection of this with defconfigs containing the |
| 986 | # target config |
| 987 | common_defconfigs = imply_defconfig & defconfigs |
| 988 | |
| 989 | # Get the set of defconfigs containing this config which DO NOT |
| 990 | # also contain the taret config. If this set is non-empty it means |
| 991 | # that this config affects other defconfigs as well as (possibly) |
| 992 | # the ones affected by the target config. This means it implies |
| 993 | # things we don't want to imply. |
| 994 | not_common_defconfigs = imply_defconfig & non_defconfigs |
| 995 | if not_common_defconfigs: |
| 996 | continue |
| 997 | |
| 998 | # If there are common defconfigs, imply_config may be useful |
| 999 | if common_defconfigs: |
| 1000 | skip = False |
| 1001 | if find_superset: |
Simon Glass | 793dca3 | 2019-10-31 07:42:57 -0600 | [diff] [blame] | 1002 | for prev in list(imply_configs.keys()): |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 1003 | prev_count = len(imply_configs[prev]) |
| 1004 | count = len(common_defconfigs) |
| 1005 | if (prev_count > count and |
| 1006 | (imply_configs[prev] & common_defconfigs == |
| 1007 | common_defconfigs)): |
| 1008 | # skip imply_config because prev is a superset |
| 1009 | skip = True |
| 1010 | break |
Simon Glass | f297ba3 | 2023-09-23 13:44:05 -0600 | [diff] [blame] | 1011 | if count > prev_count: |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 1012 | # delete prev because imply_config is a superset |
| 1013 | del imply_configs[prev] |
| 1014 | if not skip: |
| 1015 | imply_configs[imply_config] = common_defconfigs |
| 1016 | |
| 1017 | # Now we have a dict imply_configs of configs which imply each config |
| 1018 | # The value of each dict item is the set of defconfigs containing that |
| 1019 | # config. Rank them so that we print the configs that imply the largest |
| 1020 | # number of defconfigs first. |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1021 | ranked_iconfigs = sorted(imply_configs, |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 1022 | key=lambda k: len(imply_configs[k]), reverse=True) |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1023 | kconfig_info = '' |
| 1024 | cwd = os.getcwd() |
| 1025 | add_list = collections.defaultdict(list) |
| 1026 | for iconfig in ranked_iconfigs: |
| 1027 | num_common = len(imply_configs[iconfig]) |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 1028 | |
| 1029 | # Don't bother if there are less than 5 defconfigs affected. |
Simon Glass | 9b2a2e8 | 2017-06-15 21:39:32 -0600 | [diff] [blame] | 1030 | if num_common < (2 if imply_flags & IMPLY_MIN_2 else 5): |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 1031 | continue |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1032 | missing = defconfigs - imply_configs[iconfig] |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 1033 | missing_str = ', '.join(missing) if missing else 'all' |
| 1034 | missing_str = '' |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1035 | show = True |
| 1036 | if kconf: |
| 1037 | sym = find_kconfig_rules(kconf, config[CONFIG_LEN:], |
| 1038 | iconfig[CONFIG_LEN:]) |
| 1039 | kconfig_info = '' |
| 1040 | if sym: |
Simon Glass | ea40b20 | 2021-07-21 21:35:53 -0600 | [diff] [blame] | 1041 | nodes = sym.nodes |
| 1042 | if len(nodes) == 1: |
| 1043 | fname, linenum = nodes[0].filename, nodes[0].linenr |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1044 | if cwd and fname.startswith(cwd): |
| 1045 | fname = fname[len(cwd) + 1:] |
Simon Glass | 1bd4306 | 2023-09-23 13:43:59 -0600 | [diff] [blame] | 1046 | kconfig_info = f'{fname}:{linenum}' |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1047 | if skip_added: |
| 1048 | show = False |
| 1049 | else: |
Tom Rini | 65e05dd | 2019-09-20 17:42:09 -0400 | [diff] [blame] | 1050 | sym = kconf.syms.get(iconfig[CONFIG_LEN:]) |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1051 | fname = '' |
| 1052 | if sym: |
Simon Glass | ea40b20 | 2021-07-21 21:35:53 -0600 | [diff] [blame] | 1053 | nodes = sym.nodes |
| 1054 | if len(nodes) == 1: |
| 1055 | fname, linenum = nodes[0].filename, nodes[0].linenr |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1056 | if cwd and fname.startswith(cwd): |
| 1057 | fname = fname[len(cwd) + 1:] |
| 1058 | in_arch_board = not sym or (fname.startswith('arch') or |
| 1059 | fname.startswith('board')) |
| 1060 | if (not in_arch_board and |
Simon Glass | 91197aa | 2021-12-18 14:54:35 -0700 | [diff] [blame] | 1061 | not imply_flags & IMPLY_NON_ARCH_BOARD): |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1062 | continue |
| 1063 | |
| 1064 | if add_imply and (add_imply == 'all' or |
| 1065 | iconfig in add_imply): |
| 1066 | fname, linenum, kconfig_info = (check_imply_rule(kconf, |
Simon Glass | f876e96 | 2024-07-17 16:56:52 +0100 | [diff] [blame] | 1067 | iconfig[CONFIG_LEN:])) |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1068 | if fname: |
| 1069 | add_list[fname].append(linenum) |
| 1070 | |
| 1071 | if show and kconfig_info != 'skip': |
Simon Glass | 0e03fb1 | 2024-07-17 16:56:53 +0100 | [diff] [blame] | 1072 | print(f'{num_common:5} : ' |
| 1073 | f'{iconfig.ljust(30)}{kconfig_info.ljust(25)} {missing_str}') |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1074 | |
| 1075 | # Having collected a list of things to add, now we add them. We process |
| 1076 | # each file from the largest line number to the smallest so that |
| 1077 | # earlier additions do not affect our line numbers. E.g. if we added an |
| 1078 | # imply at line 20 it would change the position of each line after |
| 1079 | # that. |
Simon Glass | 793dca3 | 2019-10-31 07:42:57 -0600 | [diff] [blame] | 1080 | for fname, linenums in add_list.items(): |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1081 | for linenum in sorted(linenums, reverse=True): |
| 1082 | add_imply_rule(config[CONFIG_LEN:], fname, linenum) |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 1083 | |
Simon Glass | 941671a | 2022-02-08 11:49:46 -0700 | [diff] [blame] | 1084 | def defconfig_matches(configs, re_match): |
| 1085 | """Check if any CONFIG option matches a regex |
| 1086 | |
| 1087 | The match must be complete, i.e. from the start to end of the CONFIG option. |
| 1088 | |
| 1089 | Args: |
| 1090 | configs (dict): Dict of CONFIG options: |
| 1091 | key: CONFIG option |
| 1092 | value: Value of option |
| 1093 | re_match (re.Pattern): Match to check |
| 1094 | |
| 1095 | Returns: |
| 1096 | bool: True if any CONFIG matches the regex |
| 1097 | """ |
| 1098 | for cfg in configs: |
Simon Glass | d9c958f | 2022-03-05 20:18:54 -0700 | [diff] [blame] | 1099 | if re_match.fullmatch(cfg): |
Simon Glass | 941671a | 2022-02-08 11:49:46 -0700 | [diff] [blame] | 1100 | return True |
| 1101 | return False |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 1102 | |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1103 | def do_find_config(config_list): |
| 1104 | """Find boards with a given combination of CONFIGs |
| 1105 | |
Simon Glass | 630a9c9 | 2024-07-17 16:57:04 +0100 | [diff] [blame] | 1106 | Args: |
| 1107 | config_list (list of str): List of CONFIG options to check (each a regex |
| 1108 | consisting of a config option, with or without a CONFIG_ prefix. If |
| 1109 | an option is preceded by a tilde (~) then it must be false, |
| 1110 | otherwise it must be true) |
| 1111 | |
| 1112 | Returns: |
| 1113 | int: exit code (0 for success) |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1114 | """ |
Simon Glass | 62fae4b | 2023-09-23 13:44:00 -0600 | [diff] [blame] | 1115 | _, all_defconfigs, config_db, _ = read_database() |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1116 | |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1117 | # Start with all defconfigs |
| 1118 | out = all_defconfigs |
| 1119 | |
| 1120 | # Work through each config in turn |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1121 | for item in config_list: |
| 1122 | # Get the real config name and whether we want this config or not |
| 1123 | cfg = item |
| 1124 | want = True |
| 1125 | if cfg[0] == '~': |
| 1126 | want = False |
| 1127 | cfg = cfg[1:] |
| 1128 | |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1129 | # Search everything that is still in the running. If it has a config |
| 1130 | # that we want, or doesn't have one that we don't, add it into the |
| 1131 | # running for the next stage |
| 1132 | in_list = out |
| 1133 | out = set() |
Simon Glass | 941671a | 2022-02-08 11:49:46 -0700 | [diff] [blame] | 1134 | re_match = re.compile(cfg) |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1135 | for defc in in_list: |
Simon Glass | 941671a | 2022-02-08 11:49:46 -0700 | [diff] [blame] | 1136 | has_cfg = defconfig_matches(config_db[defc], re_match) |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1137 | if has_cfg == want: |
| 1138 | out.add(defc) |
Tom Rini | 9ef3ba8 | 2022-12-04 10:14:16 -0500 | [diff] [blame] | 1139 | print(f'{len(out)} matches') |
| 1140 | print(' '.join(item.split('_defconfig')[0] for item in out)) |
Simon Glass | 630a9c9 | 2024-07-17 16:57:04 +0100 | [diff] [blame] | 1141 | return 0 |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1142 | |
| 1143 | |
| 1144 | def prefix_config(cfg): |
| 1145 | """Prefix a config with CONFIG_ if needed |
| 1146 | |
| 1147 | This handles ~ operator, which indicates that the CONFIG should be disabled |
| 1148 | |
| 1149 | >>> prefix_config('FRED') |
| 1150 | 'CONFIG_FRED' |
| 1151 | >>> prefix_config('CONFIG_FRED') |
| 1152 | 'CONFIG_FRED' |
| 1153 | >>> prefix_config('~FRED') |
| 1154 | '~CONFIG_FRED' |
| 1155 | >>> prefix_config('~CONFIG_FRED') |
| 1156 | '~CONFIG_FRED' |
| 1157 | >>> prefix_config('A123') |
| 1158 | 'CONFIG_A123' |
| 1159 | """ |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 1160 | oper = '' |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1161 | if cfg[0] == '~': |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 1162 | oper = cfg[0] |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1163 | cfg = cfg[1:] |
| 1164 | if not cfg.startswith('CONFIG_'): |
| 1165 | cfg = 'CONFIG_' + cfg |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 1166 | return oper + cfg |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1167 | |
| 1168 | |
Simon Glass | 9827571 | 2023-09-23 13:43:57 -0600 | [diff] [blame] | 1169 | RE_MK_CONFIGS = re.compile(r'CONFIG_(\$\(SPL_(?:TPL_)?\))?([A-Za-z0-9_]*)') |
| 1170 | RE_IFDEF = re.compile(r'(ifdef|ifndef)') |
| 1171 | RE_C_CONFIGS = re.compile(r'CONFIG_([A-Za-z0-9_]*)') |
| 1172 | RE_CONFIG_IS = re.compile(r'CONFIG_IS_ENABLED\(([A-Za-z0-9_]*)\)') |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1173 | |
| 1174 | class ConfigUse: |
Simon Glass | f876e96 | 2024-07-17 16:56:52 +0100 | [diff] [blame] | 1175 | """Tracks whether a config relates to SPL or not""" |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1176 | def __init__(self, cfg, is_spl, fname, rest): |
Simon Glass | f876e96 | 2024-07-17 16:56:52 +0100 | [diff] [blame] | 1177 | """Set up a new ConfigUse |
| 1178 | |
| 1179 | Args: |
| 1180 | cfg (str): CONFIG option, without any CONFIG_ or SPL_ prefix |
| 1181 | is_spl (bool): True if this option relates to SPL |
| 1182 | fname (str): Makefile filename where the CONFIG option was found |
| 1183 | rest (str): Line of the Makefile |
| 1184 | """ |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1185 | self.cfg = cfg |
| 1186 | self.is_spl = is_spl |
| 1187 | self.fname = fname |
| 1188 | self.rest = rest |
| 1189 | |
| 1190 | def __hash__(self): |
| 1191 | return hash((self.cfg, self.is_spl)) |
| 1192 | |
| 1193 | def scan_makefiles(fnames): |
| 1194 | """Scan Makefiles looking for Kconfig options |
| 1195 | |
| 1196 | Looks for uses of CONFIG options in Makefiles |
| 1197 | |
| 1198 | Args: |
| 1199 | fnames (list of tuple): |
| 1200 | str: Makefile filename where the option was found |
| 1201 | str: Line of the Makefile |
| 1202 | |
| 1203 | Returns: |
| 1204 | tuple: |
| 1205 | dict: all_uses |
| 1206 | key (ConfigUse): object |
| 1207 | value (list of str): matching lines |
| 1208 | dict: Uses by filename |
| 1209 | key (str): filename |
| 1210 | value (set of ConfigUse): uses in that filename |
| 1211 | |
| 1212 | >>> RE_MK_CONFIGS.search('CONFIG_FRED').groups() |
| 1213 | (None, 'FRED') |
| 1214 | >>> RE_MK_CONFIGS.search('CONFIG_$(SPL_)MARY').groups() |
| 1215 | ('$(SPL_)', 'MARY') |
| 1216 | >>> RE_MK_CONFIGS.search('CONFIG_$(SPL_TPL_)MARY').groups() |
| 1217 | ('$(SPL_TPL_)', 'MARY') |
| 1218 | """ |
| 1219 | all_uses = collections.defaultdict(list) |
| 1220 | fname_uses = {} |
| 1221 | for fname, rest in fnames: |
| 1222 | m_iter = RE_MK_CONFIGS.finditer(rest) |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 1223 | for mat in m_iter: |
| 1224 | real_opt = mat.group(2) |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1225 | if real_opt == '': |
| 1226 | continue |
| 1227 | is_spl = False |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 1228 | if mat.group(1): |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1229 | is_spl = True |
| 1230 | use = ConfigUse(real_opt, is_spl, fname, rest) |
| 1231 | if fname not in fname_uses: |
| 1232 | fname_uses[fname] = set() |
| 1233 | fname_uses[fname].add(use) |
| 1234 | all_uses[use].append(rest) |
| 1235 | return all_uses, fname_uses |
| 1236 | |
| 1237 | |
| 1238 | def scan_src_files(fnames): |
| 1239 | """Scan source files (other than Makefiles) looking for Kconfig options |
| 1240 | |
| 1241 | Looks for uses of CONFIG options |
| 1242 | |
| 1243 | Args: |
| 1244 | fnames (list of tuple): |
| 1245 | str: Makefile filename where the option was found |
| 1246 | str: Line of the Makefile |
| 1247 | |
| 1248 | Returns: |
| 1249 | tuple: |
| 1250 | dict: all_uses |
| 1251 | key (ConfigUse): object |
| 1252 | value (list of str): matching lines |
| 1253 | dict: Uses by filename |
| 1254 | key (str): filename |
| 1255 | value (set of ConfigUse): uses in that filename |
| 1256 | |
| 1257 | >>> RE_C_CONFIGS.search('CONFIG_FRED').groups() |
| 1258 | ('FRED',) |
| 1259 | >>> RE_CONFIG_IS.search('CONFIG_IS_ENABLED(MARY)').groups() |
| 1260 | ('MARY',) |
| 1261 | >>> RE_CONFIG_IS.search('#if CONFIG_IS_ENABLED(OF_PLATDATA)').groups() |
| 1262 | ('OF_PLATDATA',) |
| 1263 | """ |
Simon Glass | 62fae4b | 2023-09-23 13:44:00 -0600 | [diff] [blame] | 1264 | fname = None |
| 1265 | rest = None |
| 1266 | |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1267 | def add_uses(m_iter, is_spl): |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 1268 | for mat in m_iter: |
| 1269 | real_opt = mat.group(1) |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1270 | if real_opt == '': |
| 1271 | continue |
| 1272 | use = ConfigUse(real_opt, is_spl, fname, rest) |
| 1273 | if fname not in fname_uses: |
| 1274 | fname_uses[fname] = set() |
| 1275 | fname_uses[fname].add(use) |
| 1276 | all_uses[use].append(rest) |
| 1277 | |
| 1278 | all_uses = collections.defaultdict(list) |
| 1279 | fname_uses = {} |
| 1280 | for fname, rest in fnames: |
| 1281 | m_iter = RE_C_CONFIGS.finditer(rest) |
| 1282 | add_uses(m_iter, False) |
| 1283 | |
| 1284 | m_iter2 = RE_CONFIG_IS.finditer(rest) |
| 1285 | add_uses(m_iter2, True) |
| 1286 | |
| 1287 | return all_uses, fname_uses |
| 1288 | |
| 1289 | |
| 1290 | MODE_NORMAL, MODE_SPL, MODE_PROPER = range(3) |
| 1291 | |
| 1292 | def do_scan_source(path, do_update): |
| 1293 | """Scan the source tree for Kconfig inconsistencies |
| 1294 | |
| 1295 | Args: |
| 1296 | path (str): Path to source tree |
| 1297 | do_update (bool) : True to write to scripts/kconf_... files |
| 1298 | """ |
| 1299 | def is_not_proper(name): |
| 1300 | for prefix in SPL_PREFIXES: |
| 1301 | if name.startswith(prefix): |
| 1302 | return name[len(prefix):] |
| 1303 | return False |
| 1304 | |
| 1305 | def check_not_found(all_uses, spl_mode): |
| 1306 | """Check for Kconfig options mentioned in the source but not in Kconfig |
| 1307 | |
| 1308 | Args: |
| 1309 | all_uses (dict): |
| 1310 | key (ConfigUse): object |
| 1311 | value (list of str): matching lines |
| 1312 | spl_mode (int): If MODE_SPL, look at source code which implies |
| 1313 | an SPL_ option, but for which there is none; |
| 1314 | for MOD_PROPER, look at source code which implies a Proper |
| 1315 | option (i.e. use of CONFIG_IS_ENABLED() or $(SPL_) or |
| 1316 | $(SPL_TPL_) but for which there none; |
| 1317 | if MODE_NORMAL, ignore SPL |
| 1318 | |
| 1319 | Returns: |
| 1320 | dict: |
| 1321 | key (str): CONFIG name (without 'CONFIG_' prefix |
| 1322 | value (list of ConfigUse): List of uses of this CONFIG |
| 1323 | """ |
| 1324 | # Make sure we know about all the options |
| 1325 | not_found = collections.defaultdict(list) |
Simon Glass | 62fae4b | 2023-09-23 13:44:00 -0600 | [diff] [blame] | 1326 | for use, _ in all_uses.items(): |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1327 | name = use.cfg |
| 1328 | if name in IGNORE_SYMS: |
| 1329 | continue |
| 1330 | check = True |
| 1331 | |
| 1332 | if spl_mode == MODE_SPL: |
| 1333 | check = use.is_spl |
| 1334 | |
| 1335 | # If it is an SPL symbol, try prepending all SPL_ prefixes to |
| 1336 | # find at least one SPL symbol |
| 1337 | if use.is_spl: |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1338 | for prefix in SPL_PREFIXES: |
| 1339 | try_name = prefix + name |
| 1340 | sym = kconf.syms.get(try_name) |
| 1341 | if sym: |
| 1342 | break |
| 1343 | if not sym: |
| 1344 | not_found[f'SPL_{name}'].append(use) |
| 1345 | continue |
| 1346 | elif spl_mode == MODE_PROPER: |
| 1347 | # Try to find the Proper version of this symbol, i.e. without |
| 1348 | # the SPL_ prefix |
| 1349 | proper_name = is_not_proper(name) |
| 1350 | if proper_name: |
| 1351 | name = proper_name |
| 1352 | elif not use.is_spl: |
| 1353 | check = False |
| 1354 | else: # MODE_NORMAL |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1355 | sym = kconf.syms.get(name) |
| 1356 | if not sym: |
| 1357 | proper_name = is_not_proper(name) |
| 1358 | if proper_name: |
| 1359 | name = proper_name |
| 1360 | sym = kconf.syms.get(name) |
| 1361 | if not sym: |
| 1362 | for prefix in SPL_PREFIXES: |
| 1363 | try_name = prefix + name |
| 1364 | sym = kconf.syms.get(try_name) |
| 1365 | if sym: |
| 1366 | break |
| 1367 | if not sym: |
| 1368 | not_found[name].append(use) |
| 1369 | continue |
| 1370 | |
| 1371 | sym = kconf.syms.get(name) |
| 1372 | if not sym and check: |
| 1373 | not_found[name].append(use) |
| 1374 | return not_found |
| 1375 | |
| 1376 | def show_uses(uses): |
| 1377 | """Show a list of uses along with their filename and code snippet |
| 1378 | |
| 1379 | Args: |
| 1380 | uses (dict): |
| 1381 | key (str): CONFIG name (without 'CONFIG_' prefix |
| 1382 | value (list of ConfigUse): List of uses of this CONFIG |
| 1383 | """ |
| 1384 | for name in sorted(uses): |
| 1385 | print(f'{name}: ', end='') |
| 1386 | for i, use in enumerate(uses[name]): |
| 1387 | print(f'{" " if i else ""}{use.fname}: {use.rest.strip()}') |
| 1388 | |
| 1389 | |
| 1390 | print('Scanning Kconfig') |
Simon Glass | 0a0c124 | 2024-07-17 16:56:51 +0100 | [diff] [blame] | 1391 | kconf = scan_kconfig() |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1392 | print(f'Scanning source in {path}') |
| 1393 | args = ['git', 'grep', '-E', r'IS_ENABLED|\bCONFIG'] |
| 1394 | with subprocess.Popen(args, stdout=subprocess.PIPE) as proc: |
Simon Glass | 62fae4b | 2023-09-23 13:44:00 -0600 | [diff] [blame] | 1395 | out, _ = proc.communicate() |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1396 | lines = out.splitlines() |
| 1397 | re_fname = re.compile('^([^:]*):(.*)') |
| 1398 | src_list = [] |
| 1399 | mk_list = [] |
| 1400 | for line in lines: |
| 1401 | linestr = line.decode('utf-8') |
| 1402 | m_fname = re_fname.search(linestr) |
| 1403 | if not m_fname: |
| 1404 | continue |
| 1405 | fname, rest = m_fname.groups() |
| 1406 | dirname, leaf = os.path.split(fname) |
| 1407 | root, ext = os.path.splitext(leaf) |
| 1408 | if ext == '.autoconf': |
| 1409 | pass |
| 1410 | elif ext in ['.c', '.h', '.S', '.lds', '.dts', '.dtsi', '.asl', '.cfg', |
| 1411 | '.env', '.tmpl']: |
| 1412 | src_list.append([fname, rest]) |
| 1413 | elif 'Makefile' in root or ext == '.mk': |
| 1414 | mk_list.append([fname, rest]) |
| 1415 | elif ext in ['.yml', '.sh', '.py', '.awk', '.pl', '.rst', '', '.sed']: |
| 1416 | pass |
| 1417 | elif 'Kconfig' in root or 'Kbuild' in root: |
| 1418 | pass |
| 1419 | elif 'README' in root: |
| 1420 | pass |
| 1421 | elif dirname in ['configs']: |
| 1422 | pass |
| 1423 | elif dirname.startswith('doc') or dirname.startswith('scripts/kconfig'): |
| 1424 | pass |
| 1425 | else: |
| 1426 | print(f'Not sure how to handle file {fname}') |
| 1427 | |
| 1428 | # Scan the Makefiles |
Simon Glass | 62fae4b | 2023-09-23 13:44:00 -0600 | [diff] [blame] | 1429 | all_uses, _ = scan_makefiles(mk_list) |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1430 | |
| 1431 | spl_not_found = set() |
| 1432 | proper_not_found = set() |
| 1433 | |
| 1434 | # Make sure we know about all the options |
| 1435 | print('\nCONFIG options present in Makefiles but not Kconfig:') |
| 1436 | not_found = check_not_found(all_uses, MODE_NORMAL) |
| 1437 | show_uses(not_found) |
| 1438 | |
| 1439 | print('\nCONFIG options present in Makefiles but not Kconfig (SPL):') |
| 1440 | not_found = check_not_found(all_uses, MODE_SPL) |
| 1441 | show_uses(not_found) |
Simon Glass | b774ba5 | 2023-09-23 13:44:03 -0600 | [diff] [blame] | 1442 | spl_not_found |= {is_not_proper(key) or key for key in not_found.keys()} |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1443 | |
| 1444 | print('\nCONFIG options used as Proper in Makefiles but without a non-SPL_ variant:') |
| 1445 | not_found = check_not_found(all_uses, MODE_PROPER) |
| 1446 | show_uses(not_found) |
Simon Glass | b774ba5 | 2023-09-23 13:44:03 -0600 | [diff] [blame] | 1447 | proper_not_found |= {not_found.keys()} |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1448 | |
| 1449 | # Scan the source code |
Simon Glass | 62fae4b | 2023-09-23 13:44:00 -0600 | [diff] [blame] | 1450 | all_uses, _ = scan_src_files(src_list) |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1451 | |
| 1452 | # Make sure we know about all the options |
| 1453 | print('\nCONFIG options present in source but not Kconfig:') |
| 1454 | not_found = check_not_found(all_uses, MODE_NORMAL) |
| 1455 | show_uses(not_found) |
| 1456 | |
| 1457 | print('\nCONFIG options present in source but not Kconfig (SPL):') |
| 1458 | not_found = check_not_found(all_uses, MODE_SPL) |
| 1459 | show_uses(not_found) |
Simon Glass | b774ba5 | 2023-09-23 13:44:03 -0600 | [diff] [blame] | 1460 | spl_not_found |= {is_not_proper(key) or key for key in not_found.keys()} |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1461 | |
| 1462 | print('\nCONFIG options used as Proper in source but without a non-SPL_ variant:') |
| 1463 | not_found = check_not_found(all_uses, MODE_PROPER) |
| 1464 | show_uses(not_found) |
Simon Glass | b774ba5 | 2023-09-23 13:44:03 -0600 | [diff] [blame] | 1465 | proper_not_found |= {not_found.keys()} |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1466 | |
| 1467 | print('\nCONFIG options used as SPL but without an SPL_ variant:') |
| 1468 | for item in sorted(spl_not_found): |
| 1469 | print(f' {item}') |
| 1470 | |
| 1471 | print('\nCONFIG options used as Proper but without a non-SPL_ variant:') |
| 1472 | for item in sorted(proper_not_found): |
| 1473 | print(f' {item}') |
| 1474 | |
| 1475 | # Write out the updated information |
| 1476 | if do_update: |
Simon Glass | e6c686f | 2023-09-23 13:44:04 -0600 | [diff] [blame] | 1477 | with open(os.path.join(path, 'scripts', 'conf_nospl'), 'w', |
| 1478 | encoding='utf-8') as out: |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1479 | print('# These options should not be enabled in SPL builds\n', |
| 1480 | file=out) |
| 1481 | for item in sorted(spl_not_found): |
| 1482 | print(item, file=out) |
Simon Glass | e6c686f | 2023-09-23 13:44:04 -0600 | [diff] [blame] | 1483 | with open(os.path.join(path, 'scripts', 'conf_noproper'), 'w', |
| 1484 | encoding='utf-8') as out: |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1485 | print('# These options should not be enabled in Proper builds\n', |
| 1486 | file=out) |
| 1487 | for item in sorted(proper_not_found): |
| 1488 | print(item, file=out) |
Simon Glass | d63357e | 2024-07-17 16:57:02 +0100 | [diff] [blame] | 1489 | return 0 |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1490 | |
| 1491 | |
Simon Glass | a056c42 | 2024-07-17 16:56:55 +0100 | [diff] [blame] | 1492 | def parse_args(): |
| 1493 | """Parse the program arguments |
| 1494 | |
| 1495 | Returns: |
| 1496 | tuple: |
| 1497 | argparse.ArgumentParser: parser |
| 1498 | argparse.Namespace: Parsed arguments |
| 1499 | """ |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 1500 | try: |
| 1501 | cpu_count = multiprocessing.cpu_count() |
| 1502 | except NotImplementedError: |
| 1503 | cpu_count = 1 |
| 1504 | |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1505 | epilog = '''Move config options from headers to defconfig files. See |
| 1506 | doc/develop/moveconfig.rst for documentation.''' |
| 1507 | |
| 1508 | parser = ArgumentParser(epilog=epilog) |
| 1509 | # Add arguments here |
| 1510 | parser.add_argument('-a', '--add-imply', type=str, default='', |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1511 | help='comma-separated list of CONFIG options to add ' |
| 1512 | "an 'imply' statement to for the CONFIG in -i") |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1513 | parser.add_argument('-A', '--skip-added', action='store_true', default=False, |
Simon Glass | cb00883 | 2017-06-15 21:39:33 -0600 | [diff] [blame] | 1514 | help="don't show options which are already marked as " |
| 1515 | 'implying others') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1516 | parser.add_argument('-b', '--build-db', action='store_true', default=False, |
Simon Glass | d73fcb1 | 2017-06-01 19:39:02 -0600 | [diff] [blame] | 1517 | help='build a CONFIG database') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1518 | parser.add_argument('-C', '--commit', action='store_true', default=False, |
Simon Glass | 9ede212 | 2016-09-12 23:18:21 -0600 | [diff] [blame] | 1519 | help='Create a git commit for the operation') |
Simon Glass | 15f19ab | 2023-09-23 13:44:09 -0600 | [diff] [blame] | 1520 | parser.add_argument('--nocolour', action='store_true', default=False, |
| 1521 | help="don't display the log in colour") |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1522 | parser.add_argument('-d', '--defconfigs', type=str, |
Simon Glass | ee4e61b | 2017-06-01 19:38:59 -0600 | [diff] [blame] | 1523 | help='a file containing a list of defconfigs to move, ' |
| 1524 | "one per line (for example 'snow_defconfig') " |
| 1525 | "or '-' to read from stdin") |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1526 | parser.add_argument('-e', '--exit-on-error', action='store_true', |
Simon Glass | e1ae563 | 2021-12-18 08:09:44 -0700 | [diff] [blame] | 1527 | default=False, |
| 1528 | help='exit immediately on any error') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1529 | parser.add_argument('-f', '--find', action='store_true', default=False, |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1530 | help='Find boards with a given config combination') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1531 | parser.add_argument('-i', '--imply', action='store_true', default=False, |
Simon Glass | 99b6660 | 2017-06-01 19:39:03 -0600 | [diff] [blame] | 1532 | help='find options which imply others') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1533 | parser.add_argument('-I', '--imply-flags', type=str, default='', |
Simon Glass | 9b2a2e8 | 2017-06-15 21:39:32 -0600 | [diff] [blame] | 1534 | help="control the -i option ('help' for help") |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1535 | parser.add_argument('-j', '--jobs', type=int, default=cpu_count, |
Simon Glass | e1ae563 | 2021-12-18 08:09:44 -0700 | [diff] [blame] | 1536 | help='the number of jobs to run simultaneously') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1537 | parser.add_argument('-n', '--dry-run', action='store_true', default=False, |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 1538 | help='perform a trial run (show log with no changes)') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1539 | parser.add_argument('-r', '--git-ref', type=str, |
Simon Glass | e1ae563 | 2021-12-18 08:09:44 -0700 | [diff] [blame] | 1540 | help='the git ref to clone for building the autoconf.mk') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1541 | parser.add_argument('-s', '--force-sync', action='store_true', default=False, |
Masahiro Yamada | 8513dc0 | 2016-05-19 15:52:08 +0900 | [diff] [blame] | 1542 | help='force sync by savedefconfig') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1543 | parser.add_argument('-S', '--spl', action='store_true', default=False, |
Masahiro Yamada | 07913d1 | 2016-08-22 22:18:22 +0900 | [diff] [blame] | 1544 | help='parse config options defined for SPL build') |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1545 | parser.add_argument('--scan-source', action='store_true', default=False, |
| 1546 | help='scan source for uses of CONFIG options') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1547 | parser.add_argument('-t', '--test', action='store_true', default=False, |
Simon Glass | e1ae563 | 2021-12-18 08:09:44 -0700 | [diff] [blame] | 1548 | help='run unit tests') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1549 | parser.add_argument('-y', '--yes', action='store_true', default=False, |
Simon Glass | 6b403df | 2016-09-12 23:18:20 -0600 | [diff] [blame] | 1550 | help="respond 'yes' to any prompts") |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1551 | parser.add_argument('-u', '--update', action='store_true', default=False, |
| 1552 | help="update scripts/ files (use with --scan-source)") |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1553 | parser.add_argument('-v', '--verbose', action='store_true', default=False, |
Joe Hershberger | 95bf9c7 | 2015-05-19 13:21:24 -0500 | [diff] [blame] | 1554 | help='show any build errors as boards are built') |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1555 | parser.add_argument('configs', nargs='*') |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 1556 | |
Simon Glass | a056c42 | 2024-07-17 16:56:55 +0100 | [diff] [blame] | 1557 | return parser, parser.parse_args() |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 1558 | |
Simon Glass | a056c42 | 2024-07-17 16:56:55 +0100 | [diff] [blame] | 1559 | |
Simon Glass | cc628f5 | 2024-07-17 16:57:03 +0100 | [diff] [blame] | 1560 | def imply(args): |
| 1561 | """Handle checking for flags which imply others |
| 1562 | |
| 1563 | Args: |
| 1564 | args (argparse.Namespace): Program arguments |
| 1565 | |
| 1566 | Returns: |
| 1567 | int: exit code (0 for success) |
| 1568 | """ |
| 1569 | imply_flags = 0 |
| 1570 | if args.imply_flags == 'all': |
| 1571 | imply_flags = -1 |
| 1572 | |
| 1573 | elif args.imply_flags: |
| 1574 | for flag in args.imply_flags.split(','): |
| 1575 | bad = flag not in IMPLY_FLAGS |
| 1576 | if bad: |
| 1577 | print(f"Invalid flag '{flag}'") |
| 1578 | if flag == 'help' or bad: |
| 1579 | print("Imply flags: (separate with ',')") |
| 1580 | for name, info in IMPLY_FLAGS.items(): |
| 1581 | print(f' {name:-15s}: {info[1]}') |
| 1582 | return 1 |
| 1583 | imply_flags |= IMPLY_FLAGS[flag][0] |
| 1584 | |
| 1585 | do_imply_config(args.configs, args.add_imply, imply_flags, args.skip_added) |
| 1586 | return 0 |
| 1587 | |
| 1588 | |
Simon Glass | 6c2a438 | 2024-07-17 16:57:06 +0100 | [diff] [blame] | 1589 | def add_commit(configs): |
| 1590 | """Add a commit indicating which CONFIG options were converted |
| 1591 | |
| 1592 | Args: |
| 1593 | configs (list of str) List of CONFIG_... options to process |
| 1594 | """ |
| 1595 | subprocess.call(['git', 'add', '-u']) |
| 1596 | if configs: |
| 1597 | part = 'et al ' if len(configs) > 1 else '' |
| 1598 | msg = f'Convert {configs[0]} {part}to Kconfig' |
| 1599 | msg += ('\n\nThis converts the following to Kconfig:\n %s\n' % |
| 1600 | '\n '.join(configs)) |
| 1601 | else: |
| 1602 | msg = 'configs: Resync with savedefconfig' |
| 1603 | msg += '\n\nRsync all defconfig files using moveconfig.py' |
| 1604 | subprocess.call(['git', 'commit', '-s', '-m', msg]) |
| 1605 | |
| 1606 | |
Simon Glass | 7e68804 | 2024-07-17 16:57:01 +0100 | [diff] [blame] | 1607 | def do_tests(): |
| 1608 | """Run doctests and unit tests (so far there are no unit tests)""" |
| 1609 | sys.argv = [sys.argv[0]] |
| 1610 | fail, _ = doctest.testmod() |
| 1611 | if fail: |
| 1612 | return 1 |
| 1613 | unittest.main() |
| 1614 | return 0 |
| 1615 | |
| 1616 | |
Simon Glass | a056c42 | 2024-07-17 16:56:55 +0100 | [diff] [blame] | 1617 | def main(): |
| 1618 | """Main program""" |
| 1619 | parser, args = parse_args() |
Simon Glass | 382c662 | 2024-07-17 16:56:57 +0100 | [diff] [blame] | 1620 | if not any((args.force_sync, args.build_db, args.imply, args.find, |
| 1621 | args.scan_source, args.test)): |
| 1622 | parser.print_usage() |
| 1623 | sys.exit(1) |
Simon Glass | c50b6f1 | 2024-07-17 16:56:59 +0100 | [diff] [blame] | 1624 | |
| 1625 | check_top_directory() |
| 1626 | |
Simon Glass | fd35fbe | 2024-07-17 16:57:00 +0100 | [diff] [blame] | 1627 | # prefix the option name with CONFIG_ if missing |
| 1628 | args.configs = [prefix_config(cfg) for cfg in args.configs] |
| 1629 | |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1630 | if args.test: |
Simon Glass | 7e68804 | 2024-07-17 16:57:01 +0100 | [diff] [blame] | 1631 | return do_tests() |
Simon Glass | 65e6203 | 2023-02-01 13:19:12 -0700 | [diff] [blame] | 1632 | if args.scan_source: |
Simon Glass | d63357e | 2024-07-17 16:57:02 +0100 | [diff] [blame] | 1633 | return do_scan_source(os.getcwd(), args.update) |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1634 | if args.imply: |
Simon Glass | cc628f5 | 2024-07-17 16:57:03 +0100 | [diff] [blame] | 1635 | if imply(args): |
| 1636 | parser.print_usage() |
| 1637 | sys.exit(1) |
Simon Glass | f297ba3 | 2023-09-23 13:44:05 -0600 | [diff] [blame] | 1638 | return 0 |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1639 | if args.find: |
Simon Glass | 630a9c9 | 2024-07-17 16:57:04 +0100 | [diff] [blame] | 1640 | return do_find_config(args.configs) |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1641 | |
Simon Glass | 035ecb4 | 2024-07-17 16:56:58 +0100 | [diff] [blame] | 1642 | col = terminal.Color(terminal.COLOR_NEVER if args.nocolour |
| 1643 | else terminal.COLOR_IF_TERMINAL) |
Simon Glass | eb1df3f | 2024-07-17 16:57:05 +0100 | [diff] [blame] | 1644 | config_db, progress = move_config(args, col) |
Joe Hershberger | 2144f88 | 2015-05-19 13:21:20 -0500 | [diff] [blame] | 1645 | |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1646 | if args.commit: |
Simon Glass | 6c2a438 | 2024-07-17 16:57:06 +0100 | [diff] [blame] | 1647 | add_commit(args.configs) |
Simon Glass | 9ede212 | 2016-09-12 23:18:21 -0600 | [diff] [blame] | 1648 | |
Simon Glass | b2e83c6 | 2021-12-18 14:54:31 -0700 | [diff] [blame] | 1649 | if args.build_db: |
Simon Glass | 6c2a438 | 2024-07-17 16:57:06 +0100 | [diff] [blame] | 1650 | configs = args.configs |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 1651 | with open(CONFIG_DATABASE, 'w', encoding='utf-8') as outf: |
Simon Glass | 793dca3 | 2019-10-31 07:42:57 -0600 | [diff] [blame] | 1652 | for defconfig, configs in config_db.items(): |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 1653 | outf.write(f'{defconfig}\n') |
Simon Glass | d73fcb1 | 2017-06-01 19:39:02 -0600 | [diff] [blame] | 1654 | for config in sorted(configs.keys()): |
Simon Glass | a4c9d17 | 2023-09-23 13:44:01 -0600 | [diff] [blame] | 1655 | outf.write(f' {config}={configs[config]}\n') |
| 1656 | outf.write('\n') |
Simon Glass | 94e2ed7 | 2023-09-23 13:44:13 -0600 | [diff] [blame] | 1657 | print(col.build( |
Simon Glass | 0229eee | 2024-07-17 16:57:07 +0100 | [diff] [blame^] | 1658 | col.RED if progress.failed else col.GREEN, |
| 1659 | f'{progress.failure_msg}{len(config_db)} boards written to {CONFIG_DATABASE}')) |
Simon Glass | 94e2ed7 | 2023-09-23 13:44:13 -0600 | [diff] [blame] | 1660 | else: |
Simon Glass | 0229eee | 2024-07-17 16:57:07 +0100 | [diff] [blame^] | 1661 | if progress.failed: |
| 1662 | print(col.build(col.RED, f'{progress.failure_msg}see {FAILED_LIST}', True)) |
Simon Glass | 94e2ed7 | 2023-09-23 13:44:13 -0600 | [diff] [blame] | 1663 | else: |
| 1664 | # Add enough spaces to overwrite the progress indicator |
| 1665 | print(col.build( |
| 1666 | col.GREEN, f'{progress.total} processed ', bright=True)) |
| 1667 | |
Simon Glass | f297ba3 | 2023-09-23 13:44:05 -0600 | [diff] [blame] | 1668 | return 0 |
| 1669 | |
Simon Glass | d73fcb1 | 2017-06-01 19:39:02 -0600 | [diff] [blame] | 1670 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 1671 | if __name__ == '__main__': |
Simon Glass | 65d7fce | 2021-12-18 08:09:46 -0700 | [diff] [blame] | 1672 | sys.exit(main()) |