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