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