Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # |
| 3 | # Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
| 4 | # |
| 5 | # SPDX-License-Identifier: GPL-2.0+ |
| 6 | # |
| 7 | |
| 8 | """ |
| 9 | Move config options from headers to defconfig files. |
| 10 | |
| 11 | Since Kconfig was introduced to U-Boot, we have worked on moving |
| 12 | config options from headers to Kconfig (defconfig). |
| 13 | |
| 14 | This tool intends to help this tremendous work. |
| 15 | |
| 16 | |
| 17 | Usage |
| 18 | ----- |
| 19 | |
| 20 | This tool takes one input file. (let's say 'recipe' file here.) |
| 21 | The recipe describes the list of config options you want to move. |
| 22 | Each line takes the form: |
| 23 | <config_name> <type> <default> |
| 24 | (the fields must be separated with whitespaces.) |
| 25 | |
| 26 | <config_name> is the name of config option. |
| 27 | |
| 28 | <type> is the type of the option. It must be one of bool, tristate, |
| 29 | string, int, and hex. |
| 30 | |
| 31 | <default> is the default value of the option. It must be appropriate |
| 32 | value corresponding to the option type. It must be either y or n for |
| 33 | the bool type. Tristate options can also take m (although U-Boot has |
| 34 | not supported the module feature). |
| 35 | |
| 36 | You can add two or more lines in the recipe file, so you can move |
| 37 | multiple options at once. |
| 38 | |
| 39 | Let's say, for example, you want to move CONFIG_CMD_USB and |
| 40 | CONFIG_SYS_TEXT_BASE. |
| 41 | |
| 42 | The type should be bool, hex, respectively. So, the recipe file |
| 43 | should look like this: |
| 44 | |
| 45 | $ cat recipe |
| 46 | CONFIG_CMD_USB bool n |
| 47 | CONFIG_SYS_TEXT_BASE hex 0x00000000 |
| 48 | |
Joe Hershberger | 96464ba | 2015-05-19 13:21:17 -0500 | [diff] [blame] | 49 | Next you must edit the Kconfig to add the menu entries for the configs |
| 50 | you are moving. |
| 51 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 52 | And then run this tool giving the file name of the recipe |
| 53 | |
| 54 | $ tools/moveconfig.py recipe |
| 55 | |
| 56 | The tool walks through all the defconfig files to move the config |
| 57 | options specified by the recipe file. |
| 58 | |
| 59 | The log is also displayed on the terminal. |
| 60 | |
| 61 | Each line is printed in the format |
| 62 | <defconfig_name> : <action> |
| 63 | |
| 64 | <defconfig_name> is the name of the defconfig |
| 65 | (without the suffix _defconfig). |
| 66 | |
| 67 | <action> shows what the tool did for that defconfig. |
| 68 | It looks like one of the followings: |
| 69 | |
| 70 | - Move 'CONFIG_... ' |
| 71 | This config option was moved to the defconfig |
| 72 | |
| 73 | - Default value 'CONFIG_...'. Do nothing. |
| 74 | The value of this option is the same as default. |
| 75 | We do not have to add it to the defconfig. |
| 76 | |
| 77 | - 'CONFIG_...' already exists in Kconfig. Do nothing. |
| 78 | This config option is already defined in Kconfig. |
| 79 | We do not need/want to touch it. |
| 80 | |
| 81 | - Undefined. Do nothing. |
| 82 | This config option was not found in the config header. |
| 83 | Nothing to do. |
| 84 | |
| 85 | - Failed to process. Skip. |
| 86 | An error occurred during processing this defconfig. Skipped. |
| 87 | (If -e option is passed, the tool exits immediately on error.) |
| 88 | |
| 89 | Finally, you will be asked, Clean up headers? [y/n]: |
| 90 | |
| 91 | If you say 'y' here, the unnecessary config defines are removed |
| 92 | from the config headers (include/configs/*.h). |
| 93 | It just uses the regex method, so you should not rely on it. |
| 94 | Just in case, please do 'git diff' to see what happened. |
| 95 | |
| 96 | |
| 97 | How does it works? |
| 98 | ------------------ |
| 99 | |
| 100 | This tool runs configuration and builds include/autoconf.mk for every |
| 101 | defconfig. The config options defined in Kconfig appear in the .config |
| 102 | file (unless they are hidden because of unmet dependency.) |
| 103 | On the other hand, the config options defined by board headers are seen |
| 104 | in include/autoconf.mk. The tool looks for the specified options in both |
| 105 | of them to decide the appropriate action for the options. If the option |
| 106 | is found in the .config or the value is the same as the specified default, |
| 107 | the option does not need to be touched. If the option is found in |
| 108 | include/autoconf.mk, but not in the .config, and the value is different |
| 109 | from the default, the tools adds the option to the defconfig. |
| 110 | |
| 111 | For faster processing, this tool handles multi-threading. It creates |
| 112 | separate build directories where the out-of-tree build is run. The |
| 113 | temporary build directories are automatically created and deleted as |
| 114 | needed. The number of threads are chosen based on the number of the CPU |
| 115 | cores of your system although you can change it via -j (--jobs) option. |
| 116 | |
| 117 | |
| 118 | Toolchains |
| 119 | ---------- |
| 120 | |
| 121 | Appropriate toolchain are necessary to generate include/autoconf.mk |
| 122 | for all the architectures supported by U-Boot. Most of them are available |
| 123 | at the kernel.org site, some are not provided by kernel.org. |
| 124 | |
| 125 | The default per-arch CROSS_COMPILE used by this tool is specified by |
| 126 | the list below, CROSS_COMPILE. You may wish to update the list to |
| 127 | use your own. Instead of modifying the list directly, you can give |
| 128 | them via environments. |
| 129 | |
| 130 | |
| 131 | Available options |
| 132 | ----------------- |
| 133 | |
| 134 | -c, --color |
| 135 | Surround each portion of the log with escape sequences to display it |
| 136 | in color on the terminal. |
| 137 | |
Joe Hershberger | 91040e8 | 2015-05-19 13:21:19 -0500 | [diff] [blame] | 138 | -d, --defconfigs |
| 139 | Specify a file containing a list of defconfigs to move |
| 140 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 141 | -n, --dry-run |
| 142 | Peform a trial run that does not make any changes. It is useful to |
| 143 | see what is going to happen before one actually runs it. |
| 144 | |
| 145 | -e, --exit-on-error |
| 146 | Exit immediately if Make exits with a non-zero status while processing |
| 147 | a defconfig file. |
| 148 | |
Joe Hershberger | 2144f88 | 2015-05-19 13:21:20 -0500 | [diff] [blame] | 149 | -H, --headers-only |
| 150 | Only cleanup the headers; skip the defconfig processing |
| 151 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 152 | -j, --jobs |
| 153 | Specify the number of threads to run simultaneously. If not specified, |
| 154 | the number of threads is the same as the number of CPU cores. |
| 155 | |
| 156 | To see the complete list of supported options, run |
| 157 | |
| 158 | $ tools/moveconfig.py -h |
| 159 | |
| 160 | """ |
| 161 | |
| 162 | import fnmatch |
| 163 | import multiprocessing |
| 164 | import optparse |
| 165 | import os |
| 166 | import re |
| 167 | import shutil |
| 168 | import subprocess |
| 169 | import sys |
| 170 | import tempfile |
| 171 | import time |
| 172 | |
| 173 | SHOW_GNU_MAKE = 'scripts/show-gnu-make' |
| 174 | SLEEP_TIME=0.03 |
| 175 | |
| 176 | # Here is the list of cross-tools I use. |
| 177 | # Most of them are available at kernel.org |
| 178 | # (https://www.kernel.org/pub/tools/crosstool/files/bin/), except the followings: |
| 179 | # arc: https://github.com/foss-for-synopsys-dwc-arc-processors/toolchain/releases |
| 180 | # blackfin: http://sourceforge.net/projects/adi-toolchain/files/ |
| 181 | # nds32: http://osdk.andestech.com/packages/ |
| 182 | # nios2: https://sourcery.mentor.com/GNUToolchain/subscription42545 |
| 183 | # sh: http://sourcery.mentor.com/public/gnu_toolchain/sh-linux-gnu |
| 184 | CROSS_COMPILE = { |
| 185 | 'arc': 'arc-linux-', |
| 186 | 'aarch64': 'aarch64-linux-', |
| 187 | 'arm': 'arm-unknown-linux-gnueabi-', |
| 188 | 'avr32': 'avr32-linux-', |
| 189 | 'blackfin': 'bfin-elf-', |
| 190 | 'm68k': 'm68k-linux-', |
| 191 | 'microblaze': 'microblaze-linux-', |
| 192 | 'mips': 'mips-linux-', |
| 193 | 'nds32': 'nds32le-linux-', |
| 194 | 'nios2': 'nios2-linux-gnu-', |
| 195 | 'openrisc': 'or32-linux-', |
| 196 | 'powerpc': 'powerpc-linux-', |
| 197 | 'sh': 'sh-linux-gnu-', |
| 198 | 'sparc': 'sparc-linux-', |
| 199 | 'x86': 'i386-linux-' |
| 200 | } |
| 201 | |
| 202 | STATE_IDLE = 0 |
| 203 | STATE_DEFCONFIG = 1 |
| 204 | STATE_AUTOCONF = 2 |
Joe Hershberger | 96464ba | 2015-05-19 13:21:17 -0500 | [diff] [blame] | 205 | STATE_SAVEDEFCONFIG = 3 |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 206 | |
| 207 | ACTION_MOVE = 0 |
| 208 | ACTION_DEFAULT_VALUE = 1 |
| 209 | ACTION_ALREADY_EXIST = 2 |
| 210 | ACTION_UNDEFINED = 3 |
| 211 | |
| 212 | COLOR_BLACK = '0;30' |
| 213 | COLOR_RED = '0;31' |
| 214 | COLOR_GREEN = '0;32' |
| 215 | COLOR_BROWN = '0;33' |
| 216 | COLOR_BLUE = '0;34' |
| 217 | COLOR_PURPLE = '0;35' |
| 218 | COLOR_CYAN = '0;36' |
| 219 | COLOR_LIGHT_GRAY = '0;37' |
| 220 | COLOR_DARK_GRAY = '1;30' |
| 221 | COLOR_LIGHT_RED = '1;31' |
| 222 | COLOR_LIGHT_GREEN = '1;32' |
| 223 | COLOR_YELLOW = '1;33' |
| 224 | COLOR_LIGHT_BLUE = '1;34' |
| 225 | COLOR_LIGHT_PURPLE = '1;35' |
| 226 | COLOR_LIGHT_CYAN = '1;36' |
| 227 | COLOR_WHITE = '1;37' |
| 228 | |
| 229 | ### helper functions ### |
| 230 | def get_devnull(): |
| 231 | """Get the file object of '/dev/null' device.""" |
| 232 | try: |
| 233 | devnull = subprocess.DEVNULL # py3k |
| 234 | except AttributeError: |
| 235 | devnull = open(os.devnull, 'wb') |
| 236 | return devnull |
| 237 | |
| 238 | def check_top_directory(): |
| 239 | """Exit if we are not at the top of source directory.""" |
| 240 | for f in ('README', 'Licenses'): |
| 241 | if not os.path.exists(f): |
| 242 | sys.exit('Please run at the top of source directory.') |
| 243 | |
| 244 | def get_make_cmd(): |
| 245 | """Get the command name of GNU Make. |
| 246 | |
| 247 | U-Boot needs GNU Make for building, but the command name is not |
| 248 | necessarily "make". (for example, "gmake" on FreeBSD). |
| 249 | Returns the most appropriate command name on your system. |
| 250 | """ |
| 251 | process = subprocess.Popen([SHOW_GNU_MAKE], stdout=subprocess.PIPE) |
| 252 | ret = process.communicate() |
| 253 | if process.returncode: |
| 254 | sys.exit('GNU Make not found') |
| 255 | return ret[0].rstrip() |
| 256 | |
| 257 | def color_text(color_enabled, color, string): |
| 258 | """Return colored string.""" |
| 259 | if color_enabled: |
| 260 | return '\033[' + color + 'm' + string + '\033[0m' |
| 261 | else: |
| 262 | return string |
| 263 | |
| 264 | def log_msg(color_enabled, color, defconfig, msg): |
| 265 | """Return the formated line for the log.""" |
| 266 | return defconfig[:-len('_defconfig')].ljust(37) + ': ' + \ |
| 267 | color_text(color_enabled, color, msg) + '\n' |
| 268 | |
| 269 | def update_cross_compile(): |
| 270 | """Update per-arch CROSS_COMPILE via enviroment variables |
| 271 | |
| 272 | The default CROSS_COMPILE values are available |
| 273 | in the CROSS_COMPILE list above. |
| 274 | |
| 275 | You can override them via enviroment variables |
| 276 | CROSS_COMPILE_{ARCH}. |
| 277 | |
| 278 | For example, if you want to override toolchain prefixes |
| 279 | for ARM and PowerPC, you can do as follows in your shell: |
| 280 | |
| 281 | export CROSS_COMPILE_ARM=... |
| 282 | export CROSS_COMPILE_POWERPC=... |
| 283 | """ |
| 284 | archs = [] |
| 285 | |
| 286 | for arch in os.listdir('arch'): |
| 287 | if os.path.exists(os.path.join('arch', arch, 'Makefile')): |
| 288 | archs.append(arch) |
| 289 | |
| 290 | # arm64 is a special case |
| 291 | archs.append('aarch64') |
| 292 | |
| 293 | for arch in archs: |
| 294 | env = 'CROSS_COMPILE_' + arch.upper() |
| 295 | cross_compile = os.environ.get(env) |
| 296 | if cross_compile: |
| 297 | CROSS_COMPILE[arch] = cross_compile |
| 298 | |
| 299 | def cleanup_one_header(header_path, patterns, dry_run): |
| 300 | """Clean regex-matched lines away from a file. |
| 301 | |
| 302 | Arguments: |
| 303 | header_path: path to the cleaned file. |
| 304 | patterns: list of regex patterns. Any lines matching to these |
| 305 | patterns are deleted. |
| 306 | dry_run: make no changes, but still display log. |
| 307 | """ |
| 308 | with open(header_path) as f: |
| 309 | lines = f.readlines() |
| 310 | |
| 311 | matched = [] |
| 312 | for i, line in enumerate(lines): |
| 313 | for pattern in patterns: |
| 314 | m = pattern.search(line) |
| 315 | if m: |
| 316 | print '%s: %s: %s' % (header_path, i + 1, line), |
| 317 | matched.append(i) |
| 318 | break |
| 319 | |
| 320 | if dry_run or not matched: |
| 321 | return |
| 322 | |
| 323 | with open(header_path, 'w') as f: |
| 324 | for i, line in enumerate(lines): |
| 325 | if not i in matched: |
| 326 | f.write(line) |
| 327 | |
| 328 | def cleanup_headers(config_attrs, dry_run): |
| 329 | """Delete config defines from board headers. |
| 330 | |
| 331 | Arguments: |
| 332 | config_attrs: A list of dictionaris, each of them includes the name, |
| 333 | the type, and the default value of the target config. |
| 334 | dry_run: make no changes, but still display log. |
| 335 | """ |
| 336 | while True: |
| 337 | choice = raw_input('Clean up headers? [y/n]: ').lower() |
| 338 | print choice |
| 339 | if choice == 'y' or choice == 'n': |
| 340 | break |
| 341 | |
| 342 | if choice == 'n': |
| 343 | return |
| 344 | |
| 345 | patterns = [] |
| 346 | for config_attr in config_attrs: |
| 347 | config = config_attr['config'] |
| 348 | patterns.append(re.compile(r'#\s*define\s+%s\W' % config)) |
| 349 | patterns.append(re.compile(r'#\s*undef\s+%s\W' % config)) |
| 350 | |
Joe Hershberger | 60727f5 | 2015-05-19 13:21:21 -0500 | [diff] [blame] | 351 | for dir in 'include', 'arch', 'board': |
| 352 | for (dirpath, dirnames, filenames) in os.walk(dir): |
| 353 | for filename in filenames: |
| 354 | if not fnmatch.fnmatch(filename, '*~'): |
| 355 | cleanup_one_header(os.path.join(dirpath, filename), |
| 356 | patterns, dry_run) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 357 | |
| 358 | ### classes ### |
| 359 | class KconfigParser: |
| 360 | |
| 361 | """A parser of .config and include/autoconf.mk.""" |
| 362 | |
| 363 | re_arch = re.compile(r'CONFIG_SYS_ARCH="(.*)"') |
| 364 | re_cpu = re.compile(r'CONFIG_SYS_CPU="(.*)"') |
| 365 | |
| 366 | def __init__(self, config_attrs, options, build_dir): |
| 367 | """Create a new parser. |
| 368 | |
| 369 | Arguments: |
| 370 | config_attrs: A list of dictionaris, each of them includes the name, |
| 371 | the type, and the default value of the target config. |
| 372 | options: option flags. |
| 373 | build_dir: Build directory. |
| 374 | """ |
| 375 | self.config_attrs = config_attrs |
| 376 | self.options = options |
| 377 | self.build_dir = build_dir |
| 378 | |
| 379 | def get_cross_compile(self): |
| 380 | """Parse .config file and return CROSS_COMPILE. |
| 381 | |
| 382 | Returns: |
| 383 | A string storing the compiler prefix for the architecture. |
| 384 | """ |
| 385 | arch = '' |
| 386 | cpu = '' |
| 387 | dotconfig = os.path.join(self.build_dir, '.config') |
| 388 | for line in open(dotconfig): |
| 389 | m = self.re_arch.match(line) |
| 390 | if m: |
| 391 | arch = m.group(1) |
| 392 | continue |
| 393 | m = self.re_cpu.match(line) |
| 394 | if m: |
| 395 | cpu = m.group(1) |
| 396 | |
| 397 | assert arch, 'Error: arch is not defined in %s' % defconfig |
| 398 | |
| 399 | # fix-up for aarch64 |
| 400 | if arch == 'arm' and cpu == 'armv8': |
| 401 | arch = 'aarch64' |
| 402 | |
| 403 | return CROSS_COMPILE.get(arch, '') |
| 404 | |
Joe Hershberger | 96464ba | 2015-05-19 13:21:17 -0500 | [diff] [blame] | 405 | def parse_one_config(self, config_attr, defconfig_lines, autoconf_lines): |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 406 | """Parse .config, defconfig, include/autoconf.mk for one config. |
| 407 | |
| 408 | This function looks for the config options in the lines from |
| 409 | defconfig, .config, and include/autoconf.mk in order to decide |
| 410 | which action should be taken for this defconfig. |
| 411 | |
| 412 | Arguments: |
| 413 | config_attr: A dictionary including the name, the type, |
| 414 | and the default value of the target config. |
| 415 | defconfig_lines: lines from the original defconfig file. |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 416 | autoconf_lines: lines from the include/autoconf.mk file. |
| 417 | |
| 418 | Returns: |
| 419 | A tupple of the action for this defconfig and the line |
| 420 | matched for the config. |
| 421 | """ |
| 422 | config = config_attr['config'] |
| 423 | not_set = '# %s is not set' % config |
| 424 | |
| 425 | if config_attr['type'] in ('bool', 'tristate') and \ |
| 426 | config_attr['default'] == 'n': |
| 427 | default = not_set |
| 428 | else: |
| 429 | default = config + '=' + config_attr['default'] |
| 430 | |
Joe Hershberger | 96464ba | 2015-05-19 13:21:17 -0500 | [diff] [blame] | 431 | for line in defconfig_lines: |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 432 | line = line.rstrip() |
| 433 | if line.startswith(config + '=') or line == not_set: |
| 434 | return (ACTION_ALREADY_EXIST, line) |
| 435 | |
| 436 | if config_attr['type'] in ('bool', 'tristate'): |
| 437 | value = not_set |
| 438 | else: |
| 439 | value = '(undefined)' |
| 440 | |
| 441 | for line in autoconf_lines: |
| 442 | line = line.rstrip() |
| 443 | if line.startswith(config + '='): |
| 444 | value = line |
| 445 | break |
| 446 | |
| 447 | if value == default: |
| 448 | action = ACTION_DEFAULT_VALUE |
| 449 | elif value == '(undefined)': |
| 450 | action = ACTION_UNDEFINED |
| 451 | else: |
| 452 | action = ACTION_MOVE |
| 453 | |
| 454 | return (action, value) |
| 455 | |
| 456 | def update_defconfig(self, defconfig): |
| 457 | """Parse files for the config options and update the defconfig. |
| 458 | |
| 459 | This function parses the given defconfig, the generated .config |
| 460 | and include/autoconf.mk searching the target options. |
| 461 | Move the config option(s) to the defconfig or do nothing if unneeded. |
| 462 | Also, display the log to show what happened to this defconfig. |
| 463 | |
| 464 | Arguments: |
| 465 | defconfig: defconfig name. |
| 466 | """ |
| 467 | |
| 468 | defconfig_path = os.path.join('configs', defconfig) |
| 469 | dotconfig_path = os.path.join(self.build_dir, '.config') |
| 470 | autoconf_path = os.path.join(self.build_dir, 'include', 'autoconf.mk') |
| 471 | results = [] |
| 472 | |
| 473 | with open(defconfig_path) as f: |
| 474 | defconfig_lines = f.readlines() |
| 475 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 476 | with open(autoconf_path) as f: |
| 477 | autoconf_lines = f.readlines() |
| 478 | |
| 479 | for config_attr in self.config_attrs: |
| 480 | result = self.parse_one_config(config_attr, defconfig_lines, |
Joe Hershberger | 96464ba | 2015-05-19 13:21:17 -0500 | [diff] [blame] | 481 | autoconf_lines) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 482 | results.append(result) |
| 483 | |
| 484 | log = '' |
| 485 | |
| 486 | for (action, value) in results: |
| 487 | if action == ACTION_MOVE: |
| 488 | actlog = "Move '%s'" % value |
| 489 | log_color = COLOR_LIGHT_GREEN |
| 490 | elif action == ACTION_DEFAULT_VALUE: |
| 491 | actlog = "Default value '%s'. Do nothing." % value |
| 492 | log_color = COLOR_LIGHT_BLUE |
| 493 | elif action == ACTION_ALREADY_EXIST: |
| 494 | actlog = "'%s' already defined in Kconfig. Do nothing." % value |
| 495 | log_color = COLOR_LIGHT_PURPLE |
| 496 | elif action == ACTION_UNDEFINED: |
| 497 | actlog = "Undefined. Do nothing." |
| 498 | log_color = COLOR_DARK_GRAY |
| 499 | else: |
| 500 | sys.exit("Internal Error. This should not happen.") |
| 501 | |
| 502 | log += log_msg(self.options.color, log_color, defconfig, actlog) |
| 503 | |
| 504 | # Some threads are running in parallel. |
| 505 | # Print log in one shot to not mix up logs from different threads. |
| 506 | print log, |
| 507 | |
| 508 | if not self.options.dry_run: |
Joe Hershberger | 96464ba | 2015-05-19 13:21:17 -0500 | [diff] [blame] | 509 | with open(dotconfig_path, 'a') as f: |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 510 | for (action, value) in results: |
| 511 | if action == ACTION_MOVE: |
| 512 | f.write(value + '\n') |
| 513 | |
| 514 | os.remove(os.path.join(self.build_dir, 'include', 'config', 'auto.conf')) |
| 515 | os.remove(autoconf_path) |
| 516 | |
| 517 | class Slot: |
| 518 | |
| 519 | """A slot to store a subprocess. |
| 520 | |
| 521 | Each instance of this class handles one subprocess. |
| 522 | This class is useful to control multiple threads |
| 523 | for faster processing. |
| 524 | """ |
| 525 | |
| 526 | def __init__(self, config_attrs, options, devnull, make_cmd): |
| 527 | """Create a new process slot. |
| 528 | |
| 529 | Arguments: |
| 530 | config_attrs: A list of dictionaris, each of them includes the name, |
| 531 | the type, and the default value of the target config. |
| 532 | options: option flags. |
| 533 | devnull: A file object of '/dev/null'. |
| 534 | make_cmd: command name of GNU Make. |
| 535 | """ |
| 536 | self.options = options |
| 537 | self.build_dir = tempfile.mkdtemp() |
| 538 | self.devnull = devnull |
| 539 | self.make_cmd = (make_cmd, 'O=' + self.build_dir) |
| 540 | self.parser = KconfigParser(config_attrs, options, self.build_dir) |
| 541 | self.state = STATE_IDLE |
| 542 | self.failed_boards = [] |
| 543 | |
| 544 | def __del__(self): |
| 545 | """Delete the working directory |
| 546 | |
| 547 | This function makes sure the temporary directory is cleaned away |
| 548 | even if Python suddenly dies due to error. It should be done in here |
| 549 | because it is guranteed the destructor is always invoked when the |
| 550 | instance of the class gets unreferenced. |
| 551 | |
| 552 | If the subprocess is still running, wait until it finishes. |
| 553 | """ |
| 554 | if self.state != STATE_IDLE: |
| 555 | while self.ps.poll() == None: |
| 556 | pass |
| 557 | shutil.rmtree(self.build_dir) |
| 558 | |
| 559 | def add(self, defconfig): |
| 560 | """Assign a new subprocess for defconfig and add it to the slot. |
| 561 | |
| 562 | If the slot is vacant, create a new subprocess for processing the |
| 563 | given defconfig and add it to the slot. Just returns False if |
| 564 | the slot is occupied (i.e. the current subprocess is still running). |
| 565 | |
| 566 | Arguments: |
| 567 | defconfig: defconfig name. |
| 568 | |
| 569 | Returns: |
| 570 | Return True on success or False on failure |
| 571 | """ |
| 572 | if self.state != STATE_IDLE: |
| 573 | return False |
| 574 | cmd = list(self.make_cmd) |
| 575 | cmd.append(defconfig) |
Joe Hershberger | 2540009 | 2015-05-19 13:21:23 -0500 | [diff] [blame^] | 576 | self.ps = subprocess.Popen(cmd, stdout=self.devnull, |
| 577 | stderr=subprocess.PIPE) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 578 | self.defconfig = defconfig |
| 579 | self.state = STATE_DEFCONFIG |
| 580 | return True |
| 581 | |
| 582 | def poll(self): |
| 583 | """Check the status of the subprocess and handle it as needed. |
| 584 | |
| 585 | Returns True if the slot is vacant (i.e. in idle state). |
| 586 | If the configuration is successfully finished, assign a new |
| 587 | subprocess to build include/autoconf.mk. |
| 588 | If include/autoconf.mk is generated, invoke the parser to |
| 589 | parse the .config and the include/autoconf.mk, and then set the |
| 590 | slot back to the idle state. |
| 591 | |
| 592 | Returns: |
| 593 | Return True if the subprocess is terminated, False otherwise |
| 594 | """ |
| 595 | if self.state == STATE_IDLE: |
| 596 | return True |
| 597 | |
| 598 | if self.ps.poll() == None: |
| 599 | return False |
| 600 | |
| 601 | if self.ps.poll() != 0: |
Joe Hershberger | 2540009 | 2015-05-19 13:21:23 -0500 | [diff] [blame^] | 602 | errmsg = 'Failed to process.' |
| 603 | errout = self.ps.stderr.read() |
| 604 | if errout.find('gcc: command not found') != -1: |
| 605 | errmsg = 'Compiler not found (' |
| 606 | errmsg += color_text(self.options.color, COLOR_YELLOW, |
| 607 | self.cross_compile) |
| 608 | errmsg += color_text(self.options.color, COLOR_LIGHT_RED, |
| 609 | ')') |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 610 | print >> sys.stderr, log_msg(self.options.color, |
| 611 | COLOR_LIGHT_RED, |
| 612 | self.defconfig, |
Joe Hershberger | 2540009 | 2015-05-19 13:21:23 -0500 | [diff] [blame^] | 613 | errmsg), |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 614 | if self.options.exit_on_error: |
| 615 | sys.exit("Exit on error.") |
| 616 | else: |
| 617 | # If --exit-on-error flag is not set, |
| 618 | # skip this board and continue. |
| 619 | # Record the failed board. |
| 620 | self.failed_boards.append(self.defconfig) |
| 621 | self.state = STATE_IDLE |
| 622 | return True |
| 623 | |
| 624 | if self.state == STATE_AUTOCONF: |
| 625 | self.parser.update_defconfig(self.defconfig) |
Joe Hershberger | 96464ba | 2015-05-19 13:21:17 -0500 | [diff] [blame] | 626 | |
| 627 | """Save off the defconfig in a consistent way""" |
| 628 | cmd = list(self.make_cmd) |
| 629 | cmd.append('savedefconfig') |
| 630 | self.ps = subprocess.Popen(cmd, stdout=self.devnull, |
Joe Hershberger | 2540009 | 2015-05-19 13:21:23 -0500 | [diff] [blame^] | 631 | stderr=subprocess.PIPE) |
Joe Hershberger | 96464ba | 2015-05-19 13:21:17 -0500 | [diff] [blame] | 632 | self.state = STATE_SAVEDEFCONFIG |
| 633 | return False |
| 634 | |
| 635 | if self.state == STATE_SAVEDEFCONFIG: |
| 636 | defconfig_path = os.path.join(self.build_dir, 'defconfig') |
| 637 | shutil.move(defconfig_path, |
| 638 | os.path.join('configs', self.defconfig)) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 639 | self.state = STATE_IDLE |
| 640 | return True |
| 641 | |
Joe Hershberger | 2540009 | 2015-05-19 13:21:23 -0500 | [diff] [blame^] | 642 | self.cross_compile = self.parser.get_cross_compile() |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 643 | cmd = list(self.make_cmd) |
Joe Hershberger | 2540009 | 2015-05-19 13:21:23 -0500 | [diff] [blame^] | 644 | if self.cross_compile: |
| 645 | cmd.append('CROSS_COMPILE=%s' % self.cross_compile) |
Joe Hershberger | 7740f65 | 2015-05-19 13:21:18 -0500 | [diff] [blame] | 646 | cmd.append('KCONFIG_IGNORE_DUPLICATES=1') |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 647 | cmd.append('include/config/auto.conf') |
Joe Hershberger | 2540009 | 2015-05-19 13:21:23 -0500 | [diff] [blame^] | 648 | """This will be screen-scraped, so be sure the expected text will be |
| 649 | returned consistently on every machine by setting LANG=C""" |
| 650 | self.ps = subprocess.Popen(cmd, stdout=self.devnull, |
| 651 | env=dict(os.environ, LANG='C'), |
| 652 | stderr=subprocess.PIPE) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 653 | self.state = STATE_AUTOCONF |
| 654 | return False |
| 655 | |
| 656 | def get_failed_boards(self): |
| 657 | """Returns a list of failed boards (defconfigs) in this slot. |
| 658 | """ |
| 659 | return self.failed_boards |
| 660 | |
| 661 | class Slots: |
| 662 | |
| 663 | """Controller of the array of subprocess slots.""" |
| 664 | |
| 665 | def __init__(self, config_attrs, options): |
| 666 | """Create a new slots controller. |
| 667 | |
| 668 | Arguments: |
| 669 | config_attrs: A list of dictionaris containing the name, the type, |
| 670 | and the default value of the target CONFIG. |
| 671 | options: option flags. |
| 672 | """ |
| 673 | self.options = options |
| 674 | self.slots = [] |
| 675 | devnull = get_devnull() |
| 676 | make_cmd = get_make_cmd() |
| 677 | for i in range(options.jobs): |
| 678 | self.slots.append(Slot(config_attrs, options, devnull, make_cmd)) |
| 679 | |
| 680 | def add(self, defconfig): |
| 681 | """Add a new subprocess if a vacant slot is found. |
| 682 | |
| 683 | Arguments: |
| 684 | defconfig: defconfig name to be put into. |
| 685 | |
| 686 | Returns: |
| 687 | Return True on success or False on failure |
| 688 | """ |
| 689 | for slot in self.slots: |
| 690 | if slot.add(defconfig): |
| 691 | return True |
| 692 | return False |
| 693 | |
| 694 | def available(self): |
| 695 | """Check if there is a vacant slot. |
| 696 | |
| 697 | Returns: |
| 698 | Return True if at lease one vacant slot is found, False otherwise. |
| 699 | """ |
| 700 | for slot in self.slots: |
| 701 | if slot.poll(): |
| 702 | return True |
| 703 | return False |
| 704 | |
| 705 | def empty(self): |
| 706 | """Check if all slots are vacant. |
| 707 | |
| 708 | Returns: |
| 709 | Return True if all the slots are vacant, False otherwise. |
| 710 | """ |
| 711 | ret = True |
| 712 | for slot in self.slots: |
| 713 | if not slot.poll(): |
| 714 | ret = False |
| 715 | return ret |
| 716 | |
| 717 | def show_failed_boards(self): |
| 718 | """Display all of the failed boards (defconfigs).""" |
| 719 | failed_boards = [] |
| 720 | |
| 721 | for slot in self.slots: |
| 722 | failed_boards += slot.get_failed_boards() |
| 723 | |
| 724 | if len(failed_boards) > 0: |
| 725 | msg = [ "The following boards were not processed due to error:" ] |
| 726 | msg += failed_boards |
| 727 | for line in msg: |
| 728 | print >> sys.stderr, color_text(self.options.color, |
| 729 | COLOR_LIGHT_RED, line) |
| 730 | |
Joe Hershberger | 2559cd8 | 2015-05-19 13:21:22 -0500 | [diff] [blame] | 731 | with open('moveconfig.failed', 'w') as f: |
| 732 | for board in failed_boards: |
| 733 | f.write(board + '\n') |
| 734 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 735 | def move_config(config_attrs, options): |
| 736 | """Move config options to defconfig files. |
| 737 | |
| 738 | Arguments: |
| 739 | config_attrs: A list of dictionaris, each of them includes the name, |
| 740 | the type, and the default value of the target config. |
| 741 | options: option flags |
| 742 | """ |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 743 | if len(config_attrs) == 0: |
| 744 | print 'Nothing to do. exit.' |
| 745 | sys.exit(0) |
| 746 | |
| 747 | print 'Move the following CONFIG options (jobs: %d)' % options.jobs |
| 748 | for config_attr in config_attrs: |
| 749 | print ' %s (type: %s, default: %s)' % (config_attr['config'], |
| 750 | config_attr['type'], |
| 751 | config_attr['default']) |
| 752 | |
Joe Hershberger | 91040e8 | 2015-05-19 13:21:19 -0500 | [diff] [blame] | 753 | if options.defconfigs: |
| 754 | defconfigs = [line.strip() for line in open(options.defconfigs)] |
| 755 | for i, defconfig in enumerate(defconfigs): |
| 756 | if not defconfig.endswith('_defconfig'): |
| 757 | defconfigs[i] = defconfig + '_defconfig' |
| 758 | if not os.path.exists(os.path.join('configs', defconfigs[i])): |
| 759 | sys.exit('%s - defconfig does not exist. Stopping.' % |
| 760 | defconfigs[i]) |
| 761 | else: |
| 762 | # All the defconfig files to be processed |
| 763 | defconfigs = [] |
| 764 | for (dirpath, dirnames, filenames) in os.walk('configs'): |
| 765 | dirpath = dirpath[len('configs') + 1:] |
| 766 | for filename in fnmatch.filter(filenames, '*_defconfig'): |
| 767 | defconfigs.append(os.path.join(dirpath, filename)) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 768 | |
| 769 | slots = Slots(config_attrs, options) |
| 770 | |
| 771 | # Main loop to process defconfig files: |
| 772 | # Add a new subprocess into a vacant slot. |
| 773 | # Sleep if there is no available slot. |
| 774 | for defconfig in defconfigs: |
| 775 | while not slots.add(defconfig): |
| 776 | while not slots.available(): |
| 777 | # No available slot: sleep for a while |
| 778 | time.sleep(SLEEP_TIME) |
| 779 | |
| 780 | # wait until all the subprocesses finish |
| 781 | while not slots.empty(): |
| 782 | time.sleep(SLEEP_TIME) |
| 783 | |
| 784 | slots.show_failed_boards() |
| 785 | |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 786 | def bad_recipe(filename, linenum, msg): |
| 787 | """Print error message with the file name and the line number and exit.""" |
| 788 | sys.exit("%s: line %d: error : " % (filename, linenum) + msg) |
| 789 | |
| 790 | def parse_recipe(filename): |
| 791 | """Parse the recipe file and retrieve the config attributes. |
| 792 | |
| 793 | This function parses the given recipe file and gets the name, |
| 794 | the type, and the default value of the target config options. |
| 795 | |
| 796 | Arguments: |
| 797 | filename: path to file to be parsed. |
| 798 | Returns: |
| 799 | A list of dictionaris, each of them includes the name, |
| 800 | the type, and the default value of the target config. |
| 801 | """ |
| 802 | config_attrs = [] |
| 803 | linenum = 1 |
| 804 | |
| 805 | for line in open(filename): |
| 806 | tokens = line.split() |
| 807 | if len(tokens) != 3: |
| 808 | bad_recipe(filename, linenum, |
| 809 | "%d fields in this line. Each line must contain 3 fields" |
| 810 | % len(tokens)) |
| 811 | |
| 812 | (config, type, default) = tokens |
| 813 | |
| 814 | # prefix the option name with CONFIG_ if missing |
| 815 | if not config.startswith('CONFIG_'): |
| 816 | config = 'CONFIG_' + config |
| 817 | |
| 818 | # sanity check of default values |
| 819 | if type == 'bool': |
| 820 | if not default in ('y', 'n'): |
| 821 | bad_recipe(filename, linenum, |
| 822 | "default for bool type must be either y or n") |
| 823 | elif type == 'tristate': |
| 824 | if not default in ('y', 'm', 'n'): |
| 825 | bad_recipe(filename, linenum, |
| 826 | "default for tristate type must be y, m, or n") |
| 827 | elif type == 'string': |
| 828 | if default[0] != '"' or default[-1] != '"': |
| 829 | bad_recipe(filename, linenum, |
| 830 | "default for string type must be surrounded by double-quotations") |
| 831 | elif type == 'int': |
| 832 | try: |
| 833 | int(default) |
| 834 | except: |
| 835 | bad_recipe(filename, linenum, |
| 836 | "type is int, but default value is not decimal") |
| 837 | elif type == 'hex': |
| 838 | if len(default) < 2 or default[:2] != '0x': |
| 839 | bad_recipe(filename, linenum, |
| 840 | "default for hex type must be prefixed with 0x") |
| 841 | try: |
| 842 | int(default, 16) |
| 843 | except: |
| 844 | bad_recipe(filename, linenum, |
| 845 | "type is hex, but default value is not hexadecimal") |
| 846 | else: |
| 847 | bad_recipe(filename, linenum, |
| 848 | "unsupported type '%s'. type must be one of bool, tristate, string, int, hex" |
| 849 | % type) |
| 850 | |
| 851 | config_attrs.append({'config': config, 'type': type, 'default': default}) |
| 852 | linenum += 1 |
| 853 | |
| 854 | return config_attrs |
| 855 | |
| 856 | def main(): |
| 857 | try: |
| 858 | cpu_count = multiprocessing.cpu_count() |
| 859 | except NotImplementedError: |
| 860 | cpu_count = 1 |
| 861 | |
| 862 | parser = optparse.OptionParser() |
| 863 | # Add options here |
| 864 | parser.add_option('-c', '--color', action='store_true', default=False, |
| 865 | help='display the log in color') |
Joe Hershberger | 91040e8 | 2015-05-19 13:21:19 -0500 | [diff] [blame] | 866 | parser.add_option('-d', '--defconfigs', type='string', |
| 867 | help='a file containing a list of defconfigs to move') |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 868 | parser.add_option('-n', '--dry-run', action='store_true', default=False, |
| 869 | help='perform a trial run (show log with no changes)') |
| 870 | parser.add_option('-e', '--exit-on-error', action='store_true', |
| 871 | default=False, |
| 872 | help='exit immediately on any error') |
Joe Hershberger | 2144f88 | 2015-05-19 13:21:20 -0500 | [diff] [blame] | 873 | parser.add_option('-H', '--headers-only', dest='cleanup_headers_only', |
| 874 | action='store_true', default=False, |
| 875 | help='only cleanup the headers') |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 876 | parser.add_option('-j', '--jobs', type='int', default=cpu_count, |
| 877 | help='the number of jobs to run simultaneously') |
| 878 | parser.usage += ' recipe_file\n\n' + \ |
| 879 | 'The recipe_file should describe config options you want to move.\n' + \ |
| 880 | 'Each line should contain config_name, type, default_value\n\n' + \ |
| 881 | 'Example:\n' + \ |
| 882 | 'CONFIG_FOO bool n\n' + \ |
| 883 | 'CONFIG_BAR int 100\n' + \ |
| 884 | 'CONFIG_BAZ string "hello"\n' |
| 885 | |
| 886 | (options, args) = parser.parse_args() |
| 887 | |
| 888 | if len(args) != 1: |
| 889 | parser.print_usage() |
| 890 | sys.exit(1) |
| 891 | |
| 892 | config_attrs = parse_recipe(args[0]) |
| 893 | |
| 894 | update_cross_compile() |
| 895 | |
Joe Hershberger | 2144f88 | 2015-05-19 13:21:20 -0500 | [diff] [blame] | 896 | check_top_directory() |
| 897 | |
| 898 | if not options.cleanup_headers_only: |
| 899 | move_config(config_attrs, options) |
| 900 | |
| 901 | cleanup_headers(config_attrs, options.dry_run) |
Masahiro Yamada | 5a27c73 | 2015-05-20 11:36:07 +0900 | [diff] [blame] | 902 | |
| 903 | if __name__ == '__main__': |
| 904 | main() |