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