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