3 # Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5 # SPDX-License-Identifier: GPL-2.0+
9 Move config options from headers to defconfig files.
11 Since Kconfig was introduced to U-Boot, we have worked on moving
12 config options from headers to Kconfig (defconfig).
14 This tool intends to help this tremendous work.
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.)
26 <config_name> is the name of config option.
28 <type> is the type of the option. It must be one of bool, tristate,
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).
36 You can add two or more lines in the recipe file, so you can move
37 multiple options at once.
39 Let's say, for example, you want to move CONFIG_CMD_USB and
42 The type should be bool, hex, respectively. So, the recipe file
43 should look like this:
47 CONFIG_SYS_TEXT_BASE hex 0x00000000
49 And then run this tool giving the file name of the recipe
51 $ tools/moveconfig.py recipe
53 The tool walks through all the defconfig files to move the config
54 options specified by the recipe file.
56 The log is also displayed on the terminal.
58 Each line is printed in the format
59 <defconfig_name> : <action>
61 <defconfig_name> is the name of the defconfig
62 (without the suffix _defconfig).
64 <action> shows what the tool did for that defconfig.
65 It looks like one of the followings:
68 This config option was moved to the defconfig
70 - Default value 'CONFIG_...'. Do nothing.
71 The value of this option is the same as default.
72 We do not have to add it to the defconfig.
74 - 'CONFIG_...' already exists in Kconfig. Do nothing.
75 This config option is already defined in Kconfig.
76 We do not need/want to touch it.
78 - Undefined. Do nothing.
79 This config option was not found in the config header.
82 - Failed to process. Skip.
83 An error occurred during processing this defconfig. Skipped.
84 (If -e option is passed, the tool exits immediately on error.)
86 Finally, you will be asked, Clean up headers? [y/n]:
88 If you say 'y' here, the unnecessary config defines are removed
89 from the config headers (include/configs/*.h).
90 It just uses the regex method, so you should not rely on it.
91 Just in case, please do 'git diff' to see what happened.
97 This tool runs configuration and builds include/autoconf.mk for every
98 defconfig. The config options defined in Kconfig appear in the .config
99 file (unless they are hidden because of unmet dependency.)
100 On the other hand, the config options defined by board headers are seen
101 in include/autoconf.mk. The tool looks for the specified options in both
102 of them to decide the appropriate action for the options. If the option
103 is found in the .config or the value is the same as the specified default,
104 the option does not need to be touched. If the option is found in
105 include/autoconf.mk, but not in the .config, and the value is different
106 from the default, the tools adds the option to the defconfig.
108 For faster processing, this tool handles multi-threading. It creates
109 separate build directories where the out-of-tree build is run. The
110 temporary build directories are automatically created and deleted as
111 needed. The number of threads are chosen based on the number of the CPU
112 cores of your system although you can change it via -j (--jobs) option.
118 Appropriate toolchain are necessary to generate include/autoconf.mk
119 for all the architectures supported by U-Boot. Most of them are available
120 at the kernel.org site, some are not provided by kernel.org.
122 The default per-arch CROSS_COMPILE used by this tool is specified by
123 the list below, CROSS_COMPILE. You may wish to update the list to
124 use your own. Instead of modifying the list directly, you can give
125 them via environments.
132 Surround each portion of the log with escape sequences to display it
133 in color on the terminal.
136 Peform a trial run that does not make any changes. It is useful to
137 see what is going to happen before one actually runs it.
140 Exit immediately if Make exits with a non-zero status while processing
144 Specify the number of threads to run simultaneously. If not specified,
145 the number of threads is the same as the number of CPU cores.
147 To see the complete list of supported options, run
149 $ tools/moveconfig.py -h
154 import multiprocessing
164 SHOW_GNU_MAKE = 'scripts/show-gnu-make'
167 # Here is the list of cross-tools I use.
168 # Most of them are available at kernel.org
169 # (https://www.kernel.org/pub/tools/crosstool/files/bin/), except the followings:
170 # arc: https://github.com/foss-for-synopsys-dwc-arc-processors/toolchain/releases
171 # blackfin: http://sourceforge.net/projects/adi-toolchain/files/
172 # nds32: http://osdk.andestech.com/packages/
173 # nios2: https://sourcery.mentor.com/GNUToolchain/subscription42545
174 # sh: http://sourcery.mentor.com/public/gnu_toolchain/sh-linux-gnu
177 'aarch64': 'aarch64-linux-',
178 'arm': 'arm-unknown-linux-gnueabi-',
179 'avr32': 'avr32-linux-',
180 'blackfin': 'bfin-elf-',
181 'm68k': 'm68k-linux-',
182 'microblaze': 'microblaze-linux-',
183 'mips': 'mips-linux-',
184 'nds32': 'nds32le-linux-',
185 'nios2': 'nios2-linux-gnu-',
186 'openrisc': 'or32-linux-',
187 'powerpc': 'powerpc-linux-',
188 'sh': 'sh-linux-gnu-',
189 'sparc': 'sparc-linux-',
198 ACTION_DEFAULT_VALUE = 1
199 ACTION_ALREADY_EXIST = 2
207 COLOR_PURPLE = '0;35'
209 COLOR_LIGHT_GRAY = '0;37'
210 COLOR_DARK_GRAY = '1;30'
211 COLOR_LIGHT_RED = '1;31'
212 COLOR_LIGHT_GREEN = '1;32'
213 COLOR_YELLOW = '1;33'
214 COLOR_LIGHT_BLUE = '1;34'
215 COLOR_LIGHT_PURPLE = '1;35'
216 COLOR_LIGHT_CYAN = '1;36'
219 ### helper functions ###
221 """Get the file object of '/dev/null' device."""
223 devnull = subprocess.DEVNULL # py3k
224 except AttributeError:
225 devnull = open(os.devnull, 'wb')
228 def check_top_directory():
229 """Exit if we are not at the top of source directory."""
230 for f in ('README', 'Licenses'):
231 if not os.path.exists(f):
232 sys.exit('Please run at the top of source directory.')
235 """Get the command name of GNU Make.
237 U-Boot needs GNU Make for building, but the command name is not
238 necessarily "make". (for example, "gmake" on FreeBSD).
239 Returns the most appropriate command name on your system.
241 process = subprocess.Popen([SHOW_GNU_MAKE], stdout=subprocess.PIPE)
242 ret = process.communicate()
243 if process.returncode:
244 sys.exit('GNU Make not found')
245 return ret[0].rstrip()
247 def color_text(color_enabled, color, string):
248 """Return colored string."""
250 return '\033[' + color + 'm' + string + '\033[0m'
254 def log_msg(color_enabled, color, defconfig, msg):
255 """Return the formated line for the log."""
256 return defconfig[:-len('_defconfig')].ljust(37) + ': ' + \
257 color_text(color_enabled, color, msg) + '\n'
259 def update_cross_compile():
260 """Update per-arch CROSS_COMPILE via enviroment variables
262 The default CROSS_COMPILE values are available
263 in the CROSS_COMPILE list above.
265 You can override them via enviroment variables
266 CROSS_COMPILE_{ARCH}.
268 For example, if you want to override toolchain prefixes
269 for ARM and PowerPC, you can do as follows in your shell:
271 export CROSS_COMPILE_ARM=...
272 export CROSS_COMPILE_POWERPC=...
276 for arch in os.listdir('arch'):
277 if os.path.exists(os.path.join('arch', arch, 'Makefile')):
280 # arm64 is a special case
281 archs.append('aarch64')
284 env = 'CROSS_COMPILE_' + arch.upper()
285 cross_compile = os.environ.get(env)
287 CROSS_COMPILE[arch] = cross_compile
289 def cleanup_one_header(header_path, patterns, dry_run):
290 """Clean regex-matched lines away from a file.
293 header_path: path to the cleaned file.
294 patterns: list of regex patterns. Any lines matching to these
295 patterns are deleted.
296 dry_run: make no changes, but still display log.
298 with open(header_path) as f:
299 lines = f.readlines()
302 for i, line in enumerate(lines):
303 for pattern in patterns:
304 m = pattern.search(line)
306 print '%s: %s: %s' % (header_path, i + 1, line),
310 if dry_run or not matched:
313 with open(header_path, 'w') as f:
314 for i, line in enumerate(lines):
318 def cleanup_headers(config_attrs, dry_run):
319 """Delete config defines from board headers.
322 config_attrs: A list of dictionaris, each of them includes the name,
323 the type, and the default value of the target config.
324 dry_run: make no changes, but still display log.
327 choice = raw_input('Clean up headers? [y/n]: ').lower()
329 if choice == 'y' or choice == 'n':
336 for config_attr in config_attrs:
337 config = config_attr['config']
338 patterns.append(re.compile(r'#\s*define\s+%s\W' % config))
339 patterns.append(re.compile(r'#\s*undef\s+%s\W' % config))
341 for (dirpath, dirnames, filenames) in os.walk('include'):
342 for filename in filenames:
343 if not fnmatch.fnmatch(filename, '*~'):
344 cleanup_one_header(os.path.join(dirpath, filename), patterns,
350 """A parser of .config and include/autoconf.mk."""
352 re_arch = re.compile(r'CONFIG_SYS_ARCH="(.*)"')
353 re_cpu = re.compile(r'CONFIG_SYS_CPU="(.*)"')
355 def __init__(self, config_attrs, options, build_dir):
356 """Create a new parser.
359 config_attrs: A list of dictionaris, each of them includes the name,
360 the type, and the default value of the target config.
361 options: option flags.
362 build_dir: Build directory.
364 self.config_attrs = config_attrs
365 self.options = options
366 self.build_dir = build_dir
368 def get_cross_compile(self):
369 """Parse .config file and return CROSS_COMPILE.
372 A string storing the compiler prefix for the architecture.
376 dotconfig = os.path.join(self.build_dir, '.config')
377 for line in open(dotconfig):
378 m = self.re_arch.match(line)
382 m = self.re_cpu.match(line)
386 assert arch, 'Error: arch is not defined in %s' % defconfig
389 if arch == 'arm' and cpu == 'armv8':
392 return CROSS_COMPILE.get(arch, '')
394 def parse_one_config(self, config_attr, defconfig_lines,
395 dotconfig_lines, autoconf_lines):
396 """Parse .config, defconfig, include/autoconf.mk for one config.
398 This function looks for the config options in the lines from
399 defconfig, .config, and include/autoconf.mk in order to decide
400 which action should be taken for this defconfig.
403 config_attr: A dictionary including the name, the type,
404 and the default value of the target config.
405 defconfig_lines: lines from the original defconfig file.
406 dotconfig_lines: lines from the .config file.
407 autoconf_lines: lines from the include/autoconf.mk file.
410 A tupple of the action for this defconfig and the line
411 matched for the config.
413 config = config_attr['config']
414 not_set = '# %s is not set' % config
416 if config_attr['type'] in ('bool', 'tristate') and \
417 config_attr['default'] == 'n':
420 default = config + '=' + config_attr['default']
422 for line in defconfig_lines + dotconfig_lines:
424 if line.startswith(config + '=') or line == not_set:
425 return (ACTION_ALREADY_EXIST, line)
427 if config_attr['type'] in ('bool', 'tristate'):
430 value = '(undefined)'
432 for line in autoconf_lines:
434 if line.startswith(config + '='):
439 action = ACTION_DEFAULT_VALUE
440 elif value == '(undefined)':
441 action = ACTION_UNDEFINED
445 return (action, value)
447 def update_defconfig(self, defconfig):
448 """Parse files for the config options and update the defconfig.
450 This function parses the given defconfig, the generated .config
451 and include/autoconf.mk searching the target options.
452 Move the config option(s) to the defconfig or do nothing if unneeded.
453 Also, display the log to show what happened to this defconfig.
456 defconfig: defconfig name.
459 defconfig_path = os.path.join('configs', defconfig)
460 dotconfig_path = os.path.join(self.build_dir, '.config')
461 autoconf_path = os.path.join(self.build_dir, 'include', 'autoconf.mk')
464 with open(defconfig_path) as f:
465 defconfig_lines = f.readlines()
467 with open(dotconfig_path) as f:
468 dotconfig_lines = f.readlines()
470 with open(autoconf_path) as f:
471 autoconf_lines = f.readlines()
473 for config_attr in self.config_attrs:
474 result = self.parse_one_config(config_attr, defconfig_lines,
475 dotconfig_lines, autoconf_lines)
476 results.append(result)
480 for (action, value) in results:
481 if action == ACTION_MOVE:
482 actlog = "Move '%s'" % value
483 log_color = COLOR_LIGHT_GREEN
484 elif action == ACTION_DEFAULT_VALUE:
485 actlog = "Default value '%s'. Do nothing." % value
486 log_color = COLOR_LIGHT_BLUE
487 elif action == ACTION_ALREADY_EXIST:
488 actlog = "'%s' already defined in Kconfig. Do nothing." % value
489 log_color = COLOR_LIGHT_PURPLE
490 elif action == ACTION_UNDEFINED:
491 actlog = "Undefined. Do nothing."
492 log_color = COLOR_DARK_GRAY
494 sys.exit("Internal Error. This should not happen.")
496 log += log_msg(self.options.color, log_color, defconfig, actlog)
498 # Some threads are running in parallel.
499 # Print log in one shot to not mix up logs from different threads.
502 if not self.options.dry_run:
503 with open(defconfig_path, 'a') as f:
504 for (action, value) in results:
505 if action == ACTION_MOVE:
506 f.write(value + '\n')
508 os.remove(os.path.join(self.build_dir, 'include', 'config', 'auto.conf'))
509 os.remove(autoconf_path)
513 """A slot to store a subprocess.
515 Each instance of this class handles one subprocess.
516 This class is useful to control multiple threads
517 for faster processing.
520 def __init__(self, config_attrs, options, devnull, make_cmd):
521 """Create a new process slot.
524 config_attrs: A list of dictionaris, each of them includes the name,
525 the type, and the default value of the target config.
526 options: option flags.
527 devnull: A file object of '/dev/null'.
528 make_cmd: command name of GNU Make.
530 self.options = options
531 self.build_dir = tempfile.mkdtemp()
532 self.devnull = devnull
533 self.make_cmd = (make_cmd, 'O=' + self.build_dir)
534 self.parser = KconfigParser(config_attrs, options, self.build_dir)
535 self.state = STATE_IDLE
536 self.failed_boards = []
539 """Delete the working directory
541 This function makes sure the temporary directory is cleaned away
542 even if Python suddenly dies due to error. It should be done in here
543 because it is guranteed the destructor is always invoked when the
544 instance of the class gets unreferenced.
546 If the subprocess is still running, wait until it finishes.
548 if self.state != STATE_IDLE:
549 while self.ps.poll() == None:
551 shutil.rmtree(self.build_dir)
553 def add(self, defconfig):
554 """Assign a new subprocess for defconfig and add it to the slot.
556 If the slot is vacant, create a new subprocess for processing the
557 given defconfig and add it to the slot. Just returns False if
558 the slot is occupied (i.e. the current subprocess is still running).
561 defconfig: defconfig name.
564 Return True on success or False on failure
566 if self.state != STATE_IDLE:
568 cmd = list(self.make_cmd)
569 cmd.append(defconfig)
570 self.ps = subprocess.Popen(cmd, stdout=self.devnull)
571 self.defconfig = defconfig
572 self.state = STATE_DEFCONFIG
576 """Check the status of the subprocess and handle it as needed.
578 Returns True if the slot is vacant (i.e. in idle state).
579 If the configuration is successfully finished, assign a new
580 subprocess to build include/autoconf.mk.
581 If include/autoconf.mk is generated, invoke the parser to
582 parse the .config and the include/autoconf.mk, and then set the
583 slot back to the idle state.
586 Return True if the subprocess is terminated, False otherwise
588 if self.state == STATE_IDLE:
591 if self.ps.poll() == None:
594 if self.ps.poll() != 0:
596 print >> sys.stderr, log_msg(self.options.color,
599 "failed to process.")
600 if self.options.exit_on_error:
601 sys.exit("Exit on error.")
603 # If --exit-on-error flag is not set,
604 # skip this board and continue.
605 # Record the failed board.
606 self.failed_boards.append(self.defconfig)
607 self.state = STATE_IDLE
610 if self.state == STATE_AUTOCONF:
611 self.parser.update_defconfig(self.defconfig)
612 self.state = STATE_IDLE
615 cross_compile = self.parser.get_cross_compile()
616 cmd = list(self.make_cmd)
618 cmd.append('CROSS_COMPILE=%s' % cross_compile)
619 cmd.append('include/config/auto.conf')
620 self.ps = subprocess.Popen(cmd, stdout=self.devnull)
621 self.state = STATE_AUTOCONF
624 def get_failed_boards(self):
625 """Returns a list of failed boards (defconfigs) in this slot.
627 return self.failed_boards
631 """Controller of the array of subprocess slots."""
633 def __init__(self, config_attrs, options):
634 """Create a new slots controller.
637 config_attrs: A list of dictionaris containing the name, the type,
638 and the default value of the target CONFIG.
639 options: option flags.
641 self.options = options
643 devnull = get_devnull()
644 make_cmd = get_make_cmd()
645 for i in range(options.jobs):
646 self.slots.append(Slot(config_attrs, options, devnull, make_cmd))
648 def add(self, defconfig):
649 """Add a new subprocess if a vacant slot is found.
652 defconfig: defconfig name to be put into.
655 Return True on success or False on failure
657 for slot in self.slots:
658 if slot.add(defconfig):
663 """Check if there is a vacant slot.
666 Return True if at lease one vacant slot is found, False otherwise.
668 for slot in self.slots:
674 """Check if all slots are vacant.
677 Return True if all the slots are vacant, False otherwise.
680 for slot in self.slots:
685 def show_failed_boards(self):
686 """Display all of the failed boards (defconfigs)."""
689 for slot in self.slots:
690 failed_boards += slot.get_failed_boards()
692 if len(failed_boards) > 0:
693 msg = [ "The following boards were not processed due to error:" ]
696 print >> sys.stderr, color_text(self.options.color,
697 COLOR_LIGHT_RED, line)
699 def move_config(config_attrs, options):
700 """Move config options to defconfig files.
703 config_attrs: A list of dictionaris, each of them includes the name,
704 the type, and the default value of the target config.
705 options: option flags
707 check_top_directory()
709 if len(config_attrs) == 0:
710 print 'Nothing to do. exit.'
713 print 'Move the following CONFIG options (jobs: %d)' % options.jobs
714 for config_attr in config_attrs:
715 print ' %s (type: %s, default: %s)' % (config_attr['config'],
717 config_attr['default'])
719 # All the defconfig files to be processed
721 for (dirpath, dirnames, filenames) in os.walk('configs'):
722 dirpath = dirpath[len('configs') + 1:]
723 for filename in fnmatch.filter(filenames, '*_defconfig'):
724 defconfigs.append(os.path.join(dirpath, filename))
726 slots = Slots(config_attrs, options)
728 # Main loop to process defconfig files:
729 # Add a new subprocess into a vacant slot.
730 # Sleep if there is no available slot.
731 for defconfig in defconfigs:
732 while not slots.add(defconfig):
733 while not slots.available():
734 # No available slot: sleep for a while
735 time.sleep(SLEEP_TIME)
737 # wait until all the subprocesses finish
738 while not slots.empty():
739 time.sleep(SLEEP_TIME)
741 slots.show_failed_boards()
743 cleanup_headers(config_attrs, options.dry_run)
745 def bad_recipe(filename, linenum, msg):
746 """Print error message with the file name and the line number and exit."""
747 sys.exit("%s: line %d: error : " % (filename, linenum) + msg)
749 def parse_recipe(filename):
750 """Parse the recipe file and retrieve the config attributes.
752 This function parses the given recipe file and gets the name,
753 the type, and the default value of the target config options.
756 filename: path to file to be parsed.
758 A list of dictionaris, each of them includes the name,
759 the type, and the default value of the target config.
764 for line in open(filename):
765 tokens = line.split()
767 bad_recipe(filename, linenum,
768 "%d fields in this line. Each line must contain 3 fields"
771 (config, type, default) = tokens
773 # prefix the option name with CONFIG_ if missing
774 if not config.startswith('CONFIG_'):
775 config = 'CONFIG_' + config
777 # sanity check of default values
779 if not default in ('y', 'n'):
780 bad_recipe(filename, linenum,
781 "default for bool type must be either y or n")
782 elif type == 'tristate':
783 if not default in ('y', 'm', 'n'):
784 bad_recipe(filename, linenum,
785 "default for tristate type must be y, m, or n")
786 elif type == 'string':
787 if default[0] != '"' or default[-1] != '"':
788 bad_recipe(filename, linenum,
789 "default for string type must be surrounded by double-quotations")
794 bad_recipe(filename, linenum,
795 "type is int, but default value is not decimal")
797 if len(default) < 2 or default[:2] != '0x':
798 bad_recipe(filename, linenum,
799 "default for hex type must be prefixed with 0x")
803 bad_recipe(filename, linenum,
804 "type is hex, but default value is not hexadecimal")
806 bad_recipe(filename, linenum,
807 "unsupported type '%s'. type must be one of bool, tristate, string, int, hex"
810 config_attrs.append({'config': config, 'type': type, 'default': default})
817 cpu_count = multiprocessing.cpu_count()
818 except NotImplementedError:
821 parser = optparse.OptionParser()
823 parser.add_option('-c', '--color', action='store_true', default=False,
824 help='display the log in color')
825 parser.add_option('-n', '--dry-run', action='store_true', default=False,
826 help='perform a trial run (show log with no changes)')
827 parser.add_option('-e', '--exit-on-error', action='store_true',
829 help='exit immediately on any error')
830 parser.add_option('-j', '--jobs', type='int', default=cpu_count,
831 help='the number of jobs to run simultaneously')
832 parser.usage += ' recipe_file\n\n' + \
833 'The recipe_file should describe config options you want to move.\n' + \
834 'Each line should contain config_name, type, default_value\n\n' + \
836 'CONFIG_FOO bool n\n' + \
837 'CONFIG_BAR int 100\n' + \
838 'CONFIG_BAZ string "hello"\n'
840 (options, args) = parser.parse_args()
846 config_attrs = parse_recipe(args[0])
848 update_cross_compile()
850 move_config(config_attrs, options)
852 if __name__ == '__main__':