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