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